Logo AND Algorithmique Numérique Distribuée

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