Logo AND Algorithmique Numérique Distribuée

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