2 * Copyright (c) 1995-2004, Index Data
3 * See the file LICENSE for details.
5 * $Id: log.c,v 1.3 2004-11-02 11:37:21 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] = "";
50 static char l_old_default_format[]="%H:%M:%S-%d/%m";
51 static char l_new_default_format[]="%Y%m%d-%H%M%S";
52 #define TIMEFORMAT_LEN 50
53 static char l_custom_format[TIMEFORMAT_LEN]="";
54 static char *l_actual_format=l_old_default_format;
61 { LOG_FATAL, "fatal"},
62 { LOG_DEBUG, "debug"},
66 { LOG_MALLOC, "malloc"},
69 { LOG_APP2 , "app2" },
70 { LOG_APP3 , "app3" },
76 FILE *yaz_log_file(void)
83 void yaz_log_init_file (const char *fname)
87 strncpy(l_fname, fname, sizeof(l_fname)-1);
88 l_fname[sizeof(l_fname)-1] = '\0';
95 void yaz_log_reopen(void)
103 else if (!(new_file = fopen(l_fname, "a")))
105 if (l_file != stderr)
109 setvbuf(new_file, 0, _IONBF, 0);
113 void yaz_log_init_level (int level)
118 void yaz_log_init_prefix (const char *prefix)
120 if (prefix && *prefix)
121 sprintf(l_prefix, "%.511s ", prefix);
126 void yaz_log_init_prefix2 (const char *prefix)
128 if (prefix && *prefix)
129 sprintf(l_prefix2, "%.511s ", prefix);
134 void yaz_log_init(int level, const char *prefix, const char *fname)
136 yaz_log_init_level (level);
137 yaz_log_init_prefix (prefix);
139 yaz_log_init_file (fname);
142 static void (*start_hook_func)(int, const char *, void *) = NULL;
143 static void *start_hook_info;
144 static void (*end_hook_func)(int, const char *, void *) = NULL;
145 static void *end_hook_info;
147 void log_event_start (void (*func)(int, const char *, void *), void *info)
149 start_hook_func = func;
150 start_hook_info = info;
153 void log_event_end (void (*func)(int, const char *, void *), void *info)
155 end_hook_func = func;
156 end_hook_info = info;
159 void yaz_log(int level, const char *fmt, ...)
162 char buf[4096], flags[1024];
166 char tbuf[TIMEFORMAT_LEN]="";
169 if (!(level & l_level))
174 for (i = 0; level && mask_names[i].name; i++)
175 if (mask_names[i].mask & level)
177 if (*mask_names[i].name)
178 sprintf(flags + strlen(flags), "[%s]", mask_names[i].name);
179 level -= mask_names[i].mask;
183 _vsnprintf(buf, sizeof(buf)-1, fmt, ap);
187 vsnprintf(buf, sizeof(buf), fmt, ap);
189 vsprintf(buf, fmt, ap);
193 if (o_level & LOG_ERRNO)
196 yaz_strerror(buf+strlen(buf), 2048);
201 (*start_hook_func)(o_level, buf, start_hook_info);
203 tim = localtime(&ti);
204 if (l_level & LOG_NOTIME)
207 strftime(tbuf, TIMEFORMAT_LEN-1, l_actual_format, tim);
208 tbuf[TIMEFORMAT_LEN-1]='\0';
209 fprintf(l_file, "%s %s%s %s%s\n", tbuf, l_prefix, flags,
213 (*end_hook_func)(o_level, buf, end_hook_info);
216 void yaz_log_time_format(const char *fmt)
219 { /* no format, default to new */
220 l_actual_format = l_new_default_format;
223 if (0==strcmp(fmt,"old"))
224 { /* force the old format */
225 l_actual_format = l_old_default_format;
228 /* else use custom format */
229 strncpy(l_custom_format, fmt, TIMEFORMAT_LEN-1);
230 l_custom_format[TIMEFORMAT_LEN-1]='\0';
231 l_actual_format = l_custom_format;
234 int yaz_log_mask_str (const char *str)
236 return yaz_log_mask_str_x (str, LOG_DEFAULT_LEVEL);
239 int yaz_log_mask_str_x (const char *str, int level)
246 for (p = str; *p && *p != ','; p++)
248 if (*str == '-' || isdigit(*str))
251 for (i = 0; mask_names[i].name; i++)
252 if (strlen (mask_names[i].name) == (size_t) (p-str) &&
253 memcmp (mask_names[i].name, str, p-str) == 0)
255 if (mask_names[i].mask)
256 level |= mask_names[i].mask;