Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
bandwidth.c -> Bandwidth/
[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 <stdarg.h>
12 #include <ctype.h>
13
14 #include "xbt_modinter.h"
15
16 #include "xbt/misc.h"
17 #include "xbt/sysdep.h"
18 #include "xbt/log.h"
19 #include "xbt/error.h"
20 #include "xbt/dynar.h"
21
22 typedef struct {
23   char *catname;
24   e_gras_log_priority_t thresh;
25 } s_gras_log_setting_t,*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     gras_free(set->catname);
32 /*    gras_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 s_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
53 GRAS_LOG_NEW_SUBCATEGORY(gras,GRAS_LOG_ROOT_CAT,"All GRAS categories");
54 GRAS_LOG_NEW_SUBCATEGORY(xbt,GRAS_LOG_ROOT_CAT,"All XBT categories (gras toolbox)");
55 GRAS_LOG_NEW_DEFAULT_SUBCATEGORY(log,xbt,"Loggings from the logging mecanism itself");
56
57
58 static void _apply_control(gras_log_category_t cat) {
59   int cursor;
60   gras_log_setting_t setting=NULL;
61   int found = 0;
62
63   if (!gras_log_settings)
64     return;
65
66   gras_assert0(cat,"NULL category");
67   gras_assert(cat->name);
68
69   gras_dynar_foreach(gras_log_settings,cursor,setting) {
70     gras_assert0(setting,"Damnit, NULL cat in the list");
71     gras_assert1(setting->catname,"NULL setting(=%p)->catname",(void*)setting);
72
73     if (!strcmp(setting->catname,cat->name)) {
74       found = 1;
75
76       gras_log_threshold_set(cat, setting->thresh);
77       gras_dynar_cursor_rm(gras_log_settings,&cursor);
78
79       if (cat->threshold <= gras_log_priority_verbose) {
80         s_gras_log_event_t _log_ev = 
81           {cat,gras_log_priority_verbose,__FILE__,_GRAS_GNUC_FUNCTION,__LINE__};
82         _gras_log_event_log(&_log_ev,
83                  "Apply settings for category '%s': set threshold to %s (=%d)",
84                  cat->name, 
85                  gras_log_priority_names[cat->threshold], cat->threshold);
86       }
87     }
88   }
89   if (!found && cat->threshold <= gras_log_priority_verbose) {
90     s_gras_log_event_t _log_ev = 
91       {cat,gras_log_priority_verbose,__FILE__,_GRAS_GNUC_FUNCTION,__LINE__};
92     _gras_log_event_log(&_log_ev,
93                         "Category '%s': inherited threshold = %s (=%d)",
94                         cat->name,
95                         gras_log_priority_names[cat->threshold], cat->threshold);
96   }
97
98 }
99
100 void _gras_log_event_log( gras_log_event_t ev, const char *fmt, ...) {
101   gras_log_category_t cat = ev->cat;
102   va_start(ev->ap, fmt);
103   while(1) {
104     gras_log_appender_t appender = cat->appender;
105     if (appender != NULL) {
106       appender->do_append(appender, ev, fmt);
107     }
108     if (!cat->willLogToParent)
109       break;
110
111     cat = cat->parent;
112   } 
113   va_end(ev->ap);
114 }
115
116 static void _cat_init(gras_log_category_t category) {
117   if (category == &_GRAS_LOGV(GRAS_LOG_ROOT_CAT)) {
118     category->threshold = gras_log_priority_info;
119     category->appender = gras_log_default_appender;
120   } else {
121     gras_log_parent_set(category, category->parent);
122   }
123   _apply_control(category);
124 }
125
126 /*
127  * This gets called the first time a category is referenced and performs the
128  * initialization. 
129  * Also resets threshold to inherited!
130  */
131 int _gras_log_cat_init(e_gras_log_priority_t priority,
132                        gras_log_category_t   category) {
133     
134   _cat_init(category);
135         
136   return priority >= category->threshold;
137 }
138
139 void gras_log_parent_set(gras_log_category_t cat,
140                          gras_log_category_t parent) {
141
142   gras_assert0(cat,"NULL category to be given a parent");
143   gras_assert1(parent,"The parent category of %s is NULL",cat->name);
144
145   /* unlink from current parent */
146   if (cat->threshold != gras_log_priority_uninitialized) {
147     gras_log_category_t* cpp = &parent->firstChild;
148     while(*cpp != cat && *cpp != NULL) {
149       cpp = &(*cpp)->nextSibling;
150     }
151     gras_assert(*cpp == cat);
152     *cpp = cat->nextSibling;
153   }
154
155   /* Set new parent */
156   cat->parent = parent;
157   cat->nextSibling = parent->firstChild;
158   parent->firstChild = cat;
159
160   /* Make sure parent is initialized */
161   if (parent->threshold == gras_log_priority_uninitialized) {
162     _cat_init(parent);
163   }
164     
165   /* Reset priority */
166   cat->threshold = parent->threshold;
167   cat->isThreshInherited = 1;
168 } /* log_setParent */
169
170 static void _set_inherited_thresholds(gras_log_category_t cat) {
171   gras_log_category_t child = cat->firstChild;
172   for( ; child != NULL; child = child->nextSibling) {
173     if (child->isThreshInherited) {
174       if (cat != &_GRAS_LOGV(log))
175         VERB3("Set category threshold of %s to %s (=%d)",
176               child->name,gras_log_priority_names[cat->threshold],cat->threshold);
177       child->threshold = cat->threshold;
178       _set_inherited_thresholds(child);
179     }
180   }
181 }
182
183 void gras_log_threshold_set(gras_log_category_t   cat,
184                             e_gras_log_priority_t threshold) {
185   cat->threshold = threshold;
186   cat->isThreshInherited = 0;
187   _set_inherited_thresholds(cat);
188 }
189
190 static void _gras_log_parse_setting(const char*        control_string,
191                                     gras_log_setting_t set) {
192   const char *name, *dot, *eq;
193   
194   set->catname=NULL;
195   if (!*control_string) 
196     return;
197   DEBUG1("Parse log setting '%s'",control_string);
198
199   control_string += strspn(control_string, " ");
200   name = control_string;
201   control_string += strcspn(control_string, ".= ");
202   dot = control_string;
203   control_string += strcspn(control_string, "= ");
204   eq = control_string;
205   control_string += strcspn(control_string, " ");
206
207   gras_assert1(*dot == '.' && *eq == '=',
208                "Invalid control string '%s'",control_string);
209
210   if (!strncmp(dot + 1, "thresh", min(eq - dot - 1,strlen("thresh")))) {
211     int i;
212     char *neweq=gras_strdup(eq+1);
213     char *p=neweq-1;
214     
215     while (*(++p) != '\0') {
216       if (*p >= 'a' && *p <= 'z') {
217         *p-='a'-'A';
218       }
219     }
220     
221     DEBUG1("New priority name = %s",neweq);
222     for (i=0; i<gras_log_priority_infinite-1; i++) {
223       if (!strncmp(gras_log_priority_names[i],neweq,p-eq)) {
224         DEBUG1("This is priority %d",i);
225         break;
226       }
227     }
228     if (i<gras_log_priority_infinite-1) {
229       set->thresh=i;
230     } else {
231       gras_assert1(FALSE,"Unknown priority name: %s",eq+1);
232     }
233     gras_free(neweq);
234   } else {
235     char buff[512];
236     snprintf(buff,min(512,eq - dot - 1),"%s",dot+1);
237     gras_assert1(FALSE,"Unknown setting of the log category: %s",buff);
238   }
239   set->catname=(char*)gras_malloc(dot - name+1);
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 }
245
246 static gras_error_t _gras_log_cat_searchsub(gras_log_category_t cat,char *name,
247                                             /*OUT*/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  *
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 void 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;
323   if (gras_log_settings == NULL)
324     gras_log_settings = gras_dynar_new(sizeof(gras_log_setting_t),
325                                        _free_setting);
326
327   set = gras_new(s_gras_log_setting_t,1);
328   cs=gras_strdup(control_string);
329
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     _gras_log_parse_setting(p,set);
344     
345     errcode = _gras_log_cat_searchsub(&_GRAS_LOGV(root),set->catname,&cat);
346     if (errcode == mismatch_error) {
347       DEBUG0("Store for further application");
348       DEBUG1("push %p to the settings",(void*)set);
349       gras_dynar_push(gras_log_settings,&set);
350       /* malloc in advance the next slot */
351       set = gras_new(s_gras_log_setting_t,1);
352     } else {
353       DEBUG0("Apply directly");
354       gras_free(set->catname);
355       gras_log_threshold_set(cat,set->thresh);
356     }
357   }
358   gras_free(set);
359   gras_free(cs);
360
361
362 void gras_log_appender_set(gras_log_category_t cat, gras_log_appender_t app) {
363   cat->appender = app;
364 }
365
366 void gras_log_exit(void) {
367   VERB0("Exiting log");
368   gras_dynar_free(&gras_log_settings);
369   VERB0("Exited log");
370 }