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;
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057 int
00058 lcas_log_open(char * path, FILE * fp, unsigned short logtype )
00059 {
00060 char * debug_env = NULL;
00061
00062 if ((logtype & DO_SYSLOG) == DO_SYSLOG)
00063 {
00064 fprintf(stderr,"lcas_log_open() error: attempt to do syslogging,");
00065 fprintf(stderr," not supported yet\n");
00066 #if 0
00067 logging_syslog=1;
00068
00069 openlog("EDG LCAS", LOG_PID, LOG_DAEMON);
00070 #endif
00071 }
00072 if ((logtype & DO_USRLOG) == DO_USRLOG)
00073 {
00074 logging_usrlog=1;
00075 if (fp != NULL)
00076 {
00077
00078 lcas_logfp=fp;
00079 }
00080 else if (path != NULL)
00081 {
00082
00083 if ((lcas_logfp = fopen(path, "a")) == NULL)
00084 {
00085 fprintf(stderr, "lcas_log_open(): Cannot open logfile %s: %s\n",
00086 path, sys_errlist[errno]);
00087 if (logging_syslog)
00088 {
00089 syslog(LOG_ERR, "lcas_log_open(): Cannot open logfile %s\n", path);
00090 }
00091 return 1;
00092 }
00093 }
00094 else
00095 {
00096 fprintf(stderr, "lcas_log_open(): Please specify either (open) file descriptor");
00097 fprintf(stderr, " or name of logfile\n");
00098 return 1;
00099 }
00100 }
00101
00102
00103
00104
00105
00106
00107 if ( (int)(DEBUG_LEVEL) > 0 )
00108 {
00109 debug_level = (int)(DEBUG_LEVEL);
00110 }
00111 else if ( (debug_env = getenv("LCAS_DEBUG_LEVEL")) )
00112 {
00113
00114 int j = 0;
00115
00116 for (j = 0; j < strlen(debug_env); j++)
00117 {
00118 if (!isdigit((debug_env)[j]))
00119 {
00120 fprintf(stderr,"lcas_log_open(): found non-digit in environment variable in \"LCAS_DEBUG_LEVEL\" = %s\n", debug_env);
00121 return 1;
00122 }
00123 }
00124 debug_level = atoi(debug_env);
00125 if (debug_level < 0)
00126 {
00127 fprintf(stderr,"lcas_log_open(): environment variable in \"LCAS_DEBUG_LEVEL\" should be >= 0\n");
00128 return 1;
00129 }
00130 }
00131 else
00132 {
00133 debug_level = 0;
00134 }
00135
00136 if (debug_level > 0)
00137 {
00138 lcas_log(0,"lcas_log_open(): setting debugging level to %d\n", debug_level);
00139 }
00140
00141 return 0;
00142 }
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153 int
00154 lcas_log(int prty, char * fmt, ...)
00155 {
00156 va_list pvar;
00157 char buf[MAX_LOG_BUFFER_SIZE];
00158 int res;
00159
00160 va_start(pvar, fmt);
00161 res=vsnprintf(buf,MAX_LOG_BUFFER_SIZE,fmt,pvar);
00162 va_end(pvar);
00163 if ( (res >= MAX_LOG_BUFFER_SIZE) || (res < 0) )
00164 {
00165 fprintf(stderr,"lcas_log(): log string too long (> %d)\n",
00166 MAX_LOG_BUFFER_SIZE);
00167 }
00168 if (logging_usrlog)
00169 {
00170 if (lcas_logfp == NULL)
00171 {
00172 fprintf(stderr,"lcas_log() error: cannot open file descriptor\n");
00173 return 1;
00174 }
00175 fprintf(lcas_logfp,"LCAS %d: %s",prty,buf);
00176 fflush(lcas_logfp);
00177 }
00178 if (logging_syslog && prty)
00179 {
00180 syslog(prty, buf);
00181 }
00182
00183 return 0;
00184 }
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195 int
00196 lcas_log_debug(int debug_lvl, char * fmt, ...)
00197 {
00198 va_list pvar;
00199 char buf[MAX_LOG_BUFFER_SIZE];
00200 int res;
00201
00202 va_start(pvar, fmt);
00203 res=vsnprintf(buf,MAX_LOG_BUFFER_SIZE,fmt,pvar);
00204 va_end(pvar);
00205 if ( (res >= MAX_LOG_BUFFER_SIZE) || (res < 0) )
00206 {
00207 fprintf(stderr,"lcas_log(): log string too long (> %d)\n",
00208 MAX_LOG_BUFFER_SIZE);
00209 }
00210 if (debug_lvl <= debug_level)
00211 {
00212 lcas_log(0,buf);
00213 return 0;
00214 }
00215 return 1;
00216 }
00217
00218
00219
00220
00221
00222
00223
00224 int
00225 lcas_log_close()
00226 {
00227 if (lcas_logfp == NULL)
00228 return 1;
00229
00230 fclose(lcas_logfp);
00231 lcas_logfp=NULL;
00232 return 0;
00233 }
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00263 int
00264 lcas_log_time(int prty, char * fmt, ...)
00265 {
00266 va_list pvar;
00267 char buf[MAX_LOG_BUFFER_SIZE];
00268 char * datetime;
00269 char * tmpbuf;
00270 int res;
00271 time_t clock;
00272 struct tm * tmp;
00273
00274
00275 va_start(pvar, fmt);
00276 res=vsnprintf(buf,MAX_LOG_BUFFER_SIZE,fmt,pvar);
00277 va_end(pvar);
00278 if ( (res >= MAX_LOG_BUFFER_SIZE) || (res < 0) )
00279 {
00280 fprintf(stderr,"lcas_log_time(): log string too long (> %d)\n",
00281 MAX_LOG_BUFFER_SIZE);
00282 }
00283
00284 time(&clock);
00285 tmp = localtime(&clock);
00286
00287 datetime = malloc(sizeof(char) * 20);
00288
00289 res=snprintf(datetime, 20, "%04d-%02d-%02d.%02d:%02d:%02d",
00290 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
00291 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
00292 if ( (res >= 20) || (res < 0) )
00293 {
00294 fprintf(stderr,"lcas_log_time(): date string too long (> %d)\n",
00295 20);
00296 }
00297
00298 tmpbuf = (char *) malloc ((strlen(datetime) + strlen(buf) + strlen(" : ")) * sizeof(char) + 1);
00299 strcpy(tmpbuf, datetime);
00300 strcat(tmpbuf, " : ");
00301 strcat(tmpbuf, buf);
00302
00303 if (logging_usrlog)
00304 {
00305 if (lcas_logfp == NULL)
00306 {
00307 fprintf(stderr,"lcas_log_time() error: cannot open file descriptor\n");
00308 return 1;
00309 }
00310 fprintf(lcas_logfp,"LCAS %d: %s",prty,tmpbuf);
00311 fflush(lcas_logfp);
00312 }
00313 if (logging_syslog && prty)
00314 {
00315 syslog(prty, tmpbuf);
00316 }
00317
00318 free(datetime);
00319 free(tmpbuf);
00320
00321 return 0;
00322 }
00323
00324
00325
00326
00327
00328
00329
00330
00337 int
00338 lcas_get_debug_level()
00339 {
00340 return debug_level;
00341 }
00342
00343
00344
00345
00346
00347
00348
00349