Logo AND Algorithmique Numérique Distribuée

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