Initialize a log. The function sets the parameters of a logging thread. The parameters are the usual memory allocation and deallocation functions and the log file name format string. This format string can contain at most four %d as formatting element. This will be passed to sprintf with arguments as year, month, day and hour in this order. This will ease log rotating.
Note that log file name calculation is a CPU consuming process and therefore it is not performed for each log item. The log system recalculates the log file name and closes the old log file and opens a new one whenever the actual log to be written and the last log wrote is in a different time interval. The time interval is identified by the time stamp value divided (integer division) by the time span value. This is 3600 when you want to rotate the log hourly, 86400 if you want to rotate the log daily. Other rotations, like monthly do not work correctly.
To do this the caller has to set the TimeSpan field of the log structure. There is no support function to set this.
For example:
if( log_init(&ErrLog,alloc_Alloc,alloc_Free,pM_AppLog,CONFIG("log.err.file"),LOGTYPE_NORMAL) )
return 1;
if( cft_GetEx(&MyCONF,"log.err.span",&ConfNode,NULL,&(ErrLog.TimeSpan),NULL,NULL) )
ErrLog.TimeSpan = 0;
as you can see in the file ad.c Setting TimeSpan to zero results no log rotation.
Note that it is a good practice to set the TimeSpan value to positive (non zero) even if the log is not rotated. If you ever delete the log file while the logging application is running the log is not written anymore until the log file is reopened.
The log type can be LOGTYPE_NORMAL to perform asynchronous high performance logging and
LOGTYPE_SYNCHRONOUS for syncronous, "panic" logging. Panic logging keeps the file continously
opened until the log is shut down and does not perform log rotation.
int log_init(ptLogger pLOG,
void *(*memory_allocating_function)(size_t, void *),
void (*memory_releasing_function)(void *, void *),
void *pMemorySegment,
char *pszLogFileName,
int iLogType
){