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;
00046 static char * extra_logstr = NULL;
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00080 int
00081 lcmaps_log_open(char * path, FILE * fp, unsigned short logtype )
00082 {
00083 char * debug_env = NULL;
00084 char * logstr_env = NULL;
00085
00086 if ((logtype & DO_SYSLOG) == DO_SYSLOG)
00087 {
00088 fprintf(stderr,"lcmaps_log_open() error: attempt to do syslogging,");
00089 fprintf(stderr," not supported yet\n");
00090 #if 0
00091 logging_syslog=1;
00092
00093 openlog("EDG LCMAPS", LOG_PID, LOG_DAEMON);
00094 #endif
00095 }
00096 if ((logtype & DO_USRLOG) == DO_USRLOG)
00097 {
00098 logging_usrlog=1;
00099 if (fp != NULL)
00100 {
00101
00102 lcmaps_logfp=fp;
00103 }
00104 else if (path != NULL)
00105 {
00106
00107 if ((lcmaps_logfp = fopen(path, "a")) == NULL)
00108 {
00109 fprintf(stderr, "lcmaps_log_open(): Cannot open logfile %s: %s\n",
00110 path, sys_errlist[errno]);
00111 if (logging_syslog)
00112 {
00113 syslog(LOG_ERR, "lcmaps_log_open(): Cannot open logfile %s\n", path);
00114 }
00115 return 1;
00116 }
00117 }
00118 else
00119 {
00120 fprintf(stderr, "lcmaps_log_open(): Please specify either (open) file descriptor");
00121 fprintf(stderr, " or name of logfile\n");
00122 return 1;
00123 }
00124 }
00125
00126
00127
00128
00129
00130
00131 if ( (int)(DEBUG_LEVEL) > 0 )
00132 {
00133 debug_level = (int)(DEBUG_LEVEL);
00134 }
00135 else if ( (debug_env = getenv("LCMAPS_DEBUG_LEVEL")) )
00136 {
00137
00138 int j = 0;
00139
00140 for (j = 0; j < strlen(debug_env); j++)
00141 {
00142 if (!isdigit((debug_env)[j]))
00143 {
00144 fprintf(stderr,"lcmaps_log_open(): found non-digit in environment variable in \"LCMAPS_DEBUG_LEVEL\" = %s\n", debug_env);
00145 return 1;
00146 }
00147 }
00148 debug_level = atoi(debug_env);
00149 if (debug_level < 0)
00150 {
00151 fprintf(stderr,"lcmaps_log_open(): environment variable in \"LCMAPS_DEBUG_LEVEL\" should be >= 0\n");
00152 return 1;
00153 }
00154 }
00155 else
00156 {
00157 debug_level = 0;
00158 }
00159
00160 if (debug_level > 0)
00161 {
00162 lcmaps_log(0,"lcmaps_log_open(): setting debugging level to %d\n", debug_level);
00163 }
00164
00165
00166
00167
00168
00169 if ( (logstr_env = getenv("JOB_REPOSITORY_ID")) != NULL )
00170 {
00171 extra_logstr = strdup(logstr_env);
00172 }
00173 else if ( (logstr_env = getenv("GATEKEEPER_JM_ID")) != NULL )
00174 {
00175 extra_logstr = strdup(logstr_env);
00176 }
00177
00178 return 0;
00179 }
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00209 int
00210 lcmaps_log(int prty, char * fmt, ...)
00211 {
00212 va_list pvar;
00213 char buf[MAX_LOG_BUFFER_SIZE];
00214 int res;
00215
00216 va_start(pvar, fmt);
00217 res=vsnprintf(buf,MAX_LOG_BUFFER_SIZE,fmt,pvar);
00218 va_end(pvar);
00219 if ( (res >= MAX_LOG_BUFFER_SIZE) || (res < 0) )
00220 {
00221 fprintf(stderr,"lcmaps_log(): log string too long (> %d)\n",
00222 MAX_LOG_BUFFER_SIZE);
00223 }
00224 if (logging_usrlog)
00225 {
00226 if (lcmaps_logfp == NULL)
00227 {
00228 fprintf(stderr,"lcmaps_log() error: cannot open file descriptor\n");
00229 return 1;
00230 }
00231 if (extra_logstr == NULL)
00232 {
00233 fprintf(lcmaps_logfp,"LCMAPS %d: %s", prty, buf);
00234 }
00235 else
00236 {
00237 fprintf(lcmaps_logfp,"LCMAPS %d: %s : %s", prty, extra_logstr, buf);
00238 }
00239 fflush(lcmaps_logfp);
00240 }
00241 if (logging_syslog && prty)
00242 {
00243 syslog(prty, buf);
00244 }
00245
00246 return 0;
00247 }
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00276 int
00277 lcmaps_log_debug(int debug_lvl, char * fmt, ...)
00278 {
00279 va_list pvar;
00280 char buf[MAX_LOG_BUFFER_SIZE];
00281 int res;
00282
00283 va_start(pvar, fmt);
00284 res=vsnprintf(buf,MAX_LOG_BUFFER_SIZE,fmt,pvar);
00285 va_end(pvar);
00286 if ( (res >= MAX_LOG_BUFFER_SIZE) || (res < 0) )
00287 {
00288 fprintf(stderr,"lcmaps_log(): log string too long (> %d)\n",
00289 MAX_LOG_BUFFER_SIZE);
00290 }
00291 if (debug_lvl <= debug_level)
00292 {
00293 lcmaps_log(0,buf);
00294 return 0;
00295 }
00296 return 1;
00297 }
00298
00299
00300
00301
00302
00303
00304
00316 int
00317 lcmaps_log_close()
00318 {
00319 if (extra_logstr != NULL)
00320 {
00321 free(extra_logstr);
00322 extra_logstr = NULL;
00323 }
00324
00325 if (lcmaps_logfp == NULL)
00326 return 1;
00327
00328 fclose(lcmaps_logfp);
00329 lcmaps_logfp=NULL;
00330 return 0;
00331 }
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00362 int
00363 lcmaps_log_time(int prty, char * fmt, ...)
00364 {
00365 va_list pvar;
00366 char buf[MAX_LOG_BUFFER_SIZE];
00367 char * datetime = NULL;
00368 char * tmpbuf = NULL;
00369 int res;
00370 time_t clock;
00371 struct tm * tmp = NULL;
00372
00373
00374 va_start(pvar, fmt);
00375 res=vsnprintf(buf,MAX_LOG_BUFFER_SIZE,fmt,pvar);
00376 va_end(pvar);
00377 if ( (res >= MAX_LOG_BUFFER_SIZE) || (res < 0) )
00378 {
00379 fprintf(stderr,"lcmaps_log_time(): log string too long (> %d)\n",
00380 MAX_LOG_BUFFER_SIZE);
00381 }
00382
00383 if (extra_logstr == NULL)
00384 {
00385 time(&clock);
00386 tmp = localtime(&clock);
00387
00388 datetime = malloc(sizeof(char) * 20);
00389
00390 res=snprintf(datetime, 20, "%04d-%02d-%02d.%02d:%02d:%02d",
00391 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
00392 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
00393 if ( (res >= 20) || (res < 0) )
00394 {
00395 fprintf(stderr,"lcmaps_log_time(): date string too long (> %d)\n",
00396 20);
00397 }
00398
00399 tmpbuf = (char *) malloc ((strlen(datetime) + strlen(buf) + strlen(" : ")) * sizeof(char) + 1);
00400 strcpy(tmpbuf, datetime);
00401 strcat(tmpbuf, " : ");
00402 strcat(tmpbuf, buf);
00403 }
00404 else
00405 {
00406 tmpbuf = (char *) malloc ((strlen(extra_logstr) + strlen(buf) + strlen(" : ")) * sizeof(char) + 1);
00407 strcpy(tmpbuf, extra_logstr);
00408 strcat(tmpbuf, " : ");
00409 strcat(tmpbuf, buf);
00410 }
00411
00412 if (logging_usrlog)
00413 {
00414 if (lcmaps_logfp == NULL)
00415 {
00416 fprintf(stderr,"lcmaps_log_time() error: cannot open file descriptor\n");
00417 return 1;
00418 }
00419 fprintf(lcmaps_logfp,"LCMAPS %d: %s",prty,tmpbuf);
00420 fflush(lcmaps_logfp);
00421 }
00422 if (logging_syslog && prty)
00423 {
00424 syslog(prty, tmpbuf);
00425 }
00426
00427 if (datetime != NULL) free(datetime);
00428 if (tmpbuf != NULL) free(tmpbuf);
00429
00430 return 0;
00431 }
00432
00433
00434
00435
00436
00437
00438
00439