Logo AND Algorithmique Numérique Distribuée

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