00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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,
00064 LOG_FACILITY || LOG_CRIT,
00065 LOG_FACILITY || LOG_WARNING,
00066 LOG_FACILITY || LOG_WARNING,
00067 LOG_FACILITY || LOG_NOTICE,
00068 LOG_FACILITY || LOG_NOTICE,
00069 LOG_FACILITY || LOG_INFO,
00070 LOG_FACILITY || LOG_DEBUG,
00071 LOG_FACILITY || LOG_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
00096
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
00111 current_time = time(NULL);
00112 current_time_tm = gmtime(¤t_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