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

lcmaps_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 "_lcmaps_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 *  lcmaps_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; 
00048 /******************************************************************************
00049 Function:       lcmaps_log_open()
00050 Description:    Start logging
00051 Parameters:
00052                 path:    path of logfile
00053                 fp:      file pointer to already opened file (or NULL)
00054                 logtype: DO_USRLOG, DO_SYSLOG
00055 Returns:        0 succes
00056                 1 failure
00057 ******************************************************************************/
00080 int
00081 lcmaps_log_open(char * path, FILE * fp, unsigned short logtype )
00082 {
00083     char * debug_env = NULL;
00084     char * logstr_env = NULL;
00085 
00086     if ((logtype & DO_SYSLOG) == DO_SYSLOG)
00087     {
00088         fprintf(stderr,"lcmaps_log_open() error: attempt to do syslogging,");
00089         fprintf(stderr," not supported yet\n");
00090 #if 0
00091         logging_syslog=1;
00092         /* Not yet applicable, wait for LCMAPS to become daemon */
00093         openlog("EDG LCMAPS", LOG_PID, LOG_DAEMON);
00094 #endif
00095     }
00096     if ((logtype & DO_USRLOG) == DO_USRLOG)
00097     {
00098         logging_usrlog=1;
00099         if (fp != NULL)
00100         {
00101             /* File already opened */
00102             lcmaps_logfp=fp;
00103         }
00104         else if (path != NULL)
00105         {
00106             /* Try to append to file */
00107             if ((lcmaps_logfp = fopen(path, "a")) == NULL)
00108             {
00109                 fprintf(stderr, "lcmaps_log_open(): Cannot open logfile %s: %s\n",
00110                         path, sys_errlist[errno]);
00111                 if (logging_syslog)
00112                 {
00113                     syslog(LOG_ERR, "lcmaps_log_open(): Cannot open logfile %s\n", path);
00114                 }
00115                 return 1;
00116             }
00117         }
00118         else
00119         {
00120             fprintf(stderr, "lcmaps_log_open(): Please specify either (open) file descriptor");
00121             fprintf(stderr, " or name of logfile\n");
00122             return 1;
00123         }
00124     }
00125     /*
00126      * Set the debugging level:
00127      *    1. Try if DEBUG_LEVEL > 0
00128      *    2. Try if LCMAPS_DEBUG_LEVEL is set and if it is an integer
00129      *    3. set debug_level = 0;
00130      */
00131     if ( (int)(DEBUG_LEVEL) > 0 )
00132     {
00133         debug_level = (int)(DEBUG_LEVEL);
00134     }
00135     else if ( (debug_env = getenv("LCMAPS_DEBUG_LEVEL")) )
00136     {
00137         /* convert into integer */
00138         int j = 0;
00139 
00140         for (j = 0; j < strlen(debug_env); j++)
00141         {
00142             if (!isdigit((debug_env)[j]))
00143             {
00144                 fprintf(stderr,"lcmaps_log_open(): found non-digit in environment variable in \"LCMAPS_DEBUG_LEVEL\" = %s\n", debug_env);
00145                 return 1;
00146             }
00147         }
00148         debug_level = atoi(debug_env);
00149         if (debug_level < 0)
00150         {
00151             fprintf(stderr,"lcmaps_log_open(): environment variable in \"LCMAPS_DEBUG_LEVEL\" should be >= 0\n");
00152             return 1;
00153         }
00154     }
00155     else
00156     {
00157         debug_level = 0;
00158     }
00159 
00160     if (debug_level > 0)
00161     {
00162         lcmaps_log(0,"lcmaps_log_open(): setting debugging level to %d\n", debug_level);
00163     }
00164 
00165     /*
00166      * Check if there is an extra log string
00167      * These environment variables are checked: JOB_REPOSITORY_ID and GATEKEEPER_JM_ID
00168      */
00169     if ( (logstr_env = getenv("JOB_REPOSITORY_ID")) != NULL )
00170     {
00171         extra_logstr = strdup(logstr_env);
00172     }
00173     else if ( (logstr_env = getenv("GATEKEEPER_JM_ID")) != NULL )
00174     {
00175         extra_logstr = strdup(logstr_env);
00176     }
00177 
00178     return 0;
00179 }
00180 
00181 /******************************************************************************
00182 Function:       lcmaps_log()
00183 Description:    Log information to file and or syslog
00184 Parameters:
00185                 prty:    syslog priority (if 0 don't syslog)
00186                 fmt:     string format
00187 Returns:        0 succes
00188                 1 failure
00189 ******************************************************************************/
00209 int
00210 lcmaps_log(int prty, char * fmt, ...)
00211 {
00212     va_list pvar;
00213     char    buf[MAX_LOG_BUFFER_SIZE];
00214     int     res;
00215 
00216     va_start(pvar, fmt);
00217     res=vsnprintf(buf,MAX_LOG_BUFFER_SIZE,fmt,pvar);
00218     va_end(pvar);
00219     if ( (res >= MAX_LOG_BUFFER_SIZE) || (res < 0) )
00220     {
00221         fprintf(stderr,"lcmaps_log(): log string too long (> %d)\n",
00222                 MAX_LOG_BUFFER_SIZE);
00223     }
00224     if (logging_usrlog)
00225     {
00226         if (lcmaps_logfp == NULL)
00227         {
00228             fprintf(stderr,"lcmaps_log() error: cannot open file descriptor\n");
00229             return 1;
00230         }
00231         if (extra_logstr == NULL)
00232         {
00233             fprintf(lcmaps_logfp,"LCMAPS %d: %s", prty, buf);
00234         }
00235         else
00236         {
00237             fprintf(lcmaps_logfp,"LCMAPS %d: %s : %s", prty, extra_logstr, buf);
00238         }
00239         fflush(lcmaps_logfp);
00240     }
00241     if (logging_syslog && prty)
00242     {
00243         syslog(prty, buf);
00244     }
00245 
00246     return 0;
00247 }
00248 
00249 /******************************************************************************
00250 Function:       lcmaps_log_debug()
00251 Description:    Print debugging information
00252 Parameters:
00253                 debug_lvl: debugging level
00254                 fmt:       string format
00255 Returns:        0 succes
00256                 1 failure
00257 ******************************************************************************/
00276 int
00277 lcmaps_log_debug(int debug_lvl, char * fmt, ...)
00278 {
00279     va_list pvar;
00280     char    buf[MAX_LOG_BUFFER_SIZE];
00281     int     res;
00282 
00283     va_start(pvar, fmt);
00284     res=vsnprintf(buf,MAX_LOG_BUFFER_SIZE,fmt,pvar);
00285     va_end(pvar);
00286     if ( (res >= MAX_LOG_BUFFER_SIZE) || (res < 0) )
00287     {
00288         fprintf(stderr,"lcmaps_log(): log string too long (> %d)\n",
00289                 MAX_LOG_BUFFER_SIZE);
00290     }
00291     if (debug_lvl <= debug_level)
00292     {
00293         lcmaps_log(0,buf);
00294         return 0;
00295     }
00296     return 1;
00297 }
00298 /******************************************************************************
00299 Function:       lcmaps_log_close()
00300 Description:    Stop logging
00301 Parameters:
00302 Returns:        0 succes
00303                 1 failure
00304 ******************************************************************************/
00316 int
00317 lcmaps_log_close()
00318 {
00319     if (extra_logstr != NULL)
00320     {
00321         free(extra_logstr);
00322         extra_logstr = NULL;
00323     }
00324 
00325     if (lcmaps_logfp == NULL)
00326         return 1;
00327 
00328     fclose(lcmaps_logfp);
00329     lcmaps_logfp=NULL;
00330     return 0;
00331 }
00332 
00333 
00334 /******************************************************************************
00335 Function:       lcmaps_log_time()
00336 Description:    Log information to file and or syslog with a timestamp
00337 Parameters:
00338                 prty:    syslog priority (if 0 don't syslog)
00339                 fmt:     string format
00340 Returns:        0 succes
00341                 1 failure
00342 ******************************************************************************/
00362 int
00363 lcmaps_log_time(int prty, char * fmt, ...)
00364 {
00365     va_list     pvar;
00366     char        buf[MAX_LOG_BUFFER_SIZE];
00367     char *      datetime = NULL;
00368     char *      tmpbuf = NULL;
00369     int         res;
00370     time_t      clock;
00371     struct tm * tmp = NULL;
00372 
00373 
00374     va_start(pvar, fmt);
00375     res=vsnprintf(buf,MAX_LOG_BUFFER_SIZE,fmt,pvar);
00376     va_end(pvar);
00377     if ( (res >= MAX_LOG_BUFFER_SIZE) || (res < 0) )
00378     {
00379         fprintf(stderr,"lcmaps_log_time(): log string too long (> %d)\n",
00380                 MAX_LOG_BUFFER_SIZE);
00381     }
00382 
00383     if (extra_logstr == NULL)
00384     {
00385         time(&clock);
00386         tmp = localtime(&clock);
00387 
00388         datetime = malloc(sizeof(char) * 20);
00389 
00390         res=snprintf(datetime, 20, "%04d-%02d-%02d.%02d:%02d:%02d",
00391                tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
00392                tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
00393         if ( (res >= 20) || (res < 0) )
00394         {
00395             fprintf(stderr,"lcmaps_log_time(): date string too long (> %d)\n",
00396                     20);
00397         }
00398 
00399         tmpbuf = (char *) malloc ((strlen(datetime) + strlen(buf) + strlen(" : ")) * sizeof(char) + 1);
00400         strcpy(tmpbuf, datetime);
00401         strcat(tmpbuf, " : ");
00402         strcat(tmpbuf, buf);
00403     }
00404     else
00405     {
00406         tmpbuf = (char *) malloc ((strlen(extra_logstr) + strlen(buf) + strlen(" : ")) * sizeof(char) + 1);
00407         strcpy(tmpbuf, extra_logstr);
00408         strcat(tmpbuf, " : ");
00409         strcat(tmpbuf, buf);
00410     }
00411 
00412     if (logging_usrlog)
00413     {
00414         if (lcmaps_logfp == NULL)
00415         {
00416             fprintf(stderr,"lcmaps_log_time() error: cannot open file descriptor\n");
00417             return 1;
00418         }
00419         fprintf(lcmaps_logfp,"LCMAPS %d: %s",prty,tmpbuf);
00420         fflush(lcmaps_logfp);
00421     }
00422     if (logging_syslog && prty)
00423     {
00424         syslog(prty, tmpbuf);
00425     }
00426 
00427     if (datetime != NULL) free(datetime);
00428     if (tmpbuf != NULL) free(tmpbuf);
00429 
00430     return 0;
00431 }
00432 
00433 /******************************************************************************
00434 CVS Information:
00435     $Source: /cvs/fabric_mgt/gridification/lcmaps/src/pluginmanager/lcmaps_log.c,v $
00436     $Date: 2004/01/05 16:42:24 $
00437     $Revision: 1.8 $
00438     $Author: martijn $
00439 ******************************************************************************/

Generated at Thu Mar 4 17:39:03 2004 for edg-lcmaps by doxygen1.2.8.1 written by Dimitri van Heesch, © 1997-2001