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; 
00047 /******************************************************************************
00048 Function:       lcmaps_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 ******************************************************************************/
00079 int
00080 lcmaps_log_open(char * path, FILE * fp, unsigned short logtype )
00081 {
00082     char * debug_env = NULL;
00083 
00084     if ((logtype & DO_SYSLOG) == DO_SYSLOG)
00085     {
00086         fprintf(stderr,"lcmaps_log_open() error: attempt to do syslogging,");
00087         fprintf(stderr," not supported yet\n");
00088 #if 0
00089         logging_syslog=1;
00090         /* Not yet applicable, wait for LCMAPS to become daemon */
00091         openlog("EDG LCMAPS", LOG_PID, LOG_DAEMON);
00092 #endif
00093     }
00094     if ((logtype & DO_USRLOG) == DO_USRLOG)
00095     {
00096         logging_usrlog=1;
00097         if (fp != NULL)
00098         {
00099             /* File already opened */
00100             lcmaps_logfp=fp;
00101         }
00102         else if (path != NULL)
00103         {
00104             /* Try to append to file */
00105             if ((lcmaps_logfp = fopen(path, "a")) == NULL)
00106             {
00107                 fprintf(stderr, "lcmaps_log_open(): Cannot open logfile %s: %s\n",
00108                         path, sys_errlist[errno]);
00109                 if (logging_syslog)
00110                 {
00111                     syslog(LOG_ERR, "lcmaps_log_open(): Cannot open logfile %s\n", path);
00112                 }
00113                 return 1;
00114             }
00115         }
00116         else
00117         {
00118             fprintf(stderr, "lcmaps_log_open(): Please specify either (open) file descriptor");
00119             fprintf(stderr, " or name of logfile\n");
00120             return 1;
00121         }
00122     }
00123     /*
00124      * Set the debugging level:
00125      *    1. Try if DEBUG_LEVEL > 0
00126      *    2. Try if LCMAPS_DEBUG_LEVEL is set and if it is an integer
00127      *    3. set debug_level = 0;
00128      */
00129     if ( (int)(DEBUG_LEVEL) > 0 )
00130     {
00131         debug_level = (int)(DEBUG_LEVEL);
00132     }
00133     else if ( (debug_env = getenv("LCMAPS_DEBUG_LEVEL")) )
00134     {
00135         /* convert into integer */
00136         int j = 0;
00137 
00138         for (j = 0; j < strlen(debug_env); j++)
00139         {
00140             if (!isdigit((debug_env)[j]))
00141             {
00142                 fprintf(stderr,"lcmaps_log_open(): found non-digit in environment variable in \"LCMAPS_DEBUG_LEVEL\" = %s\n", debug_env);
00143                 return 1;
00144             }
00145         }
00146         debug_level = atoi(debug_env);
00147         if (debug_level < 0)
00148         {
00149             fprintf(stderr,"lcmaps_log_open(): environment variable in \"LCMAPS_DEBUG_LEVEL\" should be >= 0\n");
00150             return 1;
00151         }
00152     }
00153     else
00154     {
00155         debug_level = 0;
00156     }
00157 
00158     if (debug_level > 0)
00159     {
00160         lcmaps_log(0,"lcmaps_log_open(): setting debugging level to %d\n", debug_level);
00161     }
00162 
00163     return 0;
00164 }
00165 
00166 /******************************************************************************
00167 Function:       lcmaps_log()
00168 Description:    Log information to file and or syslog
00169 Parameters:
00170                 prty:    syslog priority (if 0 don't syslog)
00171                 fmt:     string format
00172 Returns:        0 succes
00173                 1 failure
00174 ******************************************************************************/
00194 int
00195 lcmaps_log(int prty, char * fmt, ...)
00196 {
00197     va_list pvar;
00198     char    buf[MAX_LOG_BUFFER_SIZE];
00199     int     res;
00200 
00201     va_start(pvar, fmt);
00202     res=vsnprintf(buf,MAX_LOG_BUFFER_SIZE,fmt,pvar);
00203     va_end(pvar);
00204     if ( (res >= MAX_LOG_BUFFER_SIZE) || (res < 0) )
00205     {
00206         fprintf(stderr,"lcmaps_log(): log string too long (> %d)\n",
00207                 MAX_LOG_BUFFER_SIZE);
00208     }
00209     if (logging_usrlog)
00210     {
00211         if (lcmaps_logfp == NULL)
00212         {
00213             fprintf(stderr,"lcmaps_log() error: cannot open file descriptor\n");
00214             return 1;
00215         }
00216         fprintf(lcmaps_logfp,"LCMAPS %d: %s",prty,buf);
00217         fflush(lcmaps_logfp);
00218     }
00219     if (logging_syslog && prty)
00220     {
00221         syslog(prty, buf);
00222     }
00223 
00224     return 0;
00225 }
00226 
00227 /******************************************************************************
00228 Function:       lcmaps_log_debug()
00229 Description:    Print debugging information
00230 Parameters:
00231                 debug_lvl: debugging level
00232                 fmt:       string format
00233 Returns:        0 succes
00234                 1 failure
00235 ******************************************************************************/
00254 int
00255 lcmaps_log_debug(int debug_lvl, char * fmt, ...)
00256 {
00257     va_list pvar;
00258     char    buf[MAX_LOG_BUFFER_SIZE];
00259     int     res;
00260 
00261     va_start(pvar, fmt);
00262     res=vsnprintf(buf,MAX_LOG_BUFFER_SIZE,fmt,pvar);
00263     va_end(pvar);
00264     if ( (res >= MAX_LOG_BUFFER_SIZE) || (res < 0) )
00265     {
00266         fprintf(stderr,"lcmaps_log(): log string too long (> %d)\n",
00267                 MAX_LOG_BUFFER_SIZE);
00268     }
00269     if (debug_lvl <= debug_level)
00270     {
00271         lcmaps_log(0,buf);
00272         return 0;
00273     }
00274     return 1;
00275 }
00276 /******************************************************************************
00277 Function:       lcmaps_log_close()
00278 Description:    Stop logging
00279 Parameters:
00280 Returns:        0 succes
00281                 1 failure
00282 ******************************************************************************/
00294 int
00295 lcmaps_log_close()
00296 {
00297     if (lcmaps_logfp == NULL)
00298         return 1;
00299 
00300     fclose(lcmaps_logfp);
00301     lcmaps_logfp=NULL;
00302     return 0;
00303 }
00304 
00305 
00306 /******************************************************************************
00307 Function:       lcmaps_log_time()
00308 Description:    Log information to file and or syslog with a timestamp
00309 Parameters:
00310                 prty:    syslog priority (if 0 don't syslog)
00311                 fmt:     string format
00312 Returns:        0 succes
00313                 1 failure
00314 ******************************************************************************/
00334 int
00335 lcmaps_log_time(int prty, char * fmt, ...)
00336 {
00337     va_list     pvar;
00338     char        buf[MAX_LOG_BUFFER_SIZE];
00339     char *      datetime;
00340     char *      tmpbuf;
00341     int         res;
00342     time_t      clock;
00343     struct tm * tmp; 
00344 
00345  
00346     va_start(pvar, fmt);
00347     res=vsnprintf(buf,MAX_LOG_BUFFER_SIZE,fmt,pvar);
00348     va_end(pvar);
00349     if ( (res >= MAX_LOG_BUFFER_SIZE) || (res < 0) )
00350     {
00351         fprintf(stderr,"lcmaps_log_time(): log string too long (> %d)\n",
00352                 MAX_LOG_BUFFER_SIZE);
00353     }
00354 
00355     time(&clock);
00356     tmp = localtime(&clock);
00357 
00358     datetime = malloc(sizeof(char) * 20);
00359  
00360     res=snprintf(datetime, 20, "%04d-%02d-%02d.%02d:%02d:%02d",
00361            tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
00362            tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
00363     if ( (res >= 20) || (res < 0) )
00364     {
00365         fprintf(stderr,"lcmaps_log_time(): date string too long (> %d)\n",
00366                 20);
00367     }
00368 
00369     tmpbuf = (char *) malloc ((strlen(datetime) + strlen(buf) + strlen(" : ")) * sizeof(char) + 1);
00370     strcpy(tmpbuf, datetime);
00371     strcat(tmpbuf, " : ");
00372     strcat(tmpbuf, buf);
00373 
00374     if (logging_usrlog)
00375     {
00376         if (lcmaps_logfp == NULL)
00377         {
00378             fprintf(stderr,"lcmaps_log_time() error: cannot open file descriptor\n");
00379             return 1;
00380         }
00381         fprintf(lcmaps_logfp,"LCMAPS %d: %s",prty,tmpbuf);
00382         fflush(lcmaps_logfp);
00383     }
00384     if (logging_syslog && prty)
00385     {
00386         syslog(prty, tmpbuf);
00387     }
00388 
00389     free(datetime);
00390     free(tmpbuf);
00391  
00392     return 0;
00393 }    
00394 
00395 /******************************************************************************
00396 CVS Information:
00397     $Source: /cvs/fabric_mgt/gridification/lcmaps/src/pluginmanager/lcmaps_log.c,v $
00398     $Date: 2003/08/15 12:43:22 $
00399     $Revision: 1.7 $
00400     $Author: martijn $
00401 ******************************************************************************/

Generated at Tue Sep 23 15:48:08 2003 for edg-lcmaps by doxygen1.2.8.1 written by Dimitri van Heesch, © 1997-2001