1 /* log - a generic logging facility in the spirit of log4j */
3 /* Copyright (c) 2004-2018. The SimGrid Team.
4 * All rights reserved. */
6 /* This program is free software; you can redistribute it and/or modify it
7 * under the terms of the license (GNU LGPL) which comes with this package. */
11 #include <stdio.h> /* snprintf */
12 #include <stdlib.h> /* snprintf */
14 #include "src/internal_config.h"
16 #include "src/xbt_modinter.h"
18 #include "src/xbt/log_private.h"
19 #include "xbt/asserts.h"
20 #include "xbt/dynar.h"
24 #include "xbt/sysdep.h"
25 #include "xbt/xbt_os_thread.h"
28 #define MIN(a, b) ((a) < (b) ? (a) : (b))
31 #define MAX(a, b) ((a) > (b) ? (a) : (b))
34 int xbt_log_no_loc = 0; /* if set to true (with --log=no_loc), file localization will be omitted (for tesh tests) */
35 static xbt_os_mutex_t log_cat_init_mutex = NULL;
37 /** \addtogroup XBT_log
39 * For more information, please refer to @ref outcomes_logs Section.
42 xbt_log_appender_t xbt_log_default_appender = NULL; /* set in log_init */
43 xbt_log_layout_t xbt_log_default_layout = NULL; /* set in log_init */
48 e_xbt_log_priority_t thresh;
50 xbt_log_appender_t appender;
51 } s_xbt_log_setting_t;
53 typedef s_xbt_log_setting_t* xbt_log_setting_t;
55 static xbt_dynar_t xbt_log_settings = NULL;
57 static void _free_setting(void *s)
59 xbt_log_setting_t set = *(xbt_log_setting_t *) s;
67 static void _xbt_log_cat_apply_set(xbt_log_category_t category, xbt_log_setting_t setting);
69 const char *xbt_log_priority_names[8] = {
80 s_xbt_log_category_t _XBT_LOGV(XBT_LOG_ROOT_CAT) = {
81 NULL /*parent */ , NULL /* firstChild */ , NULL /* nextSibling */ ,
82 "root", "The common ancestor for all categories",
83 0 /*initialized */, xbt_log_priority_uninitialized /* threshold */ ,
84 0 /* isThreshInherited */ ,
85 NULL /* appender */ , NULL /* layout */ ,
89 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(log, xbt, "Loggings from the logging mechanism itself");
91 /* create the default appender and install it in the root category,
92 which were already created (damnit. Too slow little beetle) */
93 void xbt_log_preinit(void)
95 xbt_log_default_appender = xbt_log_appender_file_new(NULL);
96 xbt_log_default_layout = xbt_log_layout_simple_new(NULL);
97 _XBT_LOGV(XBT_LOG_ROOT_CAT).appender = xbt_log_default_appender;
98 _XBT_LOGV(XBT_LOG_ROOT_CAT).layout = xbt_log_default_layout;
99 log_cat_init_mutex = xbt_os_mutex_init();
102 static void xbt_log_help(void);
103 static void xbt_log_help_categories(void);
105 /** @brief Get all logging settings from the command line
107 * xbt_log_control_set() is called on each string we got from cmd line
109 void xbt_log_init(int *argc, char **argv)
111 unsigned help_requested = 0; /* 1: logs; 2: categories */
114 /* Set logs and init log submodule */
115 for (int i = 1; i < *argc; i++) {
116 if (!strncmp(argv[i], "--log=", strlen("--log="))) {
117 char* opt = strchr(argv[i], '=');
119 xbt_log_control_set(opt);
120 XBT_DEBUG("Did apply '%s' as log setting", opt);
121 } else if (!strcmp(argv[i], "--help-logs")) {
122 help_requested |= 1U;
123 } else if (!strcmp(argv[i], "--help-log-categories")) {
124 help_requested |= 2U;
134 if (help_requested) {
135 if (help_requested & 1)
137 if (help_requested & 2)
138 xbt_log_help_categories();
143 static void log_cat_exit(xbt_log_category_t cat)
145 xbt_log_category_t child;
148 if (cat->appender->free_)
149 cat->appender->free_(cat->appender);
153 if (cat->layout->free_)
154 cat->layout->free_(cat->layout);
158 for (child = cat->firstChild; child != NULL; child = child->nextSibling)
162 void xbt_log_postexit(void)
164 XBT_VERB("Exiting log");
165 xbt_os_mutex_destroy(log_cat_init_mutex);
166 xbt_dynar_free(&xbt_log_settings);
167 log_cat_exit(&_XBT_LOGV(XBT_LOG_ROOT_CAT));
170 /* Size of the static string in which we build the log string */
171 #define XBT_LOG_STATIC_BUFFER_SIZE 2048
172 /* Minimum size of the dynamic string in which we build the log string
173 (should be greater than XBT_LOG_STATIC_BUFFER_SIZE) */
174 #define XBT_LOG_DYNAMIC_BUFFER_SIZE 4096
176 void _xbt_log_event_log(xbt_log_event_t ev, const char *fmt, ...)
178 xbt_log_category_t cat = ev->cat;
180 xbt_assert(ev->priority >= 0, "Negative logging priority naturally forbidden");
181 xbt_assert(ev->priority < sizeof(xbt_log_priority_names), "Priority %d is greater than the biggest allowed value",
185 xbt_log_appender_t appender = cat->appender;
187 if (appender != NULL) {
188 xbt_assert(cat->layout, "No valid layout for the appender of category %s", cat->name);
190 /* First, try with a static buffer */
192 char buff[XBT_LOG_STATIC_BUFFER_SIZE];
194 ev->buffer_size = sizeof buff;
195 va_start(ev->ap, fmt);
196 done = cat->layout->do_layout(cat->layout, ev, fmt);
199 appender->do_append(appender, buff);
202 /* The static buffer was too small, use a dynamically expanded one */
203 ev->buffer_size = XBT_LOG_DYNAMIC_BUFFER_SIZE;
204 ev->buffer = xbt_malloc(ev->buffer_size);
206 va_start(ev->ap, fmt);
207 done = cat->layout->do_layout(cat->layout, ev, fmt);
211 ev->buffer_size *= 2;
212 ev->buffer = xbt_realloc(ev->buffer, ev->buffer_size);
214 appender->do_append(appender, ev->buffer);
215 xbt_free(ev->buffer);
219 if (!cat->additivity)
227 * The standard logging macros use _XBT_LOG_ISENABLED, which calls _xbt_log_cat_init(). Thus, if we want to avoid an
228 * infinite recursion, we can not use the standard logging macros in _xbt_log_cat_init(), and in all functions called
231 * To circumvent the problem, we define the macro DISABLE_XBT_LOG_CAT_INIT() to hide the real _xbt_log_cat_init(). The
232 * macro has to be called at the beginning of the affected functions.
234 static int fake_xbt_log_cat_init(xbt_log_category_t XBT_ATTRIB_UNUSED category,
235 e_xbt_log_priority_t XBT_ATTRIB_UNUSED priority)
239 #define DISABLE_XBT_LOG_CAT_INIT() \
240 int (*_xbt_log_cat_init)(xbt_log_category_t, e_xbt_log_priority_t) XBT_ATTRIB_UNUSED = fake_xbt_log_cat_init;
242 static void _xbt_log_cat_apply_set(xbt_log_category_t category, xbt_log_setting_t setting)
244 DISABLE_XBT_LOG_CAT_INIT();
245 if (setting->thresh != xbt_log_priority_uninitialized) {
246 xbt_log_threshold_set(category, setting->thresh);
248 XBT_DEBUG("Apply settings for category '%s': set threshold to %s (=%d)",
249 category->name, xbt_log_priority_names[category->threshold], category->threshold);
253 xbt_log_layout_set(category, xbt_log_layout_format_new(setting->fmt));
255 XBT_DEBUG("Apply settings for category '%s': set format to %s", category->name, setting->fmt);
258 if (setting->additivity != -1) {
259 xbt_log_additivity_set(category, setting->additivity);
261 XBT_DEBUG("Apply settings for category '%s': set additivity to %s",
262 category->name, (setting->additivity ? "on" : "off"));
264 if (setting->appender) {
265 xbt_log_appender_set(category, setting->appender);
266 if (!category->layout)
267 xbt_log_layout_set(category, xbt_log_layout_simple_new(NULL));
268 category->additivity = 0;
269 XBT_DEBUG("Set %p as appender of category '%s'", setting->appender, category->name);
274 * This gets called the first time a category is referenced and performs the initialization.
275 * Also resets threshold to inherited!
277 int _xbt_log_cat_init(xbt_log_category_t category, e_xbt_log_priority_t priority)
279 DISABLE_XBT_LOG_CAT_INIT();
280 if (log_cat_init_mutex != NULL)
281 xbt_os_mutex_acquire(log_cat_init_mutex);
283 if (category->initialized) {
284 if (log_cat_init_mutex != NULL)
285 xbt_os_mutex_release(log_cat_init_mutex);
286 return priority >= category->threshold;
290 xbt_log_setting_t setting = NULL;
292 XBT_DEBUG("Initializing category '%s' (firstChild=%s, nextSibling=%s)", category->name,
293 (category->firstChild ? category->firstChild->name : "none"),
294 (category->nextSibling ? category->nextSibling->name : "none"));
296 if (category == &_XBT_LOGV(XBT_LOG_ROOT_CAT)) {
297 category->threshold = xbt_log_priority_info;
298 category->appender = xbt_log_default_appender;
299 category->layout = xbt_log_default_layout;
301 if (!category->parent)
302 category->parent = &_XBT_LOGV(XBT_LOG_ROOT_CAT);
304 XBT_DEBUG("Set %s (%s) as father of %s ", category->parent->name,
305 (category->parent->initialized ? xbt_log_priority_names[category->parent->threshold] : "uninited"),
307 xbt_log_parent_set(category, category->parent);
309 if (XBT_LOG_ISENABLED(log, xbt_log_priority_debug)) {
312 xbt_log_category_t cpp = category->parent->firstChild;
315 buf = bprintf("%s %s", res, cpp->name);
319 res = xbt_strdup(cpp->name);
321 cpp = cpp->nextSibling;
324 XBT_DEBUG("Children of %s: %s; nextSibling: %s", category->parent->name, res,
325 (category->parent->nextSibling ? category->parent->nextSibling->name : "none"));
331 /* Apply the control */
332 if (xbt_log_settings) {
333 xbt_assert(category, "NULL category");
334 xbt_assert(category->name);
337 xbt_dynar_foreach(xbt_log_settings, cursor, setting) {
338 xbt_assert(setting, "Damnit, NULL cat in the list");
339 xbt_assert(setting->catname, "NULL setting(=%p)->catname", (void *) setting);
341 if (!strcmp(setting->catname, category->name)) {
343 _xbt_log_cat_apply_set(category, setting);
344 xbt_dynar_cursor_rm(xbt_log_settings, &cursor);
349 XBT_DEBUG("Category '%s': inherited threshold = %s (=%d)",
350 category->name, xbt_log_priority_names[category->threshold], category->threshold);
353 category->initialized = 1;
354 if (log_cat_init_mutex != NULL)
355 xbt_os_mutex_release(log_cat_init_mutex);
356 return priority >= category->threshold;
359 void xbt_log_parent_set(xbt_log_category_t cat, xbt_log_category_t parent)
361 xbt_assert(cat, "NULL category to be given a parent");
362 xbt_assert(parent, "The parent category of %s is NULL", cat->name);
364 /* if the category is initialized, unlink from current parent */
365 if (cat->initialized) {
366 xbt_log_category_t *cpp = &cat->parent->firstChild;
368 while (*cpp != cat && *cpp != NULL) {
369 cpp = &(*cpp)->nextSibling;
372 xbt_assert(*cpp == cat);
373 *cpp = cat->nextSibling;
376 cat->parent = parent;
377 cat->nextSibling = parent->firstChild;
379 parent->firstChild = cat;
381 if (!parent->initialized)
382 _xbt_log_cat_init(parent, xbt_log_priority_uninitialized /* ignored */ );
384 cat->threshold = parent->threshold;
386 cat->isThreshInherited = 1;
389 static void _set_inherited_thresholds(xbt_log_category_t cat)
391 xbt_log_category_t child = cat->firstChild;
393 for (; child != NULL; child = child->nextSibling) {
394 if (child->isThreshInherited) {
395 if (cat != &_XBT_LOGV(log))
396 XBT_VERB("Set category threshold of %s to %s (=%d)",
397 child->name, xbt_log_priority_names[cat->threshold], cat->threshold);
398 child->threshold = cat->threshold;
399 _set_inherited_thresholds(child);
404 void xbt_log_threshold_set(xbt_log_category_t cat, e_xbt_log_priority_t threshold)
406 cat->threshold = threshold;
407 cat->isThreshInherited = 0;
409 _set_inherited_thresholds(cat);
412 static xbt_log_setting_t _xbt_log_parse_setting(const char *control_string)
414 const char *orig_control_string = control_string;
415 xbt_log_setting_t set = xbt_new(s_xbt_log_setting_t, 1);
418 set->thresh = xbt_log_priority_uninitialized;
420 set->additivity = -1;
421 set->appender = NULL;
423 if (!*control_string)
425 XBT_DEBUG("Parse log setting '%s'", control_string);
427 control_string += strspn(control_string, " ");
428 const char *name = control_string;
429 control_string += strcspn(control_string, ".:= ");
430 const char *dot = control_string;
431 control_string += strcspn(control_string, ":= ");
432 const char *eq = control_string;
434 xbt_assert(*dot == '.' || (*eq != '=' && *eq != ':'), "Invalid control string '%s'", orig_control_string);
436 if (!strncmp(dot + 1, "threshold", (size_t) (eq - dot - 1))) {
438 char *neweq = xbt_strdup(eq + 1);
441 while (*(++p) != '\0') {
442 if (*p >= 'a' && *p <= 'z') {
447 XBT_DEBUG("New priority name = %s", neweq);
448 for (i = 0; i < xbt_log_priority_infinite; i++) {
449 if (!strncmp(xbt_log_priority_names[i], neweq, p - eq)) {
450 XBT_DEBUG("This is priority %d", i);
455 if(i<XBT_LOG_STATIC_THRESHOLD){
457 "Priority '%s' (in setting '%s') is above allowed priority '%s'.\n\n"
458 "Compiling SimGrid with -DNDEBUG forbids the levels 'trace' and 'debug'\n"
459 "while -DNLOG forbids any logging, at any level.",
460 eq + 1, name, xbt_log_priority_names[XBT_LOG_STATIC_THRESHOLD]);
462 }else if (i < xbt_log_priority_infinite) {
463 set->thresh = (e_xbt_log_priority_t) i;
466 "Unknown priority name: %s (must be one of: trace,debug,verbose,info,warning,error,critical)", eq + 1);
469 } else if (!strncmp(dot + 1, "add", (size_t) (eq - dot - 1)) ||
470 !strncmp(dot + 1, "additivity", (size_t) (eq - dot - 1))) {
471 char *neweq = xbt_strdup(eq + 1);
474 while (*(++p) != '\0') {
475 if (*p >= 'a' && *p <= 'z') {
479 if (!strcmp(neweq, "ON") || !strcmp(neweq, "YES") || !strcmp(neweq, "1")) {
485 } else if (!strncmp(dot + 1, "app", (size_t) (eq - dot - 1)) ||
486 !strncmp(dot + 1, "appender", (size_t) (eq - dot - 1))) {
487 char *neweq = xbt_strdup(eq + 1);
489 if (!strncmp(neweq, "file:", 5)) {
490 set->appender = xbt_log_appender_file_new(neweq + 5);
491 }else if (!strncmp(neweq, "rollfile:", 9)) {
492 set->appender = xbt_log_appender2_file_new(neweq + 9,1);
493 }else if (!strncmp(neweq, "splitfile:", 10)) {
494 set->appender = xbt_log_appender2_file_new(neweq + 10,0);
496 THROWF(arg_error, 0, "Unknown appender log type: '%s'", neweq);
499 } else if (!strncmp(dot + 1, "fmt", (size_t) (eq - dot - 1))) {
500 set->fmt = xbt_strdup(eq + 1);
503 snprintf(buff, MIN(512, eq - dot), "%s", dot + 1);
504 xbt_die("Unknown setting of the log category: '%s'", buff);
506 set->catname = (char *) xbt_malloc(dot - name + 1);
508 memcpy(set->catname, name, dot - name);
509 set->catname[dot - name] = '\0'; /* Just in case */
510 XBT_DEBUG("This is for cat '%s'", set->catname);
515 static xbt_log_category_t _xbt_log_cat_searchsub(xbt_log_category_t cat, char *name)
517 xbt_log_category_t child;
518 xbt_log_category_t res;
520 XBT_DEBUG("Search '%s' into '%s' (firstChild='%s'; nextSibling='%s')", name,
521 cat->name, (cat->firstChild ? cat->firstChild->name : "none"),
522 (cat->nextSibling ? cat->nextSibling->name : "none"));
523 if (!strcmp(cat->name, name))
526 for (child = cat->firstChild; child != NULL; child = child->nextSibling) {
527 XBT_DEBUG("Dig into %s", child->name);
528 res = _xbt_log_cat_searchsub(child, name);
538 * \param control_string What to parse
540 * Typically passed a command-line argument. The string has the syntax:
542 * ( [category] "." [keyword] ":" value (" ")... )...
544 * where [category] is one the category names (see \ref XBT_log_cats for a complete list of the ones defined in the
545 * SimGrid library) and keyword is one of the following:
547 * - thres: category's threshold priority. Possible values:
548 * TRACE,DEBUG,VERBOSE,INFO,WARNING,ERROR,CRITICAL
549 * - add or additivity: whether the logging actions must be passed to the parent category.
550 * Possible values: 0, 1, no, yes, on, off.
551 * Default value: yes.
552 * - fmt: the format to use. See \ref log_use_conf_fmt for more information.
553 * - app or appender: the appender to use. See \ref log_use_conf_app for more information.
555 void xbt_log_control_set(const char *control_string)
557 xbt_log_setting_t set;
559 /* To split the string in commands, and the cursors */
560 xbt_dynar_t set_strings;
566 XBT_DEBUG("Parse log settings '%s'", control_string);
568 /* Special handling of no_loc request, which asks for any file localization to be omitted (for tesh runs) */
569 if (!strcmp(control_string, "no_loc")) {
573 /* some initialization if this is the first time that this get called */
574 if (xbt_log_settings == NULL)
575 xbt_log_settings = xbt_dynar_new(sizeof(xbt_log_setting_t), _free_setting);
577 /* split the string, and remove empty entries */
578 set_strings = xbt_str_split_quoted(control_string);
580 if (xbt_dynar_is_empty(set_strings)) { /* vicious user! */
581 xbt_dynar_free(&set_strings);
585 /* Parse each entry and either use it right now (if the category was already created), or store it for further use */
586 xbt_dynar_foreach(set_strings, cpt, str) {
587 set = _xbt_log_parse_setting(str);
588 xbt_log_category_t cat = _xbt_log_cat_searchsub(&_XBT_LOGV(XBT_LOG_ROOT_CAT), set->catname);
591 XBT_DEBUG("Apply directly");
592 _xbt_log_cat_apply_set(cat, set);
593 _free_setting((void *) &set);
595 XBT_DEBUG("Store for further application");
596 XBT_DEBUG("push %p to the settings", (void *) set);
597 xbt_dynar_push(xbt_log_settings, &set);
600 xbt_dynar_free(&set_strings);
603 void xbt_log_appender_set(xbt_log_category_t cat, xbt_log_appender_t app)
606 if (cat->appender->free_)
607 cat->appender->free_(cat->appender);
613 void xbt_log_layout_set(xbt_log_category_t cat, xbt_log_layout_t lay)
615 DISABLE_XBT_LOG_CAT_INIT();
616 if (!cat->appender) {
617 XBT_VERB ("No appender to category %s. Setting the file appender as default", cat->name);
618 xbt_log_appender_set(cat, xbt_log_appender_file_new(NULL));
621 if (cat->layout->free_) {
622 cat->layout->free_(cat->layout);
627 xbt_log_additivity_set(cat, 0);
630 void xbt_log_additivity_set(xbt_log_category_t cat, int additivity)
632 cat->additivity = additivity;
635 static void xbt_log_help(void)
637 printf("Description of the logging output:\n"
639 " Threshold configuration: --log=CATEGORY_NAME.thres:PRIORITY_LEVEL\n"
640 " CATEGORY_NAME: defined in code with function 'XBT_LOG_NEW_CATEGORY'\n"
641 " PRIORITY_LEVEL: the level to print (trace,debug,verbose,info,warning,error,critical)\n"
642 " -> trace: enter and return of some functions\n"
643 " -> debug: crufty output\n"
644 " -> verbose: verbose output for the user wanting more\n"
645 " -> info: output about the regular functioning\n"
646 " -> warning: minor issue encountered\n"
647 " -> error: issue encountered\n"
648 " -> critical: major issue encountered\n"
649 " The default priority level is 'info'.\n"
651 " Format configuration: --log=CATEGORY_NAME.fmt:FORMAT\n"
652 " FORMAT string may contain:\n"
653 " -> %%%%: the %% char\n"
654 " -> %%n: platform-dependent line separator (LOG4J compatible)\n"
655 " -> %%e: plain old space (SimGrid extension)\n"
657 " -> %%m: user-provided message\n"
659 " -> %%c: Category name (LOG4J compatible)\n"
660 " -> %%p: Priority name (LOG4J compatible)\n"
662 " -> %%h: Hostname (SimGrid extension)\n"
663 " -> %%P: Process name (SimGrid extension)\n"
664 " -> %%t: Thread \"name\" (LOG4J compatible -- actually the address of the thread in memory)\n"
665 " -> %%i: Process PID (SimGrid extension -- this is a 'i' as in 'i'dea)\n"
667 " -> %%F: file name where the log event was raised (LOG4J compatible)\n"
668 " -> %%l: location where the log event was raised (LOG4J compatible, like '%%F:%%L' -- this is a l as "
670 " -> %%L: line number where the log event was raised (LOG4J compatible)\n"
671 " -> %%M: function name (LOG4J compatible -- called method name here of course).\n"
672 " Defined only when using gcc because there is no __func__ elsewhere.\n"
674 " -> %%b: full backtrace (Called %%throwable in LOG4J). Defined only under windows or when using the "
676 " backtrace() is not defined elsewhere, and we only have a fallback for windows boxes, not "
677 "mac ones for example.\n"
678 " -> %%B: short backtrace (only the first line of the %%b). Called %%throwable{short} in LOG4J; "
679 "defined where %%b is.\n"
681 " -> %%d: date (UNIX-like epoch)\n"
682 " -> %%r: application age (time elapsed since the beginning of the application)\n"
685 " --help-log-categories Display the current hierarchy of log categories.\n"
686 " --log=no_loc Don't print file names in messages (for tesh tests).\n"
690 static int xbt_log_cat_cmp(const void *pa, const void *pb)
692 xbt_log_category_t a = *(xbt_log_category_t *)pa;
693 xbt_log_category_t b = *(xbt_log_category_t *)pb;
694 return strcmp(a->name, b->name);
697 static void xbt_log_help_categories_rec(xbt_log_category_t category, const char *prefix)
702 xbt_log_category_t cat;
707 if (category->parent) {
708 this_prefix = bprintf("%s \\_ ", prefix);
709 child_prefix = bprintf("%s | ", prefix);
711 this_prefix = xbt_strdup(prefix);
712 child_prefix = xbt_strdup(prefix);
715 xbt_dynar_t dynar = xbt_dynar_new(sizeof(xbt_log_category_t), NULL);
716 for (cat = category ; cat != NULL; cat = cat->nextSibling)
717 xbt_dynar_push_as(dynar, xbt_log_category_t, cat);
719 xbt_dynar_sort(dynar, xbt_log_cat_cmp);
721 xbt_dynar_foreach(dynar, i, cat){
722 if (i == xbt_dynar_length(dynar) - 1 && category->parent)
723 *strrchr(child_prefix, '|') = ' ';
724 printf("%s%s: %s\n", this_prefix, cat->name, cat->description);
725 xbt_log_help_categories_rec(cat->firstChild, child_prefix);
728 xbt_dynar_free(&dynar);
729 xbt_free(this_prefix);
730 xbt_free(child_prefix);
733 static void xbt_log_help_categories(void)
735 printf("Current log category hierarchy:\n");
736 xbt_log_help_categories_rec(&_XBT_LOGV(XBT_LOG_ROOT_CAT), " ");