Logo AND Algorithmique Numérique Distribuée

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