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) 2001 EU DataGrid.                                                                             
00003  * For license conditions see http://www.eu-datagrid.org/license.html                                          
00004  *
00005  * Copyright (c) 2001, 2002 by 
00006  *     Martijn Steenbakkers <martijn@nikhef.nl>,
00007  *     David Groep <davidg@nikhef.nl>,
00008  *     NIKHEF Amsterdam, the Netherlands
00009  */
00010 
00018 /*****************************************************************************
00019                             Include header files
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                           Module specific prototypes
00033 ******************************************************************************/
00034 #ifndef DEBUG_LEVEL
00035 #define DEBUG_LEVEL 0 
00036 #endif /* DEBUG_LEVEL */
00037 
00038 /******************************************************************************
00039                        Define module specific variables
00040 ******************************************************************************/
00041 static FILE *  lcas_logfp=NULL; 
00042 static int     logging_usrlog=0; 
00043 static int     logging_syslog=0; 
00044 //static int     debug_level=DEBUG_LEVEL; /*!< debugging level \internal */
00045 static int     debug_level=0; 
00046 static char *  extra_logstr = NULL; 
00047 static int     should_close_lcas_logfp = 0; 
00049 /******************************************************************************
00050 Function:       lcas_log_open()
00051 Description:    Start logging
00052 Parameters:
00053                 path:    path of logfile
00054                 fp:      file pointer to already opened file (or NULL)
00055                 logtype: DO_USRLOG, DO_SYSLOG
00056 Returns:        0 succes
00057                 1 failure
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 //        fprintf(stderr,"lcas_log_open() error: attempt to do syslogging,");
00068 //        fprintf(stderr," not supported yet\n");
00069         logging_syslog=1;
00070 #if 0
00071         /* Not yet applicable, wait for LCAS to become daemon */
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             /* File already opened */
00081             lcas_logfp=fp;
00082             should_close_lcas_logfp = 0;
00083         }
00084         else if (path != NULL)
00085         {
00086             /* Try to append to file */
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      * Set the debugging level:
00107      *    1. Try if DEBUG_LEVEL > 0
00108      *    2. Try if LCAS_DEBUG_LEVEL is set and if it is an integer
00109      *    3. set debug_level = 0;
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         /* convert into integer */
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      * Check if there is an extra log string
00147      * These environment variables are checked: JOB_REPOSITORY_ID and GATEKEEPER_JM_ID
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 Function:       lcas_log()
00167 Description:    Log information to file and or syslog
00168 Parameters:
00169                 prty:    syslog priority (if 0 don't syslog)
00170                 fmt:     string format
00171 Returns:        0 succes
00172                 1 failure
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 Function:       lcas_log_debug()
00216 Description:    Print debugging information
00217 Parameters:
00218                 debug_lvl: debugging level
00219                 fmt:       string format
00220 Returns:        0 succes
00221                 1 failure
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 Function:       lcas_log_close()
00247 Description:    Stop logging
00248 Parameters:
00249 Returns:        0 succes
00250                 1 failure
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 Function:       lcas_log_time()
00272 Description:    Log information to file and or syslog with a timestamp
00273 Parameters:
00274                 prty:    syslog priority (if 0 don't syslog)
00275                 fmt:     string format
00276 Returns:        0 succes
00277                 1 failure
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 Function:       lcas_get_debug_level()
00373 Description:    Retrieve the debug_level
00374 Parameters:
00375 Returns:        the debug_level
00376 ******************************************************************************/
00383 int
00384 lcas_get_debug_level()
00385 {
00386     return debug_level;
00387 }
00388 
00389 /******************************************************************************
00390 CVS Information:
00391     $Source: /local/reps/lcgware/fabric_mgt/gridification/lcas/src/lcas_log.c,v $
00392     $Date: 2004/09/20 10:12:33 $
00393     $Revision: 2.9 $
00394     $Author: maart $
00395 ******************************************************************************/

Generated on Mon Sep 20 15:14:15 2004 for edg-lcas by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002