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

log.h

Go to the documentation of this file.
00001 
00002 /*
00003 ** Copyright (C) 2005 Russell Leighton
00004 **
00005 ** This program is free software; you can redistribute it and/or modify
00006 ** it under the terms of the GNU General Public License as published by
00007 ** the Free Software Foundation; either version 2 of the License, or
00008 ** (at your option) any later version.
00009 **
00010 ** This program is distributed in the hope that it will be useful,
00011 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 ** GNU General Public License for more details.
00014 **
00015 ** You should have received a copy of the GNU General Public License
00016 ** along with this program; if not, write to the Free Software
00017 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00018 */
00019 
00020 
00021 #ifndef __AW_LOG__
00022 #define __AW_LOG__
00023 
00024 #include "sys.h"
00025 #include "thread.h"
00026 #include "errors.h"
00027 #include "util.h"
00028 
00057 /*
00058 
00059 Changes:
00060 
00061 05/14/03 Russ Leighton
00062 Add dedup option.
00063 
00064 03/13/02 Russ Leighton
00065 Made aw_log() thread safe.
00066 
00067 01/19/2002 Russ Leighton
00068  Added support for filename and log rotation
00069 
00070 May-8-2001 Russ Leighton 
00071  Start
00072 
00073 */
00074 
00078 typedef enum {
00079   AW_LOGGER_ENABLED = 0x1,            /*< Can log */        
00080   AW_LOGGER_CACHEMSG = 0x2,           /*< Re-use max size string for log messages */       
00081   AW_LOGGER_ERR2SYSLOG = 0x4,         /*< If error_logger == NULL , on error use syslog  */     
00082   AW_LOGGER_ERR2STDERR = 0x8,         /*< If error_logger == NULL , on error use stderr */     
00083   AW_LOGGER_PREPEND_DATE = 0x10,      /*< Preprend formatted date using asctime(time()) */
00084   AW_LOGGER_DUP2SYSLOG = 0x20,        /*< Also log to syslog */
00085   AW_LOGGER_DUP2STDERR = 0x40,        /*< Also log to stderr */
00086   AW_LOGGER_APPEND_NEWLINE = 0x80,    /*< add a \n to end */
00087   AW_LOGGER_ROTATE_HR = 0x100,        /*< rotate logs on schedule (hr) */
00088   AW_LOGGER_ROTATE_DY = 0x200,        /*< rotate logs on schedule (day) */
00089   AW_LOGGER_ROTATE_MH = 0x400,        /*< rotate logs on schedule (month) */
00090   AW_LOGGER_DEDUP = 0x800,            /*< only print unique consecutive messages */
00091   AW_LOGGER_NBITS                     /*< must be last! */
00092 } aw_logger_flag_t;
00093 
00094 
00095 
00099 #define AW_LOGGER_IS_ROTATING(flags) \
00100 ( (flags)&(AW_LOGGER_ROTATE_HR | AW_LOGGER_ROTATE_DY | AW_LOGGER_ROTATE_MH) )
00101 
00102 
00106 #define AW_LOGGER_DEFAULT_FLAGS (\
00107 AW_LOGGER_ENABLED | \
00108 AW_LOGGER_CACHEMSG | \
00109 AW_LOGGER_ERR2SYSLOG | \
00110 AW_LOGGER_ERR2STDERR | \
00111 AW_LOGGER_PREPEND_DATE | \
00112 AW_LOGGER_APPEND_NEWLINE \
00113 )
00114 
00118 typedef struct aw_logger_struct {
00119 
00120   sem_t lock;                                       /* make thread safe */
00121 
00122   int32_t min_level, max_level;                     /* level range of accepted log messages (inclusive) */
00123 
00124   const byte_t *filename;                           /* optional filename, if not NULL, I open()'d the fd and need to close() */
00125 
00126   int32_t fd;                                       /* file descriptor to write messages */
00127 
00128   u_int64_t byteswritten;                           /* total bytes written */
00129 
00130   const byte_t *(*strlevel)(int32_t level);       /* if not NULL, this adds a string level code to left of message */
00131 
00132   struct aw_logger_struct *error_logger;            /* this called if current logger has an error */
00133 
00134 
00135   /* --------- private */
00136 
00137   u_int32_t lastrotation;                           /* remember the hr/day/week of the last rotation */
00138 
00139   aw_logger_flag_t flags;                           /* config */
00140 
00141   struct aw_logger_struct                           /* aw_log() will evaluate a 'chain' of loggers */
00142    *head, *prev, *next;                    
00143 
00144   char *msg;                                        /* cached message string */
00145   u_int32_t msg_size;                               /* size of message */
00146 
00147   u_int32_t dup_count;                              /* number of consective dups */
00148 
00149 } aw_logger_t;
00150 
00151 
00155 typedef struct {
00156 
00157   /* Filename to open, overrides fd if not NULL */
00158   const byte_t *filename;
00159 
00160   /* File descriptor to write to */
00161   int fd;
00162 
00163   /* Minimum level of log messages accepted */
00164   int32_t min_level;
00165 
00166   /* Maximum level of log messages accepted */
00167   int32_t max_level;
00168 
00169   /* Function to call to map level to string token in log entry (can be NULL) */
00170   const byte_t *(*strlevel)(int32_t level);
00171 
00172   /* Capabilities */
00173   aw_logger_flag_t flags;
00174 
00175   /* Logger to call on error in logger itself (can be NULL) */
00176   aw_logger_t *error_logger;
00177 
00178 } aw_logger_params_t;
00179 
00180 
00188 int32_t aw_logger_init(aw_logger_t *logger, aw_logger_params_t *params);
00189 
00196 int32_t aw_logger_clear(aw_logger_t *logger);
00197 
00198 
00208 int32_t aw_log(aw_logger_t *logger, int32_t level, const char *fmt, ... ) __attribute__ ((format (printf, 3, 4)));
00209 
00216 int32_t aw_logger_link(aw_logger_t *logger, aw_logger_t *chain);
00217 
00222 void aw_logger_unlink(aw_logger_t *logger);
00223 
00229 void aw_logger_setflags(aw_logger_t *logger, aw_logger_flag_t new_flags);
00230 
00235 void aw_logger_enable(aw_logger_t *logger);
00236 
00241 void aw_logger_disable(aw_logger_t *logger);
00242 
00243 #endif
Aware 0.11.1 Copyright (C) 1998-2005 Russell Leighton (russ@elegant-software.com)