Logo AND Algorithmique Numérique Distribuée

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