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; 
00047 /******************************************************************************
00048 Function:       lcas_log_open()
00049 Description:    Start logging
00050 Parameters:
00051                 path:    path of logfile
00052                 fp:      file pointer to already opened file (or NULL)
00053                 logtype: DO_USRLOG, DO_SYSLOG
00054 Returns:        0 succes
00055                 1 failure
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         /* Not yet applicable, wait for LCAS to become daemon */
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             /* File already opened */
00078             lcas_logfp=fp;
00079         }
00080         else if (path != NULL)
00081         {
00082             /* Try to append to file */
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      * Set the debugging level:
00103      *    1. Try if DEBUG_LEVEL > 0
00104      *    2. Try if LCAS_DEBUG_LEVEL is set and if it is an integer
00105      *    3. set debug_level = 0;
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         /* convert into integer */
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 Function:       lcas_log()
00146 Description:    Log information to file and or syslog
00147 Parameters:
00148                 prty:    syslog priority (if 0 don't syslog)
00149                 fmt:     string format
00150 Returns:        0 succes
00151                 1 failure
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 Function:       lcas_log_debug()
00188 Description:    Print debugging information
00189 Parameters:
00190                 debug_lvl: debugging level
00191                 fmt:       string format
00192 Returns:        0 succes
00193                 1 failure
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 Function:       lcas_log_close()
00219 Description:    Stop logging
00220 Parameters:
00221 Returns:        0 succes
00222                 1 failure
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 Function:       lcas_log_time()
00237 Description:    Log information to file and or syslog with a timestamp
00238 Parameters:
00239                 prty:    syslog priority (if 0 don't syslog)
00240                 fmt:     string format
00241 Returns:        0 succes
00242                 1 failure
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 Function:       lcas_get_debug_level()
00327 Description:    Retrieve the debug_level
00328 Parameters:
00329 Returns:        the debug_level
00330 ******************************************************************************/
00337 int
00338 lcas_get_debug_level()
00339 {
00340     return debug_level;
00341 }
00342 
00343 /******************************************************************************
00344 CVS Information:
00345     $Source: /cvs/fabric_mgt/gridification/lcas/src/lcas_log.c,v $
00346     $Date: 2003/08/22 16:12:31 $
00347     $Revision: 2.7 $
00348     $Author: martijn $
00349 ******************************************************************************/

Generated at Tue Sep 23 15:06:52 2003 for edg-lcas by doxygen1.2.8.1 written by Dimitri van Heesch, © 1997-2001