Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
move resource_used to the private part
[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_SUBCATEGORY(surf,XBT_LOG_ROOT_CAT,"All SURF categories");
56 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(log,xbt,"Loggings from the logging mecanism itself");
57
58
59 static void _apply_control(xbt_log_category_t cat) {
60   int cursor;
61   xbt_log_setting_t setting=NULL;
62   int found = 0;
63
64   if (!xbt_log_settings)
65     return;
66
67   xbt_assert0(cat,"NULL category");
68   xbt_assert(cat->name);
69
70   xbt_dynar_foreach(xbt_log_settings,cursor,setting) {
71     xbt_assert0(setting,"Damnit, NULL cat in the list");
72     xbt_assert1(setting->catname,"NULL setting(=%p)->catname",(void*)setting);
73
74     if (!strcmp(setting->catname,cat->name)) {
75       found = 1;
76
77       xbt_log_threshold_set(cat, setting->thresh);
78       xbt_dynar_cursor_rm(xbt_log_settings,&cursor);
79
80       if (cat->threshold <= xbt_log_priority_verbose) {
81         s_xbt_log_event_t _log_ev = 
82           {cat,xbt_log_priority_verbose,__FILE__,_XBT_GNUC_FUNCTION,__LINE__};
83         _xbt_log_event_log(&_log_ev,
84                  "Apply settings for category '%s': set threshold to %s (=%d)",
85                  cat->name, 
86                  xbt_log_priority_names[cat->threshold], cat->threshold);
87       }
88     }
89   }
90   if (!found && cat->threshold <= xbt_log_priority_verbose) {
91     s_xbt_log_event_t _log_ev = 
92       {cat,xbt_log_priority_verbose,__FILE__,_XBT_GNUC_FUNCTION,__LINE__};
93     _xbt_log_event_log(&_log_ev,
94                         "Category '%s': inherited threshold = %s (=%d)",
95                         cat->name,
96                         xbt_log_priority_names[cat->threshold], cat->threshold);
97   }
98
99 }
100
101 void _xbt_log_event_log( xbt_log_event_t ev, const char *fmt, ...) {
102   xbt_log_category_t cat = ev->cat;
103   va_start(ev->ap, fmt);
104   while(1) {
105     xbt_log_appender_t appender = cat->appender;
106     if (appender != NULL) {
107       appender->do_append(appender, ev, fmt);
108     }
109     if (!cat->willLogToParent)
110       break;
111
112     cat = cat->parent;
113   } 
114   va_end(ev->ap);
115 }
116
117 static void _cat_init(xbt_log_category_t category) {
118   if (category == &_XBT_LOGV(XBT_LOG_ROOT_CAT)) {
119     category->threshold = xbt_log_priority_info;
120     category->appender = xbt_log_default_appender;
121   } else {
122     xbt_log_parent_set(category, category->parent);
123   }
124   _apply_control(category);
125 }
126
127 /*
128  * This gets called the first time a category is referenced and performs the
129  * initialization. 
130  * Also resets threshold to inherited!
131  */
132 int _xbt_log_cat_init(e_xbt_log_priority_t priority,
133                        xbt_log_category_t   category) {
134     
135   _cat_init(category);
136         
137   return priority >= category->threshold;
138 }
139
140 void xbt_log_parent_set(xbt_log_category_t cat,
141                          xbt_log_category_t parent) {
142
143   xbt_assert0(cat,"NULL category to be given a parent");
144   xbt_assert1(parent,"The parent category of %s is NULL",cat->name);
145
146   /* unlink from current parent */
147   if (cat->threshold != xbt_log_priority_uninitialized) {
148     xbt_log_category_t* cpp = &parent->firstChild;
149     while(*cpp != cat && *cpp != NULL) {
150       cpp = &(*cpp)->nextSibling;
151     }
152     xbt_assert(*cpp == cat);
153     *cpp = cat->nextSibling;
154   }
155
156   /* Set new parent */
157   cat->parent = parent;
158   cat->nextSibling = parent->firstChild;
159   parent->firstChild = cat;
160
161   /* Make sure parent is initialized */
162   if (parent->threshold == xbt_log_priority_uninitialized) {
163     _cat_init(parent);
164   }
165     
166   /* Reset priority */
167   cat->threshold = parent->threshold;
168   cat->isThreshInherited = 1;
169 } /* log_setParent */
170
171 static void _set_inherited_thresholds(xbt_log_category_t cat) {
172   xbt_log_category_t child = cat->firstChild;
173   for( ; child != NULL; child = child->nextSibling) {
174     if (child->isThreshInherited) {
175       if (cat != &_XBT_LOGV(log))
176         VERB3("Set category threshold of %s to %s (=%d)",
177               child->name,xbt_log_priority_names[cat->threshold],cat->threshold);
178       child->threshold = cat->threshold;
179       _set_inherited_thresholds(child);
180     }
181   }
182 }
183
184 void xbt_log_threshold_set(xbt_log_category_t   cat,
185                             e_xbt_log_priority_t threshold) {
186   cat->threshold = threshold;
187   cat->isThreshInherited = 0;
188   _set_inherited_thresholds(cat);
189 }
190
191 static void _xbt_log_parse_setting(const char*        control_string,
192                                     xbt_log_setting_t set) {
193   const char *name, *dot, *eq;
194   
195   set->catname=NULL;
196   if (!*control_string) 
197     return;
198   DEBUG1("Parse log setting '%s'",control_string);
199
200   control_string += strspn(control_string, " ");
201   name = control_string;
202   control_string += strcspn(control_string, ".= ");
203   dot = control_string;
204   control_string += strcspn(control_string, "= ");
205   eq = control_string;
206   control_string += strcspn(control_string, " ");
207
208   xbt_assert1(*dot == '.' && *eq == '=',
209                "Invalid control string '%s'",control_string);
210
211   if (!strncmp(dot + 1, "thresh", min(eq - dot - 1,strlen("thresh")))) {
212     int i;
213     char *neweq=xbt_strdup(eq+1);
214     char *p=neweq-1;
215     
216     while (*(++p) != '\0') {
217       if (*p >= 'a' && *p <= 'z') {
218         *p-='a'-'A';
219       }
220     }
221     
222     DEBUG1("New priority name = %s",neweq);
223     for (i=0; i<xbt_log_priority_infinite-1; i++) {
224       if (!strncmp(xbt_log_priority_names[i],neweq,p-eq)) {
225         DEBUG1("This is priority %d",i);
226         break;
227       }
228     }
229     if (i<xbt_log_priority_infinite-1) {
230       set->thresh=i;
231     } else {
232       xbt_assert1(FALSE,"Unknown priority name: %s",eq+1);
233     }
234     xbt_free(neweq);
235   } else {
236     char buff[512];
237     snprintf(buff,min(512,eq - dot - 1),"%s",dot+1);
238     xbt_assert1(FALSE,"Unknown setting of the log category: %s",buff);
239   }
240   set->catname=(char*)xbt_malloc(dot - name+1);
241     
242   strncpy(set->catname,name,dot-name);
243   set->catname[dot-name]='\0'; /* Just in case */
244   DEBUG1("This is for cat '%s'", set->catname);
245 }
246
247 static xbt_error_t _xbt_log_cat_searchsub(xbt_log_category_t cat,char *name,
248                                             /*OUT*/xbt_log_category_t*whereto) {
249   xbt_error_t errcode;
250   xbt_log_category_t child;
251   
252   if (!strcmp(cat->name,name)) {
253     *whereto=cat;
254     return no_error;
255   }
256   for(child=cat->firstChild ; child != NULL; child = child->nextSibling) {
257     errcode=_xbt_log_cat_searchsub(child,name,whereto);
258     if (errcode==no_error)
259       return no_error;
260   }
261   return mismatch_error;
262 }
263
264 static void _cleanup_double_spaces(char *s) {
265   char *p = s;
266   int   e = 0;
267   
268   while (1) {
269     if (!*p)
270       goto end;
271     
272     if (!isspace(*p))
273       break;
274     
275     p++;
276   }
277   
278   e = 1;
279   
280   do {
281     if (e)
282       *s++ = *p;
283     
284     if (!*++p)
285       goto end;
286     
287     if (e ^ !isspace(*p))
288       if ((e = !e))
289         *s++ = ' ';
290   } while (1);
291
292  end:
293   *s = '\0';
294 }
295
296 /**
297  * xbt_log_control_set:
298  * @cs: What to parse
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 void xbt_log_control_set(const char* control_string) {
315   xbt_error_t errcode;
316   xbt_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;
324   if (xbt_log_settings == NULL)
325     xbt_log_settings = xbt_dynar_new(sizeof(xbt_log_setting_t),
326                                        _free_setting);
327
328   set = xbt_new(s_xbt_log_setting_t,1);
329   cs=xbt_strdup(control_string);
330
331   _cleanup_double_spaces(cs);
332
333   while (!done) {
334     xbt_log_category_t cat;
335     
336     p=strrchr(cs,' ');
337     if (p) {
338       *p='\0';
339       *p++;
340     } else {
341       p=cs;
342       done = 1;
343     }
344     _xbt_log_parse_setting(p,set);
345     
346     errcode = _xbt_log_cat_searchsub(&_XBT_LOGV(root),set->catname,&cat);
347     if (errcode == mismatch_error) {
348       DEBUG0("Store for further application");
349       DEBUG1("push %p to the settings",(void*)set);
350       xbt_dynar_push(xbt_log_settings,&set);
351       /* malloc in advance the next slot */
352       set = xbt_new(s_xbt_log_setting_t,1);
353     } else {
354       DEBUG0("Apply directly");
355       xbt_free(set->catname);
356       xbt_log_threshold_set(cat,set->thresh);
357     }
358   }
359   xbt_free(set);
360   xbt_free(cs);
361
362
363 void xbt_log_appender_set(xbt_log_category_t cat, xbt_log_appender_t app) {
364   cat->appender = app;
365 }
366
367 void xbt_log_exit(void) {
368   VERB0("Exiting log");
369   xbt_dynar_free(&xbt_log_settings);
370   VERB0("Exited log");
371 }