1 /* log - a generic logging facility in the spirit of log4j */
3 /* Copyright (c) 2004-2015. 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"
27 int xbt_log_no_loc = 0; /* if set to true (with --log=no_loc), file localization will be omitted (for tesh tests) */
28 static xbt_os_mutex_t log_cat_init_mutex = NULL;
30 /** \addtogroup XBT_log
32 * For more information, please refer to @ref outcomes_logs Section.
35 xbt_log_appender_t xbt_log_default_appender = NULL; /* set in log_init */
36 xbt_log_layout_t xbt_log_default_layout = NULL; /* set in log_init */
41 e_xbt_log_priority_t thresh;
43 xbt_log_appender_t appender;
44 } s_xbt_log_setting_t;
46 typedef s_xbt_log_setting_t* xbt_log_setting_t;
48 static xbt_dynar_t xbt_log_settings = NULL;
50 static void _free_setting(void *s)
52 xbt_log_setting_t set = *(xbt_log_setting_t *) s;
60 static void _xbt_log_cat_apply_set(xbt_log_category_t category, xbt_log_setting_t setting);
62 const char *xbt_log_priority_names[8] = {
73 s_xbt_log_category_t _XBT_LOGV(XBT_LOG_ROOT_CAT) = {
74 NULL /*parent */ , NULL /* firstChild */ , NULL /* nextSibling */ ,
75 "root", "The common ancestor for all categories",
76 0 /*initialized */, xbt_log_priority_uninitialized /* threshold */ ,
77 0 /* isThreshInherited */ ,
78 NULL /* appender */ , NULL /* layout */ ,
82 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(log, xbt, "Loggings from the logging mechanism itself");
84 /* create the default appender and install it in the root category,
85 which were already created (damnit. Too slow little beetle) */
86 void xbt_log_preinit(void)
88 xbt_log_default_appender = xbt_log_appender_file_new(NULL);
89 xbt_log_default_layout = xbt_log_layout_simple_new(NULL);
90 _XBT_LOGV(XBT_LOG_ROOT_CAT).appender = xbt_log_default_appender;
91 _XBT_LOGV(XBT_LOG_ROOT_CAT).layout = xbt_log_default_layout;
92 log_cat_init_mutex = xbt_os_mutex_init();
95 static void xbt_log_connect_categories(void)
97 /* Connect our log channels: that must be done manually under windows */
98 /* Also permit that they are correctly listed by xbt_log_help_categories() */
101 XBT_LOG_CONNECT(xbt);
102 XBT_LOG_CONNECT(log);
103 XBT_LOG_CONNECT(module);
104 XBT_LOG_CONNECT(replay);
105 XBT_LOG_CONNECT(xbt_cfg);
106 XBT_LOG_CONNECT(xbt_dict);
107 XBT_LOG_CONNECT(xbt_dict_cursor);
108 XBT_LOG_CONNECT(xbt_dict_elm);
109 XBT_LOG_CONNECT(xbt_dyn);
110 XBT_LOG_CONNECT(xbt_ex);
111 XBT_LOG_CONNECT(xbt_backtrace);
112 XBT_LOG_CONNECT(xbt_exception);
113 XBT_LOG_CONNECT(xbt_graph);
114 XBT_LOG_CONNECT(xbt_heap);
115 XBT_LOG_CONNECT(xbt_lib);
116 XBT_LOG_CONNECT(xbt_mallocator);
117 XBT_LOG_CONNECT(xbt_memory_map);
118 XBT_LOG_CONNECT(xbt_parmap);
119 XBT_LOG_CONNECT(xbt_sync);
120 XBT_LOG_CONNECT(xbt_sync_os);
122 #ifdef simgrid_EXPORTS
123 /* The following categories are only defined in libsimgrid */
127 XBT_LOG_CONNECT(lua);
128 XBT_LOG_CONNECT(lua_host);
129 XBT_LOG_CONNECT(lua_platf);
130 XBT_LOG_CONNECT(lua_debug);
134 XBT_LOG_CONNECT(instr);
135 XBT_LOG_CONNECT(instr_api);
136 XBT_LOG_CONNECT(instr_config);
137 XBT_LOG_CONNECT(instr_msg);
138 XBT_LOG_CONNECT(instr_msg_process);
139 XBT_LOG_CONNECT(instr_paje_containers);
140 XBT_LOG_CONNECT(instr_paje_header);
141 XBT_LOG_CONNECT(instr_paje_trace);
142 XBT_LOG_CONNECT(instr_paje_types);
143 XBT_LOG_CONNECT(instr_paje_values);
144 XBT_LOG_CONNECT(instr_resource);
145 XBT_LOG_CONNECT(instr_routing);
146 XBT_LOG_CONNECT(instr_surf);
149 #if SIMGRID_HAVE_JEDULE
150 XBT_LOG_CONNECT(jedule);
151 XBT_LOG_CONNECT(jed_sd);
157 XBT_LOG_CONNECT(mc_checkpoint);
158 XBT_LOG_CONNECT(mc_comm_determinism);
159 XBT_LOG_CONNECT(mc_compare);
160 XBT_LOG_CONNECT(mc_dwarf);
161 XBT_LOG_CONNECT(mc_hash);
162 XBT_LOG_CONNECT(mc_liveness);
163 XBT_LOG_CONNECT(mc_memory);
164 XBT_LOG_CONNECT(mc_page_snapshot);
165 XBT_LOG_CONNECT(mc_request);
166 XBT_LOG_CONNECT(mc_safety);
167 XBT_LOG_CONNECT(mc_VisitedState);
168 XBT_LOG_CONNECT(mc_client);
169 XBT_LOG_CONNECT(mc_client_api);
170 XBT_LOG_CONNECT(mc_comm_pattern);
171 XBT_LOG_CONNECT(mc_process);
172 XBT_LOG_CONNECT(mc_protocol);
173 XBT_LOG_CONNECT(mc_Channel);
174 XBT_LOG_CONNECT(mc_ModelChecker);
175 XBT_LOG_CONNECT(mc_RegionSnaphot);
176 XBT_LOG_CONNECT(mc_Session);
177 XBT_LOG_CONNECT(mc_state);
179 XBT_LOG_CONNECT(mc_global);
180 XBT_LOG_CONNECT(mc_config);
181 XBT_LOG_CONNECT(mc_record);
184 XBT_LOG_CONNECT(msg);
185 XBT_LOG_CONNECT(msg_action);
186 XBT_LOG_CONNECT(msg_gos);
187 XBT_LOG_CONNECT(msg_io);
188 XBT_LOG_CONNECT(msg_kernel);
189 XBT_LOG_CONNECT(msg_mailbox);
190 XBT_LOG_CONNECT(msg_process);
191 XBT_LOG_CONNECT(msg_synchro);
192 XBT_LOG_CONNECT(msg_task);
193 XBT_LOG_CONNECT(msg_vm);
196 XBT_LOG_CONNECT(s4u);
197 XBT_LOG_CONNECT(s4u_activity);
198 XBT_LOG_CONNECT(s4u_actor);
199 XBT_LOG_CONNECT(s4u_netzone);
200 XBT_LOG_CONNECT(s4u_channel);
201 XBT_LOG_CONNECT(s4u_comm);
202 XBT_LOG_CONNECT(s4u_file);
205 XBT_LOG_CONNECT(sg_host);
209 XBT_LOG_CONNECT(sd_daxparse);
211 XBT_LOG_CONNECT(sd_dotparse);
213 XBT_LOG_CONNECT(sd_kernel);
214 XBT_LOG_CONNECT(sd_task);
217 XBT_LOG_CONNECT(simix);
218 XBT_LOG_CONNECT(simix_context);
219 XBT_LOG_CONNECT(simix_deployment);
220 XBT_LOG_CONNECT(simix_environment);
221 XBT_LOG_CONNECT(simix_host);
222 XBT_LOG_CONNECT(simix_io);
223 XBT_LOG_CONNECT(simix_kernel);
224 XBT_LOG_CONNECT(simix_network);
225 XBT_LOG_CONNECT(simix_process);
226 XBT_LOG_CONNECT(simix_popping);
227 XBT_LOG_CONNECT(simix_synchro);
230 /* SMPI categories are connected in smpi_global.c */
233 XBT_LOG_CONNECT(surf);
234 XBT_LOG_CONNECT(surf_config);
235 XBT_LOG_CONNECT(surf_cpu);
236 XBT_LOG_CONNECT(surf_cpu_cas);
237 XBT_LOG_CONNECT(surf_cpu_ti);
238 XBT_LOG_CONNECT(surf_energy);
239 XBT_LOG_CONNECT(surf_kernel);
240 XBT_LOG_CONNECT(surf_lagrange);
241 XBT_LOG_CONNECT(surf_lagrange_dichotomy);
242 XBT_LOG_CONNECT(surf_maxmin);
243 XBT_LOG_CONNECT(surf_network);
245 XBT_LOG_CONNECT(ns3);
247 XBT_LOG_CONNECT(surf_parse);
248 XBT_LOG_CONNECT(surf_route);
249 XBT_LOG_CONNECT(surf_routing_generic);
250 XBT_LOG_CONNECT(surf_route_cluster);
251 XBT_LOG_CONNECT(surf_route_cluster_torus);
252 XBT_LOG_CONNECT(surf_route_cluster_dragonfly);
253 XBT_LOG_CONNECT(surf_route_dijkstra);
254 XBT_LOG_CONNECT(surf_route_fat_tree);
255 XBT_LOG_CONNECT(surf_route_floyd);
256 XBT_LOG_CONNECT(surf_route_full);
257 XBT_LOG_CONNECT(surf_route_none);
258 XBT_LOG_CONNECT(surf_route_vivaldi);
259 XBT_LOG_CONNECT(surf_storage);
260 XBT_LOG_CONNECT(surf_trace);
261 XBT_LOG_CONNECT(surf_vm);
262 XBT_LOG_CONNECT(surf_host);
264 #endif /* simgrid_EXPORTS */
267 static void xbt_log_help(void);
268 static void xbt_log_help_categories(void);
270 /** @brief Get all logging settings from the command line
272 * xbt_log_control_set() is called on each string we got from cmd line
274 void xbt_log_init(int *argc, char **argv)
276 unsigned help_requested = 0; /* 1: logs; 2: categories */
281 /* uncomment to set the LOG category to debug directly */
282 // _XBT_LOGV(log).threshold = xbt_log_priority_debug;
284 xbt_log_connect_categories();
286 /* Set logs and init log submodule */
287 for (j = i = 1; i < *argc; i++) {
288 if (!strncmp(argv[i], "--log=", strlen("--log="))) {
289 opt = strchr(argv[i], '=');
291 xbt_log_control_set(opt);
292 XBT_DEBUG("Did apply '%s' as log setting", opt);
293 } else if (!strcmp(argv[i], "--help-logs")) {
294 help_requested |= 1U;
295 } else if (!strcmp(argv[i], "--help-log-categories")) {
296 help_requested |= 2U;
306 if (help_requested) {
307 if (help_requested & 1)
309 if (help_requested & 2)
310 xbt_log_help_categories();
315 static void log_cat_exit(xbt_log_category_t cat)
317 xbt_log_category_t child;
320 if (cat->appender->free_)
321 cat->appender->free_(cat->appender);
325 if (cat->layout->free_)
326 cat->layout->free_(cat->layout);
330 for (child = cat->firstChild; child != NULL; child = child->nextSibling)
334 void xbt_log_postexit(void)
336 XBT_VERB("Exiting log");
337 xbt_os_mutex_destroy(log_cat_init_mutex);
338 xbt_dynar_free(&xbt_log_settings);
339 log_cat_exit(&_XBT_LOGV(XBT_LOG_ROOT_CAT));
342 /* Size of the static string in which we build the log string */
343 #define XBT_LOG_STATIC_BUFFER_SIZE 2048
344 /* Minimum size of the dynamic string in which we build the log string
345 (should be greater than XBT_LOG_STATIC_BUFFER_SIZE) */
346 #define XBT_LOG_DYNAMIC_BUFFER_SIZE 4096
348 void _xbt_log_event_log(xbt_log_event_t ev, const char *fmt, ...)
350 xbt_log_category_t cat = ev->cat;
352 xbt_assert(ev->priority >= 0, "Negative logging priority naturally forbidden");
353 xbt_assert(ev->priority < sizeof(xbt_log_priority_names), "Priority %d is greater than the biggest allowed value",
357 xbt_log_appender_t appender = cat->appender;
360 continue; /* No appender, try next */
362 xbt_assert(cat->layout, "No valid layout for the appender of category %s", cat->name);
364 /* First, try with a static buffer */
365 if (XBT_LOG_STATIC_BUFFER_SIZE) {
366 char buff[XBT_LOG_STATIC_BUFFER_SIZE];
368 ev->buffer_size = sizeof buff;
369 va_start(ev->ap, fmt);
370 int done = cat->layout->do_layout(cat->layout, ev, fmt);
373 appender->do_append(appender, buff);
374 continue; /* Ok, that worked: go next */
378 /* The static buffer was too small, use a dynamically expanded one */
379 ev->buffer_size = XBT_LOG_DYNAMIC_BUFFER_SIZE;
380 ev->buffer = xbt_malloc(ev->buffer_size);
382 va_start(ev->ap, fmt);
383 int done = cat->layout->do_layout(cat->layout, ev, fmt);
387 ev->buffer_size *= 2;
388 ev->buffer = xbt_realloc(ev->buffer, ev->buffer_size);
390 appender->do_append(appender, ev->buffer);
391 xbt_free(ev->buffer);
393 } while (cat->additivity && (cat = cat->parent, 1));
396 #undef XBT_LOG_DYNAMIC_BUFFER_SIZE
397 #undef XBT_LOG_STATIC_BUFFER_SIZE
401 * The standard logging macros use _XBT_LOG_ISENABLED, which calls _xbt_log_cat_init(). Thus, if we want to avoid an
402 * infinite recursion, we can not use the standard logging macros in _xbt_log_cat_init(), and in all functions called
405 * To circumvent the problem, we define the macro_xbt_log_init() as (0) for the length of the affected functions, and
406 * we do not forget to undefine it at the end!
409 static void _xbt_log_cat_apply_set(xbt_log_category_t category, xbt_log_setting_t setting)
411 #define _xbt_log_cat_init(a, b) (0)
413 if (setting->thresh != xbt_log_priority_uninitialized) {
414 xbt_log_threshold_set(category, setting->thresh);
416 XBT_DEBUG("Apply settings for category '%s': set threshold to %s (=%d)",
417 category->name, xbt_log_priority_names[category->threshold], category->threshold);
421 xbt_log_layout_set(category, xbt_log_layout_format_new(setting->fmt));
423 XBT_DEBUG("Apply settings for category '%s': set format to %s", category->name, setting->fmt);
426 if (setting->additivity != -1) {
427 xbt_log_additivity_set(category, setting->additivity);
429 XBT_DEBUG("Apply settings for category '%s': set additivity to %s",
430 category->name, (setting->additivity ? "on" : "off"));
432 if (setting->appender) {
433 xbt_log_appender_set(category, setting->appender);
434 if (!category->layout)
435 xbt_log_layout_set(category, xbt_log_layout_simple_new(NULL));
436 category->additivity = 0;
437 XBT_DEBUG("Set %p as appender of category '%s'", setting->appender, category->name);
439 #undef _xbt_log_cat_init
443 * This gets called the first time a category is referenced and performs the initialization.
444 * Also resets threshold to inherited!
446 int _xbt_log_cat_init(xbt_log_category_t category, e_xbt_log_priority_t priority)
448 #define _xbt_log_cat_init(a, b) (0)
450 if (log_cat_init_mutex != NULL)
451 xbt_os_mutex_acquire(log_cat_init_mutex);
453 if (category->initialized) {
454 if (log_cat_init_mutex != NULL)
455 xbt_os_mutex_release(log_cat_init_mutex);
456 return priority >= category->threshold;
460 xbt_log_setting_t setting = NULL;
462 XBT_DEBUG("Initializing category '%s' (firstChild=%s, nextSibling=%s)", category->name,
463 (category->firstChild ? category->firstChild->name : "none"),
464 (category->nextSibling ? category->nextSibling->name : "none"));
466 if (category == &_XBT_LOGV(XBT_LOG_ROOT_CAT)) {
467 category->threshold = xbt_log_priority_info;
468 category->appender = xbt_log_default_appender;
469 category->layout = xbt_log_default_layout;
471 if (!category->parent)
472 category->parent = &_XBT_LOGV(XBT_LOG_ROOT_CAT);
474 XBT_DEBUG("Set %s (%s) as father of %s ", category->parent->name,
475 (category->parent->initialized ? xbt_log_priority_names[category->parent->threshold] : "uninited"),
477 xbt_log_parent_set(category, category->parent);
479 if (XBT_LOG_ISENABLED(log, xbt_log_priority_debug)) {
482 xbt_log_category_t cpp = category->parent->firstChild;
485 buf = bprintf("%s %s", res, cpp->name);
489 res = xbt_strdup(cpp->name);
491 cpp = cpp->nextSibling;
494 XBT_DEBUG("Children of %s: %s; nextSibling: %s", category->parent->name, res,
495 (category->parent->nextSibling ? category->parent->nextSibling->name : "none"));
501 /* Apply the control */
502 if (xbt_log_settings) {
503 xbt_assert(category, "NULL category");
504 xbt_assert(category->name);
507 xbt_dynar_foreach(xbt_log_settings, cursor, setting) {
508 xbt_assert(setting, "Damnit, NULL cat in the list");
509 xbt_assert(setting->catname, "NULL setting(=%p)->catname", (void *) setting);
511 if (!strcmp(setting->catname, category->name)) {
513 _xbt_log_cat_apply_set(category, setting);
514 xbt_dynar_cursor_rm(xbt_log_settings, &cursor);
519 XBT_DEBUG("Category '%s': inherited threshold = %s (=%d)",
520 category->name, xbt_log_priority_names[category->threshold], category->threshold);
523 category->initialized = 1;
524 if (log_cat_init_mutex != NULL)
525 xbt_os_mutex_release(log_cat_init_mutex);
526 return priority >= category->threshold;
528 #undef _xbt_log_cat_init
531 void xbt_log_parent_set(xbt_log_category_t cat, xbt_log_category_t parent)
533 xbt_assert(cat, "NULL category to be given a parent");
534 xbt_assert(parent, "The parent category of %s is NULL", cat->name);
536 /* if the category is initialized, unlink from current parent */
537 if (cat->initialized) {
538 xbt_log_category_t *cpp = &cat->parent->firstChild;
540 while (*cpp != cat && *cpp != NULL) {
541 cpp = &(*cpp)->nextSibling;
544 xbt_assert(*cpp == cat);
545 *cpp = cat->nextSibling;
548 cat->parent = parent;
549 cat->nextSibling = parent->firstChild;
551 parent->firstChild = cat;
553 if (!parent->initialized)
554 _xbt_log_cat_init(parent, xbt_log_priority_uninitialized /* ignored */ );
556 cat->threshold = parent->threshold;
558 cat->isThreshInherited = 1;
561 static void _set_inherited_thresholds(xbt_log_category_t cat)
563 xbt_log_category_t child = cat->firstChild;
565 for (; child != NULL; child = child->nextSibling) {
566 if (child->isThreshInherited) {
567 if (cat != &_XBT_LOGV(log))
568 XBT_VERB("Set category threshold of %s to %s (=%d)",
569 child->name, xbt_log_priority_names[cat->threshold], cat->threshold);
570 child->threshold = cat->threshold;
571 _set_inherited_thresholds(child);
576 void xbt_log_threshold_set(xbt_log_category_t cat, e_xbt_log_priority_t threshold)
578 cat->threshold = threshold;
579 cat->isThreshInherited = 0;
581 _set_inherited_thresholds(cat);
584 static xbt_log_setting_t _xbt_log_parse_setting(const char *control_string)
586 const char *orig_control_string = control_string;
587 xbt_log_setting_t set = xbt_new(s_xbt_log_setting_t, 1);
590 set->thresh = xbt_log_priority_uninitialized;
592 set->additivity = -1;
593 set->appender = NULL;
595 if (!*control_string)
597 XBT_DEBUG("Parse log setting '%s'", control_string);
599 control_string += strspn(control_string, " ");
600 const char *name = control_string;
601 control_string += strcspn(control_string, ".:= ");
602 const char *dot = control_string;
603 control_string += strcspn(control_string, ":= ");
604 const char *eq = control_string;
606 xbt_assert(*dot == '.' || (*eq != '=' && *eq != ':'), "Invalid control string '%s'", orig_control_string);
608 if (!strncmp(dot + 1, "threshold", (size_t) (eq - dot - 1))) {
610 char *neweq = xbt_strdup(eq + 1);
613 while (*(++p) != '\0') {
614 if (*p >= 'a' && *p <= 'z') {
619 XBT_DEBUG("New priority name = %s", neweq);
620 for (i = 0; i < xbt_log_priority_infinite; i++) {
621 if (!strncmp(xbt_log_priority_names[i], neweq, p - eq)) {
622 XBT_DEBUG("This is priority %d", i);
627 if(i<XBT_LOG_STATIC_THRESHOLD){
629 "Priority '%s' (in setting '%s') is above allowed priority '%s'.\n\n"
630 "Compiling SimGrid with -DNDEBUG forbids the levels 'trace' and 'debug'\n"
631 "while -DNLOG forbids any logging, at any level.",
632 eq + 1, name, xbt_log_priority_names[XBT_LOG_STATIC_THRESHOLD]);
634 }else if (i < xbt_log_priority_infinite) {
635 set->thresh = (e_xbt_log_priority_t) i;
638 "Unknown priority name: %s (must be one of: trace,debug,verbose,info,warning,error,critical)", eq + 1);
641 } else if (!strncmp(dot + 1, "add", (size_t) (eq - dot - 1)) ||
642 !strncmp(dot + 1, "additivity", (size_t) (eq - dot - 1))) {
643 char *neweq = xbt_strdup(eq + 1);
646 while (*(++p) != '\0') {
647 if (*p >= 'a' && *p <= 'z') {
651 if (!strcmp(neweq, "ON") || !strcmp(neweq, "YES") || !strcmp(neweq, "1")) {
657 } else if (!strncmp(dot + 1, "app", (size_t) (eq - dot - 1)) ||
658 !strncmp(dot + 1, "appender", (size_t) (eq - dot - 1))) {
659 char *neweq = xbt_strdup(eq + 1);
661 if (!strncmp(neweq, "file:", 5)) {
662 set->appender = xbt_log_appender_file_new(neweq + 5);
663 }else if (!strncmp(neweq, "rollfile:", 9)) {
664 set->appender = xbt_log_appender2_file_new(neweq + 9,1);
665 }else if (!strncmp(neweq, "splitfile:", 10)) {
666 set->appender = xbt_log_appender2_file_new(neweq + 10,0);
668 THROWF(arg_error, 0, "Unknown appender log type: '%s'", neweq);
671 } else if (!strncmp(dot + 1, "fmt", (size_t) (eq - dot - 1))) {
672 set->fmt = xbt_strdup(eq + 1);
675 snprintf(buff, MIN(512, eq - dot), "%s", dot + 1);
676 xbt_die("Unknown setting of the log category: '%s'", buff);
678 set->catname = (char *) xbt_malloc(dot - name + 1);
680 memcpy(set->catname, name, dot - name);
681 set->catname[dot - name] = '\0'; /* Just in case */
682 XBT_DEBUG("This is for cat '%s'", set->catname);
687 static xbt_log_category_t _xbt_log_cat_searchsub(xbt_log_category_t cat, char *name)
689 xbt_log_category_t child;
690 xbt_log_category_t res;
692 XBT_DEBUG("Search '%s' into '%s' (firstChild='%s'; nextSibling='%s')", name,
693 cat->name, (cat->firstChild ? cat->firstChild->name : "none"),
694 (cat->nextSibling ? cat->nextSibling->name : "none"));
695 if (!strcmp(cat->name, name))
698 for (child = cat->firstChild; child != NULL; child = child->nextSibling) {
699 XBT_DEBUG("Dig into %s", child->name);
700 res = _xbt_log_cat_searchsub(child, name);
710 * \param control_string What to parse
712 * Typically passed a command-line argument. The string has the syntax:
714 * ( [category] "." [keyword] ":" value (" ")... )...
716 * where [category] is one the category names (see \ref XBT_log_cats for a complete list of the ones defined in the
717 * SimGrid library) and keyword is one of the following:
719 * - thres: category's threshold priority. Possible values:
720 * TRACE,DEBUG,VERBOSE,INFO,WARNING,ERROR,CRITICAL
721 * - add or additivity: whether the logging actions must be passed to the parent category.
722 * Possible values: 0, 1, no, yes, on, off.
723 * Default value: yes.
724 * - fmt: the format to use. See \ref log_use_conf_fmt for more information.
725 * - app or appender: the appender to use. See \ref log_use_conf_app for more information.
727 void xbt_log_control_set(const char *control_string)
729 xbt_log_setting_t set;
731 /* To split the string in commands, and the cursors */
732 xbt_dynar_t set_strings;
738 XBT_DEBUG("Parse log settings '%s'", control_string);
740 /* Special handling of no_loc request, which asks for any file localization to be omitted (for tesh runs) */
741 if (!strcmp(control_string, "no_loc")) {
745 /* some initialization if this is the first time that this get called */
746 if (xbt_log_settings == NULL)
747 xbt_log_settings = xbt_dynar_new(sizeof(xbt_log_setting_t), _free_setting);
749 /* split the string, and remove empty entries */
750 set_strings = xbt_str_split_quoted(control_string);
752 if (xbt_dynar_is_empty(set_strings)) { /* vicious user! */
753 xbt_dynar_free(&set_strings);
757 /* Parse each entry and either use it right now (if the category was already created), or store it for further use */
758 xbt_dynar_foreach(set_strings, cpt, str) {
759 xbt_log_category_t cat = NULL;
761 set = _xbt_log_parse_setting(str);
762 cat = _xbt_log_cat_searchsub(&_XBT_LOGV(XBT_LOG_ROOT_CAT), set->catname);
765 XBT_DEBUG("Apply directly");
766 _xbt_log_cat_apply_set(cat, set);
767 _free_setting((void *) &set);
769 XBT_DEBUG("Store for further application");
770 XBT_DEBUG("push %p to the settings", (void *) set);
771 xbt_dynar_push(xbt_log_settings, &set);
774 xbt_dynar_free(&set_strings);
777 void xbt_log_appender_set(xbt_log_category_t cat, xbt_log_appender_t app)
780 if (cat->appender->free_)
781 cat->appender->free_(cat->appender);
787 void xbt_log_layout_set(xbt_log_category_t cat, xbt_log_layout_t lay)
789 #define _xbt_log_cat_init(a, b) (0)
790 if (!cat->appender) {
791 XBT_VERB ("No appender to category %s. Setting the file appender as default", cat->name);
792 xbt_log_appender_set(cat, xbt_log_appender_file_new(NULL));
795 if (cat->layout->free_) {
796 cat->layout->free_(cat->layout);
801 xbt_log_additivity_set(cat, 0);
802 #undef _xbt_log_cat_init
805 void xbt_log_additivity_set(xbt_log_category_t cat, int additivity)
807 cat->additivity = additivity;
810 static void xbt_log_help(void)
813 "Description of the logging output:\n"
815 " Threshold configuration: --log=CATEGORY_NAME.thres:PRIORITY_LEVEL\n"
816 " CATEGORY_NAME: defined in code with function 'XBT_LOG_NEW_CATEGORY'\n"
817 " PRIORITY_LEVEL: the level to print (trace,debug,verbose,info,warning,error,critical)\n"
818 " -> trace: enter and return of some functions\n"
819 " -> debug: crufty output\n"
820 " -> verbose: verbose output for the user wanting more\n"
821 " -> info: output about the regular functionning\n"
822 " -> warning: minor issue encountered\n"
823 " -> error: issue encountered\n"
824 " -> critical: major issue encountered\n"
826 " Format configuration: --log=CATEGORY_NAME.fmt:OPTIONS\n"
828 " -> %%%%: the %% char\n"
829 " -> %%n: platform-dependent line separator (LOG4J compatible)\n"
830 " -> %%e: plain old space (SimGrid extension)\n"
832 " -> %%m: user-provided message\n"
834 " -> %%c: Category name (LOG4J compatible)\n"
835 " -> %%p: Priority name (LOG4J compatible)\n"
837 " -> %%h: Hostname (SimGrid extension)\n"
838 " -> %%P: Process name (SimGrid extension)\n"
839 " -> %%t: Thread \"name\" (LOG4J compatible -- actually the address of the thread in memory)\n"
840 " -> %%i: Process PID (SimGrid extension -- this is a 'i' as in 'i'dea)\n"
842 " -> %%F: file name where the log event was raised (LOG4J compatible)\n"
843 " -> %%l: location where the log event was raised (LOG4J compatible, like '%%F:%%L' -- this is a l as in 'l'etter)\n"
844 " -> %%L: line number where the log event was raised (LOG4J compatible)\n"
845 " -> %%M: function name (LOG4J compatible -- called method name here of course).\n"
846 " Defined only when using gcc because there is no __FUNCTION__ elsewhere.\n"
848 " -> %%b: full backtrace (Called %%throwable in LOG4J). Defined only under windows or when using the GNU libc because\n"
849 " backtrace() is not defined elsewhere, and we only have a fallback for windows boxes, not mac ones for example.\n"
850 " -> %%B: short backtrace (only the first line of the %%b). Called %%throwable{short} in LOG4J; defined where %%b is.\n"
852 " -> %%d: date (UNIX-like epoch)\n"
853 " -> %%r: application age (time elapsed since the beginning of the application)\n"
856 " --help-log-categories Display the current hierarchy of log categories.\n"
857 " --log=no_loc Don't print file names in messages (for tesh tests).\n"
862 static int xbt_log_cat_cmp(const void *pa, const void *pb)
864 xbt_log_category_t a = *(xbt_log_category_t *)pa;
865 xbt_log_category_t b = *(xbt_log_category_t *)pb;
866 return strcmp(a->name, b->name);
869 static void xbt_log_help_categories_rec(xbt_log_category_t category, const char *prefix)
874 xbt_log_category_t cat;
879 if (category->parent) {
880 this_prefix = bprintf("%s \\_ ", prefix);
881 child_prefix = bprintf("%s | ", prefix);
883 this_prefix = xbt_strdup(prefix);
884 child_prefix = xbt_strdup(prefix);
887 xbt_dynar_t dynar = xbt_dynar_new(sizeof(xbt_log_category_t), NULL);
888 for (cat = category ; cat != NULL; cat = cat->nextSibling)
889 xbt_dynar_push_as(dynar, xbt_log_category_t, cat);
891 xbt_dynar_sort(dynar, xbt_log_cat_cmp);
893 xbt_dynar_foreach(dynar, i, cat){
894 if (i == xbt_dynar_length(dynar) - 1 && category->parent)
895 *strrchr(child_prefix, '|') = ' ';
896 printf("%s%s: %s\n", this_prefix, cat->name, cat->description);
897 xbt_log_help_categories_rec(cat->firstChild, child_prefix);
900 xbt_dynar_free(&dynar);
901 xbt_free(this_prefix);
902 xbt_free(child_prefix);
905 static void xbt_log_help_categories(void)
907 printf("Current log category hierarchy:\n");
908 xbt_log_help_categories_rec(&_XBT_LOGV(XBT_LOG_ROOT_CAT), " ");