2 * Copyright (c) 1995-2004, Index Data
3 * See the file LICENSE for details.
5 * $Id: log.c,v 1.7 2004-11-02 14:13:09 heikki Exp $
10 * \brief Implements logging utility
31 #define HAS_STRERROR 1
38 extern char *sys_errlist[];
39 return sys_errlist[n];
45 static int l_level = LOG_DEFAULT_LEVEL;
46 static FILE *l_file = NULL;
47 static char l_prefix[512] = "";
48 static char l_prefix2[512] = "";
49 static char l_fname[512] = "";
51 static char l_old_default_format[]="%H:%M:%S-%d/%m";
52 static char l_new_default_format[]="%Y%m%d-%H%M%S";
53 #define TIMEFORMAT_LEN 50
54 static char l_custom_format[TIMEFORMAT_LEN]="";
55 static char *l_actual_format=l_old_default_format;
57 /** l_max_size tells when to rotate the log. Default to 1 GB */
58 static int l_max_size=1024*1024*1024;
59 /* static int l_max_size=1024; */ /* while testing */
66 { LOG_FATAL, "fatal"},
67 { LOG_DEBUG, "debug"},
71 { LOG_MALLOC, "malloc"},
74 { LOG_APP2 , "app2" },
75 { LOG_APP3 , "app3" },
77 { LOG_FLUSH, "flush" },
82 FILE *yaz_log_file(void)
89 void yaz_log_init_file (const char *fname)
93 strncpy(l_fname, fname, sizeof(l_fname)-1);
94 l_fname[sizeof(l_fname)-1] = '\0';
101 void yaz_log_reopen(void)
109 else if (!(new_file = fopen(l_fname, "a")))
111 if (l_file != stderr)
115 if (l_level & LOG_FLUSH)
116 setvbuf(new_file, 0, _IONBF, 0);
120 static void rotate_log()
124 return; /* can't rotate that */
126 return; /* hmm, no name, can't rotate */
127 strncpy(newname, l_fname, 510);
128 newname[510]='\0'; /* make sure it is terminated */
129 strcat(newname,".1");
134 rename(l_fname, newname);
139 void yaz_log_init_level (int level)
141 if ( (l_level & LOG_FLUSH) != (level & LOG_FLUSH) )
144 yaz_log_reopen(); /* make sure we set buffering right */
149 void yaz_log_init_prefix (const char *prefix)
151 if (prefix && *prefix)
152 sprintf(l_prefix, "%.511s ", prefix);
157 void yaz_log_init_prefix2 (const char *prefix)
159 if (prefix && *prefix)
160 sprintf(l_prefix2, "%.511s ", prefix);
165 void yaz_log_init(int level, const char *prefix, const char *fname)
167 yaz_log_init_level (level);
168 yaz_log_init_prefix (prefix);
170 yaz_log_init_file (fname);
173 static void (*start_hook_func)(int, const char *, void *) = NULL;
174 static void *start_hook_info;
175 static void (*end_hook_func)(int, const char *, void *) = NULL;
176 static void *end_hook_info;
178 void log_event_start (void (*func)(int, const char *, void *), void *info)
180 start_hook_func = func;
181 start_hook_info = info;
184 void log_event_end (void (*func)(int, const char *, void *), void *info)
186 end_hook_func = func;
187 end_hook_info = info;
190 void yaz_log(int level, const char *fmt, ...)
193 char buf[4096], flags[1024];
197 char tbuf[TIMEFORMAT_LEN]="";
201 if (!(level & l_level))
206 if (l_file != stderr)
214 for (i = 0; level && mask_names[i].name; i++)
215 if (mask_names[i].mask & level)
217 if (*mask_names[i].name)
218 sprintf(flags + strlen(flags), "[%s]", mask_names[i].name);
219 level -= mask_names[i].mask;
223 _vsnprintf(buf, sizeof(buf)-1, fmt, ap);
227 vsnprintf(buf, sizeof(buf), fmt, ap);
229 vsprintf(buf, fmt, ap);
233 if (o_level & LOG_ERRNO)
236 yaz_strerror(buf+strlen(buf), 2048);
241 (*start_hook_func)(o_level, buf, start_hook_info);
243 tim = localtime(&ti);
244 if (l_level & LOG_NOTIME)
247 strftime(tbuf, TIMEFORMAT_LEN-1, l_actual_format, tim);
248 tbuf[TIMEFORMAT_LEN-1]='\0';
249 fprintf(l_file, "%s %s%s %s%s\n", tbuf, l_prefix, flags,
251 if (l_level & (LOG_FLUSH|LOG_DEBUG) )
254 (*end_hook_func)(o_level, buf, end_hook_info);
257 void yaz_log_time_format(const char *fmt)
260 { /* no format, default to new */
261 l_actual_format = l_new_default_format;
264 if (0==strcmp(fmt,"old"))
265 { /* force the old format */
266 l_actual_format = l_old_default_format;
269 /* else use custom format */
270 strncpy(l_custom_format, fmt, TIMEFORMAT_LEN-1);
271 l_custom_format[TIMEFORMAT_LEN-1]='\0';
272 l_actual_format = l_custom_format;
275 int yaz_log_mask_str (const char *str)
277 return yaz_log_mask_str_x (str, LOG_DEFAULT_LEVEL);
280 int yaz_log_mask_str_x (const char *str, int level)
287 for (p = str; *p && *p != ','; p++)
289 if (*str == '-' || isdigit(*str))
292 for (i = 0; mask_names[i].name; i++)
293 if (strlen (mask_names[i].name) == (size_t) (p-str) &&
294 memcmp (mask_names[i].name, str, p-str) == 0)
296 if (mask_names[i].mask)
297 level |= mask_names[i].mask;