Logo AND Algorithmique Numérique Distribuée

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