00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00025 
00026 
00027 
00028 #include <stdlib.h>
00029 #include <stdio.h>
00030 #include <string.h>
00031 #include <errno.h>
00032 #include <stdarg.h>
00033 #include <syslog.h>
00034 #include <time.h>
00035 #include <ctype.h>
00036 #include "_lcmaps_log.h"
00037 
00038 
00039 
00040 
00041 #ifndef DEBUG_LEVEL
00042 #define DEBUG_LEVEL 0 
00043 #endif 
00044 
00045 
00046 
00047 
00048 static FILE *  lcmaps_logfp=NULL; 
00049 static int     logging_usrlog=0; 
00050 static int     logging_syslog=0; 
00051 
00052 static int     debug_level=0; 
00053 static char *  extra_logstr = NULL; 
00054 static int     should_close_lcmaps_logfp = 0; 
00056 
00057 
00058 
00059 
00060 
00061 
00062 
00063 
00064 
00065 
00088 int
00089 lcmaps_log_open(char * path, FILE * fp, unsigned short logtype )
00090 {
00091     char * debug_env = NULL;
00092     char * logstr_env = NULL;
00093 
00094     if ((logtype & DO_SYSLOG) == DO_SYSLOG)
00095     {
00096 
00097 
00098         logging_syslog=1;
00099 #if 0
00100         
00101         openlog("EDG LCMAPS", LOG_PID, LOG_AUTHPRIV);
00102 #endif
00103     }
00104     if ((logtype & DO_USRLOG) == DO_USRLOG)
00105     {
00106         logging_usrlog=1;
00107         if (fp != NULL)
00108         {
00109             
00110             lcmaps_logfp=fp;
00111             should_close_lcmaps_logfp = 0;
00112         }
00113         else if (path != NULL)
00114         {
00115             
00116             if ((lcmaps_logfp = fopen(path, "a")) == NULL)
00117             {
00118                 fprintf(stderr, "lcmaps_log_open(): Cannot open logfile %s: %s\n",
00119                         path, sys_errlist[errno]);
00120                 if (logging_syslog)
00121                 {
00122                     syslog(LOG_ERR, "lcmaps_log_open(): Cannot open logfile %s\n", path);
00123                 }
00124                 return 1;
00125             }
00126             should_close_lcmaps_logfp = 1;
00127         }
00128         else
00129         {
00130             fprintf(stderr, "lcmaps_log_open(): Please specify either (open) file descriptor");
00131             fprintf(stderr, " or name of logfile\n");
00132             return 1;
00133         }
00134     }
00135     
00136 
00137 
00138 
00139 
00140 
00141     if ( (int)(DEBUG_LEVEL) > 0 )
00142     {
00143         debug_level = (int)(DEBUG_LEVEL);
00144     }
00145     else if ( (debug_env = getenv("LCMAPS_DEBUG_LEVEL")) )
00146     {
00147         
00148         int j = 0;
00149 
00150         for (j = 0; j < strlen(debug_env); j++)
00151         {
00152             if (!isdigit((debug_env)[j]))
00153             {
00154                 fprintf(stderr,"lcmaps_log_open(): found non-digit in environment variable in \"LCMAPS_DEBUG_LEVEL\" = %s\n", debug_env);
00155                 return 1;
00156             }
00157         }
00158         debug_level = atoi(debug_env);
00159         if (debug_level < 0)
00160         {
00161             fprintf(stderr,"lcmaps_log_open(): environment variable in \"LCMAPS_DEBUG_LEVEL\" should be >= 0\n");
00162             return 1;
00163         }
00164     }
00165     else
00166     {
00167         debug_level = 0;
00168     }
00169 
00170     if (debug_level > 0)
00171     {
00172         lcmaps_log(0,"lcmaps_log_open(): setting debugging level to %d\n", debug_level);
00173     }
00174 
00175     
00176 
00177 
00178 
00179     if ( (logstr_env = getenv("LCMAPS_LOG_STRING")) != NULL )
00180     {
00181         extra_logstr = strdup(logstr_env);
00182     }
00183     else if ( (logstr_env = getenv("JOB_REPOSITORY_ID")) != NULL )
00184     {
00185         extra_logstr = strdup(logstr_env);
00186     }
00187     else if ( (logstr_env = getenv("GATEKEEPER_JM_ID")) != NULL )
00188     {
00189         extra_logstr = strdup(logstr_env);
00190     }
00191 
00192     return 0;
00193 }
00194 
00195 
00196 
00197 
00198 
00199 
00200 
00201 
00202 
00203 
00223 int
00224 lcmaps_log(int prty, char * fmt, ...)
00225 {
00226     va_list pvar;
00227     char    buf[MAX_LOG_BUFFER_SIZE];
00228     int     res;
00229 
00230     va_start(pvar, fmt);
00231     res=vsnprintf(buf,MAX_LOG_BUFFER_SIZE,fmt,pvar);
00232     va_end(pvar);
00233     if ( (res >= MAX_LOG_BUFFER_SIZE) || (res < 0) )
00234     {
00235         fprintf(stderr,"lcmaps_log(): log string too long (> %d)\n",
00236                 MAX_LOG_BUFFER_SIZE);
00237     }
00238     if (logging_usrlog)
00239     {
00240         if (lcmaps_logfp == NULL)
00241         {
00242             fprintf(stderr,"lcmaps_log() error: cannot open file descriptor\n");
00243             return 1;
00244         }
00245         if (extra_logstr == NULL)
00246         {
00247             fprintf(lcmaps_logfp,"LCMAPS %d: %s", prty, buf);
00248         }
00249         else
00250         {
00251             fprintf(lcmaps_logfp,"LCMAPS %d: %s : %s", prty, extra_logstr, buf);
00252         }
00253         fflush(lcmaps_logfp);
00254     }
00255     if (logging_syslog && prty)
00256     {
00257         syslog(prty, buf);
00258     }
00259 
00260     return 0;
00261 }
00262 
00263 
00264 
00265 
00266 
00267 
00268 
00269 
00270 
00271 
00272 
00292 int
00293 lcmaps_log_a_string(int prty, char * fmt, char * the_string)
00294 {
00295     char    buf[MAX_LOG_BUFFER_SIZE];
00296     int     res;
00297 
00298     res = snprintf(buf, MAX_LOG_BUFFER_SIZE, fmt, the_string);
00299     if ( (res >= MAX_LOG_BUFFER_SIZE) || (res < 0) )
00300     {
00301         fprintf(stderr,"lcmaps_log_a_string(): log string too long (> %d)\n",
00302                 MAX_LOG_BUFFER_SIZE);
00303     }
00304     if (logging_usrlog)
00305     {
00306         if (lcmaps_logfp == NULL)
00307         {
00308             fprintf(stderr,"lcmaps_log() error: cannot open file descriptor\n");
00309             return 1;
00310         }
00311         if (extra_logstr == NULL)
00312         {
00313             fprintf(lcmaps_logfp,"LCMAPS %d: %s", prty, buf);
00314         }
00315         else
00316         {
00317             fprintf(lcmaps_logfp,"LCMAPS %d: %s : %s", prty, extra_logstr, buf);
00318         }
00319         fflush(lcmaps_logfp);
00320     }
00321     if (logging_syslog && prty)
00322     {
00323         syslog(prty, buf);
00324     }
00325 
00326     return 0;
00327 }
00328 
00329 
00330 
00331 
00332 
00333 
00334 
00335 
00336 
00337 
00356 int
00357 lcmaps_log_debug(int debug_lvl, char * fmt, ...)
00358 {
00359     va_list pvar;
00360     char    buf[MAX_LOG_BUFFER_SIZE];
00361     int     res;
00362 
00363     va_start(pvar, fmt);
00364     res=vsnprintf(buf,MAX_LOG_BUFFER_SIZE,fmt,pvar);
00365     va_end(pvar);
00366     if ( (res >= MAX_LOG_BUFFER_SIZE) || (res < 0) )
00367     {
00368         fprintf(stderr,"lcmaps_log(): log string too long (> %d)\n",
00369                 MAX_LOG_BUFFER_SIZE);
00370     }
00371     if (debug_lvl <= debug_level)
00372     {
00373         lcmaps_log(0,buf);
00374         return 0;
00375     }
00376     return 1;
00377 }
00378 
00379 
00380 
00381 
00382 
00383 
00384 
00385 
00386 
00387 
00388 
00407 int
00408 lcmaps_log_a_string_debug(int debug_lvl, char * fmt, char * the_string)
00409 {
00410     if (debug_lvl <= debug_level)
00411     {
00412         lcmaps_log_a_string(0, fmt, the_string);
00413         return 0;
00414     }
00415     return 1;
00416 }
00417 
00418 
00419 
00420 
00421 
00422 
00423 
00424 
00436 int
00437 lcmaps_log_close()
00438 {
00439     if (extra_logstr != NULL)
00440     {
00441         free(extra_logstr);
00442         extra_logstr = NULL;
00443     }
00444 
00445     if (should_close_lcmaps_logfp)
00446     {
00447         fclose(lcmaps_logfp);
00448         lcmaps_logfp=NULL;
00449     }
00450 
00451     return 0;
00452 }
00453 
00454 
00455 
00456 
00457 
00458 
00459 
00460 
00461 
00462 
00463 
00483 int
00484 lcmaps_log_time(int prty, char * fmt, ...)
00485 {
00486     va_list     pvar;
00487     char        buf[MAX_LOG_BUFFER_SIZE];
00488     char *      datetime = NULL;
00489     char *      tmpbuf = NULL;
00490     int         res;
00491     time_t      clock;
00492     struct tm * tmp = NULL;
00493 
00494 
00495     va_start(pvar, fmt);
00496     res=vsnprintf(buf,MAX_LOG_BUFFER_SIZE,fmt,pvar);
00497     va_end(pvar);
00498     if ( (res >= MAX_LOG_BUFFER_SIZE) || (res < 0) )
00499     {
00500         fprintf(stderr,"lcmaps_log_time(): log string too long (> %d)\n",
00501                 MAX_LOG_BUFFER_SIZE);
00502     }
00503 
00504     if (extra_logstr == NULL)
00505     {
00506         time(&clock);
00507         tmp = localtime(&clock);
00508 
00509         datetime = malloc(sizeof(char) * 20);
00510 
00511         res=snprintf(datetime, 20, "%04d-%02d-%02d.%02d:%02d:%02d",
00512                tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
00513                tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
00514         if ( (res >= 20) || (res < 0) )
00515         {
00516             fprintf(stderr,"lcmaps_log_time(): date string too long (> %d)\n",
00517                     20);
00518         }
00519 
00520         tmpbuf = (char *) malloc ((strlen(datetime) + strlen(buf) + strlen(" : ")) * sizeof(char) + 1);
00521         strcpy(tmpbuf, datetime);
00522         strcat(tmpbuf, " : ");
00523         strcat(tmpbuf, buf);
00524     }
00525     else
00526     {
00527         tmpbuf = (char *) malloc ((strlen(extra_logstr) + strlen(buf) + strlen(" : ")) * sizeof(char) + 1);
00528         strcpy(tmpbuf, extra_logstr);
00529         strcat(tmpbuf, " : ");
00530         strcat(tmpbuf, buf);
00531     }
00532 
00533     if (logging_usrlog)
00534     {
00535         if (lcmaps_logfp == NULL)
00536         {
00537             fprintf(stderr,"lcmaps_log_time() error: cannot open file descriptor\n");
00538             return 1;
00539         }
00540         fprintf(lcmaps_logfp,"LCMAPS %d: %s",prty,tmpbuf);
00541         fflush(lcmaps_logfp);
00542     }
00543     if (logging_syslog && prty)
00544     {
00545         syslog(prty, tmpbuf);
00546     }
00547 
00548     if (datetime != NULL) free(datetime);
00549     if (tmpbuf != NULL) free(tmpbuf);
00550 
00551     return 0;
00552 }
00553 
00554 
00555 
00556 
00557 
00558 
00559 
00560