00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00018
00019
00020
00021 #include <stdlib.h>
00022 #include <stdio.h>
00023 #include <string.h>
00024 #include <errno.h>
00025 #include <stdarg.h>
00026 #include <syslog.h>
00027 #include <time.h>
00028 #include <ctype.h>
00029 #include "_lcmaps_log.h"
00030
00031
00032
00033
00034 #ifndef DEBUG_LEVEL
00035 #define DEBUG_LEVEL 0
00036 #endif
00037
00038
00039
00040
00041 static FILE * lcmaps_logfp=NULL;
00042 static int logging_usrlog=0;
00043 static int logging_syslog=0;
00044
00045 static int debug_level=0;
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00079 int
00080 lcmaps_log_open(char * path, FILE * fp, unsigned short logtype )
00081 {
00082 char * debug_env = NULL;
00083
00084 if ((logtype & DO_SYSLOG) == DO_SYSLOG)
00085 {
00086 fprintf(stderr,"lcmaps_log_open() error: attempt to do syslogging,");
00087 fprintf(stderr," not supported yet\n");
00088 #if 0
00089 logging_syslog=1;
00090
00091 openlog("EDG LCMAPS", LOG_PID, LOG_DAEMON);
00092 #endif
00093 }
00094 if ((logtype & DO_USRLOG) == DO_USRLOG)
00095 {
00096 logging_usrlog=1;
00097 if (fp != NULL)
00098 {
00099
00100 lcmaps_logfp=fp;
00101 }
00102 else if (path != NULL)
00103 {
00104
00105 if ((lcmaps_logfp = fopen(path, "a")) == NULL)
00106 {
00107 fprintf(stderr, "lcmaps_log_open(): Cannot open logfile %s: %s\n",
00108 path, sys_errlist[errno]);
00109 if (logging_syslog)
00110 {
00111 syslog(LOG_ERR, "lcmaps_log_open(): Cannot open logfile %s\n", path);
00112 }
00113 return 1;
00114 }
00115 }
00116 else
00117 {
00118 fprintf(stderr, "lcmaps_log_open(): Please specify either (open) file descriptor");
00119 fprintf(stderr, " or name of logfile\n");
00120 return 1;
00121 }
00122 }
00123
00124
00125
00126
00127
00128
00129 if ( (int)(DEBUG_LEVEL) > 0 )
00130 {
00131 debug_level = (int)(DEBUG_LEVEL);
00132 }
00133 else if ( (debug_env = getenv("LCMAPS_DEBUG_LEVEL")) )
00134 {
00135
00136 int j = 0;
00137
00138 for (j = 0; j < strlen(debug_env); j++)
00139 {
00140 if (!isdigit((debug_env)[j]))
00141 {
00142 fprintf(stderr,"lcmaps_log_open(): found non-digit in environment variable in \"LCMAPS_DEBUG_LEVEL\" = %s\n", debug_env);
00143 return 1;
00144 }
00145 }
00146 debug_level = atoi(debug_env);
00147 if (debug_level < 0)
00148 {
00149 fprintf(stderr,"lcmaps_log_open(): environment variable in \"LCMAPS_DEBUG_LEVEL\" should be >= 0\n");
00150 return 1;
00151 }
00152 }
00153 else
00154 {
00155 debug_level = 0;
00156 }
00157
00158 if (debug_level > 0)
00159 {
00160 lcmaps_log(0,"lcmaps_log_open(): setting debugging level to %d\n", debug_level);
00161 }
00162
00163 return 0;
00164 }
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00194 int
00195 lcmaps_log(int prty, char * fmt, ...)
00196 {
00197 va_list pvar;
00198 char buf[MAX_LOG_BUFFER_SIZE];
00199 int res;
00200
00201 va_start(pvar, fmt);
00202 res=vsnprintf(buf,MAX_LOG_BUFFER_SIZE,fmt,pvar);
00203 va_end(pvar);
00204 if ( (res >= MAX_LOG_BUFFER_SIZE) || (res < 0) )
00205 {
00206 fprintf(stderr,"lcmaps_log(): log string too long (> %d)\n",
00207 MAX_LOG_BUFFER_SIZE);
00208 }
00209 if (logging_usrlog)
00210 {
00211 if (lcmaps_logfp == NULL)
00212 {
00213 fprintf(stderr,"lcmaps_log() error: cannot open file descriptor\n");
00214 return 1;
00215 }
00216 fprintf(lcmaps_logfp,"LCMAPS %d: %s",prty,buf);
00217 fflush(lcmaps_logfp);
00218 }
00219 if (logging_syslog && prty)
00220 {
00221 syslog(prty, buf);
00222 }
00223
00224 return 0;
00225 }
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00254 int
00255 lcmaps_log_debug(int debug_lvl, char * fmt, ...)
00256 {
00257 va_list pvar;
00258 char buf[MAX_LOG_BUFFER_SIZE];
00259 int res;
00260
00261 va_start(pvar, fmt);
00262 res=vsnprintf(buf,MAX_LOG_BUFFER_SIZE,fmt,pvar);
00263 va_end(pvar);
00264 if ( (res >= MAX_LOG_BUFFER_SIZE) || (res < 0) )
00265 {
00266 fprintf(stderr,"lcmaps_log(): log string too long (> %d)\n",
00267 MAX_LOG_BUFFER_SIZE);
00268 }
00269 if (debug_lvl <= debug_level)
00270 {
00271 lcmaps_log(0,buf);
00272 return 0;
00273 }
00274 return 1;
00275 }
00276
00277
00278
00279
00280
00281
00282
00294 int
00295 lcmaps_log_close()
00296 {
00297 if (lcmaps_logfp == NULL)
00298 return 1;
00299
00300 fclose(lcmaps_logfp);
00301 lcmaps_logfp=NULL;
00302 return 0;
00303 }
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00334 int
00335 lcmaps_log_time(int prty, char * fmt, ...)
00336 {
00337 va_list pvar;
00338 char buf[MAX_LOG_BUFFER_SIZE];
00339 char * datetime;
00340 char * tmpbuf;
00341 int res;
00342 time_t clock;
00343 struct tm * tmp;
00344
00345
00346 va_start(pvar, fmt);
00347 res=vsnprintf(buf,MAX_LOG_BUFFER_SIZE,fmt,pvar);
00348 va_end(pvar);
00349 if ( (res >= MAX_LOG_BUFFER_SIZE) || (res < 0) )
00350 {
00351 fprintf(stderr,"lcmaps_log_time(): log string too long (> %d)\n",
00352 MAX_LOG_BUFFER_SIZE);
00353 }
00354
00355 time(&clock);
00356 tmp = localtime(&clock);
00357
00358 datetime = malloc(sizeof(char) * 20);
00359
00360 res=snprintf(datetime, 20, "%04d-%02d-%02d.%02d:%02d:%02d",
00361 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
00362 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
00363 if ( (res >= 20) || (res < 0) )
00364 {
00365 fprintf(stderr,"lcmaps_log_time(): date string too long (> %d)\n",
00366 20);
00367 }
00368
00369 tmpbuf = (char *) malloc ((strlen(datetime) + strlen(buf) + strlen(" : ")) * sizeof(char) + 1);
00370 strcpy(tmpbuf, datetime);
00371 strcat(tmpbuf, " : ");
00372 strcat(tmpbuf, buf);
00373
00374 if (logging_usrlog)
00375 {
00376 if (lcmaps_logfp == NULL)
00377 {
00378 fprintf(stderr,"lcmaps_log_time() error: cannot open file descriptor\n");
00379 return 1;
00380 }
00381 fprintf(lcmaps_logfp,"LCMAPS %d: %s",prty,tmpbuf);
00382 fflush(lcmaps_logfp);
00383 }
00384 if (logging_syslog && prty)
00385 {
00386 syslog(prty, tmpbuf);
00387 }
00388
00389 free(datetime);
00390 free(tmpbuf);
00391
00392 return 0;
00393 }
00394
00395
00396
00397
00398
00399
00400
00401