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) Members of the EGEE Collaboration. 2004.
00003  * See http://eu-egee.org/partners/ for details on the copyright holders.
00004  * For license conditions see the license file or
00005  * http://eu-egee.org/license.html
00006  */
00007 
00008 /*                                                                                                            
00009  * Copyright (c) 2001 EU DataGrid.                                                                             
00010  * For license conditions see http://www.eu-datagrid.org/license.html                                          
00011  *
00012  * Copyright (c) 2001, 2002 by 
00013  *     Martijn Steenbakkers <martijn@nikhef.nl>,
00014  *     David Groep <davidg@nikhef.nl>,
00015  *     NIKHEF Amsterdam, the Netherlands
00016  */
00017 
00025 /*****************************************************************************
00026                             Include header files
00027 ******************************************************************************/
00028 #include <stdlib.h>
00029 #include <stdio.h>
00030 #include <string.h>
00031 #include <errno.h>
00032 #include <stdarg.h>
00033 #include <syslog.h>
00034 #include <time.h>
00035 #include <ctype.h>
00036 #include "_lcmaps_log.h"
00037 
00038 /******************************************************************************
00039                           Module specific prototypes
00040 ******************************************************************************/
00041 #ifndef DEBUG_LEVEL
00042 #define DEBUG_LEVEL 0 
00043 #endif /* DEBUG_LEVEL */
00044 
00045 /******************************************************************************
00046                        Define module specific variables
00047 ******************************************************************************/
00048 static FILE *  lcmaps_logfp=NULL; 
00049 static int     logging_usrlog=0; 
00050 static int     logging_syslog=0; 
00051 //static int     debug_level=DEBUG_LEVEL; /*!< debugging level \internal */
00052 static int     debug_level=0; 
00053 static char *  extra_logstr = NULL; 
00054 static int     should_close_lcmaps_logfp = 0; 
00056 /******************************************************************************
00057 Function:       lcmaps_log_open()
00058 Description:    Start logging
00059 Parameters:
00060                 path:    path of logfile
00061                 fp:      file pointer to already opened file (or NULL)
00062                 logtype: DO_USRLOG, DO_SYSLOG
00063 Returns:        0 succes
00064                 1 failure
00065 ******************************************************************************/
00088 int
00089 lcmaps_log_open(char * path, FILE * fp, unsigned short logtype )
00090 {
00091     char * debug_env = NULL;
00092     char * logstr_env = NULL;
00093 
00094     if ((logtype & DO_SYSLOG) == DO_SYSLOG)
00095     {
00096 //        fprintf(stderr,"lcmaps_log_open() error: attempt to do syslogging,");
00097 //        fprintf(stderr," not supported yet\n");
00098         logging_syslog=1;
00099 #if 0
00100         /* Not yet applicable, wait for LCMAPS to become daemon */
00101         openlog("EDG LCMAPS", LOG_PID, LOG_AUTHPRIV);
00102 #endif
00103     }
00104     if ((logtype & DO_USRLOG) == DO_USRLOG)
00105     {
00106         logging_usrlog=1;
00107         if (fp != NULL)
00108         {
00109             /* File already opened, should not be closed at the end */
00110             lcmaps_logfp=fp;
00111             should_close_lcmaps_logfp = 0;
00112         }
00113         else if (path != NULL)
00114         {
00115             /* Try to append to file */
00116             if ((lcmaps_logfp = fopen(path, "a")) == NULL)
00117             {
00118                 fprintf(stderr, "lcmaps_log_open(): Cannot open logfile %s: %s\n",
00119                         path, sys_errlist[errno]);
00120                 if (logging_syslog)
00121                 {
00122                     syslog(LOG_ERR, "lcmaps_log_open(): Cannot open logfile %s\n", path);
00123                 }
00124                 return 1;
00125             }
00126             should_close_lcmaps_logfp = 1;
00127         }
00128         else
00129         {
00130             fprintf(stderr, "lcmaps_log_open(): Please specify either (open) file descriptor");
00131             fprintf(stderr, " or name of logfile\n");
00132             return 1;
00133         }
00134     }
00135     /*
00136      * Set the debugging level:
00137      *    1. Try if DEBUG_LEVEL > 0
00138      *    2. Try if LCMAPS_DEBUG_LEVEL is set and if it is an integer
00139      *    3. set debug_level = 0;
00140      */
00141     if ( (int)(DEBUG_LEVEL) > 0 )
00142     {
00143         debug_level = (int)(DEBUG_LEVEL);
00144     }
00145     else if ( (debug_env = getenv("LCMAPS_DEBUG_LEVEL")) )
00146     {
00147         /* convert into integer */
00148         int j = 0;
00149 
00150         for (j = 0; j < strlen(debug_env); j++)
00151         {
00152             if (!isdigit((debug_env)[j]))
00153             {
00154                 fprintf(stderr,"lcmaps_log_open(): found non-digit in environment variable in \"LCMAPS_DEBUG_LEVEL\" = %s\n", debug_env);
00155                 return 1;
00156             }
00157         }
00158         debug_level = atoi(debug_env);
00159         if (debug_level < 0)
00160         {
00161             fprintf(stderr,"lcmaps_log_open(): environment variable in \"LCMAPS_DEBUG_LEVEL\" should be >= 0\n");
00162             return 1;
00163         }
00164     }
00165     else
00166     {
00167         debug_level = 0;
00168     }
00169 
00170     if (debug_level > 0)
00171     {
00172         lcmaps_log(0,"lcmaps_log_open(): setting debugging level to %d\n", debug_level);
00173     }
00174 
00175     /*
00176      * Check if there is an extra log string
00177      * These environment variables are checked: JOB_REPOSITORY_ID and GATEKEEPER_JM_ID
00178      */
00179     if ( (logstr_env = getenv("LCMAPS_LOG_STRING")) != NULL )
00180     {
00181         extra_logstr = strdup(logstr_env);
00182     }
00183     else if ( (logstr_env = getenv("JOB_REPOSITORY_ID")) != NULL )
00184     {
00185         extra_logstr = strdup(logstr_env);
00186     }
00187     else if ( (logstr_env = getenv("GATEKEEPER_JM_ID")) != NULL )
00188     {
00189         extra_logstr = strdup(logstr_env);
00190     }
00191 
00192     return 0;
00193 }
00194 
00195 /******************************************************************************
00196 Function:       lcmaps_log()
00197 Description:    Log information to file and or syslog
00198 Parameters:
00199                 prty:    syslog priority (if 0 don't syslog)
00200                 fmt:     string format
00201 Returns:        0 succes
00202                 1 failure
00203 ******************************************************************************/
00223 int
00224 lcmaps_log(int prty, 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,"lcmaps_log(): log string too long (> %d)\n",
00236                 MAX_LOG_BUFFER_SIZE);
00237     }
00238     if (logging_usrlog)
00239     {
00240         if (lcmaps_logfp == NULL)
00241         {
00242             fprintf(stderr,"lcmaps_log() error: cannot open file descriptor\n");
00243             return 1;
00244         }
00245         if (extra_logstr == NULL)
00246         {
00247             fprintf(lcmaps_logfp,"LCMAPS %d: %s", prty, buf);
00248         }
00249         else
00250         {
00251             fprintf(lcmaps_logfp,"LCMAPS %d: %s : %s", prty, extra_logstr, buf);
00252         }
00253         fflush(lcmaps_logfp);
00254     }
00255     if (logging_syslog && prty)
00256     {
00257         syslog(prty, buf);
00258     }
00259 
00260     return 0;
00261 }
00262 
00263 /******************************************************************************
00264 Function:       lcmaps_log_a_string()
00265 Description:    Log a string according to the passed format to file and or syslog
00266 Parameters:
00267                 prty:       syslog priority (if 0 don't syslog)
00268                 fmt:        string format
00269                 the_string: the string
00270 Returns:        0 succes
00271                 1 failure
00272 ******************************************************************************/
00292 int
00293 lcmaps_log_a_string(int prty, char * fmt, char * the_string)
00294 {
00295     char    buf[MAX_LOG_BUFFER_SIZE];
00296     int     res;
00297 
00298     res = snprintf(buf, MAX_LOG_BUFFER_SIZE, fmt, the_string);
00299     if ( (res >= MAX_LOG_BUFFER_SIZE) || (res < 0) )
00300     {
00301         fprintf(stderr,"lcmaps_log_a_string(): log string too long (> %d)\n",
00302                 MAX_LOG_BUFFER_SIZE);
00303     }
00304     if (logging_usrlog)
00305     {
00306         if (lcmaps_logfp == NULL)
00307         {
00308             fprintf(stderr,"lcmaps_log() error: cannot open file descriptor\n");
00309             return 1;
00310         }
00311         if (extra_logstr == NULL)
00312         {
00313             fprintf(lcmaps_logfp,"LCMAPS %d: %s", prty, buf);
00314         }
00315         else
00316         {
00317             fprintf(lcmaps_logfp,"LCMAPS %d: %s : %s", prty, extra_logstr, buf);
00318         }
00319         fflush(lcmaps_logfp);
00320     }
00321     if (logging_syslog && prty)
00322     {
00323         syslog(prty, buf);
00324     }
00325 
00326     return 0;
00327 }
00328 
00329 /******************************************************************************
00330 Function:       lcmaps_log_debug()
00331 Description:    Print debugging information
00332 Parameters:
00333                 debug_lvl: debugging level
00334                 fmt:       string format
00335 Returns:        0 succes
00336                 1 failure
00337 ******************************************************************************/
00356 int
00357 lcmaps_log_debug(int debug_lvl, char * fmt, ...)
00358 {
00359     va_list pvar;
00360     char    buf[MAX_LOG_BUFFER_SIZE];
00361     int     res;
00362 
00363     va_start(pvar, fmt);
00364     res=vsnprintf(buf,MAX_LOG_BUFFER_SIZE,fmt,pvar);
00365     va_end(pvar);
00366     if ( (res >= MAX_LOG_BUFFER_SIZE) || (res < 0) )
00367     {
00368         fprintf(stderr,"lcmaps_log(): log string too long (> %d)\n",
00369                 MAX_LOG_BUFFER_SIZE);
00370     }
00371     if (debug_lvl <= debug_level)
00372     {
00373         lcmaps_log(0,buf);
00374         return 0;
00375     }
00376     return 1;
00377 }
00378 
00379 /******************************************************************************
00380 Function:       lcmaps_log_a_string_debug()
00381 Description:    Print debugging information
00382 Parameters:
00383                 debug_lvl:  debugging level
00384                 fmt:        string format
00385                 the_string: the string
00386 Returns:        0 succes
00387                 1 failure
00388 ******************************************************************************/
00407 int
00408 lcmaps_log_a_string_debug(int debug_lvl, char * fmt, char * the_string)
00409 {
00410     if (debug_lvl <= debug_level)
00411     {
00412         lcmaps_log_a_string(0, fmt, the_string);
00413         return 0;
00414     }
00415     return 1;
00416 }
00417 
00418 /******************************************************************************
00419 Function:       lcmaps_log_close()
00420 Description:    Stop logging
00421 Parameters:
00422 Returns:        0 succes
00423                 1 failure
00424 ******************************************************************************/
00436 int
00437 lcmaps_log_close()
00438 {
00439     if (extra_logstr != NULL)
00440     {
00441         free(extra_logstr);
00442         extra_logstr = NULL;
00443     }
00444 
00445     if (should_close_lcmaps_logfp)
00446     {
00447         fclose(lcmaps_logfp);
00448         lcmaps_logfp=NULL;
00449     }
00450 
00451     return 0;
00452 }
00453 
00454 
00455 /******************************************************************************
00456 Function:       lcmaps_log_time()
00457 Description:    Log information to file and or syslog with a timestamp
00458 Parameters:
00459                 prty:    syslog priority (if 0 don't syslog)
00460                 fmt:     string format
00461 Returns:        0 succes
00462                 1 failure
00463 ******************************************************************************/
00483 int
00484 lcmaps_log_time(int prty, char * fmt, ...)
00485 {
00486     va_list     pvar;
00487     char        buf[MAX_LOG_BUFFER_SIZE];
00488     char *      datetime = NULL;
00489     char *      tmpbuf = NULL;
00490     int         res;
00491     time_t      clock;
00492     struct tm * tmp = NULL;
00493 
00494 
00495     va_start(pvar, fmt);
00496     res=vsnprintf(buf,MAX_LOG_BUFFER_SIZE,fmt,pvar);
00497     va_end(pvar);
00498     if ( (res >= MAX_LOG_BUFFER_SIZE) || (res < 0) )
00499     {
00500         fprintf(stderr,"lcmaps_log_time(): log string too long (> %d)\n",
00501                 MAX_LOG_BUFFER_SIZE);
00502     }
00503 
00504     if (extra_logstr == NULL)
00505     {
00506         time(&clock);
00507         tmp = localtime(&clock);
00508 
00509         datetime = malloc(sizeof(char) * 20);
00510 
00511         res=snprintf(datetime, 20, "%04d-%02d-%02d.%02d:%02d:%02d",
00512                tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
00513                tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
00514         if ( (res >= 20) || (res < 0) )
00515         {
00516             fprintf(stderr,"lcmaps_log_time(): date string too long (> %d)\n",
00517                     20);
00518         }
00519 
00520         tmpbuf = (char *) malloc ((strlen(datetime) + strlen(buf) + strlen(" : ")) * sizeof(char) + 1);
00521         strcpy(tmpbuf, datetime);
00522         strcat(tmpbuf, " : ");
00523         strcat(tmpbuf, buf);
00524     }
00525     else
00526     {
00527         tmpbuf = (char *) malloc ((strlen(extra_logstr) + strlen(buf) + strlen(" : ")) * sizeof(char) + 1);
00528         strcpy(tmpbuf, extra_logstr);
00529         strcat(tmpbuf, " : ");
00530         strcat(tmpbuf, buf);
00531     }
00532 
00533     if (logging_usrlog)
00534     {
00535         if (lcmaps_logfp == NULL)
00536         {
00537             fprintf(stderr,"lcmaps_log_time() error: cannot open file descriptor\n");
00538             return 1;
00539         }
00540         fprintf(lcmaps_logfp,"LCMAPS %d: %s",prty,tmpbuf);
00541         fflush(lcmaps_logfp);
00542     }
00543     if (logging_syslog && prty)
00544     {
00545         syslog(prty, tmpbuf);
00546     }
00547 
00548     if (datetime != NULL) free(datetime);
00549     if (tmpbuf != NULL) free(tmpbuf);
00550 
00551     return 0;
00552 }
00553 
00554 /******************************************************************************
00555 CVS Information:
00556     $Source: /cvs/jra1mw/org.glite.security.lcmaps/src/pluginmanager/lcmaps_log.c,v $
00557     $Date: 2004/12/10 17:44:31 $
00558     $Revision: 1.13 $
00559     $Author: msteenba $
00560 ******************************************************************************/

Generated on Sun May 29 21:22:10 2005 for lcmaps by doxygen 1.3.5