Logo AND Algorithmique Numérique Distribuée

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