Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Renamed any gras stuff that was in xbt and should therefore be called
[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_xbt_log_priority_t thresh;
25 } s_xbt_log_setting_t,*xbt_log_setting_t;
26
27 static xbt_dynar_t xbt_log_settings=NULL;
28 static void _free_setting(void *s) {
29   xbt_log_setting_t set=(xbt_log_setting_t)s;
30   if (set) {
31     xbt_free(set->catname);
32 /*    xbt_free(set); FIXME: uncommenting this leads to segfault when more than one chunk is passed as gras-log */
33   }
34 }
35
36 const char *xbt_log_priority_names[8] = {
37   "NONE",
38   "TRACE",
39   "DEBUG",
40   "VERBOSE",
41   "INFO",
42   "WARNING",
43   "ERROR",
44   "CRITICAL"
45 };
46
47 s_xbt_log_category_t _XBT_LOGV(XBT_LOG_ROOT_CAT) = {
48   0, 0, 0,
49   "root", xbt_log_priority_uninitialized, 0,
50   NULL, 0
51 };
52
53 XBT_LOG_NEW_SUBCATEGORY(gras,XBT_LOG_ROOT_CAT,"All GRAS categories");
54 XBT_LOG_NEW_SUBCATEGORY(xbt,XBT_LOG_ROOT_CAT,"All XBT categories (gras toolbox)");
55 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(log,xbt,"Loggings from the logging mecanism itself");
56
57
58 static void _apply_control(xbt_log_category_t cat) {
59   int cursor;
60   xbt_log_setting_t setting=NULL;
61   int found = 0;
62
63   if (!xbt_log_settings)
64     return;
65
66   xbt_assert0(cat,"NULL category");
67   xbt_assert(cat->name);
68
69   xbt_dynar_foreach(xbt_log_settings,cursor,setting) {
70     xbt_assert0(setting,"Damnit, NULL cat in the list");
71     xbt_assert1(setting->catname,"NULL setting(=%p)->catname",(void*)setting);
72
73     if (!strcmp(setting->catname,cat->name)) {
74       found = 1;
75
76       xbt_log_threshold_set(cat, setting->thresh);
77       xbt_dynar_cursor_rm(xbt_log_settings,&cursor);
78
79       if (cat->threshold <= xbt_log_priority_verbose) {
80         s_xbt_log_event_t _log_ev = 
81           {cat,xbt_log_priority_verbose,__FILE__,_XBT_GNUC_FUNCTION,__LINE__};
82         _xbt_log_event_log(&_log_ev,
83                  "Apply settings for category '%s': set threshold to %s (=%d)",
84                  cat->name, 
85                  xbt_log_priority_names[cat->threshold], cat->threshold);
86       }
87     }
88   }
89   if (!found && cat->threshold <= xbt_log_priority_verbose) {
90     s_xbt_log_event_t _log_ev = 
91       {cat,xbt_log_priority_verbose,__FILE__,_XBT_GNUC_FUNCTION,__LINE__};
92     _xbt_log_event_log(&_log_ev,
93                         "Category '%s': inherited threshold = %s (=%d)",
94                         cat->name,
95                         xbt_log_priority_names[cat->threshold], cat->threshold);
96   }
97
98 }
99
100 void _xbt_log_event_log( xbt_log_event_t ev, const char *fmt, ...) {
101   xbt_log_category_t cat = ev->cat;
102   va_start(ev->ap, fmt);
103   while(1) {
104     xbt_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(xbt_log_category_t category) {
117   if (category == &_XBT_LOGV(XBT_LOG_ROOT_CAT)) {
118     category->threshold = xbt_log_priority_info;
119     category->appender = xbt_log_default_appender;
120   } else {
121     xbt_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 _xbt_log_cat_init(e_xbt_log_priority_t priority,
132                        xbt_log_category_t   category) {
133     
134   _cat_init(category);
135         
136   return priority >= category->threshold;
137 }
138
139 void xbt_log_parent_set(xbt_log_category_t cat,
140                          xbt_log_category_t parent) {
141
142   xbt_assert0(cat,"NULL category to be given a parent");
143   xbt_assert1(parent,"The parent category of %s is NULL",cat->name);
144
145   /* unlink from current parent */
146   if (cat->threshold != xbt_log_priority_uninitialized) {
147     xbt_log_category_t* cpp = &parent->firstChild;
148     while(*cpp != cat && *cpp != NULL) {
149       cpp = &(*cpp)->nextSibling;
150     }
151     xbt_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 == xbt_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(xbt_log_category_t cat) {
171   xbt_log_category_t child = cat->firstChild;
172   for( ; child != NULL; child = child->nextSibling) {
173     if (child->isThreshInherited) {
174       if (cat != &_XBT_LOGV(log))
175         VERB3("Set category threshold of %s to %s (=%d)",
176               child->name,xbt_log_priority_names[cat->threshold],cat->threshold);
177       child->threshold = cat->threshold;
178       _set_inherited_thresholds(child);
179     }
180   }
181 }
182
183 void xbt_log_threshold_set(xbt_log_category_t   cat,
184                             e_xbt_log_priority_t threshold) {
185   cat->threshold = threshold;
186   cat->isThreshInherited = 0;
187   _set_inherited_thresholds(cat);
188 }
189
190 static void _xbt_log_parse_setting(const char*        control_string,
191                                     xbt_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   xbt_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=xbt_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<xbt_log_priority_infinite-1; i++) {
223       if (!strncmp(xbt_log_priority_names[i],neweq,p-eq)) {
224         DEBUG1("This is priority %d",i);
225         break;
226       }
227     }
228     if (i<xbt_log_priority_infinite-1) {
229       set->thresh=i;
230     } else {
231       xbt_assert1(FALSE,"Unknown priority name: %s",eq+1);
232     }
233     xbt_free(neweq);
234   } else {
235     char buff[512];
236     snprintf(buff,min(512,eq - dot - 1),"%s",dot+1);
237     xbt_assert1(FALSE,"Unknown setting of the log category: %s",buff);
238   }
239   set->catname=(char*)xbt_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 xbt_error_t _xbt_log_cat_searchsub(xbt_log_category_t cat,char *name,
247                                             /*OUT*/xbt_log_category_t*whereto) {
248   xbt_error_t errcode;
249   xbt_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=_xbt_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  * xbt_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 xbt_log_control_set(const char* control_string) {
314   xbt_error_t errcode;
315   xbt_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 (xbt_log_settings == NULL)
324     xbt_log_settings = xbt_dynar_new(sizeof(xbt_log_setting_t),
325                                        _free_setting);
326
327   set = xbt_new(s_xbt_log_setting_t,1);
328   cs=xbt_strdup(control_string);
329
330   _cleanup_double_spaces(cs);
331
332   while (!done) {
333     xbt_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     _xbt_log_parse_setting(p,set);
344     
345     errcode = _xbt_log_cat_searchsub(&_XBT_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       xbt_dynar_push(xbt_log_settings,&set);
350       /* malloc in advance the next slot */
351       set = xbt_new(s_xbt_log_setting_t,1);
352     } else {
353       DEBUG0("Apply directly");
354       xbt_free(set->catname);
355       xbt_log_threshold_set(cat,set->thresh);
356     }
357   }
358   xbt_free(set);
359   xbt_free(cs);
360
361
362 void xbt_log_appender_set(xbt_log_category_t cat, xbt_log_appender_t app) {
363   cat->appender = app;
364 }
365
366 void xbt_log_exit(void) {
367   VERB0("Exiting log");
368   xbt_dynar_free(&xbt_log_settings);
369   VERB0("Exited log");
370 }