Logo AND Algorithmique Numérique Distribuée

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