Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
proper xbt_log_init function
[simgrid.git] / src / xbt / log.c
1 /* $Id$ */
2
3 /* log - a generic logging facility in the spirit of log4j                  */
4
5 /* Copyright (c) 2004 Martin Quinson. All rights reserved.                  */
6
7 /* This program is free software; you can redistribute it and/or modify it
8  * under the terms of the license (GNU LGPL) which comes with this package. */
9
10 #include <stdarg.h>
11 #include <ctype.h>
12
13 #include "xbt_modinter.h"
14
15 #include "xbt/misc.h"
16 #include "xbt/sysdep.h"
17 #include "xbt/log.h"
18 #include "xbt/error.h"
19 #include "xbt/dynar.h"
20
21 typedef struct {
22   char *catname;
23   e_xbt_log_priority_t thresh;
24 } s_xbt_log_setting_t,*xbt_log_setting_t;
25
26 static xbt_dynar_t xbt_log_settings=NULL;
27 static void _free_setting(void *s) {
28   xbt_log_setting_t set=(xbt_log_setting_t)s;
29   if (set) {
30     xbt_free(set->catname);
31 /*    xbt_free(set); FIXME: uncommenting this leads to segfault when more than one chunk is passed as gras-log */
32   }
33 }
34
35 const char *xbt_log_priority_names[8] = {
36   "NONE",
37   "TRACE",
38   "DEBUG",
39   "VERBOSE",
40   "INFO",
41   "WARNING",
42   "ERROR",
43   "CRITICAL"
44 };
45
46 s_xbt_log_category_t _XBT_LOGV(XBT_LOG_ROOT_CAT) = {
47   0, 0, 0,
48   "root", xbt_log_priority_uninitialized, 0,
49   NULL, 0
50 };
51
52 XBT_LOG_NEW_SUBCATEGORY(gras,XBT_LOG_ROOT_CAT,"All GRAS categories");
53 XBT_LOG_NEW_SUBCATEGORY(xbt,XBT_LOG_ROOT_CAT,"All XBT categories (gras toolbox)");
54 XBT_LOG_NEW_SUBCATEGORY(surf,XBT_LOG_ROOT_CAT,"All SURF categories");
55 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(log,xbt,"Loggings from the logging mecanism itself");
56
57 void xbt_log_init(int *argc,char **argv, const char *defaultlog) {
58   int i,j;
59   char *opt;
60   int found=0;
61
62   /** Set logs and init log submodule */
63   for (i=1; i<*argc; i++) {
64     if (!strncmp(argv[i],"--gras-log=",strlen("--gras-log="))) {
65       found = 1;
66       opt=strchr(argv[i],'=');
67       opt++;
68       xbt_log_control_set(opt);
69       DEBUG1("Did apply '%s' as log setting",opt);
70       /*remove this from argv*/
71       for (j=i+1; j<*argc; j++) {
72         argv[j-1] = argv[j];
73       } 
74       argv[j-1] = NULL;
75       (*argc)--;
76       i--; /* compensate effect of next loop incrementation */
77     }
78   }
79   if (!found && defaultlog) {
80      xbt_log_control_set(defaultlog);
81   }
82 }
83
84 void xbt_log_exit(void) {
85   VERB0("Exiting log");
86   xbt_dynar_free(&xbt_log_settings);
87   VERB0("Exited log");
88 }
89
90 static void _apply_control(xbt_log_category_t cat) {
91   int cursor;
92   xbt_log_setting_t setting=NULL;
93   int found = 0;
94
95   if (!xbt_log_settings)
96     return;
97
98   xbt_assert0(cat,"NULL category");
99   xbt_assert(cat->name);
100
101   xbt_dynar_foreach(xbt_log_settings,cursor,setting) {
102     xbt_assert0(setting,"Damnit, NULL cat in the list");
103     xbt_assert1(setting->catname,"NULL setting(=%p)->catname",(void*)setting);
104
105     if (!strcmp(setting->catname,cat->name)) {
106       found = 1;
107
108       xbt_log_threshold_set(cat, setting->thresh);
109       xbt_dynar_cursor_rm(xbt_log_settings,&cursor);
110
111       if (cat->threshold <= xbt_log_priority_verbose) {
112         s_xbt_log_event_t _log_ev = 
113           {cat,xbt_log_priority_verbose,__FILE__,_XBT_GNUC_FUNCTION,__LINE__};
114         _xbt_log_event_log(&_log_ev,
115                  "Apply settings for category '%s': set threshold to %s (=%d)",
116                  cat->name, 
117                  xbt_log_priority_names[cat->threshold], cat->threshold);
118       }
119     }
120   }
121   if (!found && cat->threshold <= xbt_log_priority_verbose) {
122     s_xbt_log_event_t _log_ev = 
123       {cat,xbt_log_priority_verbose,__FILE__,_XBT_GNUC_FUNCTION,__LINE__};
124     _xbt_log_event_log(&_log_ev,
125                         "Category '%s': inherited threshold = %s (=%d)",
126                         cat->name,
127                         xbt_log_priority_names[cat->threshold], cat->threshold);
128   }
129
130 }
131
132 void _xbt_log_event_log( xbt_log_event_t ev, const char *fmt, ...) {
133   xbt_log_category_t cat = ev->cat;
134   va_start(ev->ap, fmt);
135   while(1) {
136     xbt_log_appender_t appender = cat->appender;
137     if (appender != NULL) {
138       appender->do_append(appender, ev, fmt);
139     }
140     if (!cat->willLogToParent)
141       break;
142
143     cat = cat->parent;
144   } 
145   va_end(ev->ap);
146 }
147
148 static void _cat_init(xbt_log_category_t category) {
149   if (category == &_XBT_LOGV(XBT_LOG_ROOT_CAT)) {
150     category->threshold = xbt_log_priority_info;
151     category->appender = xbt_log_default_appender;
152   } else {
153     xbt_log_parent_set(category, category->parent);
154   }
155   _apply_control(category);
156 }
157
158 /*
159  * This gets called the first time a category is referenced and performs the
160  * initialization. 
161  * Also resets threshold to inherited!
162  */
163 int _xbt_log_cat_init(e_xbt_log_priority_t priority,
164                        xbt_log_category_t   category) {
165     
166   _cat_init(category);
167         
168   return priority >= category->threshold;
169 }
170
171 void xbt_log_parent_set(xbt_log_category_t cat,
172                          xbt_log_category_t parent) {
173
174   xbt_assert0(cat,"NULL category to be given a parent");
175   xbt_assert1(parent,"The parent category of %s is NULL",cat->name);
176
177   /* unlink from current parent */
178   if (cat->threshold != xbt_log_priority_uninitialized) {
179     xbt_log_category_t* cpp = &parent->firstChild;
180     while(*cpp != cat && *cpp != NULL) {
181       cpp = &(*cpp)->nextSibling;
182     }
183     xbt_assert(*cpp == cat);
184     *cpp = cat->nextSibling;
185   }
186
187   /* Set new parent */
188   cat->parent = parent;
189   cat->nextSibling = parent->firstChild;
190   parent->firstChild = cat;
191
192   /* Make sure parent is initialized */
193   if (parent->threshold == xbt_log_priority_uninitialized) {
194     _cat_init(parent);
195   }
196     
197   /* Reset priority */
198   cat->threshold = parent->threshold;
199   cat->isThreshInherited = 1;
200 } /* log_setParent */
201
202 static void _set_inherited_thresholds(xbt_log_category_t cat) {
203   xbt_log_category_t child = cat->firstChild;
204   for( ; child != NULL; child = child->nextSibling) {
205     if (child->isThreshInherited) {
206       if (cat != &_XBT_LOGV(log))
207         VERB3("Set category threshold of %s to %s (=%d)",
208               child->name,xbt_log_priority_names[cat->threshold],cat->threshold);
209       child->threshold = cat->threshold;
210       _set_inherited_thresholds(child);
211     }
212   }
213 }
214
215 void xbt_log_threshold_set(xbt_log_category_t   cat,
216                             e_xbt_log_priority_t threshold) {
217   cat->threshold = threshold;
218   cat->isThreshInherited = 0;
219   _set_inherited_thresholds(cat);
220 }
221
222 static void _xbt_log_parse_setting(const char*        control_string,
223                                     xbt_log_setting_t set) {
224   const char *name, *dot, *eq;
225   
226   set->catname=NULL;
227   if (!*control_string) 
228     return;
229   DEBUG1("Parse log setting '%s'",control_string);
230
231   control_string += strspn(control_string, " ");
232   name = control_string;
233   control_string += strcspn(control_string, ".= ");
234   dot = control_string;
235   control_string += strcspn(control_string, "= ");
236   eq = control_string;
237   control_string += strcspn(control_string, " ");
238
239   xbt_assert1(*dot == '.' && *eq == '=',
240                "Invalid control string '%s'",control_string);
241
242   if (!strncmp(dot + 1, "thresh", min(eq - dot - 1,strlen("thresh")))) {
243     int i;
244     char *neweq=xbt_strdup(eq+1);
245     char *p=neweq-1;
246     
247     while (*(++p) != '\0') {
248       if (*p >= 'a' && *p <= 'z') {
249         *p-='a'-'A';
250       }
251     }
252     
253     DEBUG1("New priority name = %s",neweq);
254     for (i=0; i<xbt_log_priority_infinite-1; i++) {
255       if (!strncmp(xbt_log_priority_names[i],neweq,p-eq)) {
256         DEBUG1("This is priority %d",i);
257         break;
258       }
259     }
260     if (i<xbt_log_priority_infinite-1) {
261       set->thresh=i;
262     } else {
263       xbt_assert1(FALSE,"Unknown priority name: %s",eq+1);
264     }
265     xbt_free(neweq);
266   } else {
267     char buff[512];
268     snprintf(buff,min(512,eq - dot - 1),"%s",dot+1);
269     xbt_assert1(FALSE,"Unknown setting of the log category: %s",buff);
270   }
271   set->catname=(char*)xbt_malloc(dot - name+1);
272     
273   strncpy(set->catname,name,dot-name);
274   set->catname[dot-name]='\0'; /* Just in case */
275   DEBUG1("This is for cat '%s'", set->catname);
276 }
277
278 static xbt_error_t _xbt_log_cat_searchsub(xbt_log_category_t cat,char *name,
279                                             /*OUT*/xbt_log_category_t*whereto) {
280   xbt_error_t errcode;
281   xbt_log_category_t child;
282   
283   if (!strcmp(cat->name,name)) {
284     *whereto=cat;
285     return no_error;
286   }
287   for(child=cat->firstChild ; child != NULL; child = child->nextSibling) {
288     errcode=_xbt_log_cat_searchsub(child,name,whereto);
289     if (errcode==no_error)
290       return no_error;
291   }
292   return mismatch_error;
293 }
294
295 static void _cleanup_double_spaces(char *s) {
296   char *p = s;
297   int   e = 0;
298   
299   while (1) {
300     if (!*p)
301       goto end;
302     
303     if (!isspace(*p))
304       break;
305     
306     p++;
307   }
308   
309   e = 1;
310   
311   do {
312     if (e)
313       *s++ = *p;
314     
315     if (!*++p)
316       goto end;
317     
318     if (e ^ !isspace(*p))
319       if ((e = !e))
320         *s++ = ' ';
321   } while (1);
322
323  end:
324   *s = '\0';
325 }
326
327 /**
328  * xbt_log_control_set:
329  * @cs: What to parse
330  *
331  * Typically passed a command-line argument. The string has the syntax:
332  *
333  *      ( [category] "." [keyword] "=" value (" ")... )...
334  *
335  * where [category] is one the category names and keyword is one of the
336  * following:
337  *
338  *      thresh          value is an integer priority level. Sets the category's
339  *                        threshold priority.
340  *
341  * @warning
342  * This routine may only be called once and that must be before any other
343  * logging command! Typically, this is done from main().
344  */
345 void xbt_log_control_set(const char* control_string) {
346   xbt_error_t errcode;
347   xbt_log_setting_t set;
348   char *cs;
349   char *p;
350   int done = 0;
351   
352   DEBUG1("Parse log settings '%s'",control_string);
353   if (control_string == NULL)
354     return;
355   if (xbt_log_settings == NULL)
356     xbt_log_settings = xbt_dynar_new(sizeof(xbt_log_setting_t),
357                                        _free_setting);
358
359   set = xbt_new(s_xbt_log_setting_t,1);
360   cs=xbt_strdup(control_string);
361
362   _cleanup_double_spaces(cs);
363
364   while (!done) {
365     xbt_log_category_t cat;
366     
367     p=strrchr(cs,' ');
368     if (p) {
369       *p='\0';
370       *p++;
371     } else {
372       p=cs;
373       done = 1;
374     }
375     _xbt_log_parse_setting(p,set);
376     
377     errcode = _xbt_log_cat_searchsub(&_XBT_LOGV(root),set->catname,&cat);
378     if (errcode == mismatch_error) {
379       DEBUG0("Store for further application");
380       DEBUG1("push %p to the settings",(void*)set);
381       xbt_dynar_push(xbt_log_settings,&set);
382       /* malloc in advance the next slot */
383       set = xbt_new(s_xbt_log_setting_t,1);
384     } else {
385       DEBUG0("Apply directly");
386       xbt_free(set->catname);
387       xbt_log_threshold_set(cat,set->thresh);
388     }
389   }
390   xbt_free(set);
391   xbt_free(cs);
392
393
394 void xbt_log_appender_set(xbt_log_category_t cat, xbt_log_appender_t app) {
395   cat->appender = app;
396 }
397