/*----------------------------------------------------------------------------*/
/* liblog.c */
/*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*/
/* This file is part of LibLog. */
/* */
/* LibLog is free software: you can redistribute it and/or modify it */
/* under the terms of the GNU Lesser General Public License as published */
/* by the Free Software Foundation, either version 3 of the License, or */
/* (at your option) any later version. */
/* */
/* LibLog is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU Lesser General Public License for more details. */
/* */
/* You should have received a copy of the GNU Lesser General Public */
/* License along with Drummer. If not, see */
/* . */
/*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*/
/* Includes */
/*----------------------------------------------------------------------------*/
#define _LIBLOG_C_
#include
/*----------------------------------------------------------------------------*/
/* LG_Library_Open */
/*----------------------------------------------------------------------------*/
LGT_Status LG_Library_Open( LGT_Log_Writer_Ptr Log_Writer_Ptr, bool Header_Print_Flag)
{
LGG_Base.Log_Writer_Ptr = Log_Writer_Ptr;
if( Header_Print_Flag) LG_Log_Header( LGD_LOG_LEVEL_DEFAULT);
return( LGS_OK);
}
/*----------------------------------------------------------------------------*/
/* LG_Library_Close */
/*----------------------------------------------------------------------------*/
LGT_Status LG_Library_Close( bool Footer_Print_Flag)
{
if( Footer_Print_Flag) LG_Log_Footer( LGD_LOG_LEVEL_DEFAULT);
return( LGS_OK);
}
/*----------------------------------------------------------------------------*/
/* LG_Log_Stream_Out_Get */
/*----------------------------------------------------------------------------*/
/* (O) Log_Stream_Out_Ptr_Ptr: Log output stream */
/*----------------------------------------------------------------------------*/
LGT_Status LG_Log_Stream_Out_Get( FILE **Log_Stream_Out_Ptr_Ptr)
{
*Log_Stream_Out_Ptr_Ptr = LGG_Base.Log_Stream_Out_Ptr;
return( LGS_OK);
}
/*----------------------------------------------------------------------------*/
/* LG_Log_Stream_Out_Set */
/*----------------------------------------------------------------------------*/
/* (I) Log_Stream_Out_Ptr: Log output stream */
/*----------------------------------------------------------------------------*/
LGT_Status LG_Log_Stream_Out_Set( FILE *Log_Stream_Out_Ptr)
{
LGG_Base.Log_Stream_Out_Ptr = Log_Stream_Out_Ptr;
return( LGS_OK);
}
/*----------------------------------------------------------------------------*/
/* LG_Log_Header */
/*----------------------------------------------------------------------------*/
LGT_Status LG_Log_Header( LGT_Log_Type_Id Log_Type_Id, ...)
{
va_list args;
char fmt[255];
FILE *log_stream_out_ptr;
LGT_Log_Writer_Ptr log_writer_ptr;
LGT_Status status;
log_stream_out_ptr = ( ( LGG_Base.Log_Stream_Out_Ptr == NULL) ? LGD_LOG_STREAM_DEFAULT_VALUE : LGG_Base.Log_Stream_Out_Ptr);
log_writer_ptr = ( ( LGG_Base.Log_Writer_Ptr == NULL) ? LGD_LOG_WRITER_DEFAULT_VALUE : LGG_Base.Log_Writer_Ptr);
va_start( args, Log_Type_Id);
sprintf( fmt, "\nYY/MM/DD|hh:mm:ss.uuuu|Typ|Lev|Name| File Name |Line| Log Message\n");
status = ( *log_writer_ptr)( log_stream_out_ptr, Log_Type_Id, fmt, args);
sprintf( fmt, "--------+-------------+---+---+----+---------------------+----+-------------------------------------------------------------------\n");
status = ( *log_writer_ptr)( log_stream_out_ptr, Log_Type_Id, fmt, args);
va_end( args);
return( status);
}
/*----------------------------------------------------------------------------*/
/* LG_Log_Footer */
/*----------------------------------------------------------------------------*/
LGT_Status LG_Log_Footer( LGT_Log_Type_Id Log_Type_Id, ...)
{
va_list args;
char fmt[255];
FILE *log_stream_out_ptr;
LGT_Log_Writer_Ptr log_writer_ptr;
LGT_Status status;
log_stream_out_ptr = ( ( LGG_Base.Log_Stream_Out_Ptr == NULL) ? LGD_LOG_STREAM_DEFAULT_VALUE : LGG_Base.Log_Stream_Out_Ptr);
log_writer_ptr = ( ( LGG_Base.Log_Writer_Ptr == NULL) ? LGD_LOG_WRITER_DEFAULT_VALUE : LGG_Base.Log_Writer_Ptr);
va_start( args, Log_Type_Id);
sprintf( fmt, "--------+-------------+---+---+----+---------------------+----+-------------------------------------------------------------------\n");
status = ( *log_writer_ptr)( log_stream_out_ptr, Log_Type_Id, fmt, args);
va_end( args);
return( status);
}
/*----------------------------------------------------------------------------*/
/* LG_Log */
/*----------------------------------------------------------------------------*/
LGT_Status LG_Log( LGT_Log_Type_Id Log_Type_Id, LGT_Log_Level Log_Level, char *Module_Name, char *File_Name, long Line_Id, bool LF_Flag, char *Log_Fmt, ...)
{
va_list args;
char fmt[255];
int rc;
FILE *log_stream_out_ptr;
LGT_Log_Writer_Ptr log_writer_ptr;
LGT_Status status;
if( LG_Log_Format( fmt, Log_Type_Id, Log_Level, Module_Name, File_Name, Line_Id, LF_Flag, Log_Fmt) != LGS_OK)
{
fprintf( stderr, "LG_Log: Can't format log messages (%d)!!!\n", errno);
return( LGS_KO);
}
va_start( args, Log_Fmt);
log_stream_out_ptr = ( ( LGG_Base.Log_Stream_Out_Ptr == NULL) ? LGD_LOG_STREAM_DEFAULT_VALUE : LGG_Base.Log_Stream_Out_Ptr);
log_writer_ptr = ( ( LGG_Base.Log_Writer_Ptr == NULL) ? LGD_LOG_WRITER_DEFAULT_VALUE : LGG_Base.Log_Writer_Ptr);
status = ( *log_writer_ptr)( log_stream_out_ptr, Log_Type_Id, fmt, args);
va_end( args);
return( status);
}
/*----------------------------------------------------------------------------*/
/* LG_Log_Format */
/*----------------------------------------------------------------------------*/
LGT_Status LG_Log_Format( char *Out_Fmt, LGT_Log_Type_Id Log_Type_Id, LGT_Log_Level Log_Level, char *Module_Name, char *File_Name, long Line_Id, bool LF_Flag, char *Log_Fmt)
{
struct timeval cur_timeval;
struct tm *tm_ptr;
gettimeofday( &cur_timeval, NULL);
if( ( tm_ptr = localtime( &( cur_timeval.tv_sec))) == NULL)
{
fprintf( LGG_Base.Log_Stream_Out_Ptr, "LG_Log: %s: can't convert localtime (%d) !\n", __FILE__, errno);
return( LGS_KO);
}
sprintf( Out_Fmt, "%02d/%02d/%02d %02d:%02d:%02d.%04d %3s %03d %-4s %-21s %-4d %s%s",
( tm_ptr->tm_year - 100), tm_ptr->tm_mon, tm_ptr->tm_mday, tm_ptr->tm_hour, tm_ptr->tm_min, tm_ptr->tm_sec, ( cur_timeval.tv_usec / 100),
LGG_Base.Log_Type_Name_Tab[ Log_Type_Id], Log_Level, Module_Name, File_Name, Line_Id,
Log_Fmt,
( ( LF_Flag == true) ? "\n" : ""));
return( LGS_OK);
}
/*----------------------------------------------------------------------------*/
/* LG_Log_Write */
/*----------------------------------------------------------------------------*/
LGT_Status LG_Log_Write( FILE *Log_Stream_Out_Ptr, LGT_Log_Type_Id Log_Type_Id, char *Out_Fmt_Ptr, va_list Args)
{
int rc;
rc = vfprintf( Log_Stream_Out_Ptr, Out_Fmt_Ptr, Args);
if( rc < 0)
{
fprintf( stderr, "LG_Log: Can't log messages (%d)!!!\n", errno);
return( LGS_KO);
}
fflush( LGG_Base.Log_Stream_Out_Ptr);
return( LGS_OK);
}
/*------------------------------------------------------------------------------*/
/* LG_Stack_Trace */
/*------------------------------------------------------------------------------*/
LGT_Status LG_Stack_Trace( LGT_Log_Level Log_Level, char *Module_Name, char *File_Name, long Line_Id, bool LF_Flag)
{
void *array[100];
char **strings;
int size, i;
size = backtrace( array, 100);
strings = backtrace_symbols( array, size);
if( strings != NULL)
{
LG_Log( LGD_LOG_TYPE_ID_TRACE, Log_Level, Module_Name, File_Name, Line_Id, LF_Flag, "*--- Stack Trace: Obtained %d stack frames ---*", size);
for( i = 0; i < size; i++)
{
LG_Log( LGD_LOG_TYPE_ID_TRACE, Log_Level, Module_Name, File_Name, Line_Id, LF_Flag, "| %s", strings[i]);
}
LG_Log( LGD_LOG_TYPE_ID_TRACE, Log_Level, Module_Name, File_Name, Line_Id, LF_Flag, "*----------------------------------------", size);
}
free( strings);
return( LGS_OK);
}