Logo AND Algorithmique Numérique Distribuée

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