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 "_lcas_log.h"
00037 
00038 
00039 
00040 
00041 #ifndef DEBUG_LEVEL
00042 #define DEBUG_LEVEL 0 
00043 #endif 
00044 
00045 
00046 
00047 
00048 static FILE *  lcas_logfp=NULL; 
00049 static int     logging_usrlog=0; 
00050 static int     logging_syslog=0; 
00051 static int     debug_level=0; 
00052 static char *  extra_logstr = NULL; 
00053 static int     should_close_lcas_logfp = 0; 
00055 
00056 
00057 
00058 
00059 
00060 
00061 
00062 
00063 
00064 
00065 int
00066 lcas_log_open(char * path, FILE * fp, unsigned short logtype )
00067 {
00068     char * debug_env = NULL;
00069     char * logstr_env = NULL;
00070 
00071     if ((logtype & DO_SYSLOG) == DO_SYSLOG)
00072     {
00073 
00074 
00075         logging_syslog=1;
00076 #if 0
00077         
00078         openlog("EDG LCAS", LOG_PID, LOG_DAEMON);
00079 #endif
00080     }
00081     if ((logtype & DO_USRLOG) == DO_USRLOG)
00082     {
00083         logging_usrlog=1;
00084         if (fp != NULL)
00085         {
00086             
00087             lcas_logfp=fp;
00088             should_close_lcas_logfp = 0;
00089         }
00090         else if (path != NULL)
00091         {
00092             
00093             if ((lcas_logfp = fopen(path, "a")) == NULL)
00094             {
00095                 fprintf(stderr, "lcas_log_open(): Cannot open logfile %s: %s\n",
00096                         path, sys_errlist[errno]);
00097                 if (logging_syslog)
00098                 {
00099                     syslog(LOG_ERR, "lcas_log_open(): Cannot open logfile %s\n", path);
00100                 }
00101                 return 1;
00102             }
00103         }
00104         else
00105         {
00106             fprintf(stderr, "lcas_log_open(): Please specify either (open) file descriptor");
00107             fprintf(stderr, " or name of logfile\n");
00108             return 1;
00109         }
00110     }
00111     
00112 
00113 
00114 
00115 
00116 
00117     if ( (int)(DEBUG_LEVEL) > 0 )
00118     {
00119         debug_level = (int)(DEBUG_LEVEL);
00120     }
00121     else if ( (debug_env = getenv("LCAS_DEBUG_LEVEL")) )
00122     {
00123         
00124         int j = 0;
00125 
00126         for (j = 0; j < strlen(debug_env); j++)
00127         {
00128             if (!isdigit((debug_env)[j]))
00129             {
00130                 fprintf(stderr,"lcas_log_open(): found non-digit in environment variable in \"LCAS_DEBUG_LEVEL\" = %s\n", debug_env);
00131                 return 1;
00132             }
00133         }
00134         debug_level = atoi(debug_env);
00135         if (debug_level < 0)
00136         {
00137             fprintf(stderr,"lcas_log_open(): environment variable in \"LCAS_DEBUG_LEVEL\" should be >= 0\n");
00138             return 1;
00139         }
00140     }
00141     else
00142     {
00143         debug_level = 0;
00144     }
00145 
00146     if (debug_level > 0)
00147     {
00148         lcas_log(0,"lcas_log_open(): setting debugging level to %d\n", debug_level);
00149     }
00150 
00151     
00152 
00153 
00154 
00155     if ( (logstr_env = getenv("LCAS_LOG_STRING")) != NULL )
00156     {
00157         extra_logstr = strdup(logstr_env);
00158     }
00159     else if ( (logstr_env = getenv("JOB_REPOSITORY_ID")) != NULL )
00160     {
00161         extra_logstr = strdup(logstr_env);
00162     }
00163     else if ( (logstr_env = getenv("GATEKEEPER_JM_ID")) != NULL )
00164     {
00165         extra_logstr = strdup(logstr_env);
00166     }
00167 
00168     return 0;
00169 }
00170 
00171 
00172 
00173 
00174 
00175 
00176 
00177 
00178 
00179 
00180 int
00181 lcas_log(int prty, char * fmt, ...)
00182 {
00183     va_list pvar;
00184     char    buf[MAX_LOG_BUFFER_SIZE];
00185     int     res;
00186 
00187     va_start(pvar, fmt);
00188     res=vsnprintf(buf,MAX_LOG_BUFFER_SIZE,fmt,pvar);
00189     va_end(pvar);
00190     if ( (res >= MAX_LOG_BUFFER_SIZE) || (res < 0) )
00191     {
00192         fprintf(stderr,"lcas_log(): log string too long (> %d)\n",
00193                 MAX_LOG_BUFFER_SIZE);
00194     }
00195     if (logging_usrlog)
00196     {
00197         if (lcas_logfp == NULL)
00198         {
00199             fprintf(stderr,"lcas_log() error: cannot open file descriptor\n");
00200             return 1;
00201         }
00202         if (extra_logstr == NULL)
00203         {
00204             fprintf(lcas_logfp,"LCAS   %d: %s", prty, buf);
00205         }
00206         else
00207         {
00208             fprintf(lcas_logfp,"LCAS   %d: %s : %s", prty, extra_logstr, buf);
00209         }
00210         fflush(lcas_logfp);
00211     }
00212     if (logging_syslog && prty)
00213     {
00214         syslog(prty, buf);
00215     }
00216 
00217     return 0;
00218 }
00219 
00220 
00221 
00222 
00223 
00224 
00225 
00226 
00227 
00228 
00229 
00249 int
00250 lcas_log_a_string(int prty, char * fmt, char * the_string)
00251 {
00252     char    buf[MAX_LOG_BUFFER_SIZE];
00253     int     res;
00254 
00255     res = snprintf(buf, MAX_LOG_BUFFER_SIZE, fmt, the_string);
00256     if ( (res >= MAX_LOG_BUFFER_SIZE) || (res < 0) )
00257     {
00258         fprintf(stderr,"lcas_log_a_string(): log string too long (> %d)\n",
00259                 MAX_LOG_BUFFER_SIZE);
00260     }
00261     if (logging_usrlog)
00262     {
00263         if (lcas_logfp == NULL)
00264         {
00265             fprintf(stderr,"lcas_log() error: cannot open file descriptor\n");
00266             return 1;
00267         }
00268         if (extra_logstr == NULL)
00269         {
00270             fprintf(lcas_logfp,"LCAS %d: %s", prty, buf);
00271         }
00272         else
00273         {
00274             fprintf(lcas_logfp,"LCAS %d: %s : %s", prty, extra_logstr, buf);
00275         }
00276         fflush(lcas_logfp);
00277     }
00278     if (logging_syslog && prty)
00279     {
00280         syslog(prty, buf);
00281     }
00282 
00283     return 0;
00284 }
00285 
00286 
00287 
00288 
00289 
00290 
00291 
00292 
00293 
00294 
00295 int
00296 lcas_log_debug(int debug_lvl, char * fmt, ...)
00297 {
00298     va_list pvar;
00299     char    buf[MAX_LOG_BUFFER_SIZE];
00300     int     res;
00301 
00302     va_start(pvar, fmt);
00303     res=vsnprintf(buf,MAX_LOG_BUFFER_SIZE,fmt,pvar);
00304     va_end(pvar);
00305     if ( (res >= MAX_LOG_BUFFER_SIZE) || (res < 0) )
00306     {
00307         fprintf(stderr,"lcas_log(): log string too long (> %d)\n",
00308                 MAX_LOG_BUFFER_SIZE);
00309     }
00310     if (debug_lvl <= debug_level)
00311     {
00312         lcas_log(0,buf);
00313         return 0;
00314     }
00315     return 1;
00316 }
00317 
00318 
00319 
00320 
00321 
00322 
00323 
00324 
00325 
00326 
00327 
00346 int
00347 lcas_log_a_string_debug(int debug_lvl, char * fmt, char * the_string)
00348 {
00349     if (debug_lvl <= debug_level)
00350     {
00351         lcas_log_a_string(0, fmt, the_string);
00352         return 0;
00353     }
00354     return 1;
00355 }
00356 
00357 
00358 
00359 
00360 
00361 
00362 
00363 
00364 int
00365 lcas_log_close()
00366 {
00367     if (extra_logstr != NULL)
00368     {
00369         free(extra_logstr);
00370         extra_logstr = NULL;
00371     }
00372 
00373     if (should_close_lcas_logfp)
00374     {
00375         fclose(lcas_logfp);
00376         lcas_logfp=NULL;
00377     }
00378 
00379     return 0;
00380 }
00381 
00382 
00383 
00384 
00385 
00386 
00387 
00388 
00389 
00390 
00391 int
00392 lcas_log_time(int prty, char * fmt, ...)
00393 {
00394     va_list     pvar;
00395     char        buf[MAX_LOG_BUFFER_SIZE];
00396     char *      datetime = NULL;
00397     char *      tmpbuf = NULL;
00398     int         res;
00399     time_t      clock;
00400     struct tm * tmp = NULL;
00401 
00402 
00403     va_start(pvar, fmt);
00404     res=vsnprintf(buf,MAX_LOG_BUFFER_SIZE,fmt,pvar);
00405     va_end(pvar);
00406     if ( (res >= MAX_LOG_BUFFER_SIZE) || (res < 0) )
00407     {
00408         fprintf(stderr,"lcas_log_time(): log string too long (> %d)\n",
00409                 MAX_LOG_BUFFER_SIZE);
00410     }
00411 
00412     if (extra_logstr == NULL)
00413     {
00414         time(&clock);
00415         tmp = localtime(&clock);
00416 
00417         datetime = malloc(sizeof(char) * 20);
00418 
00419         res=snprintf(datetime, 20, "%04d-%02d-%02d.%02d:%02d:%02d",
00420                tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
00421                tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
00422         if ( (res >= 20) || (res < 0) )
00423         {
00424             fprintf(stderr,"lcas_log_time(): date string too long (> %d)\n",
00425                     20);
00426         }
00427 
00428         tmpbuf = (char *) malloc ((strlen(datetime) + strlen(buf) + strlen(" : ")) * sizeof(char) + 1);
00429         strcpy(tmpbuf, datetime);
00430         strcat(tmpbuf, " : ");
00431         strcat(tmpbuf, buf);
00432     }
00433     else
00434     {
00435         tmpbuf = (char *) malloc ((strlen(extra_logstr) + strlen(buf) + strlen(" : ")) * sizeof(char) + 1);
00436         strcpy(tmpbuf, extra_logstr);
00437         strcat(tmpbuf, " : ");
00438         strcat(tmpbuf, buf);
00439     }
00440 
00441 
00442     if (logging_usrlog)
00443     {
00444         if (lcas_logfp == NULL)
00445         {
00446             fprintf(stderr,"lcas_log_time() error: cannot open file descriptor\n");
00447             return 1;
00448         }
00449         fprintf(lcas_logfp,"LCAS   %d: %s",prty,tmpbuf);
00450         fflush(lcas_logfp);
00451     }
00452     if (logging_syslog && prty)
00453     {
00454         syslog(prty, tmpbuf);
00455     }
00456 
00457     if (datetime != NULL) free(datetime);
00458     if (tmpbuf != NULL) free(tmpbuf);
00459 
00460     return 0;
00461 }
00462 
00463 
00464 
00465 
00466 
00467 
00468 
00469 
00470 int
00471 lcas_get_debug_level()
00472 {
00473     return debug_level;
00474 }
00475 
00476 
00477 
00478 
00479 
00480 
00481 
00482