- Make private default Log_Writer function,

- Make private LGG_Base,
- Add LG_Log_Stream_Get/Set API,
- Minor file header fixies.
This commit is contained in:
Arnaud G. GIBERT 2024-04-17 17:52:43 +02:00
parent 29ff5acb9f
commit 17452c794b
4 changed files with 144 additions and 78 deletions

View File

@ -3,14 +3,14 @@
/*----------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/
/* This file is part of liblog. */ /* This file is part of LibLog. */
/* */ /* */
/* Drummer is free software: you can redistribute it and/or modify it */ /* LibLog is free software: you can redistribute it and/or modify it */
/* under the terms of the GNU General Public License as published by */ /* under the terms of the GNU General Public License as published by */
/* the Free Software Foundation, either version 3 of the License, or */ /* the Free Software Foundation, either version 3 of the License, or */
/* (at your option) any later version. */ /* (at your option) any later version. */
/* */ /* */
/* Drummer is distributed in the hope that it will be useful, */ /* LibLog is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */ /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */ /* GNU General Public License for more details. */
@ -19,6 +19,8 @@
/* along with Drummer. If not, see <https://www.gnu.org/licenses/>. */ /* along with Drummer. If not, see <https://www.gnu.org/licenses/>. */
/*----------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/
/* Includes */ /* Includes */
/*----------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/
@ -75,7 +77,7 @@ int main( int argc, char **argv)
LGT_Status status; LGT_Status status;
if( ( status = LG_Library_Open( stderr, LGD_LOG_WRITER_DEFAULT, true)) != LGS_OK) if( ( status = LG_Library_Open( LGD_LOG_WRITER_DEFAULT, true)) != LGS_OK)
{ {
fprintf( stderr, "Can't open LibLog library: (%d)\n", status); fprintf( stderr, "Can't open LibLog library: (%d)\n", status);
return( -1); return( -1);

View File

@ -3,14 +3,14 @@
/*----------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/
/* This file is part of liblog. */ /* This file is part of LibLog. */
/* */ /* */
/* Drummer is free software: you can redistribute it and/or modify it */ /* LibLog is free software: you can redistribute it and/or modify it */
/* under the terms of the GNU Lesser General Public License as published */ /* under the terms of the GNU Lesser General Public License as published */
/* by the Free Software Foundation, either version 3 of the License, or */ /* by the Free Software Foundation, either version 3 of the License, or */
/* (at your option) any later version. */ /* (at your option) any later version. */
/* */ /* */
/* Drummer is distributed in the hope that it will be useful, */ /* LibLog is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */ /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU Lesser General Public License for more details. */ /* GNU Lesser General Public License for more details. */
@ -20,6 +20,8 @@
/* <https://www.gnu.org/licenses/>. */ /* <https://www.gnu.org/licenses/>. */
/*----------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/
/* Includes */ /* Includes */
/*----------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/
@ -47,14 +49,6 @@
/*----------------------------------------------------------------------------*/
/* Global definitions */
/*----------------------------------------------------------------------------*/
#define LGD_LOG_WRITER_DEFAULT LG_Log_Write
/*----------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/
/* Definitions */ /* Definitions */
/*----------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/
@ -144,23 +138,18 @@ typedef short LGT_Log_Level;
/*---------------------------------------------------------------------------------*/
/* LGT_Base */
/*---------------------------------------------------------------------------------*/
typedef LGT_Status (*LGT_Log_Writer_Ptr)( LGT_Log_Type_Id, char *, va_list); /*----------------------------------------------------------------------------*/
/* Log Writer */
/*----------------------------------------------------------------------------*/
typedef struct LGT_Base typedef LGT_Status (*LGT_Log_Writer_Ptr)( FILE *, LGT_Log_Type_Id, char *, va_list);
{
FILE *Log_Stream_Out_Ptr; #define LGD_LOG_STREAM_DEFAULT ( FILE *)NULL
char *Log_Type_Name_Tab[ LGD_LOG_TYPE_NB]; #define LGD_LOG_WRITER_DEFAULT ( LGT_Log_Writer_Ptr)NULL
LGT_Log_Writer_Ptr Log_Writer_Ptr;
} LGT_Base;
LGD_API LGT_Base LGG_Base;
@ -172,12 +161,11 @@ LGD_API LGT_Base LGG_Base;
/*----------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/
/* LG_Library_Open */ /* LG_Library_Open */
/*----------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/
/* (I) Log_Stream_Out_Ptr: Log output stream */
/* (I) Log_Writer_Ptr: Log writer pointer */ /* (I) Log_Writer_Ptr: Log writer pointer */
/* (I) Header_Print_Flag: Start by printing an header */ /* (I) Header_Print_Flag: Start by printing an header */
/*----------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/
LGD_API LGT_Status LG_Library_Open( FILE *, LGT_Log_Writer_Ptr, bool); LGD_API LGT_Status LG_Library_Open( LGT_Log_Writer_Ptr, bool);
@ -191,6 +179,26 @@ LGD_API LGT_Status LG_Library_Close( bool);
/*----------------------------------------------------------------------------*/
/* LG_Log_Stream_Get */
/*----------------------------------------------------------------------------*/
/* (O) Log_Stream_Ptr_Ptr: Log output stream */
/*----------------------------------------------------------------------------*/
LGD_API LGT_Status LG_Log_Stream_Get( FILE **);
/*----------------------------------------------------------------------------*/
/* LG_Log_Stream_Set */
/*----------------------------------------------------------------------------*/
/* (I) Log_Stream_Ptr: Log output stream */
/*----------------------------------------------------------------------------*/
LGD_API LGT_Status LG_Log_Stream_Set( FILE *);
/*----------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/
/* LG_Log */ /* LG_Log */
/*----------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/
@ -199,14 +207,6 @@ LGD_API LGT_Status LG_Log( LGT_Log_Type_Id, LGT_Log_Level, char *, char *, lo
/*----------------------------------------------------------------------------*/
/* LG_Log_Write */
/*----------------------------------------------------------------------------*/
LGD_API LGT_Status LG_Log_Write( LGT_Log_Type_Id, char *, va_list);
/*------------------------------------------------------------------------------*/ /*------------------------------------------------------------------------------*/
/* LG_Stack_Trace_Print */ /* LG_Stack_Trace_Print */
/*------------------------------------------------------------------------------*/ /*------------------------------------------------------------------------------*/

View File

@ -3,14 +3,14 @@
/*----------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/
/* This file is part of liblog. */ /* This file is part of LibLog. */
/* */ /* */
/* Drummer is free software: you can redistribute it and/or modify it */ /* LibLog is free software: you can redistribute it and/or modify it */
/* under the terms of the GNU Lesser General Public License as published */ /* under the terms of the GNU Lesser General Public License as published */
/* by the Free Software Foundation, either version 3 of the License, or */ /* by the Free Software Foundation, either version 3 of the License, or */
/* (at your option) any later version. */ /* (at your option) any later version. */
/* */ /* */
/* Drummer is distributed in the hope that it will be useful, */ /* LibLog is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */ /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU Lesser General Public License for more details. */ /* GNU Lesser General Public License for more details. */
@ -20,14 +20,14 @@
/* <https://www.gnu.org/licenses/>. */ /* <https://www.gnu.org/licenses/>. */
/*----------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/
/* Includes */ /* Includes */
/*----------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/
#define _LIBLOG_C_ #define _LIBLOG_C_
#include <liblog.h> #include <liblog.h>
@ -38,9 +38,8 @@
/* LG_Library_Open */ /* LG_Library_Open */
/*----------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/
LGT_Status LG_Library_Open( FILE *Log_Stream_Out_Ptr, LGT_Log_Writer_Ptr Log_Writer_Ptr, bool Header_Print_Flag) LGT_Status LG_Library_Open( 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; LGG_Base.Log_Writer_Ptr = Log_Writer_Ptr;
if( Header_Print_Flag) LG_Log_Header( LGD_LOG_LEVEL_DEFAULT); if( Header_Print_Flag) LG_Log_Header( LGD_LOG_LEVEL_DEFAULT);
@ -67,27 +66,63 @@ LGT_Status LG_Library_Close( bool Footer_Print_Flag)
/*----------------------------------------------------------------------------*/
/* 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 */ /* LG_Log_Header */
/*----------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/
LGT_Status LG_Log_Header( LGT_Log_Type_Id Log_Type_Id, ...) LGT_Status LG_Log_Header( LGT_Log_Type_Id Log_Type_Id, ...)
{ {
va_list args; va_list args;
char fmt[255]; char fmt[255];
LGT_Log_Writer_Ptr log_writer_ptr; FILE *log_stream_out_ptr;
LGT_Status status; 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); 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); va_start( args, Log_Type_Id);
sprintf( fmt, "\nYY/MM/DD|hh:mm:ss.uuuu|Typ|Lev|Name| File Name |Line| Log Message\n"); 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); status = ( *log_writer_ptr)( log_stream_out_ptr, Log_Type_Id, fmt, args);
sprintf( fmt, "--------+-------------+---+---+----+---------------------+----+-------------------------------------------------------------------\n"); sprintf( fmt, "--------+-------------+---+---+----+---------------------+----+-------------------------------------------------------------------\n");
status = ( *log_writer_ptr)( Log_Type_Id, fmt, args); status = ( *log_writer_ptr)( log_stream_out_ptr, Log_Type_Id, fmt, args);
va_end( args); va_end( args);
@ -104,18 +139,20 @@ LGT_Status LG_Log_Header( LGT_Log_Type_Id Log_Type_Id, ...)
LGT_Status LG_Log_Footer( LGT_Log_Type_Id Log_Type_Id, ...) LGT_Status LG_Log_Footer( LGT_Log_Type_Id Log_Type_Id, ...)
{ {
va_list args; va_list args;
char fmt[255]; char fmt[255];
LGT_Log_Writer_Ptr log_writer_ptr; FILE *log_stream_out_ptr;
LGT_Status status; 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); 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); va_start( args, Log_Type_Id);
sprintf( fmt, "--------+-------------+---+---+----+---------------------+----+-------------------------------------------------------------------\n"); sprintf( fmt, "--------+-------------+---+---+----+---------------------+----+-------------------------------------------------------------------\n");
status = ( *log_writer_ptr)( Log_Type_Id, fmt, args); status = ( *log_writer_ptr)( log_stream_out_ptr, Log_Type_Id, fmt, args);
va_end( args); va_end( args);
@ -132,11 +169,12 @@ LGT_Status LG_Log_Footer( LGT_Log_Type_Id Log_Type_Id, ...)
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, ...) 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; va_list args;
char fmt[255]; char fmt[255];
int rc; int rc;
LGT_Log_Writer_Ptr log_writer_ptr; FILE *log_stream_out_ptr;
LGT_Status status; 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) if( LG_Log_Format( fmt, Log_Type_Id, Log_Level, Module_Name, File_Name, Line_Id, LF_Flag, Log_Fmt) != LGS_OK)
@ -147,9 +185,10 @@ LGT_Status LG_Log( LGT_Log_Type_Id Log_Type_Id, LGT_Log_Level Log_Level, char
va_start( args, Log_Fmt); va_start( args, Log_Fmt);
log_writer_ptr = ( ( LGG_Base.Log_Writer_Ptr == NULL) ? &LG_Log_Write : LGG_Base.Log_Writer_Ptr); 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_Type_Id, fmt, args); status = ( *log_writer_ptr)( log_stream_out_ptr, Log_Type_Id, fmt, args);
va_end( args); va_end( args);
@ -196,12 +235,12 @@ LGT_Status LG_Log_Format( char *Out_Fmt, LGT_Log_Type_Id Log_Type_Id, LGT_Log
/* LG_Log_Write */ /* LG_Log_Write */
/*----------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/
LGT_Status LG_Log_Write( LGT_Log_Type_Id Log_Type_Id, char *Out_Fmt_Ptr, va_list Args) 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; int rc;
rc = vfprintf( LGG_Base.Log_Stream_Out_Ptr, Out_Fmt_Ptr, Args); rc = vfprintf( Log_Stream_Out_Ptr, Out_Fmt_Ptr, Args);
if( rc < 0) if( rc < 0)
{ {

View File

@ -3,14 +3,14 @@
/*----------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/
/* This file is part of liblog. */ /* This file is part of LibLog. */
/* */ /* */
/* Drummer is free software: you can redistribute it and/or modify it */ /* LibLog is free software: you can redistribute it and/or modify it */
/* under the terms of the GNU Lesser General Public License as published */ /* under the terms of the GNU Lesser General Public License as published */
/* by the Free Software Foundation, either version 3 of the License, or */ /* by the Free Software Foundation, either version 3 of the License, or */
/* (at your option) any later version. */ /* (at your option) any later version. */
/* */ /* */
/* Drummer is distributed in the hope that it will be useful, */ /* LibLog is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */ /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU Lesser General Public License for more details. */ /* GNU Lesser General Public License for more details. */
@ -20,6 +20,8 @@
/* <https://www.gnu.org/licenses/>. */ /* <https://www.gnu.org/licenses/>. */
/*----------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/
/* Includes */ /* Includes */
/*----------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/
@ -45,20 +47,35 @@
/* Definitions */ /* Definitions */
/*----------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/
#define LGD_LOG_WRITER_DEFAULT LG_Log_Write
#define LGD_MODULE_NAME "lg" #define LGD_MODULE_NAME "lg"
/*----------------------------------------------------------------------------*/
/* LGT_Base */
/*----------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
/* LGT_Base */
/*---------------------------------------------------------------------------------*/
typedef struct LGT_Base
{
FILE *Log_Stream_Out_Ptr;
char *Log_Type_Name_Tab[ LGD_LOG_TYPE_NB];
LGT_Log_Writer_Ptr Log_Writer_Ptr;
} LGT_Base;
#define LGD_LOG_STREAM_DEFAULT_VALUE ( FILE *)stderr
#define LGD_LOG_WRITER_DEFAULT_VALUE ( LGT_Log_Writer_Ptr)&LG_Log_Write
LGT_Base LGG_Base = LGT_Base LGG_Base =
{ {
NULL, LGD_LOG_STREAM_DEFAULT,
{ 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} { 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},
LGD_LOG_WRITER_DEFAULT
}; };
@ -93,6 +110,14 @@ LGT_Status LG_Log_Format( char *, LGT_Log_Type_Id, LGT_Log_Level, char *, cha
/*----------------------------------------------------------------------------*/
/* LG_Log_Write */
/*----------------------------------------------------------------------------*/
LGT_Status LG_Log_Write( FILE *Log_Stream_Out_Ptr, LGT_Log_Type_Id, char *, va_list);