Logo AND Algorithmique Numérique Distribuée

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