Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Assert that the log categories are unique
[simgrid.git] / src / xbt / log.cpp
1 /* log - a generic logging facility in the spirit of log4j                  */
2
3 /* Copyright (c) 2004-2021. 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 <unordered_set>
20 #include <vector>
21
22 int xbt_log_no_loc = 0; /* if set to true (with --log=no_loc), file localization will be omitted (for tesh tests) */
23 static std::recursive_mutex* log_cat_init_mutex = nullptr;
24
25 /** @addtogroup XBT_log
26  *
27  *  For more information, please refer to @ref outcomes_logs Section.
28  */
29
30 xbt_log_appender_t xbt_log_default_appender = nullptr; /* set in log_init */
31 xbt_log_layout_t xbt_log_default_layout     = nullptr; /* set in log_init */
32
33 struct xbt_log_setting_t {
34   std::string catname;
35   std::string fmt;
36   e_xbt_log_priority_t thresh = xbt_log_priority_uninitialized;
37   int additivity              = -1;
38   xbt_log_appender_t appender = nullptr;
39 };
40
41 // This function is here to avoid static initialization order fiasco
42 static std::vector<xbt_log_setting_t>& xbt_log_settings()
43 {
44   static std::vector<xbt_log_setting_t> value;
45   return value;
46 }
47
48 constexpr std::array<const char*, 8> xbt_log_priority_names{
49     {"NONE", "TRACE", "DEBUG", "VERBOSE", "INFO", "WARNING", "ERROR", "CRITICAL"}};
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_stream(stderr);
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();
79 static void xbt_log_help_categories();
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   xbt_log_control_set("xbt_help.app:stdout xbt_help.threshold:VERBOSE xbt_help.fmt:%m%n");
92
93   /* Set logs and init log submodule */
94   for (int i = 1; i < *argc; i++) {
95     if (strcmp("--", argv[i]) == 0) {
96       parse_args = 0;
97       argv[j++]  = argv[i]; // Keep the '--' for sg_config
98     } else if (parse_args && strncmp(argv[i], "--log=", strlen("--log=")) == 0) {
99       char* opt = strchr(argv[i], '=');
100       opt++;
101       xbt_log_control_set(opt);
102       XBT_DEBUG("Did apply '%s' as log setting", opt);
103     } else if (parse_args && strcmp(argv[i], "--help-logs") == 0) {
104       help_requested |= 1U;
105     } else if (parse_args && strcmp(argv[i], "--help-log-categories") == 0) {
106       help_requested |= 2U;
107     } else {
108       argv[j++] = argv[i];
109     }
110   }
111   if (j < *argc) {
112     argv[j] = nullptr;
113     *argc = j;
114   }
115
116   if (help_requested) {
117     if (help_requested & 1)
118       xbt_log_help();
119     if (help_requested & 2)
120       xbt_log_help_categories();
121     exit(0);
122   }
123 }
124
125 static void log_cat_exit(const s_xbt_log_category_t* cat)
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 (auto const* 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   const xbt_log_category_s* cat = ev->cat;
158
159   xbt_assert(ev->priority >= 0, "Negative logging priority naturally forbidden");
160   xbt_assert(static_cast<size_t>(ev->priority) < xbt_log_priority_names.size(),
161              "Priority %d is greater than the biggest allowed value", ev->priority);
162
163   while (true) {
164     const s_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       bool done = false;
171       std::array<char, XBT_LOG_STATIC_BUFFER_SIZE> buff;
172       ev->buffer      = buff.data();
173       ev->buffer_size = buff.size();
174       va_start(ev->ap, fmt);
175       done = cat->layout->do_layout(cat->layout, ev, fmt);
176       va_end(ev->ap);
177       ev->buffer = nullptr; // Calm down, static analyzers, this pointer to local array won't leak out of the scope.
178       if (done) {
179         appender->do_append(appender, buff.data());
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 (true) {
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 /** Asserts that the provided name is unique */
252 void _xbt_log_is_name_unique(const char* name, const char* file, int line)
253 {
254   static std::unordered_set<std::string> used_names;
255
256   if (used_names.find(name) != used_names.end())
257     XBT_WARN("%s:%d: log category redefined: %s", file, line, name);
258   used_names.insert(std::string(name));
259 }
260
261 /*
262  * This gets called the first time a category is referenced and performs the initialization.
263  * Also resets threshold to inherited!
264  */
265 int _xbt_log_cat_init(xbt_log_category_t category, e_xbt_log_priority_t priority)
266 {
267   DISABLE_XBT_LOG_CAT_INIT();
268
269   if (category->initialized)
270     return priority >= category->threshold;
271
272   if (log_cat_init_mutex != nullptr)
273     log_cat_init_mutex->lock();
274
275   XBT_DEBUG("Initializing category '%s' (firstChild=%s, nextSibling=%s)", category->name,
276          (category->firstChild ? category->firstChild->name : "none"),
277          (category->nextSibling ? category->nextSibling->name : "none"));
278
279   if (category == &_XBT_LOGV(XBT_LOG_ROOT_CAT)) {
280     category->threshold = xbt_log_priority_info;
281     category->appender = xbt_log_default_appender;
282     category->layout = xbt_log_default_layout;
283   } else {
284     if (!category->parent)
285       category->parent = &_XBT_LOGV(XBT_LOG_ROOT_CAT);
286
287     XBT_DEBUG("Set %s (%s) as father of %s ", category->parent->name,
288            (category->parent->initialized ? xbt_log_priority_names[category->parent->threshold] : "uninited"),
289            category->name);
290     xbt_log_parent_set(category, category->parent);
291
292     if (XBT_LOG_ISENABLED(log, xbt_log_priority_debug)) {
293       std::string res;
294       const xbt_log_category_s* cpp = category->parent->firstChild;
295       while (cpp) {
296         res += std::string(" ") + cpp->name;
297         cpp = cpp->nextSibling;
298       }
299
300       XBT_DEBUG("Children of %s:%s; nextSibling: %s", category->parent->name, res.c_str(),
301                 (category->parent->nextSibling ? category->parent->nextSibling->name : "none"));
302     }
303   }
304
305   /* Apply the control */
306   auto iset = std::find_if(begin(xbt_log_settings()), end(xbt_log_settings()),
307                            [category](const xbt_log_setting_t& s) { return s.catname == category->name; });
308   if (iset != xbt_log_settings().end()) {
309     _xbt_log_cat_apply_set(category, *iset);
310     xbt_log_settings().erase(iset);
311   } else {
312     XBT_DEBUG("Category '%s': inherited threshold = %s (=%d)", category->name,
313               xbt_log_priority_names[category->threshold], category->threshold);
314   }
315
316   category->initialized = 1;
317   if (log_cat_init_mutex != nullptr)
318     log_cat_init_mutex->unlock();
319   return priority >= category->threshold;
320 }
321
322 void xbt_log_parent_set(xbt_log_category_t cat, xbt_log_category_t parent)
323 {
324   xbt_assert(cat, "NULL category to be given a parent");
325   xbt_assert(parent, "The parent category of %s is NULL", cat->name);
326
327   /* if the category is initialized, unlink from current parent */
328   if (cat->initialized) {
329     xbt_log_category_t *cpp = &cat->parent->firstChild;
330
331     while (*cpp != cat && *cpp != nullptr) {
332       cpp = &(*cpp)->nextSibling;
333     }
334
335     xbt_assert(*cpp == cat);
336     *cpp = cat->nextSibling;
337   }
338
339   cat->parent = parent;
340   cat->nextSibling = parent->firstChild;
341
342   parent->firstChild = cat;
343
344   if (!parent->initialized)
345     _xbt_log_cat_init(parent, xbt_log_priority_uninitialized /* ignored */ );
346
347   cat->threshold = parent->threshold;
348
349   cat->isThreshInherited = 1;
350 }
351
352 static void _set_inherited_thresholds(const s_xbt_log_category_t* cat)
353 {
354   xbt_log_category_t child = cat->firstChild;
355
356   for (; child != nullptr; child = child->nextSibling) {
357     if (child->isThreshInherited) {
358       if (cat != &_XBT_LOGV(log))
359         XBT_VERB("Set category threshold of %s to %s (=%d)",
360               child->name, xbt_log_priority_names[cat->threshold], cat->threshold);
361       child->threshold = cat->threshold;
362       _set_inherited_thresholds(child);
363     }
364   }
365 }
366
367 void xbt_log_threshold_set(xbt_log_category_t cat, e_xbt_log_priority_t threshold)
368 {
369   cat->threshold = threshold;
370   cat->isThreshInherited = 0;
371
372   _set_inherited_thresholds(cat);
373 }
374
375 static xbt_log_setting_t _xbt_log_parse_setting(const char *control_string)
376 {
377   const char *orig_control_string = control_string;
378   xbt_log_setting_t set;
379
380   if (!*control_string)
381     return set;
382   XBT_DEBUG("Parse log setting '%s'", control_string);
383
384   control_string += strspn(control_string, " ");
385   const char* name = control_string;
386   control_string += strcspn(control_string, ".:= ");
387   const char* option = control_string;
388   control_string += strcspn(control_string, ":= ");
389   const char* value = control_string;
390
391   xbt_assert(*option == '.' && (*value == '=' || *value == ':'), "Invalid control string '%s'", orig_control_string);
392
393   size_t name_len = option - name;
394   ++option;
395   size_t option_len = value - option;
396   ++value;
397
398   if (strncmp(option, "threshold", option_len) == 0) {
399     XBT_DEBUG("New priority name = %s", value);
400     int i;
401     for (i = 0; i < xbt_log_priority_infinite; i++) {
402       if (strcasecmp(value, xbt_log_priority_names[i]) == 0) {
403         XBT_DEBUG("This is priority %d", i);
404         break;
405       }
406     }
407
408     if(i<XBT_LOG_STATIC_THRESHOLD){
409       fprintf(stderr, "Priority '%s' (in setting '%s') is above allowed priority '%s'.\n\n"
410                       "Compiling SimGrid with -DNDEBUG forbids the levels 'trace' and 'debug'\n"
411                       "while -DNLOG forbids any logging, at any level.",
412               value, name, xbt_log_priority_names[XBT_LOG_STATIC_THRESHOLD]);
413       exit(1);
414     }else if (i < xbt_log_priority_infinite) {
415       set.thresh = (e_xbt_log_priority_t)i;
416     } else {
417       throw std::invalid_argument(simgrid::xbt::string_printf(
418           "Unknown priority name: %s (must be one of: trace,debug,verbose,info,warning,error,critical)", value));
419     }
420   } else if (strncmp(option, "additivity", option_len) == 0) {
421     set.additivity = (strcasecmp(value, "ON") == 0 || strcasecmp(value, "YES") == 0 || strcmp(value, "1") == 0);
422   } else if (strncmp(option, "appender", option_len) == 0) {
423     if (strncmp(value, "file:", 5) == 0) {
424       set.appender = xbt_log_appender_file_new(value + 5);
425     } else if (strncmp(value, "rollfile:", 9) == 0) {
426       set.appender = xbt_log_appender2_file_new(value + 9, 1);
427     } else if (strncmp(value, "splitfile:", 10) == 0) {
428       set.appender = xbt_log_appender2_file_new(value + 10, 0);
429     } else if (strcmp(value, "stderr") == 0) {
430       set.appender = xbt_log_appender_stream(stderr);
431     } else if (strcmp(value, "stdout") == 0) {
432       set.appender = xbt_log_appender_stream(stdout);
433     } else {
434       throw std::invalid_argument(simgrid::xbt::string_printf("Unknown appender log type: '%s'", value));
435     }
436   } else if (strncmp(option, "fmt", option_len) == 0) {
437     set.fmt = std::string(value);
438   } else {
439     xbt_die("Unknown setting of the log category: '%.*s'", static_cast<int>(option_len), option);
440   }
441   set.catname = std::string(name, name_len);
442
443   XBT_DEBUG("This is for cat '%s'", set.catname.c_str());
444
445   return set;
446 }
447
448 static xbt_log_category_t _xbt_log_cat_searchsub(xbt_log_category_t cat, const char* name)
449 {
450   XBT_DEBUG("Search '%s' into '%s' (firstChild='%s'; nextSibling='%s')", name,
451          cat->name, (cat->firstChild ? cat->firstChild->name : "none"),
452          (cat->nextSibling ? cat->nextSibling->name : "none"));
453   if (strcmp(cat->name, name) == 0)
454     return cat;
455
456   for (xbt_log_category_t child = cat->firstChild; child != nullptr; child = child->nextSibling) {
457     XBT_DEBUG("Dig into %s", child->name);
458     xbt_log_category_t res = _xbt_log_cat_searchsub(child, name);
459     if (res)
460       return res;
461   }
462
463   return nullptr;
464 }
465
466 /**
467  * @ingroup XBT_log
468  * @param control_string What to parse
469  *
470  * Typically passed a command-line argument. The string has the syntax:
471  *
472  *      ( [category] "." [keyword] ":" value (" ")... )...
473  *
474  * where [category] is one the category names (see @ref XBT_log_cats for a complete list of the ones defined in the
475  * SimGrid library) and keyword is one of the following:
476  *
477  *    - thres: category's threshold priority. Possible values:
478  *             TRACE,DEBUG,VERBOSE,INFO,WARNING,ERROR,CRITICAL
479  *    - add or additivity: whether the logging actions must be passed to the parent category.
480  *      Possible values: 0, 1, no, yes, on, off.
481  *      Default value: yes.
482  *    - fmt: the format to use. See @ref log_use_conf_fmt for more information.
483  *    - app or appender: the appender to use. See @ref log_use_conf_app for more information.
484  */
485 void xbt_log_control_set(const char *control_string)
486 {
487   /* To split the string in commands, and the cursors */
488   xbt_dynar_t set_strings;
489   char *str;
490   unsigned int cpt;
491
492   if (!control_string)
493     return;
494   XBT_DEBUG("Parse log settings '%s'", control_string);
495
496   /* Special handling of no_loc request, which asks for any file localization to be omitted (for tesh runs) */
497   if (strcmp(control_string, "no_loc") == 0) {
498     xbt_log_no_loc = 1;
499     return;
500   }
501   /* split the string, and remove empty entries */
502   set_strings = xbt_str_split_quoted(control_string);
503
504   if (xbt_dynar_is_empty(set_strings)) {     /* vicious user! */
505     xbt_dynar_free(&set_strings);
506     return;
507   }
508
509   /* Parse each entry and either use it right now (if the category was already created), or store it for further use */
510   xbt_dynar_foreach(set_strings, cpt, str) {
511     xbt_log_setting_t set  = _xbt_log_parse_setting(str);
512     xbt_log_category_t cat = _xbt_log_cat_searchsub(&_XBT_LOGV(XBT_LOG_ROOT_CAT), set.catname.c_str());
513
514     if (cat) {
515       XBT_DEBUG("Apply directly");
516       _xbt_log_cat_apply_set(cat, set);
517     } else {
518       XBT_DEBUG("Store for further application");
519       XBT_DEBUG("push %p to the settings", &set);
520       xbt_log_settings().emplace_back(std::move(set));
521     }
522   }
523   xbt_dynar_free(&set_strings);
524 }
525
526 void xbt_log_appender_set(xbt_log_category_t cat, xbt_log_appender_t app)
527 {
528   if (cat->appender) {
529     if (cat->appender->free_)
530       cat->appender->free_(cat->appender);
531     xbt_free(cat->appender);
532   }
533   cat->appender = app;
534 }
535
536 void xbt_log_layout_set(xbt_log_category_t cat, xbt_log_layout_t lay)
537 {
538   DISABLE_XBT_LOG_CAT_INIT();
539   if (!cat->appender) {
540     XBT_VERB ("No appender to category %s. Setting the file appender as default", cat->name);
541     xbt_log_appender_set(cat, xbt_log_appender_file_new(nullptr));
542   }
543   if (cat->layout) {
544     if (cat->layout->free_) {
545       cat->layout->free_(cat->layout);
546     }
547     xbt_free(cat->layout);
548   }
549   cat->layout = lay;
550   xbt_log_additivity_set(cat, 0);
551 }
552
553 void xbt_log_additivity_set(xbt_log_category_t cat, int additivity)
554 {
555   cat->additivity = additivity;
556 }
557
558 static void xbt_log_help()
559 {
560   XBT_HELP(
561       "Description of the logging output:\n"
562       "\n"
563       "   Threshold configuration: --log=CATEGORY_NAME.thres:PRIORITY_LEVEL\n"
564       "      CATEGORY_NAME: defined in code with function 'XBT_LOG_NEW_CATEGORY'\n"
565       "      PRIORITY_LEVEL: the level to print (trace,debug,verbose,info,warning,error,critical)\n"
566       "         -> trace: enter and return of some functions\n"
567       "         -> debug: crufty output\n"
568       "         -> verbose: verbose output for the user wanting more\n"
569       "         -> info: output about the regular functioning\n"
570       "         -> warning: minor issue encountered\n"
571       "         -> error: issue encountered\n"
572       "         -> critical: major issue encountered\n"
573       "      The default priority level is 'info'.\n"
574       "\n"
575       "   Format configuration: --log=CATEGORY_NAME.fmt:FORMAT\n"
576       "      FORMAT string may contain:\n"
577       "         -> %%%%: the %% char\n"
578       "         -> %%n: platform-dependent line separator (LOG4J compatible)\n"
579       "         -> %%e: plain old space (SimGrid extension)\n"
580       "\n"
581       "         -> %%m: user-provided message\n"
582       "\n"
583       "         -> %%c: Category name (LOG4J compatible)\n"
584       "         -> %%p: Priority name (LOG4J compatible)\n"
585       "\n"
586       "         -> %%h: Hostname (SimGrid extension)\n"
587       "         -> %%P: Process name (SimGrid extension)\n"
588       "         -> %%t: Thread \"name\" (LOG4J compatible -- actually the address of the thread in memory)\n"
589       "         -> %%i: Process PID (SimGrid extension -- this is a 'i' as in 'i'dea)\n"
590       "\n"
591       "         -> %%F: file name where the log event was raised (LOG4J compatible)\n"
592       "         -> %%l: location where the log event was raised (LOG4J compatible, like '%%F:%%L' -- this is a l as "
593       "in 'l'etter)\n"
594       "         -> %%L: line number where the log event was raised (LOG4J compatible)\n"
595       "         -> %%M: function name (LOG4J compatible -- called method name here of course).\n"
596       "\n"
597       "         -> %%b: full backtrace (Called %%throwable in LOG4J). Defined only under windows or when using the "
598       "GNU libc because\n"
599       "                 backtrace() is not defined elsewhere, and we only have a fallback for windows boxes, not "
600       "mac ones for example.\n"
601       "         -> %%B: short backtrace (only the first line of the %%b). Called %%throwable{short} in LOG4J; "
602       "defined where %%b is.\n"
603       "\n"
604       "         -> %%d: date (UNIX-like epoch)\n"
605       "         -> %%r: application age (time elapsed since the beginning of the application)\n"
606       "\n"
607       "   Category appender: --log=CATEGORY_NAME.app:APPENDER\n"
608       "      APPENDER may be:\n"
609       "         -> stdout or stderr: standard output streams\n"
610       "         -> file:NAME: append to file with given name\n"
611       "         -> splitfile:SIZE:NAME: append to files with maximum size SIZE per file.\n"
612       "                                 NAME may contain the %% wildcard as a placeholder for the file number.\n"
613       "         -> rollfile:SIZE:NAME: append to file with maximum size SIZE.\n"
614       "\n"
615       "   Category additivity: --log=CATEGORY_NAME.add:VALUE\n"
616       "      VALUE:  '0', '1', 'no', 'yes', 'on', or 'off'\n"
617       "\n"
618       "   Miscellaneous:\n"
619       "      --help-log-categories    Display the current hierarchy of log categories.\n"
620       "      --log=no_loc             Don't print file names in messages (for tesh tests).\n");
621 }
622
623 static void xbt_log_help_categories_rec(xbt_log_category_t category, const std::string& prefix)
624 {
625   if (!category)
626     return;
627
628   std::string this_prefix(prefix);
629   std::string child_prefix(prefix);
630   if (category->parent) {
631     this_prefix  += " \\_ ";
632     child_prefix += " |  ";
633   }
634
635   std::vector<xbt_log_category_t> cats;
636   for (xbt_log_category_t cat = category; cat != nullptr; cat = cat->nextSibling)
637     cats.push_back(cat);
638
639   std::sort(begin(cats), end(cats),
640             [](const s_xbt_log_category_t* a, const s_xbt_log_category_t* b) { return strcmp(a->name, b->name) < 0; });
641
642   for (auto const& cat : cats) {
643     XBT_HELP("%s%s: %s", this_prefix.c_str(), cat->name, cat->description);
644     if (cat == cats.back() && category->parent)
645       child_prefix[child_prefix.rfind('|')] = ' ';
646     xbt_log_help_categories_rec(cat->firstChild, child_prefix);
647   }
648 }
649
650 static void xbt_log_help_categories()
651 {
652   XBT_HELP("Current log category hierarchy:");
653   xbt_log_help_categories_rec(&_XBT_LOGV(XBT_LOG_ROOT_CAT), "   ");
654   XBT_HELP("%s", "");
655 }