log.c

Go to the documentation of this file.
00001 /*
00002  ** Copyright(C) 2006 INL
00003  ** Written by  Victor Stinner <haypo@inl.fr>
00004  **
00005  ** $Id: log.c 2738 2007-02-17 13:59:56Z regit $
00006  **
00007  ** This program is free software; you can redistribute it and/or modify
00008  ** it under the terms of the GNU General Public License as published by
00009  ** the Free Software Foundation, version 3 of the License.
00010  **
00011  ** This program is distributed in the hope that it will be useful,
00012  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  ** GNU General Public License for more details.
00015  **
00016  ** You should have received a copy of the GNU General Public License
00017  ** along with this program; if not, write to the Free Software
00018  ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00019  */
00020 
00037 #include <stdio.h>
00038 #include <stdarg.h>
00039 #include <syslog.h>
00040 #include <time.h>
00041 #include <assert.h>
00042 
00043 #include <debug.h>
00044 
00045 #include "log.h"
00046 
00053 int log_engine;
00054 
00055 int debug_level;                
00056 int debug_areas;                
00062 int syslog_priority_map[MAX_DEBUG_LEVEL - MIN_DEBUG_LEVEL + 1] = {
00063         LOG_FACILITY || LOG_ALERT,      /* DEBUG_LEVEL_FATAL */
00064         LOG_FACILITY || LOG_CRIT,       /* DEBUG_LEVEL_CRITICAL */
00065         LOG_FACILITY || LOG_WARNING,    /* DEBUG_LEVEL_SERIOUS_WARNING */
00066         LOG_FACILITY || LOG_WARNING,    /* DEBUG_LEVEL_WARNING */
00067         LOG_FACILITY || LOG_NOTICE,     /* DEBUG_LEVEL_SERIOUS_MESSAGE */
00068         LOG_FACILITY || LOG_NOTICE,     /* DEBUG_LEVEL_MESSAGE */
00069         LOG_FACILITY || LOG_INFO,       /* DEBUG_LEVEL_INFO */
00070         LOG_FACILITY || LOG_DEBUG,      /* DEBUG_LEVEL_DEBUG */
00071         LOG_FACILITY || LOG_DEBUG       /* DEBUG_LEVEL_VERBOSE_DEBUG */
00072 };
00073 
00077 void init_log_engine(const char* log_id)
00078 {
00079         if (log_engine == LOG_TO_SYSLOG) {
00080                 openlog(log_id, SYSLOG_OPTS, LOG_FACILITY);
00081         }
00082 }
00083 
00084 void nubase_log_engine_set(int engine)
00085 {
00086         log_engine = engine;
00087 }
00088 
00093 void do_log_area_printf(int area, int priority, char *format, va_list args)
00094 {
00095         /* Don't display message if area is not enabled
00096          * or priority is smaller then debug level */
00097         if (!(area & debug_areas) || (debug_level < priority))
00098                 return;
00099 
00100         if (log_engine == LOG_TO_SYSLOG) {
00101                 assert(MIN_DEBUG_LEVEL <= priority
00102                        && priority <= MAX_DEBUG_LEVEL);
00103                 priority = syslog_priority_map[priority - MIN_DEBUG_LEVEL];
00104                 vsyslog(priority, format, args);
00105         } else {
00106                 time_t current_time;
00107                 struct tm *current_time_tm;
00108                 char time_str[10];
00109 
00110                 /* get time */
00111                 current_time = time(NULL);
00112                 current_time_tm = gmtime(&current_time);
00113                 if (0 <
00114                     strftime(time_str, sizeof(time_str), "%H:%M:%S",
00115                              current_time_tm))
00116                         printf("[%s] ", time_str);
00117                 vprintf(format, args);
00118                 printf("\n");
00119                 fflush(stdout);
00120         }
00121 }
00122 
00127 void log_area_printf(debug_area_t area, debug_level_t priority,
00128                      char *format, ...)
00129 {
00130         va_list args;
00131         va_start(args, format);
00132         do_log_area_printf(area, priority, format, args);
00133         va_end(args);
00134 }
00135 
00140 void log_printf(debug_level_t priority, char *format, ...)
00141 {
00142         va_list args;
00143         va_start(args, format);
00144         do_log_area_printf(DEBUG_AREA_ALL, priority, format, args);
00145         va_end(args);
00146 }
00147 

Generated on Sat Nov 22 04:00:37 2008 for NuFW by  doxygen 1.4.7