Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
do not define MIN() and MAX() in xbt/base.h to not polute our mpi.h
[simgrid.git] / src / xbt / log.c
1 /* log - a generic logging facility in the spirit of log4j                  */
2
3 /* Copyright (c) 2004-2018. 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 #define MIN(a, b) ((a) < (b) ? (a) : (b))
28 #define MAX(a, b) ((a) > (b) ? (a) : (b))
29
30 int xbt_log_no_loc = 0; /* if set to true (with --log=no_loc), file localization will be omitted (for tesh tests) */
31 static xbt_os_mutex_t log_cat_init_mutex = NULL;
32
33 /** \addtogroup XBT_log
34  *
35  *  For more information, please refer to @ref outcomes_logs Section.
36  */
37
38 xbt_log_appender_t xbt_log_default_appender = NULL;     /* set in log_init */
39 xbt_log_layout_t xbt_log_default_layout = NULL; /* set in log_init */
40
41 typedef struct {
42   char *catname;
43   char *fmt;
44   e_xbt_log_priority_t thresh;
45   int additivity;
46   xbt_log_appender_t appender;
47 } s_xbt_log_setting_t;
48
49 typedef s_xbt_log_setting_t* xbt_log_setting_t;
50
51 static xbt_dynar_t xbt_log_settings = NULL;
52
53 static void _free_setting(void *s)
54 {
55   xbt_log_setting_t set = *(xbt_log_setting_t *) s;
56   if (set) {
57     free(set->catname);
58     free(set->fmt);
59     free(set);
60   }
61 }
62
63 static void _xbt_log_cat_apply_set(xbt_log_category_t category, xbt_log_setting_t setting);
64
65 const char *xbt_log_priority_names[8] = {
66   "NONE",
67   "TRACE",
68   "DEBUG",
69   "VERBOSE",
70   "INFO",
71   "WARNING",
72   "ERROR",
73   "CRITICAL"
74 };
75
76 s_xbt_log_category_t _XBT_LOGV(XBT_LOG_ROOT_CAT) = {
77   NULL /*parent */ , NULL /* firstChild */ , NULL /* nextSibling */ ,
78       "root", "The common ancestor for all categories",
79       0 /*initialized */, xbt_log_priority_uninitialized /* threshold */ ,
80       0 /* isThreshInherited */ ,
81       NULL /* appender */ , NULL /* layout */ ,
82       0                         /* additivity */
83 };
84
85 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(log, xbt, "Loggings from the logging mechanism itself");
86
87 /* create the default appender and install it in the root category,
88    which were already created (damnit. Too slow little beetle) */
89 void xbt_log_preinit(void)
90 {
91   xbt_log_default_appender = xbt_log_appender_file_new(NULL);
92   xbt_log_default_layout = xbt_log_layout_simple_new(NULL);
93   _XBT_LOGV(XBT_LOG_ROOT_CAT).appender = xbt_log_default_appender;
94   _XBT_LOGV(XBT_LOG_ROOT_CAT).layout = xbt_log_default_layout;
95   log_cat_init_mutex = xbt_os_mutex_init();
96 }
97
98 static void xbt_log_help(void);
99 static void xbt_log_help_categories(void);
100
101 /** @brief Get all logging settings from the command line
102  *
103  * xbt_log_control_set() is called on each string we got from cmd line
104  */
105 void xbt_log_init(int *argc, char **argv)
106 {
107   unsigned help_requested = 0;  /* 1: logs; 2: categories */
108   int j                   = 1;
109
110   /* Set logs and init log submodule */
111   for (int i = 1; i < *argc; i++) {
112     if (!strncmp(argv[i], "--log=", strlen("--log="))) {
113       char* opt = strchr(argv[i], '=');
114       opt++;
115       xbt_log_control_set(opt);
116       XBT_DEBUG("Did apply '%s' as log setting", opt);
117     } else if (!strcmp(argv[i], "--help-logs")) {
118       help_requested |= 1U;
119     } else if (!strcmp(argv[i], "--help-log-categories")) {
120       help_requested |= 2U;
121     } else {
122       argv[j++] = argv[i];
123     }
124   }
125   if (j < *argc) {
126     argv[j] = NULL;
127     *argc = j;
128   }
129
130   if (help_requested) {
131     if (help_requested & 1)
132       xbt_log_help();
133     if (help_requested & 2)
134       xbt_log_help_categories();
135     exit(0);
136   }
137 }
138
139 static void log_cat_exit(xbt_log_category_t cat)
140 {
141   xbt_log_category_t child;
142
143   if (cat->appender) {
144     if (cat->appender->free_)
145       cat->appender->free_(cat->appender);
146     free(cat->appender);
147   }
148   if (cat->layout) {
149     if (cat->layout->free_)
150       cat->layout->free_(cat->layout);
151     free(cat->layout);
152   }
153
154   for (child = cat->firstChild; child != NULL; child = child->nextSibling)
155     log_cat_exit(child);
156 }
157
158 void xbt_log_postexit(void)
159 {
160   XBT_VERB("Exiting log");
161   xbt_os_mutex_destroy(log_cat_init_mutex);
162   xbt_dynar_free(&xbt_log_settings);
163   log_cat_exit(&_XBT_LOGV(XBT_LOG_ROOT_CAT));
164 }
165
166 /* Size of the static string in which we build the log string */
167 #define XBT_LOG_STATIC_BUFFER_SIZE 2048
168 /* Minimum size of the dynamic string in which we build the log string
169    (should be greater than XBT_LOG_STATIC_BUFFER_SIZE) */
170 #define XBT_LOG_DYNAMIC_BUFFER_SIZE 4096
171
172 void _xbt_log_event_log(xbt_log_event_t ev, const char *fmt, ...)
173 {
174   xbt_log_category_t cat = ev->cat;
175
176   xbt_assert(ev->priority >= 0, "Negative logging priority naturally forbidden");
177   xbt_assert(ev->priority < sizeof(xbt_log_priority_names), "Priority %d is greater than the biggest allowed value",
178              ev->priority);
179
180   while (1) {
181     xbt_log_appender_t appender = cat->appender;
182
183     if (appender != NULL) {
184       xbt_assert(cat->layout, "No valid layout for the appender of category %s", cat->name);
185
186       /* First, try with a static buffer */
187       int done = 0;
188       char buff[XBT_LOG_STATIC_BUFFER_SIZE];
189       ev->buffer      = buff;
190       ev->buffer_size = sizeof buff;
191       va_start(ev->ap, fmt);
192       done = cat->layout->do_layout(cat->layout, ev, fmt);
193       va_end(ev->ap);
194       if (done) {
195         appender->do_append(appender, buff);
196       } else {
197
198         /* The static buffer was too small, use a dynamically expanded one */
199         ev->buffer_size = XBT_LOG_DYNAMIC_BUFFER_SIZE;
200         ev->buffer      = xbt_malloc(ev->buffer_size);
201         while (1) {
202           va_start(ev->ap, fmt);
203           done = cat->layout->do_layout(cat->layout, ev, fmt);
204           va_end(ev->ap);
205           if (done)
206             break; /* Got it */
207           ev->buffer_size *= 2;
208           ev->buffer = xbt_realloc(ev->buffer, ev->buffer_size);
209         }
210         appender->do_append(appender, ev->buffer);
211         xbt_free(ev->buffer);
212       }
213     }
214
215     if (!cat->additivity)
216       break;
217     cat = cat->parent;
218   }
219 }
220
221 /* NOTE:
222  *
223  * The standard logging macros use _XBT_LOG_ISENABLED, which calls _xbt_log_cat_init().  Thus, if we want to avoid an
224  * infinite recursion, we can not use the standard logging macros in _xbt_log_cat_init(), and in all functions called
225  * from it.
226  *
227  * To circumvent the problem, we define the macro DISABLE_XBT_LOG_CAT_INIT() to hide the real _xbt_log_cat_init(). The
228  * macro has to be called at the beginning of the affected functions.
229  */
230 static int fake_xbt_log_cat_init(xbt_log_category_t XBT_ATTRIB_UNUSED category,
231                                  e_xbt_log_priority_t XBT_ATTRIB_UNUSED priority)
232 {
233   return 0;
234 }
235 #define DISABLE_XBT_LOG_CAT_INIT()                                                                                     \
236   int (*_xbt_log_cat_init)(xbt_log_category_t, e_xbt_log_priority_t) XBT_ATTRIB_UNUSED = fake_xbt_log_cat_init;
237
238 static void _xbt_log_cat_apply_set(xbt_log_category_t category, xbt_log_setting_t setting)
239 {
240   DISABLE_XBT_LOG_CAT_INIT();
241   if (setting->thresh != xbt_log_priority_uninitialized) {
242     xbt_log_threshold_set(category, setting->thresh);
243
244     XBT_DEBUG("Apply settings for category '%s': set threshold to %s (=%d)",
245            category->name, xbt_log_priority_names[category->threshold], category->threshold);
246   }
247
248   if (setting->fmt) {
249     xbt_log_layout_set(category, xbt_log_layout_format_new(setting->fmt));
250
251     XBT_DEBUG("Apply settings for category '%s': set format to %s", category->name, setting->fmt);
252   }
253
254   if (setting->additivity != -1) {
255     xbt_log_additivity_set(category, setting->additivity);
256
257     XBT_DEBUG("Apply settings for category '%s': set additivity to %s",
258            category->name, (setting->additivity ? "on" : "off"));
259   }
260   if (setting->appender) {
261     xbt_log_appender_set(category, setting->appender);
262     if (!category->layout)
263       xbt_log_layout_set(category, xbt_log_layout_simple_new(NULL));
264     category->additivity = 0;
265     XBT_DEBUG("Set %p as appender of category '%s'", setting->appender, category->name);
266   }
267 }
268
269 /*
270  * This gets called the first time a category is referenced and performs the initialization.
271  * Also resets threshold to inherited!
272  */
273 int _xbt_log_cat_init(xbt_log_category_t category, e_xbt_log_priority_t priority)
274 {
275   DISABLE_XBT_LOG_CAT_INIT();
276   if (log_cat_init_mutex != NULL)
277     xbt_os_mutex_acquire(log_cat_init_mutex);
278
279   if (category->initialized) {
280     if (log_cat_init_mutex != NULL)
281       xbt_os_mutex_release(log_cat_init_mutex);
282     return priority >= category->threshold;
283   }
284
285   unsigned int cursor;
286   xbt_log_setting_t setting = NULL;
287
288   XBT_DEBUG("Initializing category '%s' (firstChild=%s, nextSibling=%s)", category->name,
289          (category->firstChild ? category->firstChild->name : "none"),
290          (category->nextSibling ? category->nextSibling->name : "none"));
291
292   if (category == &_XBT_LOGV(XBT_LOG_ROOT_CAT)) {
293     category->threshold = xbt_log_priority_info;
294     category->appender = xbt_log_default_appender;
295     category->layout = xbt_log_default_layout;
296   } else {
297     if (!category->parent)
298       category->parent = &_XBT_LOGV(XBT_LOG_ROOT_CAT);
299
300     XBT_DEBUG("Set %s (%s) as father of %s ", category->parent->name,
301            (category->parent->initialized ? xbt_log_priority_names[category->parent->threshold] : "uninited"),
302            category->name);
303     xbt_log_parent_set(category, category->parent);
304
305     if (XBT_LOG_ISENABLED(log, xbt_log_priority_debug)) {
306       char *buf;
307       char *res = NULL;
308       xbt_log_category_t cpp = category->parent->firstChild;
309       while (cpp) {
310         if (res) {
311           buf = bprintf("%s %s", res, cpp->name);
312           free(res);
313           res = buf;
314         } else {
315           res = xbt_strdup(cpp->name);
316         }
317         cpp = cpp->nextSibling;
318       }
319
320       XBT_DEBUG("Children of %s: %s; nextSibling: %s", category->parent->name, res,
321              (category->parent->nextSibling ? category->parent->nextSibling->name : "none"));
322
323       free(res);
324     }
325   }
326
327   /* Apply the control */
328   if (xbt_log_settings) {
329     xbt_assert(category, "NULL category");
330     xbt_assert(category->name);
331     int found = 0;
332
333     xbt_dynar_foreach(xbt_log_settings, cursor, setting) {
334       xbt_assert(setting, "Damnit, NULL cat in the list");
335       xbt_assert(setting->catname, "NULL setting(=%p)->catname", (void *) setting);
336
337       if (!strcmp(setting->catname, category->name)) {
338         found = 1;
339         _xbt_log_cat_apply_set(category, setting);
340         xbt_dynar_cursor_rm(xbt_log_settings, &cursor);
341       }
342     }
343
344     if (!found)
345       XBT_DEBUG("Category '%s': inherited threshold = %s (=%d)",
346                 category->name, xbt_log_priority_names[category->threshold], category->threshold);
347   }
348
349   category->initialized = 1;
350   if (log_cat_init_mutex != NULL)
351     xbt_os_mutex_release(log_cat_init_mutex);
352   return priority >= category->threshold;
353 }
354
355 void xbt_log_parent_set(xbt_log_category_t cat, xbt_log_category_t parent)
356 {
357   xbt_assert(cat, "NULL category to be given a parent");
358   xbt_assert(parent, "The parent category of %s is NULL", cat->name);
359
360   /* if the category is initialized, unlink from current parent */
361   if (cat->initialized) {
362     xbt_log_category_t *cpp = &cat->parent->firstChild;
363
364     while (*cpp != cat && *cpp != NULL) {
365       cpp = &(*cpp)->nextSibling;
366     }
367
368     xbt_assert(*cpp == cat);
369     *cpp = cat->nextSibling;
370   }
371
372   cat->parent = parent;
373   cat->nextSibling = parent->firstChild;
374
375   parent->firstChild = cat;
376
377   if (!parent->initialized)
378     _xbt_log_cat_init(parent, xbt_log_priority_uninitialized /* ignored */ );
379
380   cat->threshold = parent->threshold;
381
382   cat->isThreshInherited = 1;
383 }
384
385 static void _set_inherited_thresholds(xbt_log_category_t cat)
386 {
387   xbt_log_category_t child = cat->firstChild;
388
389   for (; child != NULL; child = child->nextSibling) {
390     if (child->isThreshInherited) {
391       if (cat != &_XBT_LOGV(log))
392         XBT_VERB("Set category threshold of %s to %s (=%d)",
393               child->name, xbt_log_priority_names[cat->threshold], cat->threshold);
394       child->threshold = cat->threshold;
395       _set_inherited_thresholds(child);
396     }
397   }
398 }
399
400 void xbt_log_threshold_set(xbt_log_category_t cat, e_xbt_log_priority_t threshold)
401 {
402   cat->threshold = threshold;
403   cat->isThreshInherited = 0;
404
405   _set_inherited_thresholds(cat);
406 }
407
408 static xbt_log_setting_t _xbt_log_parse_setting(const char *control_string)
409 {
410   const char *orig_control_string = control_string;
411   xbt_log_setting_t set = xbt_new(s_xbt_log_setting_t, 1);
412
413   set->catname = NULL;
414   set->thresh = xbt_log_priority_uninitialized;
415   set->fmt = NULL;
416   set->additivity = -1;
417   set->appender = NULL;
418
419   if (!*control_string)
420     return set;
421   XBT_DEBUG("Parse log setting '%s'", control_string);
422
423   control_string += strspn(control_string, " ");
424   const char *name = control_string;
425   control_string += strcspn(control_string, ".:= ");
426   const char *dot = control_string;
427   control_string += strcspn(control_string, ":= ");
428   const char *eq = control_string;
429
430   xbt_assert(*dot == '.' || (*eq != '=' && *eq != ':'), "Invalid control string '%s'", orig_control_string);
431
432   if (!strncmp(dot + 1, "threshold", (size_t) (eq - dot - 1))) {
433     int i;
434     char *neweq = xbt_strdup(eq + 1);
435     char *p = neweq - 1;
436
437     while (*(++p) != '\0') {
438       if (*p >= 'a' && *p <= 'z') {
439         *p -= 'a' - 'A';
440       }
441     }
442
443     XBT_DEBUG("New priority name = %s", neweq);
444     for (i = 0; i < xbt_log_priority_infinite; i++) {
445       if (!strncmp(xbt_log_priority_names[i], neweq, p - eq)) {
446         XBT_DEBUG("This is priority %d", i);
447         break;
448       }
449     }
450
451     if(i<XBT_LOG_STATIC_THRESHOLD){
452      fprintf(stderr,
453          "Priority '%s' (in setting '%s') is above allowed priority '%s'.\n\n"
454          "Compiling SimGrid with -DNDEBUG forbids the levels 'trace' and 'debug'\n"
455          "while -DNLOG forbids any logging, at any level.",
456              eq + 1, name, xbt_log_priority_names[XBT_LOG_STATIC_THRESHOLD]);
457      exit(1);
458     }else if (i < xbt_log_priority_infinite) {
459       set->thresh = (e_xbt_log_priority_t) i;
460     } else {
461       THROWF(arg_error, 0,
462              "Unknown priority name: %s (must be one of: trace,debug,verbose,info,warning,error,critical)", eq + 1);
463     }
464     free(neweq);
465   } else if (!strncmp(dot + 1, "add", (size_t) (eq - dot - 1)) ||
466              !strncmp(dot + 1, "additivity", (size_t) (eq - dot - 1))) {
467     char *neweq = xbt_strdup(eq + 1);
468     char *p = neweq - 1;
469
470     while (*(++p) != '\0') {
471       if (*p >= 'a' && *p <= 'z') {
472         *p -= 'a' - 'A';
473       }
474     }
475     if (!strcmp(neweq, "ON") || !strcmp(neweq, "YES") || !strcmp(neweq, "1")) {
476       set->additivity = 1;
477     } else {
478       set->additivity = 0;
479     }
480     free(neweq);
481   } else if (!strncmp(dot + 1, "app", (size_t) (eq - dot - 1)) ||
482              !strncmp(dot + 1, "appender", (size_t) (eq - dot - 1))) {
483     char *neweq = xbt_strdup(eq + 1);
484
485     if (!strncmp(neweq, "file:", 5)) {
486       set->appender = xbt_log_appender_file_new(neweq + 5);
487     }else if (!strncmp(neweq, "rollfile:", 9)) {
488       set->appender = xbt_log_appender2_file_new(neweq + 9,1);
489     }else if (!strncmp(neweq, "splitfile:", 10)) {
490       set->appender = xbt_log_appender2_file_new(neweq + 10,0);
491     } else {
492       THROWF(arg_error, 0, "Unknown appender log type: '%s'", neweq);
493     }
494     free(neweq);
495   } else if (!strncmp(dot + 1, "fmt", (size_t) (eq - dot - 1))) {
496     set->fmt = xbt_strdup(eq + 1);
497   } else {
498     char buff[512];
499     snprintf(buff, MIN(512, eq - dot), "%s", dot + 1);
500     xbt_die("Unknown setting of the log category: '%s'", buff);
501   }
502   set->catname = (char *) xbt_malloc(dot - name + 1);
503
504   memcpy(set->catname, name, dot - name);
505   set->catname[dot - name] = '\0';      /* Just in case */
506   XBT_DEBUG("This is for cat '%s'", set->catname);
507
508   return set;
509 }
510
511 static xbt_log_category_t _xbt_log_cat_searchsub(xbt_log_category_t cat, char *name)
512 {
513   xbt_log_category_t child;
514   xbt_log_category_t res;
515
516   XBT_DEBUG("Search '%s' into '%s' (firstChild='%s'; nextSibling='%s')", name,
517          cat->name, (cat->firstChild ? cat->firstChild->name : "none"),
518          (cat->nextSibling ? cat->nextSibling->name : "none"));
519   if (!strcmp(cat->name, name))
520     return cat;
521
522   for (child = cat->firstChild; child != NULL; child = child->nextSibling) {
523     XBT_DEBUG("Dig into %s", child->name);
524     res = _xbt_log_cat_searchsub(child, name);
525     if (res)
526       return res;
527   }
528
529   return NULL;
530 }
531
532 /**
533  * \ingroup XBT_log
534  * \param control_string What to parse
535  *
536  * Typically passed a command-line argument. The string has the syntax:
537  *
538  *      ( [category] "." [keyword] ":" value (" ")... )...
539  *
540  * where [category] is one the category names (see \ref XBT_log_cats for a complete list of the ones defined in the
541  * SimGrid library) and keyword is one of the following:
542  *
543  *    - thres: category's threshold priority. Possible values:
544  *             TRACE,DEBUG,VERBOSE,INFO,WARNING,ERROR,CRITICAL
545  *    - add or additivity: whether the logging actions must be passed to the parent category.
546  *      Possible values: 0, 1, no, yes, on, off.
547  *      Default value: yes.
548  *    - fmt: the format to use. See \ref log_use_conf_fmt for more information.
549  *    - app or appender: the appender to use. See \ref log_use_conf_app for more information.
550  */
551 void xbt_log_control_set(const char *control_string)
552 {
553   xbt_log_setting_t set;
554
555   /* To split the string in commands, and the cursors */
556   xbt_dynar_t set_strings;
557   char *str;
558   unsigned int cpt;
559
560   if (!control_string)
561     return;
562   XBT_DEBUG("Parse log settings '%s'", control_string);
563
564   /* Special handling of no_loc request, which asks for any file localization to be omitted (for tesh runs) */
565   if (!strcmp(control_string, "no_loc")) {
566     xbt_log_no_loc = 1;
567     return;
568   }
569   /* some initialization if this is the first time that this get called */
570   if (xbt_log_settings == NULL)
571     xbt_log_settings = xbt_dynar_new(sizeof(xbt_log_setting_t), _free_setting);
572
573   /* split the string, and remove empty entries */
574   set_strings = xbt_str_split_quoted(control_string);
575
576   if (xbt_dynar_is_empty(set_strings)) {     /* vicious user! */
577     xbt_dynar_free(&set_strings);
578     return;
579   }
580
581   /* Parse each entry and either use it right now (if the category was already created), or store it for further use */
582   xbt_dynar_foreach(set_strings, cpt, str) {
583     set = _xbt_log_parse_setting(str);
584     xbt_log_category_t cat = _xbt_log_cat_searchsub(&_XBT_LOGV(XBT_LOG_ROOT_CAT), set->catname);
585
586     if (cat) {
587       XBT_DEBUG("Apply directly");
588       _xbt_log_cat_apply_set(cat, set);
589       _free_setting((void *) &set);
590     } else {
591       XBT_DEBUG("Store for further application");
592       XBT_DEBUG("push %p to the settings", (void *) set);
593       xbt_dynar_push(xbt_log_settings, &set);
594     }
595   }
596   xbt_dynar_free(&set_strings);
597 }
598
599 void xbt_log_appender_set(xbt_log_category_t cat, xbt_log_appender_t app)
600 {
601   if (cat->appender) {
602     if (cat->appender->free_)
603       cat->appender->free_(cat->appender);
604     free(cat->appender);
605   }
606   cat->appender = app;
607 }
608
609 void xbt_log_layout_set(xbt_log_category_t cat, xbt_log_layout_t lay)
610 {
611   DISABLE_XBT_LOG_CAT_INIT();
612   if (!cat->appender) {
613     XBT_VERB ("No appender to category %s. Setting the file appender as default", cat->name);
614     xbt_log_appender_set(cat, xbt_log_appender_file_new(NULL));
615   }
616   if (cat->layout) {
617     if (cat->layout->free_) {
618       cat->layout->free_(cat->layout);
619     }
620     free(cat->layout);
621   }
622   cat->layout = lay;
623   xbt_log_additivity_set(cat, 0);
624 }
625
626 void xbt_log_additivity_set(xbt_log_category_t cat, int additivity)
627 {
628   cat->additivity = additivity;
629 }
630
631 static void xbt_log_help(void)
632 {
633   printf("Description of the logging output:\n"
634          "\n"
635          "   Threshold configuration: --log=CATEGORY_NAME.thres:PRIORITY_LEVEL\n"
636          "      CATEGORY_NAME: defined in code with function 'XBT_LOG_NEW_CATEGORY'\n"
637          "      PRIORITY_LEVEL: the level to print (trace,debug,verbose,info,warning,error,critical)\n"
638          "         -> trace: enter and return of some functions\n"
639          "         -> debug: crufty output\n"
640          "         -> verbose: verbose output for the user wanting more\n"
641          "         -> info: output about the regular functioning\n"
642          "         -> warning: minor issue encountered\n"
643          "         -> error: issue encountered\n"
644          "         -> critical: major issue encountered\n"
645          "      The default priority level is 'info'.\n"
646          "\n"
647          "   Format configuration: --log=CATEGORY_NAME.fmt:FORMAT\n"
648          "      FORMAT string may contain:\n"
649          "         -> %%%%: the %% char\n"
650          "         -> %%n: platform-dependent line separator (LOG4J compatible)\n"
651          "         -> %%e: plain old space (SimGrid extension)\n"
652          "\n"
653          "         -> %%m: user-provided message\n"
654          "\n"
655          "         -> %%c: Category name (LOG4J compatible)\n"
656          "         -> %%p: Priority name (LOG4J compatible)\n"
657          "\n"
658          "         -> %%h: Hostname (SimGrid extension)\n"
659          "         -> %%P: Process name (SimGrid extension)\n"
660          "         -> %%t: Thread \"name\" (LOG4J compatible -- actually the address of the thread in memory)\n"
661          "         -> %%i: Process PID (SimGrid extension -- this is a 'i' as in 'i'dea)\n"
662          "\n"
663          "         -> %%F: file name where the log event was raised (LOG4J compatible)\n"
664          "         -> %%l: location where the log event was raised (LOG4J compatible, like '%%F:%%L' -- this is a l as "
665          "in 'l'etter)\n"
666          "         -> %%L: line number where the log event was raised (LOG4J compatible)\n"
667          "         -> %%M: function name (LOG4J compatible -- called method name here of course).\n"
668          "                 Defined only when using gcc because there is no __func__ elsewhere.\n"
669          "\n"
670          "         -> %%b: full backtrace (Called %%throwable in LOG4J). Defined only under windows or when using the "
671          "GNU libc because\n"
672          "                 backtrace() is not defined elsewhere, and we only have a fallback for windows boxes, not "
673          "mac ones for example.\n"
674          "         -> %%B: short backtrace (only the first line of the %%b). Called %%throwable{short} in LOG4J; "
675          "defined where %%b is.\n"
676          "\n"
677          "         -> %%d: date (UNIX-like epoch)\n"
678          "         -> %%r: application age (time elapsed since the beginning of the application)\n"
679          "\n"
680          "   Miscellaneous:\n"
681          "      --help-log-categories    Display the current hierarchy of log categories.\n"
682          "      --log=no_loc             Don't print file names in messages (for tesh tests).\n"
683          "\n");
684 }
685
686 static int xbt_log_cat_cmp(const void *pa, const void *pb)
687 {
688   xbt_log_category_t a = *(xbt_log_category_t *)pa;
689   xbt_log_category_t b = *(xbt_log_category_t *)pb;
690   return strcmp(a->name, b->name);
691 }
692
693 static void xbt_log_help_categories_rec(xbt_log_category_t category, const char *prefix)
694 {
695   char *this_prefix;
696   char *child_prefix;
697   unsigned i;
698   xbt_log_category_t cat;
699
700   if (!category)
701     return;
702
703   if (category->parent) {
704     this_prefix = bprintf("%s \\_ ", prefix);
705     child_prefix = bprintf("%s |  ", prefix);
706   } else {
707     this_prefix = xbt_strdup(prefix);
708     child_prefix = xbt_strdup(prefix);
709   }
710
711   xbt_dynar_t dynar = xbt_dynar_new(sizeof(xbt_log_category_t), NULL);
712   for (cat = category ; cat != NULL; cat = cat->nextSibling)
713     xbt_dynar_push_as(dynar, xbt_log_category_t, cat);
714
715   xbt_dynar_sort(dynar, xbt_log_cat_cmp);
716
717   xbt_dynar_foreach(dynar, i, cat){
718     if (i == xbt_dynar_length(dynar) - 1 && category->parent)
719       *strrchr(child_prefix, '|') = ' ';
720     printf("%s%s: %s\n", this_prefix, cat->name, cat->description);
721     xbt_log_help_categories_rec(cat->firstChild, child_prefix);
722   }
723
724   xbt_dynar_free(&dynar);
725   xbt_free(this_prefix);
726   xbt_free(child_prefix);
727 }
728
729 static void xbt_log_help_categories(void)
730 {
731   printf("Current log category hierarchy:\n");
732   xbt_log_help_categories_rec(&_XBT_LOGV(XBT_LOG_ROOT_CAT), "   ");
733   printf("\n");
734 }