2 * Copyright (c) 1995-2004, Index Data
3 * See the file LICENSE for details.
5 * $Id: log.c,v 1.2 2004-10-15 00:19:00 adam Exp $
10 * \brief Implements logging utility
31 #define HAS_STRERROR 1
38 extern char *sys_errlist[];
39 return sys_errlist[n];
44 static int l_level = LOG_DEFAULT_LEVEL;
45 static FILE *l_file = NULL;
46 static char l_prefix[512] = "";
47 static char l_prefix2[512] = "";
48 static char l_fname[512] = "";
55 { LOG_FATAL, "fatal"},
56 { LOG_DEBUG, "debug"},
60 { LOG_MALLOC, "malloc"},
63 { LOG_APP2 , "app2" },
64 { LOG_APP3 , "app3" },
70 FILE *yaz_log_file(void)
77 void yaz_log_init_file (const char *fname)
81 strncpy(l_fname, fname, sizeof(l_fname)-1);
82 l_fname[sizeof(l_fname)-1] = '\0';
89 void yaz_log_reopen(void)
97 else if (!(new_file = fopen(l_fname, "a")))
103 setvbuf(new_file, 0, _IONBF, 0);
107 void yaz_log_init_level (int level)
112 void yaz_log_init_prefix (const char *prefix)
114 if (prefix && *prefix)
115 sprintf(l_prefix, "%.511s ", prefix);
120 void yaz_log_init_prefix2 (const char *prefix)
122 if (prefix && *prefix)
123 sprintf(l_prefix2, "%.511s ", prefix);
128 void yaz_log_init(int level, const char *prefix, const char *fname)
130 yaz_log_init_level (level);
131 yaz_log_init_prefix (prefix);
133 yaz_log_init_file (fname);
136 static void (*start_hook_func)(int, const char *, void *) = NULL;
137 static void *start_hook_info;
138 static void (*end_hook_func)(int, const char *, void *) = NULL;
139 static void *end_hook_info;
141 void log_event_start (void (*func)(int, const char *, void *), void *info)
143 start_hook_func = func;
144 start_hook_info = info;
147 void log_event_end (void (*func)(int, const char *, void *), void *info)
149 end_hook_func = func;
150 end_hook_info = info;
153 void yaz_log(int level, const char *fmt, ...)
156 char buf[4096], flags[1024];
163 if (!(level & l_level))
168 for (i = 0; level && mask_names[i].name; i++)
169 if (mask_names[i].mask & level)
171 if (*mask_names[i].name)
172 sprintf(flags + strlen(flags), "[%s]", mask_names[i].name);
173 level -= mask_names[i].mask;
177 _vsnprintf(buf, sizeof(buf)-1, fmt, ap);
181 vsnprintf(buf, sizeof(buf), fmt, ap);
183 vsprintf(buf, fmt, ap);
187 if (o_level & LOG_ERRNO)
190 yaz_strerror(buf+strlen(buf), 2048);
195 (*start_hook_func)(o_level, buf, start_hook_info);
197 tim = localtime(&ti);
198 if (l_level & LOG_NOTIME)
201 strftime(tbuf, 50, "%H:%M:%S-%d/%m: ", tim);
202 fprintf(l_file, "%s%s%s %s%s\n", tbuf, l_prefix, flags,
206 (*end_hook_func)(o_level, buf, end_hook_info);
209 int yaz_log_mask_str (const char *str)
211 return yaz_log_mask_str_x (str, LOG_DEFAULT_LEVEL);
214 int yaz_log_mask_str_x (const char *str, int level)
221 for (p = str; *p && *p != ','; p++)
223 if (*str == '-' || isdigit(*str))
226 for (i = 0; mask_names[i].name; i++)
227 if (strlen (mask_names[i].name) == (size_t) (p-str) &&
228 memcmp (mask_names[i].name, str, p-str) == 0)
230 if (mask_names[i].mask)
231 level |= mask_names[i].mask;