- Initial creation.
This commit is contained in:
22
lib/Makefile
Normal file
22
lib/Makefile
Normal file
@@ -0,0 +1,22 @@
|
||||
SRC := liblog.c
|
||||
TARGETS := liblog.a liblog.so
|
||||
FILE_LIB := liblog.a liblog.so
|
||||
|
||||
|
||||
|
||||
include ../Makefile.var
|
||||
include ../Makefile.rule
|
||||
|
||||
|
||||
|
||||
DEP += liblog.h ../include/log.h Makefile
|
||||
INCLUDE += -I . -I ../include
|
||||
|
||||
ifdef _LIBVER_SUPPORT
|
||||
DEP += ../../libver/ver.h
|
||||
INCLUDE += -I ../../libver/lib
|
||||
endif
|
||||
|
||||
|
||||
|
||||
liblog.o : liblog.c $(DEP)
|
||||
250
lib/liblog.c
Normal file
250
lib/liblog.c
Normal file
@@ -0,0 +1,250 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* liblog.c */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* This file is part of liblog. */
|
||||
/* */
|
||||
/* Drummer 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. */
|
||||
/* */
|
||||
/* Drummer 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 */
|
||||
/* <https://www.gnu.org/licenses/>. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Includes */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#define _LIBLOG_C_
|
||||
|
||||
|
||||
|
||||
#include <liblog.h>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* LG_Library_Open */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
LGT_Status LG_Library_Open( FILE *Log_Stream_Out_Ptr, LGT_Log_Writer_Ptr Log_Writer_Ptr, bool Header_Print_Flag)
|
||||
{
|
||||
LGG_Base.Log_Stream_Out_Ptr = Log_Stream_Out_Ptr;
|
||||
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_Header */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
LGT_Status LG_Log_Header( LGT_Log_Type_Id Log_Type_Id, ...)
|
||||
{
|
||||
va_list args;
|
||||
char fmt[255];
|
||||
LGT_Log_Writer_Ptr log_writer_ptr;
|
||||
LGT_Status status;
|
||||
|
||||
|
||||
log_writer_ptr = ( ( LGG_Base.Log_Writer_Ptr == NULL) ? &LG_Log_Write : 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_Type_Id, fmt, args);
|
||||
|
||||
sprintf( fmt, "--------+-------------+---+---+----+---------------------+----+-------------------------------------------------------------------\n");
|
||||
status = ( *log_writer_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];
|
||||
LGT_Log_Writer_Ptr log_writer_ptr;
|
||||
LGT_Status status;
|
||||
|
||||
|
||||
log_writer_ptr = ( ( LGG_Base.Log_Writer_Ptr == NULL) ? &LG_Log_Write : LGG_Base.Log_Writer_Ptr);
|
||||
|
||||
va_start( args, Log_Type_Id);
|
||||
|
||||
sprintf( fmt, "--------+-------------+---+---+----+---------------------+----+-------------------------------------------------------------------\n");
|
||||
status = ( *log_writer_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;
|
||||
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_writer_ptr = ( ( LGG_Base.Log_Writer_Ptr == NULL) ? &LG_Log_Write : LGG_Base.Log_Writer_Ptr);
|
||||
|
||||
status = ( *log_writer_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( LGT_Log_Type_Id Log_Type_Id, char *Out_Fmt_Ptr, va_list Args)
|
||||
{
|
||||
int rc;
|
||||
|
||||
|
||||
rc = vfprintf( LGG_Base.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);
|
||||
}
|
||||
101
lib/liblog.h
Normal file
101
lib/liblog.h
Normal file
@@ -0,0 +1,101 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* liblog.h */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* This file is part of liblog. */
|
||||
/* */
|
||||
/* Drummer 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. */
|
||||
/* */
|
||||
/* Drummer 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 */
|
||||
/* <https://www.gnu.org/licenses/>. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Includes */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#include <log.h>
|
||||
#include <execinfo.h>
|
||||
|
||||
#include <log.h>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Pre definitions */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Definitions */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#define LGD_LOG_WRITER_DEFAULT LG_Log_Write
|
||||
|
||||
#define LGD_MODULE_NAME "lg"
|
||||
|
||||
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* LGT_Base */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
LGT_Base LGG_Base =
|
||||
{
|
||||
NULL,
|
||||
{ LGD_LOG_TYPE_NAME_UNKNOWN, LGD_LOG_TYPE_NAME_TRACE, LGD_LOG_TYPE_NAME_INFO, LGD_LOG_TYPE_NAME_WARNING, LGD_LOG_TYPE_NAME_ERROR}
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Private Prototypes */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* LG_Log_Header */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
LGT_Status LG_Log_Header( LGT_Log_Type_Id, ...);
|
||||
|
||||
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* LG_Log_Footer */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
LGT_Status LG_Log_Footer( LGT_Log_Type_Id, ...);
|
||||
|
||||
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* LG_Log_Format */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
LGT_Status LG_Log_Format( char *, LGT_Log_Type_Id, LGT_Log_Level, char *, char *, long, bool, char *);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
Reference in New Issue
Block a user