Logo AND Algorithmique Numérique Distribuée

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