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