Logo AND Algorithmique Numérique Distribuée

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