Logo AND Algorithmique Numérique Distribuée

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