Main Page | Modules | Data Structures | File List | Data Fields | Globals | Related Pages

lcas_log.c

Go to the documentation of this file.
00001 /*                                                                                                            
00002  * Copyright (c) Members of the EGEE Collaboration. 2004.
00003  * See http://eu-egee.org/partners/ for details on the copyright holders.
00004  * For license conditions see the license file or
00005  * http://eu-egee.org/license.html
00006  */
00007 
00008 /*                                                                                                            
00009  * Copyright (c) 2001 EU DataGrid.                                                                             
00010  * For license conditions see http://www.eu-datagrid.org/license.html                                          
00011  *
00012  * Copyright (c) 2001, 2002 by 
00013  *     Martijn Steenbakkers <martijn@nikhef.nl>,
00014  *     David Groep <davidg@nikhef.nl>,
00015  *     NIKHEF Amsterdam, the Netherlands
00016  */
00017 
00025 /*****************************************************************************
00026                             Include header files
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                           Module specific prototypes
00040 ******************************************************************************/
00041 #ifndef DEBUG_LEVEL
00042 #define DEBUG_LEVEL 0 
00043 #endif /* DEBUG_LEVEL */
00044 
00045 /******************************************************************************
00046                        Define module specific variables
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 Function:       lcas_log_open()
00057 Description:    Start logging
00058 Parameters:
00059                 path:    path of logfile
00060                 fp:      file pointer to already opened file (or NULL)
00061                 logtype: DO_USRLOG, DO_SYSLOG
00062 Returns:        0 succes
00063                 1 failure
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 //        fprintf(stderr,"lcas_log_open() error: attempt to do syslogging,");
00074 //        fprintf(stderr," not supported yet\n");
00075         logging_syslog=1;
00076 #if 0
00077         /* Not yet applicable, wait for LCAS to become daemon */
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             /* File already opened */
00087             lcas_logfp=fp;
00088             should_close_lcas_logfp = 0;
00089         }
00090         else if (path != NULL)
00091         {
00092             /* Try to append to file */
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      * Set the debugging level:
00113      *    1. Try if DEBUG_LEVEL > 0
00114      *    2. Try if LCAS_DEBUG_LEVEL is set and if it is an integer
00115      *    3. set debug_level = 0;
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         /* convert into integer */
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      * Check if there is an extra log string
00153      * These environment variables are checked: JOB_REPOSITORY_ID and GATEKEEPER_JM_ID
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 Function:       lcas_log()
00173 Description:    Log information to file and or syslog
00174 Parameters:
00175                 prty:    syslog priority (if 0 don't syslog)
00176                 fmt:     string format
00177 Returns:        0 succes
00178                 1 failure
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 Function:       lcas_log_a_string()
00222 Description:    Log a string according to the passed format to file and or syslog
00223 Parameters:
00224                 prty:       syslog priority (if 0 don't syslog)
00225                 fmt:        string format
00226                 the_string: the string
00227 Returns:        0 succes
00228                 1 failure
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 Function:       lcas_log_debug()
00288 Description:    Print debugging information
00289 Parameters:
00290                 debug_lvl: debugging level
00291                 fmt:       string format
00292 Returns:        0 succes
00293                 1 failure
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 Function:       lcas_log_a_string_debug()
00320 Description:    Print debugging information
00321 Parameters:
00322                 debug_lvl:  debugging level
00323                 fmt:        string format
00324                 the_string: the string
00325 Returns:        0 succes
00326                 1 failure
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 Function:       lcas_log_close()
00359 Description:    Stop logging
00360 Parameters:
00361 Returns:        0 succes
00362                 1 failure
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 Function:       lcas_log_time()
00384 Description:    Log information to file and or syslog with a timestamp
00385 Parameters:
00386                 prty:    syslog priority (if 0 don't syslog)
00387                 fmt:     string format
00388 Returns:        0 succes
00389                 1 failure
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 Function:       lcas_get_debug_level()
00466 Description:    Retrieve the debug_level
00467 Parameters:
00468 Returns:        the debug_level
00469 ******************************************************************************/
00470 int
00471 lcas_get_debug_level()
00472 {
00473     return debug_level;
00474 }
00475 
00476 /******************************************************************************
00477 CVS Information:
00478     $Source: /cvs/jra1mw/org.glite.security.lcas/src/lcas_log.c,v $
00479     $Date: 2005/04/04 15:23:57 $
00480     $Revision: 2.13 $
00481     $Author: msteenba $
00482 ******************************************************************************/

Generated on Fri May 27 18:10:48 2005 for lcas by doxygen 1.3.5