Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines with new year.
[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 <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(const s_xbt_log_category_t* cat)
127 {
128   if (cat->appender) {
129     if (cat->appender->free_)
130       cat->appender->free_(cat->appender);
131     xbt_free(cat->appender);
132   }
133   if (cat->layout) {
134     if (cat->layout->free_)
135       cat->layout->free_(cat->layout);
136     xbt_free(cat->layout);
137   }
138
139   for (auto const* child = cat->firstChild; child != nullptr; child = child->nextSibling)
140     log_cat_exit(child);
141 }
142
143 void xbt_log_postexit(void)
144 {
145   XBT_VERB("Exiting log");
146   delete log_cat_init_mutex;
147   log_cat_exit(&_XBT_LOGV(XBT_LOG_ROOT_CAT));
148 }
149
150 /* Size of the static string in which we build the log string */
151 static constexpr size_t XBT_LOG_STATIC_BUFFER_SIZE = 2048;
152 /* Minimum size of the dynamic string in which we build the log string
153    (should be greater than XBT_LOG_STATIC_BUFFER_SIZE) */
154 static constexpr size_t XBT_LOG_DYNAMIC_BUFFER_SIZE = 4096;
155
156 void _xbt_log_event_log(xbt_log_event_t ev, const char *fmt, ...)
157 {
158   const xbt_log_category_s* cat = ev->cat;
159
160   xbt_assert(ev->priority >= 0, "Negative logging priority naturally forbidden");
161   xbt_assert(static_cast<size_t>(ev->priority) < sizeof(xbt_log_priority_names)/sizeof(xbt_log_priority_names[0]),
162              "Priority %d is greater than the biggest allowed value", ev->priority);
163
164   while (1) {
165     const s_xbt_log_appender_t* appender = cat->appender;
166
167     if (appender != nullptr) {
168       xbt_assert(cat->layout, "No valid layout for the appender of category %s", cat->name);
169
170       /* First, try with a static buffer */
171       int done = 0;
172       char buff[XBT_LOG_STATIC_BUFFER_SIZE];
173       ev->buffer      = buff;
174       ev->buffer_size = sizeof buff;
175       va_start(ev->ap, fmt);
176       done = cat->layout->do_layout(cat->layout, ev, fmt);
177       va_end(ev->ap);
178       if (done) {
179         appender->do_append(appender, buff);
180       } else {
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       const xbt_log_category_s* 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(const s_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* option = control_string;
377   control_string += strcspn(control_string, ":= ");
378   const char* value = control_string;
379
380   xbt_assert(*option == '.' && (*value == '=' || *value == ':'), "Invalid control string '%s'", orig_control_string);
381
382   size_t name_len = option - name;
383   ++option;
384   size_t option_len = value - option;
385   ++value;
386
387   if (strncmp(option, "threshold", option_len) == 0) {
388     XBT_DEBUG("New priority name = %s", value);
389     int i;
390     for (i = 0; i < xbt_log_priority_infinite; i++) {
391       if (strcasecmp(value, xbt_log_priority_names[i]) == 0) {
392         XBT_DEBUG("This is priority %d", i);
393         break;
394       }
395     }
396
397     if(i<XBT_LOG_STATIC_THRESHOLD){
398       fprintf(stderr, "Priority '%s' (in setting '%s') is above allowed priority '%s'.\n\n"
399                       "Compiling SimGrid with -DNDEBUG forbids the levels 'trace' and 'debug'\n"
400                       "while -DNLOG forbids any logging, at any level.",
401               value, name, xbt_log_priority_names[XBT_LOG_STATIC_THRESHOLD]);
402       exit(1);
403     }else if (i < xbt_log_priority_infinite) {
404       set.thresh = (e_xbt_log_priority_t)i;
405     } else {
406       throw std::invalid_argument(simgrid::xbt::string_printf(
407           "Unknown priority name: %s (must be one of: trace,debug,verbose,info,warning,error,critical)", value));
408     }
409   } else if (strncmp(option, "additivity", option_len) == 0) {
410     set.additivity = (strcasecmp(value, "ON") == 0 || strcasecmp(value, "YES") == 0 || strcmp(value, "1") == 0);
411   } else if (strncmp(option, "appender", option_len) == 0) {
412     if (strncmp(value, "file:", 5) == 0) {
413       set.appender = xbt_log_appender_file_new(value + 5);
414     } else if (strncmp(value, "rollfile:", 9) == 0) {
415       set.appender = xbt_log_appender2_file_new(value + 9, 1);
416     } else if (strncmp(value, "splitfile:", 10) == 0) {
417       set.appender = xbt_log_appender2_file_new(value + 10, 0);
418     } else if (strcmp(value, "stderr") == 0) {
419       set.appender = xbt_log_appender_stream(stderr);
420     } else if (strcmp(value, "stdout") == 0) {
421       set.appender = xbt_log_appender_stream(stdout);
422     } else {
423       throw std::invalid_argument(simgrid::xbt::string_printf("Unknown appender log type: '%s'", value));
424     }
425   } else if (strncmp(option, "fmt", option_len) == 0) {
426     set.fmt = std::string(value);
427   } else {
428     xbt_die("Unknown setting of the log category: '%.*s'", static_cast<int>(option_len), option);
429   }
430   set.catname = std::string(name, name_len);
431
432   XBT_DEBUG("This is for cat '%s'", set.catname.c_str());
433
434   return set;
435 }
436
437 static xbt_log_category_t _xbt_log_cat_searchsub(xbt_log_category_t cat, const char* name)
438 {
439   xbt_log_category_t child;
440   xbt_log_category_t res;
441
442   XBT_DEBUG("Search '%s' into '%s' (firstChild='%s'; nextSibling='%s')", name,
443          cat->name, (cat->firstChild ? cat->firstChild->name : "none"),
444          (cat->nextSibling ? cat->nextSibling->name : "none"));
445   if (strcmp(cat->name, name) == 0)
446     return cat;
447
448   for (child = cat->firstChild; child != nullptr; child = child->nextSibling) {
449     XBT_DEBUG("Dig into %s", child->name);
450     res = _xbt_log_cat_searchsub(child, name);
451     if (res)
452       return res;
453   }
454
455   return nullptr;
456 }
457
458 /**
459  * @ingroup XBT_log
460  * @param control_string What to parse
461  *
462  * Typically passed a command-line argument. The string has the syntax:
463  *
464  *      ( [category] "." [keyword] ":" value (" ")... )...
465  *
466  * where [category] is one the category names (see @ref XBT_log_cats for a complete list of the ones defined in the
467  * SimGrid library) and keyword is one of the following:
468  *
469  *    - thres: category's threshold priority. Possible values:
470  *             TRACE,DEBUG,VERBOSE,INFO,WARNING,ERROR,CRITICAL
471  *    - add or additivity: whether the logging actions must be passed to the parent category.
472  *      Possible values: 0, 1, no, yes, on, off.
473  *      Default value: yes.
474  *    - fmt: the format to use. See @ref log_use_conf_fmt for more information.
475  *    - app or appender: the appender to use. See @ref log_use_conf_app for more information.
476  */
477 void xbt_log_control_set(const char *control_string)
478 {
479   /* To split the string in commands, and the cursors */
480   xbt_dynar_t set_strings;
481   char *str;
482   unsigned int cpt;
483
484   if (!control_string)
485     return;
486   XBT_DEBUG("Parse log settings '%s'", control_string);
487
488   /* Special handling of no_loc request, which asks for any file localization to be omitted (for tesh runs) */
489   if (strcmp(control_string, "no_loc") == 0) {
490     xbt_log_no_loc = 1;
491     return;
492   }
493   /* split the string, and remove empty entries */
494   set_strings = xbt_str_split_quoted(control_string);
495
496   if (xbt_dynar_is_empty(set_strings)) {     /* vicious user! */
497     xbt_dynar_free(&set_strings);
498     return;
499   }
500
501   /* Parse each entry and either use it right now (if the category was already created), or store it for further use */
502   xbt_dynar_foreach(set_strings, cpt, str) {
503     xbt_log_setting_t set  = _xbt_log_parse_setting(str);
504     xbt_log_category_t cat = _xbt_log_cat_searchsub(&_XBT_LOGV(XBT_LOG_ROOT_CAT), set.catname.c_str());
505
506     if (cat) {
507       XBT_DEBUG("Apply directly");
508       _xbt_log_cat_apply_set(cat, set);
509     } else {
510       XBT_DEBUG("Store for further application");
511       XBT_DEBUG("push %p to the settings", &set);
512       xbt_log_settings.emplace_back(std::move(set));
513     }
514   }
515   xbt_dynar_free(&set_strings);
516 }
517
518 void xbt_log_appender_set(xbt_log_category_t cat, xbt_log_appender_t app)
519 {
520   if (cat->appender) {
521     if (cat->appender->free_)
522       cat->appender->free_(cat->appender);
523     xbt_free(cat->appender);
524   }
525   cat->appender = app;
526 }
527
528 void xbt_log_layout_set(xbt_log_category_t cat, xbt_log_layout_t lay)
529 {
530   DISABLE_XBT_LOG_CAT_INIT();
531   if (!cat->appender) {
532     XBT_VERB ("No appender to category %s. Setting the file appender as default", cat->name);
533     xbt_log_appender_set(cat, xbt_log_appender_file_new(nullptr));
534   }
535   if (cat->layout) {
536     if (cat->layout->free_) {
537       cat->layout->free_(cat->layout);
538     }
539     xbt_free(cat->layout);
540   }
541   cat->layout = lay;
542   xbt_log_additivity_set(cat, 0);
543 }
544
545 void xbt_log_additivity_set(xbt_log_category_t cat, int additivity)
546 {
547   cat->additivity = additivity;
548 }
549
550 static void xbt_log_help(void)
551 {
552   XBT_HELP(
553       "Description of the logging output:\n"
554       "\n"
555       "   Threshold configuration: --log=CATEGORY_NAME.thres:PRIORITY_LEVEL\n"
556       "      CATEGORY_NAME: defined in code with function 'XBT_LOG_NEW_CATEGORY'\n"
557       "      PRIORITY_LEVEL: the level to print (trace,debug,verbose,info,warning,error,critical)\n"
558       "         -> trace: enter and return of some functions\n"
559       "         -> debug: crufty output\n"
560       "         -> verbose: verbose output for the user wanting more\n"
561       "         -> info: output about the regular functioning\n"
562       "         -> warning: minor issue encountered\n"
563       "         -> error: issue encountered\n"
564       "         -> critical: major issue encountered\n"
565       "      The default priority level is 'info'.\n"
566       "\n"
567       "   Format configuration: --log=CATEGORY_NAME.fmt:FORMAT\n"
568       "      FORMAT string may contain:\n"
569       "         -> %%%%: the %% char\n"
570       "         -> %%n: platform-dependent line separator (LOG4J compatible)\n"
571       "         -> %%e: plain old space (SimGrid extension)\n"
572       "\n"
573       "         -> %%m: user-provided message\n"
574       "\n"
575       "         -> %%c: Category name (LOG4J compatible)\n"
576       "         -> %%p: Priority name (LOG4J compatible)\n"
577       "\n"
578       "         -> %%h: Hostname (SimGrid extension)\n"
579       "         -> %%P: Process name (SimGrid extension)\n"
580       "         -> %%t: Thread \"name\" (LOG4J compatible -- actually the address of the thread in memory)\n"
581       "         -> %%i: Process PID (SimGrid extension -- this is a 'i' as in 'i'dea)\n"
582       "\n"
583       "         -> %%F: file name where the log event was raised (LOG4J compatible)\n"
584       "         -> %%l: location where the log event was raised (LOG4J compatible, like '%%F:%%L' -- this is a l as "
585       "in 'l'etter)\n"
586       "         -> %%L: line number where the log event was raised (LOG4J compatible)\n"
587       "         -> %%M: function name (LOG4J compatible -- called method name here of course).\n"
588       "\n"
589       "         -> %%b: full backtrace (Called %%throwable in LOG4J). Defined only under windows or when using the "
590       "GNU libc because\n"
591       "                 backtrace() is not defined elsewhere, and we only have a fallback for windows boxes, not "
592       "mac ones for example.\n"
593       "         -> %%B: short backtrace (only the first line of the %%b). Called %%throwable{short} in LOG4J; "
594       "defined where %%b is.\n"
595       "\n"
596       "         -> %%d: date (UNIX-like epoch)\n"
597       "         -> %%r: application age (time elapsed since the beginning of the application)\n"
598       "\n"
599       "   Category appender: --log=CATEGORY_NAME.app:APPENDER\n"
600       "      APPENDER may be:\n"
601       "         -> stdout or stderr: standard output streams\n"
602       "         -> file:NAME: append to file with given name\n"
603       "         -> splitfile:SIZE:NAME: append to files with maximum size SIZE per file.\n"
604       "                                 NAME may contain the %% wildcard as a placeholder for the file number.\n"
605       "         -> rollfile:SIZE:NAME: append to file with maximum size SIZE.\n"
606       "\n"
607       "   Category additivity: --log=CATEGORY_NAME.add:VALUE\n"
608       "      VALUE:  '0', '1', 'no', 'yes', 'on', or 'off'\n"
609       "\n"
610       "   Miscellaneous:\n"
611       "      --help-log-categories    Display the current hierarchy of log categories.\n"
612       "      --log=no_loc             Don't print file names in messages (for tesh tests).\n");
613 }
614
615 static void xbt_log_help_categories_rec(xbt_log_category_t category, const std::string& prefix)
616 {
617   if (!category)
618     return;
619
620   std::string this_prefix(prefix);
621   std::string child_prefix(prefix);
622   if (category->parent) {
623     this_prefix  += " \\_ ";
624     child_prefix += " |  ";
625   }
626
627   std::vector<xbt_log_category_t> cats;
628   for (xbt_log_category_t cat = category; cat != nullptr; cat = cat->nextSibling)
629     cats.push_back(cat);
630
631   std::sort(begin(cats), end(cats),
632             [](const s_xbt_log_category_t* a, const s_xbt_log_category_t* b) { return strcmp(a->name, b->name) < 0; });
633
634   for (auto const& cat : cats) {
635     XBT_HELP("%s%s: %s", this_prefix.c_str(), cat->name, cat->description);
636     if (cat == cats.back() && category->parent)
637       child_prefix[child_prefix.rfind('|')] = ' ';
638     xbt_log_help_categories_rec(cat->firstChild, child_prefix);
639   }
640 }
641
642 static void xbt_log_help_categories(void)
643 {
644   XBT_HELP("Current log category hierarchy:");
645   xbt_log_help_categories_rec(&_XBT_LOGV(XBT_LOG_ROOT_CAT), "   ");
646   XBT_HELP("%s", "");
647 }