Logo AND Algorithmique Numérique Distribuée

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