Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

loghandler.cc File Reference

#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <time.h>
#include "loghandler.h"

Go to the source code of this file.

Compounds

struct  loghandle_struct

Functions

int logfilter (int logcode, const char *logmsg)
int loghandler_init (int id, const char *logdevice, int logclass, int loglevel, int logformat)
int loghandler_init (int id,const char *logdevice)
int loghandler_setlevel (int id, int loglevel)
int loghandler_getlevel (int id)
int loghandler_setclass (int id, int logclass)
int loghandler_getclass (int id)
void loghandler (int loglevel, int logclass, const char *msg, ...)
void logf (int logcode, const char *msg, ...)
void debugf (int logcode, const char *msg, ...)

Variables

loghandle_struct loghandle [LOGHANDLE_MAX]
const char* event_code []


Function Documentation

int logfilter ( int logcode,
const char * logmsg )
 

Definition at line 119 of file loghandler.cc.

Referenced by debugf(), logf(), and loghandler().

00120 {   
00121     char time_str[LOGMSG_MAX];
00122     char date_str[LOGMSG_MAX];
00123     char year_str[LOGMSG_MAX];
00124 
00125     time_t log_time;
00126     struct tm *time_tm;
00127 
00128     int logclass;
00129     int loglevel;
00130     char *str_ptr;
00131 
00132     /* Decode log code */
00133     loglevel = logcode & 15;
00134     logclass = logcode & 240;
00135 
00136     /* Format timestamp */
00137     log_time = time(NULL);
00138     time_tm = localtime(&log_time);
00139     strftime(year_str,LOGMSG_MAX,"%G",time_tm);
00140     strftime(date_str,LOGMSG_MAX,"%m%d",time_tm);
00141     strftime(time_str,LOGMSG_MAX,"%H:%M:%S",time_tm);
00142 
00143     /* Replace eventual line feeds */
00144     while ((str_ptr=strchr(logmsg,'\n'))){
00145         *str_ptr = ' ';
00146     }
00147 
00148     /* Loop all loghandles and see if they handle this message */
00149     for (int id=0; id <= LOGHANDLE_MAX; id++){
00150         if ((loghandle[id].logdevice != NULL) &&        // Current loghandle is configured
00151             (loghandle[id].logclass & logclass) &&      // loghandle handles this class 
00152             (loghandle[id].loglevel >= loglevel))       // loghandle handles this level
00153             {               
00154                 /* Format message for output */
00155                 char outmsg[LOGMSG_MAX];
00156                 char buf[LOGMSG_MAX];
00157 
00158                 // Prefix format
00159                 if (loghandle[id].logformat & SHOW_YEAR){
00160                     sprintf(buf,"%s ",year_str);
00161                     strcat(outmsg,buf);
00162                 }
00163                 if (loghandle[id].logformat & SHOW_DATE){
00164                     sprintf(buf,"%s ",date_str);
00165                     strcat(outmsg,buf);
00166                 }
00167                 if (loghandle[id].logformat & SHOW_TIME){
00168                     sprintf(buf,"%s ",time_str);
00169                     strcat(outmsg,buf);
00170                 }
00171 
00172                 strcat(outmsg,"[");
00173 
00174                 if (loghandle[id].logformat & SHOW_ID){
00175                     sprintf(buf,"%i.",id);
00176                     strcat(outmsg,buf);
00177                 }
00178                 if (loghandle[id].logformat & SHOW_CLASS){
00179                     sprintf(buf,"%i.",logclass);
00180                     strcat(outmsg,buf);
00181                 }
00182                 if (loghandle[id].logformat & SHOW_LEVEL){
00183                     sprintf(buf,"%s",event_code[loglevel]);
00184                     strcat(outmsg,buf);
00185                 }
00186                 // Message
00187                 snprintf(buf,sizeof(buf)-1,"] %s",logmsg);
00188                 strncat(outmsg,buf,sizeof(outmsg)-strlen(outmsg)-1);
00189                 
00190                 /* Write message to logdevice */
00191                 FILE *fout = fopen (loghandle[id].logdevice,"a");
00192                 if (fout != NULL){
00193                     fprintf(fout,"%s\n",outmsg);
00194                     fclose(fout);
00195                 }
00196                 else
00197                     printf("loghandler FATAL: Could not write to logfile %s",loghandle[id].logdevice);
00198             }
00199     }
00200     return (0);
00201 }

int loghandler_init ( int id,
const char * logdevice,
int logclass,
int loglevel,
int logformat )
 

Definition at line 203 of file loghandler.cc.

00208 {
00209     char msg[LOGMSG_MAX];
00210     
00211     // Check parameters
00212     if (id > LOGHANDLE_MAX) return(-1);
00213     if (logdevice == NULL) return(-1);
00214     FILE *fout = fopen (logdevice,"a");
00215     if (fout==NULL)
00216         return (-1);
00217     else
00218         fclose (fout);
00219 
00220     if (logformat < 0) logformat = DEFAULT_LOGFORMAT;
00221     if (logclass < 0) logclass = DEFAULT_LOGCLASS;
00222     if (loglevel < 0) loglevel = DEFAULT_LOGLEVEL;
00223     
00224     // Set config
00225     loghandle[id].logdevice = logdevice;
00226     loghandle[id].logformat = logformat;
00227     loghandle[id].logclass = logclass;
00228     loghandle[id].loglevel = loglevel;
00229 
00230     // Notify
00231     snprintf(msg,
00232              LOGMSG_MAX,
00233              "INIT::loghandle[%i] device[%s] format[%i] classfilter[%i] level[%i]",
00234              id,
00235              logdevice,
00236              logformat,
00237              logclass,
00238              loglevel);
00239     loghandler(logclass,D_CHECK,msg);
00240     return(0);
00241 }

int loghandler_init ( int id,
const char * logdevice )
 

Definition at line 243 of file loghandler.cc.

00244 {
00245     return (loghandler_init(id,logdevice,-1,-1,-1));
00246 }

int loghandler_setlevel ( int id,
int loglevel )
 

Definition at line 248 of file loghandler.cc.

00249 {
00250     loghandler(loghandle[id].logclass,
00251                D_CHECK,
00252                "loghandle[%i].setlevel %i",
00253                id,
00254                loglevel);
00255     
00256     loghandle[id].loglevel = loglevel;
00257     
00258     return(0);
00259 }

int loghandler_getlevel ( int id )
 

Definition at line 261 of file loghandler.cc.

00262 {
00263     return (loghandle[id].loglevel);
00264 }

int loghandler_setclass ( int id,
int logclass )
 

Definition at line 266 of file loghandler.cc.

00267 {
00268     loghandle[id].logclass = logclass;
00269 
00270     return(0);
00271 }

int loghandler_getclass ( int id )
 

Definition at line 273 of file loghandler.cc.

00274 {
00275     return (loghandle[id].logclass);
00276 }

void loghandler ( int loglevel,
int logclass,
const char * msg,
... )
 

Definition at line 278 of file loghandler.cc.

00279 {   
00280     char logmsg[LOGMSG_MAX];
00281     int logcode;
00282 
00283     /* Combine class and level to logcode */
00284     logcode = loglevel | logclass;
00285 
00286     // Format message
00287     va_list list;
00288     va_start (list,msg);
00289     vsprintf (logmsg,msg,list);
00290     va_end (list);
00291     
00292     logfilter(logcode, logmsg);     
00293 }

void logf ( int logcode,
const char * msg,
... )
 

Definition at line 295 of file loghandler.cc.

00296 {   
00297     char logmsg[LOGMSG_MAX];
00298 
00299     logcode |= MAIN_LOG;
00300 
00301     // Format message
00302     va_list list;
00303     va_start (list,msg);
00304     vsprintf (logmsg,msg,list);
00305     va_end (list);
00306 
00307     logfilter(logcode, logmsg);
00308 }

void debugf ( int logcode,
const char * msg,
... )
 

Definition at line 310 of file loghandler.cc.

00311 {   
00312     char logmsg[LOGMSG_MAX];
00313 
00314     logcode |= DEBUG_LOG; // Add debug flag
00315     logcode |= MAIN_LOG;  // Temporary hack
00316  
00317     // Format message
00318     va_list list;
00319     va_start (list,msg);
00320     vsprintf (logmsg,msg,list);
00321     va_end (list);
00322 
00323     logfilter(logcode, logmsg);
00324 }


Variable Documentation

loghandle_struct loghandle[LOGHANDLE_MAX]
 

Definition at line 98 of file loghandler.cc.

const char * event_code[] [static]
 

Initializer:

{
    "mark",
    "fatal",
    "error",
    "warning",
    "normal",
    "notice",
    "info",
    "verbose",
    "check",
    "logic",
    "argh",
    "hmm",
    "func",
    "var",
    "io",
    "trace"
}

Definition at line 100 of file loghandler.cc.


Generated at Mon Jan 22 08:35:15 2001 for ldapconf by doxygen1.2.1 written by Dimitri van Heesch, © 1997-2000