Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
bugs, smells and cosmetics of the day
[simgrid.git] / src / xbt / log.c
1 /* log - a generic logging facility in the spirit of log4j                  */
2
3 /* Copyright (c) 2004-2015. The SimGrid Team.
4  * All rights reserved.                                                     */
5
6 /* This program is free software; you can redistribute it and/or modify it
7  * under the terms of the license (GNU LGPL) which comes with this package. */
8
9 #include <stdarg.h>
10 #include <ctype.h>
11 #include <stdio.h>              /* snprintf */
12 #include <stdlib.h>             /* snprintf */
13
14 #include "src/internal_config.h"
15
16 #include "src/xbt_modinter.h"
17
18 #include "src/xbt/log_private.h"
19 #include "xbt/asserts.h"
20 #include "xbt/dynar.h"
21 #include "xbt/ex.h"
22 #include "xbt/misc.h"
23 #include "xbt/str.h"
24 #include "xbt/sysdep.h"
25 #include "xbt/xbt_os_thread.h"
26
27 int xbt_log_no_loc = 0; /* if set to true (with --log=no_loc), file localization will be omitted (for tesh tests) */
28 static xbt_os_mutex_t log_cat_init_mutex = NULL;
29
30 /** \addtogroup XBT_log
31  *
32  *  For more information, please refer to @ref outcomes_logs Section.
33  */
34
35 xbt_log_appender_t xbt_log_default_appender = NULL;     /* set in log_init */
36 xbt_log_layout_t xbt_log_default_layout = NULL; /* set in log_init */
37
38 typedef struct {
39   char *catname;
40   char *fmt;
41   e_xbt_log_priority_t thresh;
42   int additivity;
43   xbt_log_appender_t appender;
44 } s_xbt_log_setting_t;
45
46 typedef s_xbt_log_setting_t* xbt_log_setting_t;
47
48 static xbt_dynar_t xbt_log_settings = NULL;
49
50 static void _free_setting(void *s)
51 {
52   xbt_log_setting_t set = *(xbt_log_setting_t *) s;
53   if (set) {
54     free(set->catname);
55     free(set->fmt);
56     free(set);
57   }
58 }
59
60 static void _xbt_log_cat_apply_set(xbt_log_category_t category, xbt_log_setting_t setting);
61
62 const char *xbt_log_priority_names[8] = {
63   "NONE",
64   "TRACE",
65   "DEBUG",
66   "VERBOSE",
67   "INFO",
68   "WARNING",
69   "ERROR",
70   "CRITICAL"
71 };
72
73 s_xbt_log_category_t _XBT_LOGV(XBT_LOG_ROOT_CAT) = {
74   NULL /*parent */ , NULL /* firstChild */ , NULL /* nextSibling */ ,
75       "root", "The common ancestor for all categories",
76       0 /*initialized */, xbt_log_priority_uninitialized /* threshold */ ,
77       0 /* isThreshInherited */ ,
78       NULL /* appender */ , NULL /* layout */ ,
79       0                         /* additivity */
80 };
81
82 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(log, xbt, "Loggings from the logging mechanism itself");
83
84 /* create the default appender and install it in the root category,
85    which were already created (damnit. Too slow little beetle) */
86 void xbt_log_preinit(void)
87 {
88   xbt_log_default_appender = xbt_log_appender_file_new(NULL);
89   xbt_log_default_layout = xbt_log_layout_simple_new(NULL);
90   _XBT_LOGV(XBT_LOG_ROOT_CAT).appender = xbt_log_default_appender;
91   _XBT_LOGV(XBT_LOG_ROOT_CAT).layout = xbt_log_default_layout;
92   log_cat_init_mutex = xbt_os_mutex_init();
93 }
94
95 static void xbt_log_connect_categories(void)
96 {
97   /* Connect our log channels: that must be done manually under windows */
98   /* Also permit that they are correctly listed by xbt_log_help_categories() */
99
100   /* xbt */
101   XBT_LOG_CONNECT(xbt);
102   XBT_LOG_CONNECT(log);
103   XBT_LOG_CONNECT(module);
104   XBT_LOG_CONNECT(replay);
105   XBT_LOG_CONNECT(strbuff);
106   XBT_LOG_CONNECT(xbt_cfg);
107   XBT_LOG_CONNECT(xbt_dict);
108   XBT_LOG_CONNECT(xbt_dict_cursor);
109   XBT_LOG_CONNECT(xbt_dict_elm);
110   XBT_LOG_CONNECT(xbt_dyn);
111   XBT_LOG_CONNECT(xbt_ex);
112   XBT_LOG_CONNECT(xbt_backtrace);
113   XBT_LOG_CONNECT(xbt_exception);
114   XBT_LOG_CONNECT(xbt_graph);
115   XBT_LOG_CONNECT(xbt_heap);
116   XBT_LOG_CONNECT(xbt_lib);
117   XBT_LOG_CONNECT(xbt_mallocator);
118   XBT_LOG_CONNECT(xbt_matrix);
119   XBT_LOG_CONNECT(xbt_memory_map);
120   XBT_LOG_CONNECT(xbt_parmap);
121   XBT_LOG_CONNECT(xbt_sync);
122   XBT_LOG_CONNECT(xbt_sync_os);
123
124 #ifdef simgrid_EXPORTS
125   /* The following categories are only defined in libsimgrid */
126
127   /* bindings */
128 #if SIMGRID_HAVE_LUA
129   XBT_LOG_CONNECT(lua);
130   XBT_LOG_CONNECT(lua_host);
131   XBT_LOG_CONNECT(lua_platf);
132   XBT_LOG_CONNECT(lua_debug);
133 #endif
134
135   /* instr */
136   XBT_LOG_CONNECT(instr);
137   XBT_LOG_CONNECT(instr_api);
138   XBT_LOG_CONNECT(instr_config);
139   XBT_LOG_CONNECT(instr_msg);
140   XBT_LOG_CONNECT(instr_msg_process);
141   XBT_LOG_CONNECT(instr_paje_containers);
142   XBT_LOG_CONNECT(instr_paje_header);
143   XBT_LOG_CONNECT(instr_paje_trace);
144   XBT_LOG_CONNECT(instr_paje_types);
145   XBT_LOG_CONNECT(instr_paje_values);
146   XBT_LOG_CONNECT(instr_resource);
147   XBT_LOG_CONNECT(instr_routing);
148   XBT_LOG_CONNECT(instr_surf);
149   XBT_LOG_CONNECT(instr_trace);
150   XBT_LOG_CONNECT(instr_TI_trace);
151
152   /* jedule */
153 #if SIMGRID_HAVE_JEDULE
154   XBT_LOG_CONNECT(jedule);
155   XBT_LOG_CONNECT(jed_sd);
156 #endif
157
158   /* mc */
159 #if SIMGRID_HAVE_MC
160   XBT_LOG_CONNECT(mc);
161   XBT_LOG_CONNECT(mc_checkpoint);
162   XBT_LOG_CONNECT(mc_comm_determinism);
163   XBT_LOG_CONNECT(mc_compare);
164   XBT_LOG_CONNECT(mc_dwarf);
165   XBT_LOG_CONNECT(mc_hash);
166   XBT_LOG_CONNECT(mc_liveness);
167   XBT_LOG_CONNECT(mc_memory);
168   XBT_LOG_CONNECT(mc_page_snapshot);
169   XBT_LOG_CONNECT(mc_request);
170   XBT_LOG_CONNECT(mc_safety);
171   XBT_LOG_CONNECT(mc_VisitedState);
172   XBT_LOG_CONNECT(mc_client);
173   XBT_LOG_CONNECT(mc_client_api);
174   XBT_LOG_CONNECT(mc_comm_pattern);
175   XBT_LOG_CONNECT(mc_process);
176   XBT_LOG_CONNECT(mc_protocol);
177   XBT_LOG_CONNECT(mc_Channel);
178   XBT_LOG_CONNECT(mc_ModelChecker);
179   XBT_LOG_CONNECT(mc_RegionSnaphot);
180   XBT_LOG_CONNECT(mc_Session);
181   XBT_LOG_CONNECT(mc_state);
182 #endif
183   XBT_LOG_CONNECT(mc_global);
184   XBT_LOG_CONNECT(mc_config);
185   XBT_LOG_CONNECT(mc_record);
186
187   /* msg */
188   XBT_LOG_CONNECT(msg);
189   XBT_LOG_CONNECT(msg_action);
190   XBT_LOG_CONNECT(msg_gos);
191   XBT_LOG_CONNECT(msg_io);
192   XBT_LOG_CONNECT(msg_kernel);
193   XBT_LOG_CONNECT(msg_mailbox);
194   XBT_LOG_CONNECT(msg_process);
195   XBT_LOG_CONNECT(msg_synchro);
196   XBT_LOG_CONNECT(msg_task);
197   XBT_LOG_CONNECT(msg_vm);
198
199   /* s4u */
200   XBT_LOG_CONNECT(s4u);
201   XBT_LOG_CONNECT(s4u_activity);
202   XBT_LOG_CONNECT(s4u_actor);
203   XBT_LOG_CONNECT(s4u_netzone);
204   XBT_LOG_CONNECT(s4u_channel);
205   XBT_LOG_CONNECT(s4u_comm);
206   XBT_LOG_CONNECT(s4u_file);
207    
208   /* sg */
209   XBT_LOG_CONNECT(sg_host);
210
211   /* simdag */
212   XBT_LOG_CONNECT(sd);
213   XBT_LOG_CONNECT(sd_daxparse);
214 #if HAVE_GRAPHVIZ
215   XBT_LOG_CONNECT(sd_dotparse);
216 #endif
217   XBT_LOG_CONNECT(sd_kernel);
218   XBT_LOG_CONNECT(sd_task);
219
220   /* simix */
221   XBT_LOG_CONNECT(simix);
222   XBT_LOG_CONNECT(simix_context);
223   XBT_LOG_CONNECT(simix_deployment);
224   XBT_LOG_CONNECT(simix_environment);
225   XBT_LOG_CONNECT(simix_host);
226   XBT_LOG_CONNECT(simix_io);
227   XBT_LOG_CONNECT(simix_kernel);
228   XBT_LOG_CONNECT(simix_network);
229   XBT_LOG_CONNECT(simix_process);
230   XBT_LOG_CONNECT(simix_popping);
231   XBT_LOG_CONNECT(simix_synchro);
232
233   /* smpi */
234   /* SMPI categories are connected in smpi_global.c */
235
236   /* surf */
237   XBT_LOG_CONNECT(surf);
238   XBT_LOG_CONNECT(surf_config);
239   XBT_LOG_CONNECT(surf_cpu);
240   XBT_LOG_CONNECT(surf_cpu_cas);
241   XBT_LOG_CONNECT(surf_cpu_ti);
242   XBT_LOG_CONNECT(surf_energy);
243   XBT_LOG_CONNECT(surf_kernel);
244   XBT_LOG_CONNECT(surf_lagrange);
245   XBT_LOG_CONNECT(surf_lagrange_dichotomy);
246   XBT_LOG_CONNECT(surf_maxmin);
247   XBT_LOG_CONNECT(surf_network);
248 #if SIMGRID_HAVE_NS3
249   XBT_LOG_CONNECT(ns3);
250 #endif
251   XBT_LOG_CONNECT(surf_parse);
252   XBT_LOG_CONNECT(surf_route);
253   XBT_LOG_CONNECT(surf_routing_generic);
254   XBT_LOG_CONNECT(surf_route_cluster);
255   XBT_LOG_CONNECT(surf_route_cluster_torus);
256   XBT_LOG_CONNECT(surf_route_cluster_dragonfly);
257   XBT_LOG_CONNECT(surf_route_dijkstra);
258   XBT_LOG_CONNECT(surf_route_fat_tree);
259   XBT_LOG_CONNECT(surf_route_floyd);
260   XBT_LOG_CONNECT(surf_route_full);
261   XBT_LOG_CONNECT(surf_route_none);
262   XBT_LOG_CONNECT(surf_route_vivaldi);
263   XBT_LOG_CONNECT(surf_storage);
264   XBT_LOG_CONNECT(surf_trace);
265   XBT_LOG_CONNECT(surf_vm);
266   XBT_LOG_CONNECT(surf_host);
267    
268 #endif /* simgrid_EXPORTS */
269 }
270
271 static void xbt_log_help(void);
272 static void xbt_log_help_categories(void);
273
274 /** @brief Get all logging settings from the command line
275  *
276  * xbt_log_control_set() is called on each string we got from cmd line
277  */
278 void xbt_log_init(int *argc, char **argv)
279 {
280   unsigned help_requested = 0;  /* 1: logs; 2: categories */
281   int i;
282   int j;
283   char *opt;
284
285   /* uncomment to set the LOG category to debug directly */
286   //    _XBT_LOGV(log).threshold = xbt_log_priority_debug;
287
288   xbt_log_connect_categories();
289
290   /* Set logs and init log submodule */
291   for (j = i = 1; i < *argc; i++) {
292     if (!strncmp(argv[i], "--log=", strlen("--log="))) {
293       opt = strchr(argv[i], '=');
294       opt++;
295       xbt_log_control_set(opt);
296       XBT_DEBUG("Did apply '%s' as log setting", opt);
297     } else if (!strcmp(argv[i], "--help-logs")) {
298       help_requested |= 1U;
299     } else if (!strcmp(argv[i], "--help-log-categories")) {
300       help_requested |= 2U;
301     } else {
302       argv[j++] = argv[i];
303     }
304   }
305   if (j < *argc) {
306     argv[j] = NULL;
307     *argc = j;
308   }
309
310   if (help_requested) {
311     if (help_requested & 1)
312       xbt_log_help();
313     if (help_requested & 2)
314       xbt_log_help_categories();
315     exit(0);
316   }
317 }
318
319 static void log_cat_exit(xbt_log_category_t cat)
320 {
321   xbt_log_category_t child;
322
323   if (cat->appender) {
324     if (cat->appender->free_)
325       cat->appender->free_(cat->appender);
326     free(cat->appender);
327   }
328   if (cat->layout) {
329     if (cat->layout->free_)
330       cat->layout->free_(cat->layout);
331     free(cat->layout);
332   }
333
334   for (child = cat->firstChild; child != NULL; child = child->nextSibling)
335     log_cat_exit(child);
336 }
337
338 void xbt_log_postexit(void)
339 {
340   XBT_VERB("Exiting log");
341   xbt_os_mutex_destroy(log_cat_init_mutex);
342   xbt_dynar_free(&xbt_log_settings);
343   log_cat_exit(&_XBT_LOGV(XBT_LOG_ROOT_CAT));
344 }
345
346  /* Size of the static string in which we  build the log string */
347 #define XBT_LOG_STATIC_BUFFER_SIZE 2048
348 /* Minimum size of the dynamic string in which we build the log string
349    (should be greater than XBT_LOG_STATIC_BUFFER_SIZE) */
350 #define XBT_LOG_DYNAMIC_BUFFER_SIZE 4096
351
352 void _xbt_log_event_log(xbt_log_event_t ev, const char *fmt, ...)
353 {
354   xbt_log_category_t cat = ev->cat;
355
356   xbt_assert(ev->priority >= 0, "Negative logging priority naturally forbidden");
357   xbt_assert(ev->priority < sizeof(xbt_log_priority_names), "Priority %d is greater than the biggest allowed value",
358              ev->priority);
359
360   do {
361     xbt_log_appender_t appender = cat->appender;
362
363     if (!appender)
364       continue;                 /* No appender, try next */
365
366     xbt_assert(cat->layout, "No valid layout for the appender of category %s", cat->name);
367
368     /* First, try with a static buffer */
369     if (XBT_LOG_STATIC_BUFFER_SIZE) {
370       char buff[XBT_LOG_STATIC_BUFFER_SIZE];
371       ev->buffer = buff;
372       ev->buffer_size = sizeof buff;
373       va_start(ev->ap, fmt);
374       int done = cat->layout->do_layout(cat->layout, ev, fmt);
375       va_end(ev->ap);
376       if (done) {
377         appender->do_append(appender, buff);
378         continue;               /* Ok, that worked: go next */
379       }
380     }
381
382     /* The static buffer was too small, use a dynamically expanded one */
383     ev->buffer_size = XBT_LOG_DYNAMIC_BUFFER_SIZE;
384     ev->buffer = xbt_malloc(ev->buffer_size);
385     while (1) {
386       va_start(ev->ap, fmt);
387       int done = cat->layout->do_layout(cat->layout, ev, fmt);
388       va_end(ev->ap);
389       if (done)
390         break;                  /* Got it */
391       ev->buffer_size *= 2;
392       ev->buffer = xbt_realloc(ev->buffer, ev->buffer_size);
393     }
394     appender->do_append(appender, ev->buffer);
395     xbt_free(ev->buffer);
396
397   } while (cat->additivity && (cat = cat->parent, 1));
398 }
399
400 #undef XBT_LOG_DYNAMIC_BUFFER_SIZE
401 #undef XBT_LOG_STATIC_BUFFER_SIZE
402
403 /* NOTE:
404  *
405  * The standard logging macros use _XBT_LOG_ISENABLED, which calls _xbt_log_cat_init().  Thus, if we want to avoid an
406  * infinite recursion, we can not use the standard logging macros in _xbt_log_cat_init(), and in all functions called
407  * from it.
408  *
409  * To circumvent the problem, we define the macro_xbt_log_init() as (0) for the length of the affected functions, and
410  * we do not forget to undefine it at the end!
411  */
412
413 static void _xbt_log_cat_apply_set(xbt_log_category_t category, xbt_log_setting_t setting)
414 {
415 #define _xbt_log_cat_init(a, b) (0)
416
417   if (setting->thresh != xbt_log_priority_uninitialized) {
418     xbt_log_threshold_set(category, setting->thresh);
419
420     XBT_DEBUG("Apply settings for category '%s': set threshold to %s (=%d)",
421            category->name, xbt_log_priority_names[category->threshold], category->threshold);
422   }
423
424   if (setting->fmt) {
425     xbt_log_layout_set(category, xbt_log_layout_format_new(setting->fmt));
426
427     XBT_DEBUG("Apply settings for category '%s': set format to %s", category->name, setting->fmt);
428   }
429
430   if (setting->additivity != -1) {
431     xbt_log_additivity_set(category, setting->additivity);
432
433     XBT_DEBUG("Apply settings for category '%s': set additivity to %s",
434            category->name, (setting->additivity ? "on" : "off"));
435   }
436   if (setting->appender) {
437     xbt_log_appender_set(category, setting->appender);
438     if (!category->layout)
439       xbt_log_layout_set(category, xbt_log_layout_simple_new(NULL));
440     category->additivity = 0;
441     XBT_DEBUG("Set %p as appender of category '%s'", setting->appender, category->name);
442   }
443 #undef _xbt_log_cat_init
444 }
445
446 /*
447  * This gets called the first time a category is referenced and performs the initialization.
448  * Also resets threshold to inherited!
449  */
450 int _xbt_log_cat_init(xbt_log_category_t category, e_xbt_log_priority_t priority)
451 {
452 #define _xbt_log_cat_init(a, b) (0)
453
454   if (log_cat_init_mutex != NULL)
455     xbt_os_mutex_acquire(log_cat_init_mutex);
456
457   if (category->initialized) {
458     if (log_cat_init_mutex != NULL)
459       xbt_os_mutex_release(log_cat_init_mutex);
460     return priority >= category->threshold;
461   }
462
463   unsigned int cursor;
464   xbt_log_setting_t setting = NULL;
465   int found = 0;
466
467   XBT_DEBUG("Initializing category '%s' (firstChild=%s, nextSibling=%s)", category->name,
468          (category->firstChild ? category->firstChild->name : "none"),
469          (category->nextSibling ? category->nextSibling->name : "none"));
470
471   if (category == &_XBT_LOGV(XBT_LOG_ROOT_CAT)) {
472     category->threshold = xbt_log_priority_info;
473     category->appender = xbt_log_default_appender;
474     category->layout = xbt_log_default_layout;
475   } else {
476     if (!category->parent)
477       category->parent = &_XBT_LOGV(XBT_LOG_ROOT_CAT);
478
479     XBT_DEBUG("Set %s (%s) as father of %s ", category->parent->name,
480            (category->parent->initialized ? xbt_log_priority_names[category->parent->threshold] : "uninited"),
481            category->name);
482     xbt_log_parent_set(category, category->parent);
483
484     if (XBT_LOG_ISENABLED(log, xbt_log_priority_debug)) {
485       char *buf;
486       char *res = NULL;
487       xbt_log_category_t cpp = category->parent->firstChild;
488       while (cpp) {
489         if (res) {
490           buf = bprintf("%s %s", res, cpp->name);
491           free(res);
492           res = buf;
493         } else {
494           res = xbt_strdup(cpp->name);
495         }
496         cpp = cpp->nextSibling;
497       }
498
499       XBT_DEBUG("Children of %s: %s; nextSibling: %s", category->parent->name, res,
500              (category->parent->nextSibling ? category->parent->nextSibling->name : "none"));
501
502       free(res);
503     }
504   }
505
506   /* Apply the control */
507   if (xbt_log_settings) {
508     xbt_assert(category, "NULL category");
509     xbt_assert(category->name);
510
511     xbt_dynar_foreach(xbt_log_settings, cursor, setting) {
512       xbt_assert(setting, "Damnit, NULL cat in the list");
513       xbt_assert(setting->catname, "NULL setting(=%p)->catname", (void *) setting);
514
515       if (!strcmp(setting->catname, category->name)) {
516         found = 1;
517         _xbt_log_cat_apply_set(category, setting);
518         xbt_dynar_cursor_rm(xbt_log_settings, &cursor);
519       }
520     }
521
522     if (!found)
523       XBT_DEBUG("Category '%s': inherited threshold = %s (=%d)",
524                 category->name, xbt_log_priority_names[category->threshold], category->threshold);
525   }
526
527   category->initialized = 1;
528   if (log_cat_init_mutex != NULL)
529     xbt_os_mutex_release(log_cat_init_mutex);
530   return priority >= category->threshold;
531
532 #undef _xbt_log_cat_init
533 }
534
535 void xbt_log_parent_set(xbt_log_category_t cat, xbt_log_category_t parent)
536 {
537   xbt_assert(cat, "NULL category to be given a parent");
538   xbt_assert(parent, "The parent category of %s is NULL", cat->name);
539
540   /* if the category is initialized, unlink from current parent */
541   if (cat->initialized) {
542     xbt_log_category_t *cpp = &cat->parent->firstChild;
543
544     while (*cpp != cat && *cpp != NULL) {
545       cpp = &(*cpp)->nextSibling;
546     }
547
548     xbt_assert(*cpp == cat);
549     *cpp = cat->nextSibling;
550   }
551
552   cat->parent = parent;
553   cat->nextSibling = parent->firstChild;
554
555   parent->firstChild = cat;
556
557   if (!parent->initialized)
558     _xbt_log_cat_init(parent, xbt_log_priority_uninitialized /* ignored */ );
559
560   cat->threshold = parent->threshold;
561
562   cat->isThreshInherited = 1;
563 }
564
565 static void _set_inherited_thresholds(xbt_log_category_t cat)
566 {
567   xbt_log_category_t child = cat->firstChild;
568
569   for (; child != NULL; child = child->nextSibling) {
570     if (child->isThreshInherited) {
571       if (cat != &_XBT_LOGV(log))
572         XBT_VERB("Set category threshold of %s to %s (=%d)",
573               child->name, xbt_log_priority_names[cat->threshold], cat->threshold);
574       child->threshold = cat->threshold;
575       _set_inherited_thresholds(child);
576     }
577   }
578 }
579
580 void xbt_log_threshold_set(xbt_log_category_t cat, e_xbt_log_priority_t threshold)
581 {
582   cat->threshold = threshold;
583   cat->isThreshInherited = 0;
584
585   _set_inherited_thresholds(cat);
586 }
587
588 static xbt_log_setting_t _xbt_log_parse_setting(const char *control_string)
589 {
590   const char *orig_control_string = control_string;
591   xbt_log_setting_t set = xbt_new(s_xbt_log_setting_t, 1);
592
593   set->catname = NULL;
594   set->thresh = xbt_log_priority_uninitialized;
595   set->fmt = NULL;
596   set->additivity = -1;
597   set->appender = NULL;
598
599   if (!*control_string)
600     return set;
601   XBT_DEBUG("Parse log setting '%s'", control_string);
602
603   control_string += strspn(control_string, " ");
604   const char *name = control_string;
605   control_string += strcspn(control_string, ".:= ");
606   const char *dot = control_string;
607   control_string += strcspn(control_string, ":= ");
608   const char *eq = control_string;
609
610   xbt_assert(*dot == '.' || (*eq != '=' && *eq != ':'), "Invalid control string '%s'", orig_control_string);
611
612   if (!strncmp(dot + 1, "threshold", (size_t) (eq - dot - 1))) {
613     int i;
614     char *neweq = xbt_strdup(eq + 1);
615     char *p = neweq - 1;
616
617     while (*(++p) != '\0') {
618       if (*p >= 'a' && *p <= 'z') {
619         *p -= 'a' - 'A';
620       }
621     }
622
623     XBT_DEBUG("New priority name = %s", neweq);
624     for (i = 0; i < xbt_log_priority_infinite; i++) {
625       if (!strncmp(xbt_log_priority_names[i], neweq, p - eq)) {
626         XBT_DEBUG("This is priority %d", i);
627         break;
628       }
629     }
630
631     if(i<XBT_LOG_STATIC_THRESHOLD){
632      fprintf(stderr,
633          "Priority '%s' (in setting '%s') is above allowed priority '%s'.\n\n"
634          "Compiling SimGrid with -DNDEBUG forbids the levels 'trace' and 'debug'\n"
635          "while -DNLOG forbids any logging, at any level.",
636              eq + 1, name, xbt_log_priority_names[XBT_LOG_STATIC_THRESHOLD]);
637      exit(1);
638     }else if (i < xbt_log_priority_infinite) {
639       set->thresh = (e_xbt_log_priority_t) i;
640     } else {
641       THROWF(arg_error, 0,
642              "Unknown priority name: %s (must be one of: trace,debug,verbose,info,warning,error,critical)", eq + 1);
643     }
644     free(neweq);
645   } else if (!strncmp(dot + 1, "add", (size_t) (eq - dot - 1)) ||
646              !strncmp(dot + 1, "additivity", (size_t) (eq - dot - 1))) {
647     char *neweq = xbt_strdup(eq + 1);
648     char *p = neweq - 1;
649
650     while (*(++p) != '\0') {
651       if (*p >= 'a' && *p <= 'z') {
652         *p -= 'a' - 'A';
653       }
654     }
655     if (!strcmp(neweq, "ON") || !strcmp(neweq, "YES") || !strcmp(neweq, "1")) {
656       set->additivity = 1;
657     } else {
658       set->additivity = 0;
659     }
660     free(neweq);
661   } else if (!strncmp(dot + 1, "app", (size_t) (eq - dot - 1)) ||
662              !strncmp(dot + 1, "appender", (size_t) (eq - dot - 1))) {
663     char *neweq = xbt_strdup(eq + 1);
664
665     if (!strncmp(neweq, "file:", 5)) {
666       set->appender = xbt_log_appender_file_new(neweq + 5);
667     }else if (!strncmp(neweq, "rollfile:", 9)) {
668       set->appender = xbt_log_appender2_file_new(neweq + 9,1);
669     }else if (!strncmp(neweq, "splitfile:", 10)) {
670       set->appender = xbt_log_appender2_file_new(neweq + 10,0);
671     } else {
672       THROWF(arg_error, 0, "Unknown appender log type: '%s'", neweq);
673     }
674     free(neweq);
675   } else if (!strncmp(dot + 1, "fmt", (size_t) (eq - dot - 1))) {
676     set->fmt = xbt_strdup(eq + 1);
677   } else {
678     char buff[512];
679     snprintf(buff, MIN(512, eq - dot), "%s", dot + 1);
680     xbt_die("Unknown setting of the log category: '%s'", buff);
681   }
682   set->catname = (char *) xbt_malloc(dot - name + 1);
683
684   memcpy(set->catname, name, dot - name);
685   set->catname[dot - name] = '\0';      /* Just in case */
686   XBT_DEBUG("This is for cat '%s'", set->catname);
687
688   return set;
689 }
690
691 static xbt_log_category_t _xbt_log_cat_searchsub(xbt_log_category_t cat, char *name)
692 {
693   xbt_log_category_t child;
694   xbt_log_category_t res;
695
696   XBT_DEBUG("Search '%s' into '%s' (firstChild='%s'; nextSibling='%s')", name,
697          cat->name, (cat->firstChild ? cat->firstChild->name : "none"),
698          (cat->nextSibling ? cat->nextSibling->name : "none"));
699   if (!strcmp(cat->name, name))
700     return cat;
701
702   for (child = cat->firstChild; child != NULL; child = child->nextSibling) {
703     XBT_DEBUG("Dig into %s", child->name);
704     res = _xbt_log_cat_searchsub(child, name);
705     if (res)
706       return res;
707   }
708
709   return NULL;
710 }
711
712 /**
713  * \ingroup XBT_log
714  * \param control_string What to parse
715  *
716  * Typically passed a command-line argument. The string has the syntax:
717  *
718  *      ( [category] "." [keyword] ":" value (" ")... )...
719  *
720  * where [category] is one the category names (see \ref XBT_log_cats for a complete list of the ones defined in the
721  * SimGrid library) and keyword is one of the following:
722  *
723  *    - thres: category's threshold priority. Possible values:
724  *             TRACE,DEBUG,VERBOSE,INFO,WARNING,ERROR,CRITICAL
725  *    - add or additivity: whether the logging actions must be passed to the parent category.
726  *      Possible values: 0, 1, no, yes, on, off.
727  *      Default value: yes.
728  *    - fmt: the format to use. See \ref log_use_conf_fmt for more information.
729  *    - app or appender: the appender to use. See \ref log_use_conf_app for more information.
730  */
731 void xbt_log_control_set(const char *control_string)
732 {
733   xbt_log_setting_t set;
734
735   /* To split the string in commands, and the cursors */
736   xbt_dynar_t set_strings;
737   char *str;
738   unsigned int cpt;
739
740   if (!control_string)
741     return;
742   XBT_DEBUG("Parse log settings '%s'", control_string);
743
744   /* Special handling of no_loc request, which asks for any file localization to be omitted (for tesh runs) */
745   if (!strcmp(control_string, "no_loc")) {
746     xbt_log_no_loc = 1;
747     return;
748   }
749   /* some initialization if this is the first time that this get called */
750   if (xbt_log_settings == NULL)
751     xbt_log_settings = xbt_dynar_new(sizeof(xbt_log_setting_t), _free_setting);
752
753   /* split the string, and remove empty entries */
754   set_strings = xbt_str_split_quoted(control_string);
755
756   if (xbt_dynar_is_empty(set_strings)) {     /* vicious user! */
757     xbt_dynar_free(&set_strings);
758     return;
759   }
760
761   /* Parse each entry and either use it right now (if the category was already created), or store it for further use */
762   xbt_dynar_foreach(set_strings, cpt, str) {
763     xbt_log_category_t cat = NULL;
764
765     set = _xbt_log_parse_setting(str);
766     cat = _xbt_log_cat_searchsub(&_XBT_LOGV(XBT_LOG_ROOT_CAT), set->catname);
767
768     if (cat) {
769       XBT_DEBUG("Apply directly");
770       _xbt_log_cat_apply_set(cat, set);
771       _free_setting((void *) &set);
772     } else {
773       XBT_DEBUG("Store for further application");
774       XBT_DEBUG("push %p to the settings", (void *) set);
775       xbt_dynar_push(xbt_log_settings, &set);
776     }
777   }
778   xbt_dynar_free(&set_strings);
779 }
780
781 void xbt_log_appender_set(xbt_log_category_t cat, xbt_log_appender_t app)
782 {
783   if (cat->appender) {
784     if (cat->appender->free_)
785       cat->appender->free_(cat->appender);
786     free(cat->appender);
787   }
788   cat->appender = app;
789 }
790
791 void xbt_log_layout_set(xbt_log_category_t cat, xbt_log_layout_t lay)
792 {
793 #define _xbt_log_cat_init(a, b) (0)
794   if (!cat->appender) {
795     XBT_VERB ("No appender to category %s. Setting the file appender as default", cat->name);
796     xbt_log_appender_set(cat, xbt_log_appender_file_new(NULL));
797   }
798   if (cat->layout) {
799     if (cat->layout->free_) {
800       cat->layout->free_(cat->layout);
801     }
802     free(cat->layout);
803   }
804   cat->layout = lay;
805   xbt_log_additivity_set(cat, 0);
806 #undef _xbt_log_cat_init
807 }
808
809 void xbt_log_additivity_set(xbt_log_category_t cat, int additivity)
810 {
811   cat->additivity = additivity;
812 }
813
814 static void xbt_log_help(void)
815 {
816   printf(
817 "Description of the logging output:\n"
818 "\n"
819 "   Threshold configuration: --log=CATEGORY_NAME.thres:PRIORITY_LEVEL\n"
820 "      CATEGORY_NAME: defined in code with function 'XBT_LOG_NEW_CATEGORY'\n"
821 "      PRIORITY_LEVEL: the level to print (trace,debug,verbose,info,warning,error,critical)\n"
822 "         -> trace: enter and return of some functions\n"
823 "         -> debug: crufty output\n"
824 "         -> verbose: verbose output for the user wanting more\n"
825 "         -> info: output about the regular functionning\n"
826 "         -> warning: minor issue encountered\n"
827 "         -> error: issue encountered\n"
828 "         -> critical: major issue encountered\n"
829 "\n"
830 "   Format configuration: --log=CATEGORY_NAME.fmt:OPTIONS\n"
831 "      OPTIONS may be:\n"
832 "         -> %%%%: the %% char\n"
833 "         -> %%n: platform-dependent line separator (LOG4J compatible)\n"
834 "         -> %%e: plain old space (SimGrid extension)\n"
835 "\n"
836 "         -> %%m: user-provided message\n"
837 "\n"
838 "         -> %%c: Category name (LOG4J compatible)\n"
839 "         -> %%p: Priority name (LOG4J compatible)\n"
840 "\n"
841 "         -> %%h: Hostname (SimGrid extension)\n"
842 "         -> %%P: Process name (SimGrid extension)\n"
843 "         -> %%t: Thread \"name\" (LOG4J compatible -- actually the address of the thread in memory)\n"
844 "         -> %%i: Process PID (SimGrid extension -- this is a 'i' as in 'i'dea)\n"
845 "\n"
846 "         -> %%F: file name where the log event was raised (LOG4J compatible)\n"
847 "         -> %%l: location where the log event was raised (LOG4J compatible, like '%%F:%%L' -- this is a l as in 'l'etter)\n"
848 "         -> %%L: line number where the log event was raised (LOG4J compatible)\n"
849 "         -> %%M: function name (LOG4J compatible -- called method name here of course).\n"
850 "                 Defined only when using gcc because there is no __FUNCTION__ elsewhere.\n"
851 "\n"
852 "         -> %%b: full backtrace (Called %%throwable in LOG4J). Defined only under windows or when using the GNU libc because\n"
853 "                 backtrace() is not defined elsewhere, and we only have a fallback for windows boxes, not mac ones for example.\n"
854 "         -> %%B: short backtrace (only the first line of the %%b). Called %%throwable{short} in LOG4J; defined where %%b is.\n"
855 "\n"
856 "         -> %%d: date (UNIX-like epoch)\n"
857 "         -> %%r: application age (time elapsed since the beginning of the application)\n"
858 "\n"
859 "   Miscellaneous:\n"
860 "      --help-log-categories    Display the current hierarchy of log categories.\n"
861 "      --log=no_loc             Don't print file names in messages (for tesh tests).\n"
862 "\n"
863     );
864 }
865
866 static int xbt_log_cat_cmp(const void *pa, const void *pb)
867 {
868   xbt_log_category_t a = *(xbt_log_category_t *)pa;
869   xbt_log_category_t b = *(xbt_log_category_t *)pb;
870   return strcmp(a->name, b->name);
871 }
872
873 static void xbt_log_help_categories_rec(xbt_log_category_t category, const char *prefix)
874 {
875   char *this_prefix;
876   char *child_prefix;
877   unsigned i;
878   xbt_log_category_t cat;
879
880   if (!category)
881     return;
882
883   if (category->parent) {
884     this_prefix = bprintf("%s \\_ ", prefix);
885     child_prefix = bprintf("%s |  ", prefix);
886   } else {
887     this_prefix = xbt_strdup(prefix);
888     child_prefix = xbt_strdup(prefix);
889   }
890
891   xbt_dynar_t dynar = xbt_dynar_new(sizeof(xbt_log_category_t), NULL);
892   for (cat = category ; cat != NULL; cat = cat->nextSibling)
893     xbt_dynar_push_as(dynar, xbt_log_category_t, cat);
894
895   xbt_dynar_sort(dynar, xbt_log_cat_cmp);
896
897   xbt_dynar_foreach(dynar, i, cat){
898     if (i == xbt_dynar_length(dynar) - 1 && category->parent)
899       *strrchr(child_prefix, '|') = ' ';
900     printf("%s%s: %s\n", this_prefix, cat->name, cat->description);
901     xbt_log_help_categories_rec(cat->firstChild, child_prefix);
902   }
903
904   xbt_dynar_free(&dynar);
905   xbt_free(this_prefix);
906   xbt_free(child_prefix);
907 }
908
909 static void xbt_log_help_categories(void)
910 {
911   printf("Current log category hierarchy:\n");
912   xbt_log_help_categories_rec(&_XBT_LOGV(XBT_LOG_ROOT_CAT), "   ");
913   printf("\n");
914 }