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

Generated on Sat Oct 2 02:18:26 2004 for edg-lcmaps by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002