2024-04-17 10:50:09 +02:00
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
/* liblog.c */
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
2024-04-17 17:52:43 +02:00
|
|
|
/* This file is part of LibLog. */
|
2024-04-17 10:50:09 +02:00
|
|
|
/* */
|
2024-04-17 17:52:43 +02:00
|
|
|
/* LibLog is free software: you can redistribute it and/or modify it */
|
2024-04-17 10:50:09 +02:00
|
|
|
/* 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. */
|
|
|
|
/* */
|
2024-04-17 17:52:43 +02:00
|
|
|
/* LibLog is distributed in the hope that it will be useful, */
|
2024-04-17 10:50:09 +02:00
|
|
|
/* 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 */
|
2024-04-20 10:02:06 +02:00
|
|
|
/* License along with LibLog. If not, see */
|
2024-04-17 10:50:09 +02:00
|
|
|
/* <https://www.gnu.org/licenses/>. */
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
2024-04-17 17:52:43 +02:00
|
|
|
|
|
|
|
|
2024-04-17 10:50:09 +02:00
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
/* Includes */
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
#define _LIBLOG_C_
|
|
|
|
|
|
|
|
#include <liblog.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
/* LG_Library_Open */
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
2024-04-17 17:52:43 +02:00
|
|
|
LGT_Status LG_Library_Open( LGT_Log_Writer_Ptr Log_Writer_Ptr, bool Header_Print_Flag)
|
2024-04-17 10:50:09 +02:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-04-17 17:52:43 +02:00
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
/* 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-04-17 10:50:09 +02:00
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
/* LG_Log_Header */
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
LGT_Status LG_Log_Header( LGT_Log_Type_Id Log_Type_Id, ...)
|
|
|
|
{
|
2024-04-17 17:52:43 +02:00
|
|
|
va_list args;
|
|
|
|
char fmt[255];
|
|
|
|
FILE *log_stream_out_ptr;
|
|
|
|
LGT_Log_Writer_Ptr log_writer_ptr;
|
|
|
|
LGT_Status status;
|
2024-04-17 10:50:09 +02:00
|
|
|
|
|
|
|
|
2024-04-17 17:52:43 +02:00
|
|
|
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);
|
2024-04-17 10:50:09 +02:00
|
|
|
|
|
|
|
va_start( args, Log_Type_Id);
|
|
|
|
|
2024-04-18 00:03:12 +02:00
|
|
|
sprintf( fmt, "\nYY/MM/DD|hh:mm:ss.uuuu|Typ|Lev|Name| File Name |Line| Function Name | Log Message\n");
|
2024-04-17 17:52:43 +02:00
|
|
|
status = ( *log_writer_ptr)( log_stream_out_ptr, Log_Type_Id, fmt, args);
|
2024-04-17 10:50:09 +02:00
|
|
|
|
2024-04-18 00:03:12 +02:00
|
|
|
sprintf( fmt, "--------+-------------+---+---+----+---------------------+----+----------------------------+-------------------------------------------------------------------\n");
|
2024-04-17 17:52:43 +02:00
|
|
|
status = ( *log_writer_ptr)( log_stream_out_ptr, Log_Type_Id, fmt, args);
|
2024-04-17 10:50:09 +02:00
|
|
|
|
|
|
|
va_end( args);
|
|
|
|
|
|
|
|
return( status);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
/* LG_Log_Footer */
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
LGT_Status LG_Log_Footer( LGT_Log_Type_Id Log_Type_Id, ...)
|
|
|
|
{
|
2024-04-17 17:52:43 +02:00
|
|
|
va_list args;
|
|
|
|
char fmt[255];
|
|
|
|
FILE *log_stream_out_ptr;
|
|
|
|
LGT_Log_Writer_Ptr log_writer_ptr;
|
|
|
|
LGT_Status status;
|
2024-04-17 10:50:09 +02:00
|
|
|
|
|
|
|
|
2024-04-17 17:52:43 +02:00
|
|
|
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);
|
2024-04-17 10:50:09 +02:00
|
|
|
|
|
|
|
va_start( args, Log_Type_Id);
|
|
|
|
|
2024-04-18 00:03:12 +02:00
|
|
|
sprintf( fmt, "--------+-------------+---+---+----+---------------------+----+----------------------------+-------------------------------------------------------------------\n");
|
2024-04-17 17:52:43 +02:00
|
|
|
status = ( *log_writer_ptr)( log_stream_out_ptr, Log_Type_Id, fmt, args);
|
2024-04-17 10:50:09 +02:00
|
|
|
|
|
|
|
va_end( args);
|
|
|
|
|
|
|
|
return( status);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
/* LG_Log */
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
2024-04-18 00:03:12 +02:00
|
|
|
LGT_Status LG_Log( LGT_Log_Type_Id Log_Type_Id, LGT_Log_Level Log_Level, char *Module_Name, const char *File_Name, const long Line_Id, const char *Function_Name, bool LF_Flag, char *Log_Fmt, ...)
|
2024-04-17 10:50:09 +02:00
|
|
|
{
|
2024-04-17 17:52:43 +02:00
|
|
|
va_list args;
|
|
|
|
char fmt[255];
|
|
|
|
int rc;
|
|
|
|
FILE *log_stream_out_ptr;
|
|
|
|
LGT_Log_Writer_Ptr log_writer_ptr;
|
|
|
|
LGT_Status status;
|
2024-04-17 10:50:09 +02:00
|
|
|
|
2024-04-17 17:52:43 +02:00
|
|
|
|
2024-04-18 00:03:12 +02:00
|
|
|
if( LG_Log_Format( fmt, Log_Type_Id, Log_Level, Module_Name, File_Name, Line_Id, Function_Name, LF_Flag, Log_Fmt) != LGS_OK)
|
2024-04-17 10:50:09 +02:00
|
|
|
{
|
|
|
|
fprintf( stderr, "LG_Log: Can't format log messages (%d)!!!\n", errno);
|
|
|
|
return( LGS_KO);
|
|
|
|
}
|
|
|
|
|
|
|
|
va_start( args, Log_Fmt);
|
|
|
|
|
2024-04-17 17:52:43 +02:00
|
|
|
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);
|
2024-04-17 10:50:09 +02:00
|
|
|
|
2024-04-17 17:52:43 +02:00
|
|
|
status = ( *log_writer_ptr)( log_stream_out_ptr, Log_Type_Id, fmt, args);
|
2024-04-17 10:50:09 +02:00
|
|
|
|
|
|
|
va_end( args);
|
|
|
|
|
|
|
|
return( status);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
/* LG_Log_Format */
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
2024-04-18 00:03:12 +02:00
|
|
|
LGT_Status LG_Log_Format( char *Out_Fmt, LGT_Log_Type_Id Log_Type_Id, LGT_Log_Level Log_Level, char *Module_Name, const char *File_Name, const long Line_Id, const char *Function_Name, bool LF_Flag, char *Log_Fmt)
|
2024-04-17 10:50:09 +02:00
|
|
|
{
|
|
|
|
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);
|
2024-04-17 17:52:43 +02:00
|
|
|
|
2024-04-17 10:50:09 +02:00
|
|
|
return( LGS_KO);
|
|
|
|
}
|
|
|
|
|
2024-04-18 00:03:12 +02:00
|
|
|
sprintf( Out_Fmt, "%02d/%02d/%02d %02d:%02d:%02d.%04d %3s %03d %-4s %-21s %-4d %-28s %s%s",
|
2024-04-17 10:50:09 +02:00
|
|
|
( 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),
|
2024-04-18 00:03:12 +02:00
|
|
|
LGG_Base.Log_Type_Name_Tab[ Log_Type_Id], Log_Level, Module_Name, File_Name, Line_Id, Function_Name,
|
2024-04-17 10:50:09 +02:00
|
|
|
Log_Fmt,
|
|
|
|
( ( LF_Flag == true) ? "\n" : ""));
|
|
|
|
|
|
|
|
return( LGS_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
/* LG_Log_Write */
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
2024-04-17 17:52:43 +02:00
|
|
|
LGT_Status LG_Log_Write( FILE *Log_Stream_Out_Ptr, LGT_Log_Type_Id Log_Type_Id, char *Out_Fmt_Ptr, va_list Args)
|
2024-04-17 10:50:09 +02:00
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
|
2024-04-17 17:52:43 +02:00
|
|
|
rc = vfprintf( Log_Stream_Out_Ptr, Out_Fmt_Ptr, Args);
|
2024-04-17 10:50:09 +02:00
|
|
|
|
|
|
|
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 */
|
|
|
|
/*------------------------------------------------------------------------------*/
|
|
|
|
|
2024-04-18 00:03:12 +02:00
|
|
|
LGT_Status LG_Stack_Trace( LGT_Log_Level Log_Level, char *Module_Name, const char *File_Name, const long Line_Id, const char *Function_Name, bool LF_Flag)
|
2024-04-17 10:50:09 +02:00
|
|
|
{
|
|
|
|
void *array[100];
|
|
|
|
char **strings;
|
|
|
|
int size, i;
|
|
|
|
|
|
|
|
|
|
|
|
size = backtrace( array, 100);
|
|
|
|
strings = backtrace_symbols( array, size);
|
|
|
|
|
|
|
|
if( strings != NULL)
|
|
|
|
{
|
2024-04-18 00:03:12 +02:00
|
|
|
LG_Log( LGD_LOG_TYPE_ID_TRACE, Log_Level, Module_Name, File_Name, Line_Id, Function_Name, LF_Flag, "*--- Stack Trace: Obtained %d stack frames ---*", size);
|
2024-04-17 10:50:09 +02:00
|
|
|
|
|
|
|
for( i = 0; i < size; i++)
|
|
|
|
{
|
2024-04-18 00:03:12 +02:00
|
|
|
LG_Log( LGD_LOG_TYPE_ID_TRACE, Log_Level, Module_Name, File_Name, Line_Id, Function_Name, LF_Flag, "| %s", strings[i]);
|
2024-04-17 10:50:09 +02:00
|
|
|
}
|
|
|
|
|
2024-04-18 00:03:12 +02:00
|
|
|
LG_Log( LGD_LOG_TYPE_ID_TRACE, Log_Level, Module_Name, File_Name, Line_Id, Function_Name, LF_Flag, "*----------------------------------------", size);
|
2024-04-17 10:50:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
free( strings);
|
|
|
|
|
|
|
|
return( LGS_OK);
|
|
|
|
}
|