1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
|
/* Abstract logging system used to facilitate multiple modes*/
/* of logging*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef WIN32
#include <syslog.h>
#endif
#include <errno.h>
#include <stdarg.h>
#include <ctype.h>
#include "logger.h"
#ifdef WIN32
#include "../os_specific/win_mem_alloc.h" /* MALLOC */
#else
#include "../os_specific/sci_mem_alloc.h" /* MALLOC */
#endif
#ifndef WIN32
static int _LOGGER_mode = _LOGGER_SYSLOG;
static int _LOGGER_syslog_mode = LOG_MAIL|LOG_INFO;
#else
static int _LOGGER_mode = _LOGGER_STDERR;
static int _LOGGER_syslog_mode = 0;
#endif
static FILE *_LOGGER_outf;
struct LOGGER_globals {
int wrap;
int wraplength;
};
/* Create and Initialise the global structure for LOGGER,*/
/* we init it to have NO wrapping.*/
static struct LOGGER_globals LOGGER_glb={ 0, 0 };
/*------------------------------------------------------------------------
Procedure: LOGGER_get_file ID:1
Purpose: Returns the pointer to the file being used to output logs to
Input:
Output:
Errors:
------------------------------------------------------------------------*/
FILE *LOGGER_get_file( void )
{
return _LOGGER_outf;
}
/*------------------------------------------------------------------------
Procedure: LOGGER_set_output_mode ID:1
Purpose: Sets the message/log output method, ie, stderr, stdout
or syslog
Input:
Output:
Errors:
------------------------------------------------------------------------*/
int LOGGER_set_output_mode( int modechoice )
{
_LOGGER_mode = modechoice;
return 0;
}
/*------------------------------------------------------------------------
Procedure: LOGGER_set_output_file ID:1
Purpose: Sets the output file for when _LOGGER_mode is set to
_LOGGER_file
Input:
Output:
Errors:
------------------------------------------------------------------------*/
int LOGGER_set_output_file( FILE *f )
{
_LOGGER_outf = f;
return 0;
}
/*------------------------------------------------------------------------
Procedure: LOGGER_set_syslog_mode ID:1
Purpose: Sets the mode that messaging to the syslog daemon will
be sent as (ie, LOG_MAIL|LOG_INFO)
Input:
Output:
Errors:
------------------------------------------------------------------------*/
int LOGGER_set_syslog_mode( int syslogmode )
{
_LOGGER_syslog_mode = syslogmode;
return 0;
}
/*------------------------------------------------------------------------
Procedure: LOGGER_set_logfile ID:1
Purpose: Opens and setups the internal Log file file pointer with the
log file as given by lfname
Input:
Output:
Errors:
------------------------------------------------------------------------*/
int LOGGER_set_logfile( char *lfname )
{
int result = 0;
_LOGGER_outf = fopen(lfname,"a");
if (!_LOGGER_outf)
{
#ifndef WIN32
syslog(1,"LOGGER_set_logfile: ERROR - Cannot open logfile '%s' (%s)",lfname,strerror(errno));
#else
fprintf(stderr, "LOGGER_set_logfile: ERROR - Cannot open logfile '%s' (%s)\n", lfname, strerror(errno));
#endif
result = -1;
}
return result;
}
/*------------------------------------------------------------------------
Procedure: LOGGER_set_wraplength ID:1
Purpose: Sets the character count at which LOGGER will break a line
Input: int length: Positive integer indicating number of chracters at which to wrap at
Output:
Errors:
------------------------------------------------------------------------*/
int LOGGER_set_wraplength( int length )
{
if ( length >= 0 )
{
LOGGER_glb.wraplength = length;
}
return LOGGER_glb.wraplength;
}
/*------------------------------------------------------------------------
Procedure: LOGGER_set_wrap ID:1
Purpose: Set log output wrapping to on or off
Input: int level: 0 = no wrap, > 0 = wrap.
Output:
Errors:
------------------------------------------------------------------------*/
int LOGGER_set_wrap( int level )
{
if ( level >= 0 )
{
LOGGER_glb.wrap = level;
}
return LOGGER_glb.wrap;
}
/*------------------------------------------------------------------------
Procedure: LOGGER_close_logfile ID:1
Purpose: Closes the modules log file pointer.
Input:
Output:
Errors:
------------------------------------------------------------------------*/
int LOGGER_close_logfile( void )
{
int result = 0;
if (_LOGGER_outf) fclose(_LOGGER_outf);
return result;
}
/*------------------------------------------------------------------------
Procedure: LOGGER_clean_output ID:1
Purpose: Checks through the output string for any characters which could cause
potential 'isssues' with the data writing calls, items such as stray non-escaped
% characters can cause havoc.
Input: char *string: Raw string
int maxsize: Maximum available buffer size for this string to expand to
Output:
Errors:
------------------------------------------------------------------------*/
int LOGGER_clean_output( char *string, char **buffer )
{
char *newstr;
char *p, *q;
char *next_space;
int pc;
int slen = strlen( string );
int line_size;
int maxsize = slen *2;
/* First up, allocate maxsize bytes for a temporary new string.*/
newstr = MALLOC(slen *2 +1);
if ( newstr == NULL )
{
/* FIXME - Report an error here ... to -somewhere-*/
return -1;
}
p = newstr;
q = string;
pc = 0;
line_size = 0;
while (slen--)
{
/* Do we need to apply any wrapping to the output? If so then we*/
/* shall embark on a journey of strange space and distance*/
/* evaluations to determine if we should wrap now or later*/
if ( LOGGER_glb.wrap > 0 )
{
if (isspace((int)*q))
{
next_space = strpbrk( (q+1), "\t\r\n\v " );
if (next_space != NULL)
{
if ((line_size +(next_space -q)) >= LOGGER_glb.wraplength)
{
*p = '\n';
p++;
pc++;
line_size = 0;
}
}
}
if ( line_size >= LOGGER_glb.wraplength )
{
*p = '\n';
p++;
pc++;
line_size = 0;
}
}
/* If the string has a % in it, then we need to encode it as*/
/* a DOUBLE % symbol.*/
if (*q == '%') {
/* if (strchr("fdlsxXn",*(q+1)))*/
/* {*/
*p = '%';
p++;
pc++;
/* }*/
}
/* Copy the character of the string in*/
*p = *q;
/* Move everything along.*/
q++;
p++;
pc++;
line_size++;
if ( pc > (maxsize -1) ) {
break;
}
}
*p = '\0';
/* This will have to be deallocated later!*/
if (newstr) *buffer = newstr;
return 0;
}
/*------------------------------------------------------------------------
Procedure: LOGGER_log ID:1
Purpose: Logs the params as supplied to the required
output as defined by LOGGER_set_output
Input:
Output:
Errors:
------------------------------------------------------------------------*/
int LOGGER_log( char *format, ...)
{
va_list ptr;
char tmpoutput[10240];
char linebreak[]="\n";
char nolinebreak[]="";
char *lineend;
char *output;
/* get our variable arguments*/
va_start(ptr,format);
/* produce output, and spit to the log file*/
#ifdef NO_SNPRINTF
vsprintf(tmpoutput, format, ptr);
#else
#if WIN32
vsprintf(tmpoutput, format, ptr);
#else
vsnprintf(tmpoutput,10240,format,ptr);
#endif
#endif
LOGGER_clean_output( tmpoutput, &output );
if ( output[strlen(output)-1] == '\n' ) {
lineend = nolinebreak;
}
else {
lineend = linebreak;
}
if ( output[strlen(output)-1] == '\n' ) { lineend = nolinebreak; } else { lineend = linebreak; }
/* Send the output to the appropriate output destination*/
switch (_LOGGER_mode) {
case _LOGGER_STDERR:
fprintf(stderr,"%s%s",output, lineend );
break;
case _LOGGER_SYSLOG:
#ifndef WIN32
syslog(_LOGGER_syslog_mode,output);
#endif
break;
case _LOGGER_STDOUT:
fprintf(stdout,"%s%s",output, lineend);
fflush(stdout);
break;
case _LOGGER_FILE:
fprintf(_LOGGER_outf,"%s%s",output,lineend);
fflush(_LOGGER_outf);
break;
default:
fprintf(stdout,"LOGGER-Default: %s%s",output,lineend);
}
if (output) FREE(output);
return 0;
}
|