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 "_lcas_log.h"
00030
00031
00032
00033
00034 #ifndef DEBUG_LEVEL
00035 #define DEBUG_LEVEL 0
00036 #endif
00037
00038
00039
00040
00041 static FILE * lcas_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_lcas_logfp = 0;
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059 int
00060 lcas_log_open(char * path, FILE * fp, unsigned short logtype )
00061 {
00062 char * debug_env = NULL;
00063 char * logstr_env = NULL;
00064
00065 if ((logtype & DO_SYSLOG) == DO_SYSLOG)
00066 {
00067
00068
00069 logging_syslog=1;
00070 #if 0
00071
00072 openlog("EDG LCAS", LOG_PID, LOG_DAEMON);
00073 #endif
00074 }
00075 if ((logtype & DO_USRLOG) == DO_USRLOG)
00076 {
00077 logging_usrlog=1;
00078 if (fp != NULL)
00079 {
00080
00081 lcas_logfp=fp;
00082 should_close_lcas_logfp = 0;
00083 }
00084 else if (path != NULL)
00085 {
00086
00087 if ((lcas_logfp = fopen(path, "a")) == NULL)
00088 {
00089 fprintf(stderr, "lcas_log_open(): Cannot open logfile %s: %s\n",
00090 path, sys_errlist[errno]);
00091 if (logging_syslog)
00092 {
00093 syslog(LOG_ERR, "lcas_log_open(): Cannot open logfile %s\n", path);
00094 }
00095 return 1;
00096 }
00097 }
00098 else
00099 {
00100 fprintf(stderr, "lcas_log_open(): Please specify either (open) file descriptor");
00101 fprintf(stderr, " or name of logfile\n");
00102 return 1;
00103 }
00104 }
00105
00106
00107
00108
00109
00110
00111 if ( (int)(DEBUG_LEVEL) > 0 )
00112 {
00113 debug_level = (int)(DEBUG_LEVEL);
00114 }
00115 else if ( (debug_env = getenv("LCAS_DEBUG_LEVEL")) )
00116 {
00117
00118 int j = 0;
00119
00120 for (j = 0; j < strlen(debug_env); j++)
00121 {
00122 if (!isdigit((debug_env)[j]))
00123 {
00124 fprintf(stderr,"lcas_log_open(): found non-digit in environment variable in \"LCAS_DEBUG_LEVEL\" = %s\n", debug_env);
00125 return 1;
00126 }
00127 }
00128 debug_level = atoi(debug_env);
00129 if (debug_level < 0)
00130 {
00131 fprintf(stderr,"lcas_log_open(): environment variable in \"LCAS_DEBUG_LEVEL\" should be >= 0\n");
00132 return 1;
00133 }
00134 }
00135 else
00136 {
00137 debug_level = 0;
00138 }
00139
00140 if (debug_level > 0)
00141 {
00142 lcas_log(0,"lcas_log_open(): setting debugging level to %d\n", debug_level);
00143 }
00144
00145
00146
00147
00148
00149 if ( (logstr_env = getenv("LCAS_LOG_STRING")) != NULL )
00150 {
00151 extra_logstr = strdup(logstr_env);
00152 }
00153 else if ( (logstr_env = getenv("JOB_REPOSITORY_ID")) != NULL )
00154 {
00155 extra_logstr = strdup(logstr_env);
00156 }
00157 else if ( (logstr_env = getenv("GATEKEEPER_JM_ID")) != NULL )
00158 {
00159 extra_logstr = strdup(logstr_env);
00160 }
00161
00162 return 0;
00163 }
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174 int
00175 lcas_log(int prty, char * fmt, ...)
00176 {
00177 va_list pvar;
00178 char buf[MAX_LOG_BUFFER_SIZE];
00179 int res;
00180
00181 va_start(pvar, fmt);
00182 res=vsnprintf(buf,MAX_LOG_BUFFER_SIZE,fmt,pvar);
00183 va_end(pvar);
00184 if ( (res >= MAX_LOG_BUFFER_SIZE) || (res < 0) )
00185 {
00186 fprintf(stderr,"lcas_log(): log string too long (> %d)\n",
00187 MAX_LOG_BUFFER_SIZE);
00188 }
00189 if (logging_usrlog)
00190 {
00191 if (lcas_logfp == NULL)
00192 {
00193 fprintf(stderr,"lcas_log() error: cannot open file descriptor\n");
00194 return 1;
00195 }
00196 if (extra_logstr == NULL)
00197 {
00198 fprintf(lcas_logfp,"LCAS %d: %s", prty, buf);
00199 }
00200 else
00201 {
00202 fprintf(lcas_logfp,"LCAS %d: %s : %s", prty, extra_logstr, buf);
00203 }
00204 fflush(lcas_logfp);
00205 }
00206 if (logging_syslog && prty)
00207 {
00208 syslog(prty, buf);
00209 }
00210
00211 return 0;
00212 }
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223 int
00224 lcas_log_debug(int debug_lvl, 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,"lcas_log(): log string too long (> %d)\n",
00236 MAX_LOG_BUFFER_SIZE);
00237 }
00238 if (debug_lvl <= debug_level)
00239 {
00240 lcas_log(0,buf);
00241 return 0;
00242 }
00243 return 1;
00244 }
00245
00246
00247
00248
00249
00250
00251
00252 int
00253 lcas_log_close()
00254 {
00255 if (extra_logstr != NULL)
00256 {
00257 free(extra_logstr);
00258 extra_logstr = NULL;
00259 }
00260
00261 if (should_close_lcas_logfp)
00262 {
00263 fclose(lcas_logfp);
00264 lcas_logfp=NULL;
00265 }
00266
00267 return 0;
00268 }
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00298 int
00299 lcas_log_time(int prty, char * fmt, ...)
00300 {
00301 va_list pvar;
00302 char buf[MAX_LOG_BUFFER_SIZE];
00303 char * datetime = NULL;
00304 char * tmpbuf = NULL;
00305 int res;
00306 time_t clock;
00307 struct tm * tmp = NULL;
00308
00309
00310 va_start(pvar, fmt);
00311 res=vsnprintf(buf,MAX_LOG_BUFFER_SIZE,fmt,pvar);
00312 va_end(pvar);
00313 if ( (res >= MAX_LOG_BUFFER_SIZE) || (res < 0) )
00314 {
00315 fprintf(stderr,"lcas_log_time(): log string too long (> %d)\n",
00316 MAX_LOG_BUFFER_SIZE);
00317 }
00318
00319 if (extra_logstr == NULL)
00320 {
00321 time(&clock);
00322 tmp = localtime(&clock);
00323
00324 datetime = malloc(sizeof(char) * 20);
00325
00326 res=snprintf(datetime, 20, "%04d-%02d-%02d.%02d:%02d:%02d",
00327 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
00328 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
00329 if ( (res >= 20) || (res < 0) )
00330 {
00331 fprintf(stderr,"lcas_log_time(): date string too long (> %d)\n",
00332 20);
00333 }
00334
00335 tmpbuf = (char *) malloc ((strlen(datetime) + strlen(buf) + strlen(" : ")) * sizeof(char) + 1);
00336 strcpy(tmpbuf, datetime);
00337 strcat(tmpbuf, " : ");
00338 strcat(tmpbuf, buf);
00339 }
00340 else
00341 {
00342 tmpbuf = (char *) malloc ((strlen(extra_logstr) + strlen(buf) + strlen(" : ")) * sizeof(char) + 1);
00343 strcpy(tmpbuf, extra_logstr);
00344 strcat(tmpbuf, " : ");
00345 strcat(tmpbuf, buf);
00346 }
00347
00348
00349 if (logging_usrlog)
00350 {
00351 if (lcas_logfp == NULL)
00352 {
00353 fprintf(stderr,"lcas_log_time() error: cannot open file descriptor\n");
00354 return 1;
00355 }
00356 fprintf(lcas_logfp,"LCAS %d: %s",prty,tmpbuf);
00357 fflush(lcas_logfp);
00358 }
00359 if (logging_syslog && prty)
00360 {
00361 syslog(prty, tmpbuf);
00362 }
00363
00364 if (datetime != NULL) free(datetime);
00365 if (tmpbuf != NULL) free(tmpbuf);
00366
00367 return 0;
00368 }
00369
00370
00371
00372
00373
00374
00375
00376
00383 int
00384 lcas_get_debug_level()
00385 {
00386 return debug_level;
00387 }
00388
00389
00390
00391
00392
00393
00394
00395