2
// Abstract logging system used to facilitate multiple modes
18
static int _LOGGER_mode = _LOGGER_SYSLOG;
19
static int _LOGGER_syslog_mode = LOG_MAIL|LOG_INFO;
21
static int _LOGGER_mode = _LOGGER_STDERR;
22
static int _LOGGER_syslog_mode = 0;
24
static FILE *_LOGGER_outf;
26
struct LOGGER_globals {
31
// Create and Initialise the global structure for LOGGER,
32
// we init it to have NO wrapping.
34
static struct LOGGER_globals LOGGER_glb={ 0, 0 };
37
/*------------------------------------------------------------------------
38
Procedure: LOGGER_get_file ID:1
39
Purpose: Returns the pointer to the file being used to output logs to
43
------------------------------------------------------------------------*/
44
FILE *LOGGER_get_file( void )
50
/*------------------------------------------------------------------------
51
Procedure: LOGGER_set_output_mode ID:1
52
Purpose: Sets the message/log output method, ie, stderr, stdout
57
------------------------------------------------------------------------*/
58
int LOGGER_set_output_mode( int modechoice )
60
_LOGGER_mode = modechoice;
64
/*------------------------------------------------------------------------
65
Procedure: LOGGER_set_output_file ID:1
66
Purpose: Sets the output file for when _LOGGER_mode is set to
71
------------------------------------------------------------------------*/
72
int LOGGER_set_output_file( FILE *f )
78
/*------------------------------------------------------------------------
79
Procedure: LOGGER_set_syslog_mode ID:1
80
Purpose: Sets the mode that messaging to the syslog daemon will
81
be sent as (ie, LOG_MAIL|LOG_INFO)
85
------------------------------------------------------------------------*/
86
int LOGGER_set_syslog_mode( int syslogmode )
88
_LOGGER_syslog_mode = syslogmode;
95
/*------------------------------------------------------------------------
96
Procedure: LOGGER_set_logfile ID:1
97
Purpose: Opens and setups the internal Log file file pointer with the
98
log file as given by lfname
102
------------------------------------------------------------------------*/
103
int LOGGER_set_logfile( char *lfname )
107
_LOGGER_outf = fopen(lfname,"a");
111
syslog(1,"LOGGER_set_logfile: ERROR - Cannot open logfile '%s' (%s)",lfname,strerror(errno));
113
fprintf(stderr, "LOGGER_set_logfile: ERROR - Cannot open logfile '%s' (%s)\n", lfname, strerror(errno));
123
/*------------------------------------------------------------------------
124
Procedure: LOGGER_set_wraplength ID:1
125
Purpose: Sets the character count at which LOGGER will break a line
126
Input: int length: Positive integer indicating number of chracters at which to wrap at
129
------------------------------------------------------------------------*/
130
int LOGGER_set_wraplength( int length )
134
LOGGER_glb.wraplength = length;
137
return LOGGER_glb.wraplength;
140
/*------------------------------------------------------------------------
141
Procedure: LOGGER_set_wrap ID:1
142
Purpose: Set log output wrapping to on or off
143
Input: int level: 0 = no wrap, > 0 = wrap.
146
------------------------------------------------------------------------*/
147
int LOGGER_set_wrap( int level )
151
LOGGER_glb.wrap = level;
154
return LOGGER_glb.wrap;
159
/*------------------------------------------------------------------------
160
Procedure: LOGGER_close_logfile ID:1
161
Purpose: Closes the modules log file pointer.
165
------------------------------------------------------------------------*/
166
int LOGGER_close_logfile( void )
170
if (_LOGGER_outf) fclose(_LOGGER_outf);
177
/*------------------------------------------------------------------------
178
Procedure: LOGGER_clean_output ID:1
179
Purpose: Checks through the output string for any characters which could cause
180
potential 'isssues' with the data writing calls, items such as stray non-escaped
181
% characters can cause havoc.
182
Input: char *string: Raw string
183
int maxsize: Maximum available buffer size for this string to expand to
186
------------------------------------------------------------------------*/
187
int LOGGER_clean_output( char *string, char **buffer )
194
int slen = strlen( string );
196
int maxsize = slen *2;
198
// First up, allocate maxsize bytes for a temporary new string.
199
newstr = malloc(slen *2 +1);
200
if ( newstr == NULL )
202
// FIXME - Report an error here ... to -somewhere-
214
// Do we need to apply any wrapping to the output? If so then we
215
// shall embark on a journey of strange space and distance
216
// evaluations to determine if we should wrap now or later
218
if ( LOGGER_glb.wrap > 0 )
220
if (isspace((int)*q))
222
next_space = strpbrk( (q+1), "\t\r\n\v " );
223
if (next_space != NULL)
225
if ((line_size +(next_space -q)) >= LOGGER_glb.wraplength)
235
if ( line_size >= LOGGER_glb.wraplength )
244
// If the string has a % in it, then we need to encode it as
245
// a DOUBLE % symbol.
248
// if (strchr("fdlsxXn",*(q+1)))
256
// Copy the character of the string in
259
// Move everything along.
265
if ( pc > (maxsize -1) ) {
272
// This will have to be deallocated later!
273
if (newstr) *buffer = newstr;
278
/*------------------------------------------------------------------------
279
Procedure: LOGGER_log ID:1
280
Purpose: Logs the params as supplied to the required
281
output as defined by LOGGER_set_output
285
------------------------------------------------------------------------*/
286
int LOGGER_log( char *format, ...)
289
char tmpoutput[10240];
290
char linebreak[]="\n";
291
char nolinebreak[]="";
296
// get our variable arguments
297
va_start(ptr,format);
299
// produce output, and spit to the log file
301
vsprintf(tmpoutput, format, ptr);
303
vsnprintf(tmpoutput,10240,format,ptr);
306
LOGGER_clean_output( tmpoutput, &output );
308
if ( output[strlen(output)-1] == '\n' ) {
309
lineend = nolinebreak;
315
if ( output[strlen(output)-1] == '\n' ) { lineend = nolinebreak; } else { lineend = linebreak; }
317
// Send the output to the appropriate output destination
318
switch (_LOGGER_mode) {
320
fprintf(stderr,"%s%s",output, lineend );
323
syslog(_LOGGER_syslog_mode,output);
326
fprintf(stdout,"%s%s",output, lineend);
330
fprintf(_LOGGER_outf,"%s%s",output,lineend);
331
fflush(_LOGGER_outf);
334
fprintf(stdout,"LOGGER-Default: %s%s",output,lineend);
338
if (output) free(output);