Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Adapt to lastest changes in dict: Create dicts before use
[simgrid.git] / src / xbt / log.c
1 /* $Id$ */
2
3 /* log - a generic logging facility in the spirit of log4j                  */
4
5 /* Authors: Martin Quinson                                                  */
6 /* Copyright (C) 2003, 2004 Martin Quinson.                                 */
7
8 /* This program is free software; you can redistribute it and/or modify it
9    under the terms of the license (GNU LGPL) which comes with this package. */
10
11 #include "gras_private.h"
12 #include <stdarg.h>
13 #include <assert.h>
14 #include <ctype.h>
15
16 #ifndef MIN
17 #define MIN(a,b) ((a) < (b) ? (a) : (b))
18 #endif
19
20 typedef struct {
21   char *catname;
22   gras_log_priority_t thresh;
23 } gras_log_setting_t;
24
25 static gras_dynar_t *gras_log_settings=NULL;
26 static void _free_setting(void *s) {
27   gras_log_setting_t *set=(gras_log_setting_t*)s;
28   if (set) {
29     free(set->catname);
30     free(set);
31   }
32 }
33
34 const char *gras_log_priority_names[8] = {
35   "NONE",
36   "DEBUG",
37   "VERBOSE",
38   "INFO",
39   "WARNING",
40   "ERROR",
41   "CRITICAL"
42 };
43
44 gras_log_category_t _GRAS_LOGV(GRAS_LOG_ROOT_CAT) = {
45   0, 0, 0,
46   "root", gras_log_priority_uninitialized, 0,
47   NULL, 0
48 };
49 GRAS_LOG_NEW_SUBCATEGORY(GRAS,GRAS_LOG_ROOT_CAT);
50 GRAS_LOG_NEW_DEFAULT_SUBCATEGORY(log,GRAS);
51
52
53 static void _apply_control(gras_log_category_t* cat) {
54   int cursor;
55   gras_log_setting_t *setting=NULL;
56   int found = 0;
57
58   if (!gras_log_settings)
59     return;
60
61   gras_assert0(cat,"NULL category");
62   gras_assert(cat->name);
63
64   gras_dynar_foreach(gras_log_settings,cursor,setting) {
65     gras_assert0(setting,"Damnit, NULL cat in the list");
66     gras_assert1(setting->catname,"NULL setting(=%p)->catname",setting);
67
68     if (!strcmp(setting->catname,cat->name)) {
69       found = 1;
70
71       gras_log_threshold_set(cat, setting->thresh);
72       gras_dynar_cursor_rm(gras_log_settings,&cursor);
73
74       if (cat->threshold <= gras_log_priority_verbose) {
75         gras_log_event_t _log_ev = 
76           {cat,gras_log_priority_verbose,__FILE__,__FUNCTION__,__LINE__,
77            "Apply settings for category '%s': set threshold to %s (=%d)",};
78         _gras_log_event_log(&_log_ev, cat->name,
79                             gras_log_priority_names[cat->threshold], cat->threshold);
80       }
81     }
82   }
83   if (!found && cat->threshold <= gras_log_priority_verbose) {
84     gras_log_event_t _log_ev = 
85       {cat,gras_log_priority_verbose,__FILE__,__FUNCTION__,__LINE__,
86        "Category '%s': inherited threshold = %s (=%d)",};
87     _gras_log_event_log(&_log_ev, cat->name,
88                         gras_log_priority_names[cat->threshold], cat->threshold);
89   }
90
91 }
92
93 void _gras_log_event_log( gras_log_event_t* ev, ...) {
94   gras_log_category_t* cat = ev->cat;
95   va_start(ev->ap, ev);
96   while(1) {
97     gras_log_appender_t* appender = cat->appender;
98     if (appender != NULL) {
99       appender->do_append(appender, ev);
100     }
101     if (!cat->willLogToParent)
102       break;
103
104     cat = cat->parent;
105   } 
106   va_end(ev->ap);
107 }
108
109 static void _cat_init(gras_log_category_t* category) {
110   if (category == &_GRAS_LOGV(GRAS_LOG_ROOT_CAT)) {
111     category->threshold = gras_log_priority_info;
112     category->appender = gras_log_default_appender;
113   } else {
114     gras_log_parent_set(category, category->parent);
115   }
116   _apply_control(category);
117 }
118
119 /*
120  * This gets called the first time a category is referenced and performs the
121  * initialization. 
122  * Also resets threshold to inherited!
123  */
124 int _gras_log_cat_init(gras_log_priority_t priority,
125                        gras_log_category_t* category) {
126     
127   _cat_init(category);
128         
129   return priority >= category->threshold;
130 }
131
132 void gras_log_parent_set(gras_log_category_t* cat,
133                          gras_log_category_t* parent) {
134
135   gras_assert0(cat,"NULL category to be given a parent");
136   gras_assert1(parent,"The parent category of %s is NULL",cat->name);
137
138   // unlink from current parent
139   if (cat->threshold != gras_log_priority_uninitialized) {
140     gras_log_category_t** cpp = &parent->firstChild;
141     while(*cpp != cat && *cpp != NULL) {
142       cpp = &(*cpp)->nextSibling;
143     }
144     assert(*cpp == cat);
145     *cpp = cat->nextSibling;
146   }
147
148   // Set new parent
149   cat->parent = parent;
150   cat->nextSibling = parent->firstChild;
151   parent->firstChild = cat;
152
153   // Make sure parent is initialized
154   if (parent->threshold == gras_log_priority_uninitialized) {
155     _cat_init(parent);
156   }
157     
158   // Reset priority
159   cat->threshold = parent->threshold;
160   cat->isThreshInherited = 1;
161 } // log_setParent
162
163 static void _set_inherited_thresholds(gras_log_category_t* cat) {
164   gras_log_category_t* child = cat->firstChild;
165   for( ; child != NULL; child = child->nextSibling) {
166     if (child->isThreshInherited) {
167       if (cat != &_GRAS_LOGV(log))
168         VERB3("Set category threshold of %s to %s (=%d)",
169               child->name,gras_log_priority_names[cat->threshold],cat->threshold);
170       child->threshold = cat->threshold;
171       _set_inherited_thresholds(child);
172     }
173   }
174 }
175
176 void gras_log_threshold_set(gras_log_category_t* cat, 
177                             gras_log_priority_t threshold) {
178   cat->threshold = threshold;
179   cat->isThreshInherited = 0;
180   _set_inherited_thresholds(cat);
181 }
182
183 static gras_error_t _gras_log_parse_setting(const char* control_string,
184                                             gras_log_setting_t *set) {
185   const char *name, *dot, *eq;
186   
187   set->catname=NULL;
188   if (!*control_string) 
189     return no_error;
190   DEBUG1("Parse log setting '%s'",control_string);
191
192   control_string += strspn(control_string, " ");
193   name = control_string;
194   control_string += strcspn(control_string, ".= ");
195   dot = control_string;
196   control_string += strcspn(control_string, "= ");
197   eq = control_string;
198   control_string += strcspn(control_string, " ");
199
200   gras_assert1(*dot == '.' && *eq == '=',
201                "Invalid control string '%s'",control_string);
202
203   if (!strncmp(dot + 1, "thresh", MIN(eq - dot - 1,strlen("thresh")))) {
204     int i;
205     char *neweq=strdup(eq+1);
206     char *p=neweq-1;
207     
208     while (*(++p) != '\0') {
209       if (*p >= 'a' && *p <= 'z') {
210         *p-='a'-'A';
211       }
212     }
213     
214     DEBUG1("New priority name = %s",neweq);
215     for (i=0; i<6; i++) {
216       if (!strncmp(gras_log_priority_names[i],neweq,p-eq)) {
217         DEBUG1("This is priority %d",i);
218         break;
219       }
220     }
221     if (i<6) {
222       set->thresh=i;
223     } else {
224       gras_assert1(FALSE,"Unknown priority name: %s",eq+1);
225     }
226     free(neweq);
227   } else {
228     char buff[512];
229     snprintf(buff,MIN(512,eq - dot - 1),"%s",dot+1);
230     gras_assert1(FALSE,"Unknown setting of the log category: %s",buff);
231   }
232   if (!(set->catname=malloc(dot - name+1)))
233     RAISE_MALLOC;
234     
235   strncat(set->catname,name,dot-name);
236   DEBUG1("This is for cat '%s'", set->catname);
237   return no_error;
238 }
239
240 static gras_error_t _gras_log_cat_searchsub(gras_log_category_t *cat,char *name,gras_log_category_t**whereto) {
241   gras_error_t errcode;
242   gras_log_category_t *child;
243   
244   if (!strcmp(cat->name,name)) {
245     *whereto=cat;
246     return no_error;
247   }
248   for(child=cat->firstChild ; child != NULL; child = child->nextSibling) {
249     errcode=_gras_log_cat_searchsub(child,name,whereto);
250     if (errcode==no_error)
251       return no_error;
252   }
253   return mismatch_error;
254 }
255
256 static void _cleanup_double_spaces(char *s) {
257   char *p = s;
258   int   e = 0;
259   
260   while (1) {
261     if (!*p)
262       goto end;
263     
264     if (!isspace(*p))
265       break;
266     
267     p++;
268   }
269   
270   e = 1;
271   
272   do {
273     if (e)
274       *s++ = *p;
275     
276     if (!*++p)
277       goto end;
278     
279     if (e ^ !isspace(*p))
280       if ((e = !e))
281         *s++ = ' ';
282   } while (1);
283
284  end:
285   *s = '\0';
286 }
287
288 /**
289  * gras_log_control_set:
290  * @cs: What to parse
291  * @Returns: malloc_error or no_error
292  *
293  * Typically passed a command-line argument. The string has the syntax:
294  *
295  *      ( [category] "." [keyword] "=" value (" ")... )...
296  *
297  * where [category] is one the category names and keyword is one of the
298  * following:
299  *
300  *      thresh          value is an integer priority level. Sets the category's
301  *                        threshold priority.
302  *
303  * @warning
304  * This routine may only be called once and that must be before any other
305  * logging command! Typically, this is done from main().
306  */
307 gras_error_t gras_log_control_set(const char* control_string) {
308   gras_error_t errcode;
309   gras_log_setting_t *set;
310   char *cs;
311   char *p;
312   int done = 0;
313   
314   DEBUG1("Parse log settings '%s'",control_string);
315   if (control_string == NULL)
316     return no_error;
317   if (gras_log_settings == NULL)
318     TRY(gras_dynar_new(&gras_log_settings,sizeof(gras_log_setting_t*),_free_setting));
319
320   if (!(set = malloc(sizeof(gras_log_setting_t))))
321     RAISE_MALLOC;
322
323   if (!(cs=strdup(control_string)))
324     RAISE_MALLOC;
325   _cleanup_double_spaces(cs);
326
327   while (!done) {
328     gras_log_category_t *cat;
329     
330     p=strrchr(cs,' ');
331     if (p) {
332       *p='\0';
333       *p++;
334     } else {
335       p=cs;
336       done = 1;
337     }
338     errcode = _gras_log_parse_setting(p,set);
339     if (errcode != no_error) {
340       free(set);
341       free(cs);
342     }
343     
344     TRYCATCH(_gras_log_cat_searchsub(&_GRAS_LOGV(root),set->catname,&cat),mismatch_error);
345     if (errcode == mismatch_error) {
346       DEBUG0("Store for further application");
347       DEBUG1("push %p to the settings",set);
348       TRY(gras_dynar_push(gras_log_settings,&set));
349       /* malloc in advance the next slot */
350       if (!(set = malloc(sizeof(gras_log_setting_t)))) { 
351         free(cs);
352         RAISE_MALLOC;
353       }
354     } else {
355       DEBUG0("Apply directly");
356       free(set->catname);
357       gras_log_threshold_set(cat,set->thresh);
358     }
359   }
360   free(set);
361   free(cs);
362   return no_error;
363
364
365 void gras_log_appender_set(gras_log_category_t* cat, gras_log_appender_t* app) {
366   cat->appender = app;
367 }
368
369 void gras_log_finalize(void);
370 void gras_log_finalize(void) {
371   gras_dynar_free(gras_log_settings);
372 }