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, *xbt_log_setting_t;
46 static xbt_dynar_t xbt_log_settings = NULL;
48 static void _free_setting(void *s)
50 xbt_log_setting_t set = *(xbt_log_setting_t *) s;
58 static void _xbt_log_cat_apply_set(xbt_log_category_t category, xbt_log_setting_t setting);
60 const char *xbt_log_priority_names[8] = {
71 s_xbt_log_category_t _XBT_LOGV(XBT_LOG_ROOT_CAT) = {
72 NULL /*parent */ , NULL /* firstChild */ , NULL /* nextSibling */ ,
73 "root", "The common ancestor for all categories",
74 0 /*initialized */, xbt_log_priority_uninitialized /* threshold */ ,
75 0 /* isThreshInherited */ ,
76 NULL /* appender */ , NULL /* layout */ ,
80 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(log, xbt, "Loggings from the logging mechanism itself");
82 /* create the default appender and install it in the root category,
83 which were already created (damnit. Too slow little beetle) */
84 void xbt_log_preinit(void)
86 xbt_log_default_appender = xbt_log_appender_file_new(NULL);
87 xbt_log_default_layout = xbt_log_layout_simple_new(NULL);
88 _XBT_LOGV(XBT_LOG_ROOT_CAT).appender = xbt_log_default_appender;
89 _XBT_LOGV(XBT_LOG_ROOT_CAT).layout = xbt_log_default_layout;
90 log_cat_init_mutex = xbt_os_mutex_init();
93 static void xbt_log_connect_categories(void)
95 /* Connect our log channels: that must be done manually under windows */
96 /* Also permit that they are correctly listed by xbt_log_help_categories() */
100 XBT_LOG_CONNECT(log);
101 XBT_LOG_CONNECT(module);
102 XBT_LOG_CONNECT(replay);
103 XBT_LOG_CONNECT(strbuff);
104 XBT_LOG_CONNECT(xbt_cfg);
105 XBT_LOG_CONNECT(xbt_dict);
106 XBT_LOG_CONNECT(xbt_dict_cursor);
107 XBT_LOG_CONNECT(xbt_dict_elm);
108 XBT_LOG_CONNECT(xbt_dyn);
109 XBT_LOG_CONNECT(xbt_ex);
110 XBT_LOG_CONNECT(xbt_backtrace);
111 XBT_LOG_CONNECT(xbt_exception);
112 XBT_LOG_CONNECT(xbt_graph);
113 XBT_LOG_CONNECT(xbt_heap);
114 XBT_LOG_CONNECT(xbt_lib);
115 XBT_LOG_CONNECT(xbt_mallocator);
116 XBT_LOG_CONNECT(xbt_matrix);
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);
147 XBT_LOG_CONNECT(instr_trace);
148 XBT_LOG_CONNECT(instr_TI_trace);
152 XBT_LOG_CONNECT(jedule);
153 XBT_LOG_CONNECT(jed_sd);
159 XBT_LOG_CONNECT(mc_checkpoint);
160 XBT_LOG_CONNECT(mc_comm_determinism);
161 XBT_LOG_CONNECT(mc_compare);
162 XBT_LOG_CONNECT(mc_dwarf);
163 XBT_LOG_CONNECT(mc_hash);
164 XBT_LOG_CONNECT(mc_liveness);
165 XBT_LOG_CONNECT(mc_memory);
166 XBT_LOG_CONNECT(mc_page_snapshot);
167 XBT_LOG_CONNECT(mc_request);
168 XBT_LOG_CONNECT(mc_safety);
169 XBT_LOG_CONNECT(mc_VisitedState);
170 XBT_LOG_CONNECT(mc_client);
171 XBT_LOG_CONNECT(mc_client_api);
172 XBT_LOG_CONNECT(mc_comm_pattern);
173 XBT_LOG_CONNECT(mc_process);
174 XBT_LOG_CONNECT(mc_protocol);
175 XBT_LOG_CONNECT(mc_Channel);
176 XBT_LOG_CONNECT(mc_ModelChecker);
177 XBT_LOG_CONNECT(mc_RegionSnaphot);
178 XBT_LOG_CONNECT(mc_Session);
179 XBT_LOG_CONNECT(mc_state);
181 XBT_LOG_CONNECT(mc_global);
182 XBT_LOG_CONNECT(mc_config);
183 XBT_LOG_CONNECT(mc_record);
186 XBT_LOG_CONNECT(msg);
187 XBT_LOG_CONNECT(msg_action);
188 XBT_LOG_CONNECT(msg_gos);
189 XBT_LOG_CONNECT(msg_io);
190 XBT_LOG_CONNECT(msg_kernel);
191 XBT_LOG_CONNECT(msg_mailbox);
192 XBT_LOG_CONNECT(msg_process);
193 XBT_LOG_CONNECT(msg_synchro);
194 XBT_LOG_CONNECT(msg_task);
195 XBT_LOG_CONNECT(msg_vm);
198 XBT_LOG_CONNECT(s4u);
199 XBT_LOG_CONNECT(s4u_activity);
200 XBT_LOG_CONNECT(s4u_actor);
201 XBT_LOG_CONNECT(s4u_netzone);
202 XBT_LOG_CONNECT(s4u_channel);
203 XBT_LOG_CONNECT(s4u_comm);
204 XBT_LOG_CONNECT(s4u_file);
207 XBT_LOG_CONNECT(sg_host);
211 XBT_LOG_CONNECT(sd_daxparse);
213 XBT_LOG_CONNECT(sd_dotparse);
215 XBT_LOG_CONNECT(sd_kernel);
216 XBT_LOG_CONNECT(sd_task);
219 XBT_LOG_CONNECT(simix);
220 XBT_LOG_CONNECT(simix_context);
221 XBT_LOG_CONNECT(simix_deployment);
222 XBT_LOG_CONNECT(simix_environment);
223 XBT_LOG_CONNECT(simix_host);
224 XBT_LOG_CONNECT(simix_io);
225 XBT_LOG_CONNECT(simix_kernel);
226 XBT_LOG_CONNECT(simix_network);
227 XBT_LOG_CONNECT(simix_process);
228 XBT_LOG_CONNECT(simix_popping);
229 XBT_LOG_CONNECT(simix_synchro);
232 /* SMPI categories are connected in smpi_global.c */
235 XBT_LOG_CONNECT(surf);
236 XBT_LOG_CONNECT(surf_config);
237 XBT_LOG_CONNECT(surf_cpu);
238 XBT_LOG_CONNECT(surf_cpu_cas);
239 XBT_LOG_CONNECT(surf_cpu_ti);
240 XBT_LOG_CONNECT(surf_energy);
241 XBT_LOG_CONNECT(surf_kernel);
242 XBT_LOG_CONNECT(surf_lagrange);
243 XBT_LOG_CONNECT(surf_lagrange_dichotomy);
244 XBT_LOG_CONNECT(surf_maxmin);
245 XBT_LOG_CONNECT(surf_network);
247 XBT_LOG_CONNECT(ns3);
249 XBT_LOG_CONNECT(surf_parse);
250 XBT_LOG_CONNECT(surf_route);
251 XBT_LOG_CONNECT(surf_routing_generic);
252 XBT_LOG_CONNECT(surf_route_cluster);
253 XBT_LOG_CONNECT(surf_route_cluster_torus);
254 XBT_LOG_CONNECT(surf_route_cluster_dragonfly);
255 XBT_LOG_CONNECT(surf_route_dijkstra);
256 XBT_LOG_CONNECT(surf_route_fat_tree);
257 XBT_LOG_CONNECT(surf_route_floyd);
258 XBT_LOG_CONNECT(surf_route_full);
259 XBT_LOG_CONNECT(surf_route_none);
260 XBT_LOG_CONNECT(surf_route_vivaldi);
261 XBT_LOG_CONNECT(surf_storage);
262 XBT_LOG_CONNECT(surf_trace);
263 XBT_LOG_CONNECT(surf_vm);
264 XBT_LOG_CONNECT(surf_host);
266 #endif /* simgrid_EXPORTS */
269 static void xbt_log_help(void);
270 static void xbt_log_help_categories(void);
272 /** @brief Get all logging settings from the command line
274 * xbt_log_control_set() is called on each string we got from cmd line
276 void xbt_log_init(int *argc, char **argv)
278 unsigned help_requested = 0; /* 1: logs; 2: categories */
283 /* uncomment to set the LOG category to debug directly */
284 // _XBT_LOGV(log).threshold = xbt_log_priority_debug;
286 xbt_log_connect_categories();
288 /* Set logs and init log submodule */
289 for (j = i = 1; i < *argc; i++) {
290 if (!strncmp(argv[i], "--log=", strlen("--log="))) {
291 opt = strchr(argv[i], '=');
293 xbt_log_control_set(opt);
294 XBT_DEBUG("Did apply '%s' as log setting", opt);
295 } else if (!strcmp(argv[i], "--help-logs")) {
296 help_requested |= 1U;
297 } else if (!strcmp(argv[i], "--help-log-categories")) {
298 help_requested |= 2U;
308 if (help_requested) {
309 if (help_requested & 1)
311 if (help_requested & 2)
312 xbt_log_help_categories();
317 static void log_cat_exit(xbt_log_category_t cat)
319 xbt_log_category_t child;
322 if (cat->appender->free_)
323 cat->appender->free_(cat->appender);
327 if (cat->layout->free_)
328 cat->layout->free_(cat->layout);
332 for (child = cat->firstChild; child != NULL; child = child->nextSibling)
336 void xbt_log_postexit(void)
338 XBT_VERB("Exiting log");
339 xbt_os_mutex_destroy(log_cat_init_mutex);
340 xbt_dynar_free(&xbt_log_settings);
341 log_cat_exit(&_XBT_LOGV(XBT_LOG_ROOT_CAT));
344 /* Size of the static string in which we build the log string */
345 #define XBT_LOG_STATIC_BUFFER_SIZE 2048
346 /* Minimum size of the dynamic string in which we build the log string
347 (should be greater than XBT_LOG_STATIC_BUFFER_SIZE) */
348 #define XBT_LOG_DYNAMIC_BUFFER_SIZE 4096
350 void _xbt_log_event_log(xbt_log_event_t ev, const char *fmt, ...)
352 xbt_log_category_t cat = ev->cat;
354 xbt_assert(ev->priority >= 0, "Negative logging priority naturally forbidden");
355 xbt_assert(ev->priority < sizeof(xbt_log_priority_names), "Priority %d is greater than the biggest allowed value",
359 xbt_log_appender_t appender = cat->appender;
362 continue; /* No appender, try next */
364 xbt_assert(cat->layout, "No valid layout for the appender of category %s", cat->name);
366 /* First, try with a static buffer */
367 if (XBT_LOG_STATIC_BUFFER_SIZE) {
368 char buff[XBT_LOG_STATIC_BUFFER_SIZE];
370 ev->buffer_size = sizeof buff;
371 va_start(ev->ap, fmt);
372 int done = cat->layout->do_layout(cat->layout, ev, fmt);
375 appender->do_append(appender, buff);
376 continue; /* Ok, that worked: go next */
380 /* The static buffer was too small, use a dynamically expanded one */
381 ev->buffer_size = XBT_LOG_DYNAMIC_BUFFER_SIZE;
382 ev->buffer = xbt_malloc(ev->buffer_size);
384 va_start(ev->ap, fmt);
385 int done = cat->layout->do_layout(cat->layout, ev, fmt);
389 ev->buffer_size *= 2;
390 ev->buffer = xbt_realloc(ev->buffer, ev->buffer_size);
392 appender->do_append(appender, ev->buffer);
393 xbt_free(ev->buffer);
395 } while (cat->additivity && (cat = cat->parent, 1));
398 #undef XBT_LOG_DYNAMIC_BUFFER_SIZE
399 #undef XBT_LOG_STATIC_BUFFER_SIZE
403 * The standard logging macros use _XBT_LOG_ISENABLED, which calls _xbt_log_cat_init(). Thus, if we want to avoid an
404 * infinite recursion, we can not use the standard logging macros in _xbt_log_cat_init(), and in all functions called
407 * To circumvent the problem, we define the macro_xbt_log_init() as (0) for the length of the affected functions, and
408 * we do not forget to undefine it at the end!
411 static void _xbt_log_cat_apply_set(xbt_log_category_t category, xbt_log_setting_t setting)
413 #define _xbt_log_cat_init(a, b) (0)
415 if (setting->thresh != xbt_log_priority_uninitialized) {
416 xbt_log_threshold_set(category, setting->thresh);
418 XBT_DEBUG("Apply settings for category '%s': set threshold to %s (=%d)",
419 category->name, xbt_log_priority_names[category->threshold], category->threshold);
423 xbt_log_layout_set(category, xbt_log_layout_format_new(setting->fmt));
425 XBT_DEBUG("Apply settings for category '%s': set format to %s", category->name, setting->fmt);
428 if (setting->additivity != -1) {
429 xbt_log_additivity_set(category, setting->additivity);
431 XBT_DEBUG("Apply settings for category '%s': set additivity to %s",
432 category->name, (setting->additivity ? "on" : "off"));
434 if (setting->appender) {
435 xbt_log_appender_set(category, setting->appender);
436 if (!category->layout)
437 xbt_log_layout_set(category, xbt_log_layout_simple_new(NULL));
438 category->additivity = 0;
439 XBT_DEBUG("Set %p as appender of category '%s'", setting->appender, category->name);
441 #undef _xbt_log_cat_init
445 * This gets called the first time a category is referenced and performs the initialization.
446 * Also resets threshold to inherited!
448 int _xbt_log_cat_init(xbt_log_category_t category, e_xbt_log_priority_t priority)
450 #define _xbt_log_cat_init(a, b) (0)
452 if (log_cat_init_mutex != NULL)
453 xbt_os_mutex_acquire(log_cat_init_mutex);
455 if (category->initialized) {
456 if (log_cat_init_mutex != NULL)
457 xbt_os_mutex_release(log_cat_init_mutex);
458 return priority >= category->threshold;
462 xbt_log_setting_t setting = NULL;
465 XBT_DEBUG("Initializing category '%s' (firstChild=%s, nextSibling=%s)", category->name,
466 (category->firstChild ? category->firstChild->name : "none"),
467 (category->nextSibling ? category->nextSibling->name : "none"));
469 if (category == &_XBT_LOGV(XBT_LOG_ROOT_CAT)) {
470 category->threshold = xbt_log_priority_info;
471 category->appender = xbt_log_default_appender;
472 category->layout = xbt_log_default_layout;
474 if (!category->parent)
475 category->parent = &_XBT_LOGV(XBT_LOG_ROOT_CAT);
477 XBT_DEBUG("Set %s (%s) as father of %s ", category->parent->name,
478 (category->parent->initialized ? xbt_log_priority_names[category->parent->threshold] : "uninited"),
480 xbt_log_parent_set(category, category->parent);
482 if (XBT_LOG_ISENABLED(log, xbt_log_priority_debug)) {
485 xbt_log_category_t cpp = category->parent->firstChild;
488 buf = bprintf("%s %s", res, cpp->name);
492 res = xbt_strdup(cpp->name);
494 cpp = cpp->nextSibling;
497 XBT_DEBUG("Children of %s: %s; nextSibling: %s", category->parent->name, res,
498 (category->parent->nextSibling ? category->parent->nextSibling->name : "none"));
504 /* Apply the control */
505 if (xbt_log_settings) {
506 xbt_assert(category, "NULL category");
507 xbt_assert(category->name);
509 xbt_dynar_foreach(xbt_log_settings, cursor, setting) {
510 xbt_assert(setting, "Damnit, NULL cat in the list");
511 xbt_assert(setting->catname, "NULL setting(=%p)->catname", (void *) setting);
513 if (!strcmp(setting->catname, category->name)) {
515 _xbt_log_cat_apply_set(category, setting);
516 xbt_dynar_cursor_rm(xbt_log_settings, &cursor);
521 XBT_DEBUG("Category '%s': inherited threshold = %s (=%d)",
522 category->name, xbt_log_priority_names[category->threshold], category->threshold);
525 category->initialized = 1;
526 if (log_cat_init_mutex != NULL)
527 xbt_os_mutex_release(log_cat_init_mutex);
528 return priority >= category->threshold;
530 #undef _xbt_log_cat_init
533 void xbt_log_parent_set(xbt_log_category_t cat, xbt_log_category_t parent)
535 xbt_assert(cat, "NULL category to be given a parent");
536 xbt_assert(parent, "The parent category of %s is NULL", cat->name);
538 /* if the category is initialized, unlink from current parent */
539 if (cat->initialized) {
540 xbt_log_category_t *cpp = &cat->parent->firstChild;
542 while (*cpp != cat && *cpp != NULL) {
543 cpp = &(*cpp)->nextSibling;
546 xbt_assert(*cpp == cat);
547 *cpp = cat->nextSibling;
550 cat->parent = parent;
551 cat->nextSibling = parent->firstChild;
553 parent->firstChild = cat;
555 if (!parent->initialized)
556 _xbt_log_cat_init(parent, xbt_log_priority_uninitialized /* ignored */ );
558 cat->threshold = parent->threshold;
560 cat->isThreshInherited = 1;
563 static void _set_inherited_thresholds(xbt_log_category_t cat)
565 xbt_log_category_t child = cat->firstChild;
567 for (; child != NULL; child = child->nextSibling) {
568 if (child->isThreshInherited) {
569 if (cat != &_XBT_LOGV(log))
570 XBT_VERB("Set category threshold of %s to %s (=%d)",
571 child->name, xbt_log_priority_names[cat->threshold], cat->threshold);
572 child->threshold = cat->threshold;
573 _set_inherited_thresholds(child);
578 void xbt_log_threshold_set(xbt_log_category_t cat, e_xbt_log_priority_t threshold)
580 cat->threshold = threshold;
581 cat->isThreshInherited = 0;
583 _set_inherited_thresholds(cat);
586 static xbt_log_setting_t _xbt_log_parse_setting(const char *control_string)
588 const char *orig_control_string = control_string;
589 xbt_log_setting_t set = xbt_new(s_xbt_log_setting_t, 1);
592 set->thresh = xbt_log_priority_uninitialized;
594 set->additivity = -1;
595 set->appender = NULL;
597 if (!*control_string)
599 XBT_DEBUG("Parse log setting '%s'", control_string);
601 control_string += strspn(control_string, " ");
602 const char *name = control_string;
603 control_string += strcspn(control_string, ".= ");
604 const char *dot = control_string;
605 control_string += strcspn(control_string, ":= ");
606 const char *eq = control_string;
608 if(*dot != '.' && (*eq == '=' || *eq == ':'))
609 xbt_die ("Invalid control string '%s'", orig_control_string);
611 if (!strncmp(dot + 1, "threshold", (size_t) (eq - dot - 1))) {
613 char *neweq = xbt_strdup(eq + 1);
616 while (*(++p) != '\0') {
617 if (*p >= 'a' && *p <= 'z') {
622 XBT_DEBUG("New priority name = %s", neweq);
623 for (i = 0; i < xbt_log_priority_infinite; i++) {
624 if (!strncmp(xbt_log_priority_names[i], neweq, p - eq)) {
625 XBT_DEBUG("This is priority %d", i);
630 if(i<XBT_LOG_STATIC_THRESHOLD){
632 "Priority '%s' (in setting '%s') is above allowed priority '%s'.\n\n"
633 "Compiling SimGrid with -DNDEBUG forbids the levels 'trace' and 'debug'\n"
634 "while -DNLOG forbids any logging, at any level.",
635 eq + 1, name, xbt_log_priority_names[XBT_LOG_STATIC_THRESHOLD]);
637 }else if (i < xbt_log_priority_infinite) {
638 set->thresh = (e_xbt_log_priority_t) i;
641 "Unknown priority name: %s (must be one of: trace,debug,verbose,info,warning,error,critical)", eq + 1);
644 } else if (!strncmp(dot + 1, "add", (size_t) (eq - dot - 1)) ||
645 !strncmp(dot + 1, "additivity", (size_t) (eq - dot - 1))) {
646 char *neweq = xbt_strdup(eq + 1);
649 while (*(++p) != '\0') {
650 if (*p >= 'a' && *p <= 'z') {
654 if (!strcmp(neweq, "ON") || !strcmp(neweq, "YES") || !strcmp(neweq, "1")) {
660 } else if (!strncmp(dot + 1, "app", (size_t) (eq - dot - 1)) ||
661 !strncmp(dot + 1, "appender", (size_t) (eq - dot - 1))) {
662 char *neweq = xbt_strdup(eq + 1);
664 if (!strncmp(neweq, "file:", 5)) {
665 set->appender = xbt_log_appender_file_new(neweq + 5);
666 }else if (!strncmp(neweq, "rollfile:", 9)) {
667 set->appender = xbt_log_appender2_file_new(neweq + 9,1);
668 }else if (!strncmp(neweq, "splitfile:", 10)) {
669 set->appender = xbt_log_appender2_file_new(neweq + 10,0);
671 THROWF(arg_error, 0, "Unknown appender log type: '%s'", neweq);
674 } else if (!strncmp(dot + 1, "fmt", (size_t) (eq - dot - 1))) {
675 set->fmt = xbt_strdup(eq + 1);
678 snprintf(buff, MIN(512, eq - dot), "%s", dot + 1);
679 THROWF(arg_error, 0, "Unknown setting of the log category: '%s'", buff);
681 set->catname = (char *) xbt_malloc(dot - name + 1);
683 memcpy(set->catname, name, dot - name);
684 set->catname[dot - name] = '\0'; /* Just in case */
685 XBT_DEBUG("This is for cat '%s'", set->catname);
690 static xbt_log_category_t _xbt_log_cat_searchsub(xbt_log_category_t cat, char *name)
692 xbt_log_category_t child;
693 xbt_log_category_t res;
695 XBT_DEBUG("Search '%s' into '%s' (firstChild='%s'; nextSibling='%s')", name,
696 cat->name, (cat->firstChild ? cat->firstChild->name : "none"),
697 (cat->nextSibling ? cat->nextSibling->name : "none"));
698 if (!strcmp(cat->name, name))
701 for (child = cat->firstChild; child != NULL; child = child->nextSibling) {
702 XBT_DEBUG("Dig into %s", child->name);
703 res = _xbt_log_cat_searchsub(child, name);
713 * \param control_string What to parse
715 * Typically passed a command-line argument. The string has the syntax:
717 * ( [category] "." [keyword] ":" value (" ")... )...
719 * where [category] is one the category names (see \ref XBT_log_cats for a complete list of the ones defined in the
720 * SimGrid library) and keyword is one of the following:
722 * - thres: category's threshold priority. Possible values:
723 * TRACE,DEBUG,VERBOSE,INFO,WARNING,ERROR,CRITICAL
724 * - add or additivity: whether the logging actions must be passed to the parent category.
725 * Possible values: 0, 1, no, yes, on, off.
726 * Default value: yes.
727 * - fmt: the format to use. See \ref log_use_conf_fmt for more information.
728 * - app or appender: the appender to use. See \ref log_use_conf_app for more information.
730 void xbt_log_control_set(const char *control_string)
732 xbt_log_setting_t set;
734 /* To split the string in commands, and the cursors */
735 xbt_dynar_t set_strings;
741 XBT_DEBUG("Parse log settings '%s'", control_string);
743 /* Special handling of no_loc request, which asks for any file localization to be omitted (for tesh runs) */
744 if (!strcmp(control_string, "no_loc")) {
748 /* some initialization if this is the first time that this get called */
749 if (xbt_log_settings == NULL)
750 xbt_log_settings = xbt_dynar_new(sizeof(xbt_log_setting_t), _free_setting);
752 /* split the string, and remove empty entries */
753 set_strings = xbt_str_split_quoted(control_string);
755 if (xbt_dynar_is_empty(set_strings)) { /* vicious user! */
756 xbt_dynar_free(&set_strings);
760 /* Parse each entry and either use it right now (if the category was already created), or store it for further use */
761 xbt_dynar_foreach(set_strings, cpt, str) {
762 xbt_log_category_t cat = NULL;
764 set = _xbt_log_parse_setting(str);
765 cat = _xbt_log_cat_searchsub(&_XBT_LOGV(XBT_LOG_ROOT_CAT), set->catname);
768 XBT_DEBUG("Apply directly");
769 _xbt_log_cat_apply_set(cat, set);
770 _free_setting((void *) &set);
772 XBT_DEBUG("Store for further application");
773 XBT_DEBUG("push %p to the settings", (void *) set);
774 xbt_dynar_push(xbt_log_settings, &set);
777 xbt_dynar_free(&set_strings);
780 void xbt_log_appender_set(xbt_log_category_t cat, xbt_log_appender_t app)
783 if (cat->appender->free_)
784 cat->appender->free_(cat->appender);
790 void xbt_log_layout_set(xbt_log_category_t cat, xbt_log_layout_t lay)
792 #define _xbt_log_cat_init(a, b) (0)
793 if (!cat->appender) {
794 XBT_VERB ("No appender to category %s. Setting the file appender as default", cat->name);
795 xbt_log_appender_set(cat, xbt_log_appender_file_new(NULL));
798 if (cat->layout->free_) {
799 cat->layout->free_(cat->layout);
804 xbt_log_additivity_set(cat, 0);
805 #undef _xbt_log_cat_init
808 void xbt_log_additivity_set(xbt_log_category_t cat, int additivity)
810 cat->additivity = additivity;
813 static void xbt_log_help(void)
816 "Description of the logging output:\n"
818 " Threshold configuration: --log=CATEGORY_NAME.thres:PRIORITY_LEVEL\n"
819 " CATEGORY_NAME: defined in code with function 'XBT_LOG_NEW_CATEGORY'\n"
820 " PRIORITY_LEVEL: the level to print (trace,debug,verbose,info,warning,error,critical)\n"
821 " -> trace: enter and return of some functions\n"
822 " -> debug: crufty output\n"
823 " -> verbose: verbose output for the user wanting more\n"
824 " -> info: output about the regular functionning\n"
825 " -> warning: minor issue encountered\n"
826 " -> error: issue encountered\n"
827 " -> critical: major issue encountered\n"
829 " Format configuration: --log=CATEGORY_NAME.fmt:OPTIONS\n"
831 " -> %%%%: the %% char\n"
832 " -> %%n: platform-dependent line separator (LOG4J compatible)\n"
833 " -> %%e: plain old space (SimGrid extension)\n"
835 " -> %%m: user-provided message\n"
837 " -> %%c: Category name (LOG4J compatible)\n"
838 " -> %%p: Priority name (LOG4J compatible)\n"
840 " -> %%h: Hostname (SimGrid extension)\n"
841 " -> %%P: Process name (SimGrid extension)\n"
842 " -> %%t: Thread \"name\" (LOG4J compatible -- actually the address of the thread in memory)\n"
843 " -> %%i: Process PID (SimGrid extension -- this is a 'i' as in 'i'dea)\n"
845 " -> %%F: file name where the log event was raised (LOG4J compatible)\n"
846 " -> %%l: location where the log event was raised (LOG4J compatible, like '%%F:%%L' -- this is a l as in 'l'etter)\n"
847 " -> %%L: line number where the log event was raised (LOG4J compatible)\n"
848 " -> %%M: function name (LOG4J compatible -- called method name here of course).\n"
849 " Defined only when using gcc because there is no __FUNCTION__ elsewhere.\n"
851 " -> %%b: full backtrace (Called %%throwable in LOG4J). Defined only under windows or when using the GNU libc because\n"
852 " backtrace() is not defined elsewhere, and we only have a fallback for windows boxes, not mac ones for example.\n"
853 " -> %%B: short backtrace (only the first line of the %%b). Called %%throwable{short} in LOG4J; defined where %%b is.\n"
855 " -> %%d: date (UNIX-like epoch)\n"
856 " -> %%r: application age (time elapsed since the beginning of the application)\n"
859 " --help-log-categories Display the current hierarchy of log categories.\n"
860 " --log=no_loc Don't print file names in messages (for tesh tests).\n"
865 static int xbt_log_cat_cmp(const void *pa, const void *pb)
867 xbt_log_category_t a = *(xbt_log_category_t *)pa;
868 xbt_log_category_t b = *(xbt_log_category_t *)pb;
869 return strcmp(a->name, b->name);
872 static void xbt_log_help_categories_rec(xbt_log_category_t category, const char *prefix)
877 xbt_log_category_t cat;
882 if (category->parent) {
883 this_prefix = bprintf("%s \\_ ", prefix);
884 child_prefix = bprintf("%s | ", prefix);
886 this_prefix = xbt_strdup(prefix);
887 child_prefix = xbt_strdup(prefix);
890 xbt_dynar_t dynar = xbt_dynar_new(sizeof(xbt_log_category_t), NULL);
891 for (cat = category ; cat != NULL; cat = cat->nextSibling)
892 xbt_dynar_push_as(dynar, xbt_log_category_t, cat);
894 xbt_dynar_sort(dynar, xbt_log_cat_cmp);
896 xbt_dynar_foreach(dynar, i, cat){
897 if (i == xbt_dynar_length(dynar) - 1 && category->parent)
898 *strrchr(child_prefix, '|') = ' ';
899 printf("%s%s: %s\n", this_prefix, cat->name, cat->description);
900 xbt_log_help_categories_rec(cat->firstChild, child_prefix);
903 xbt_dynar_free(&dynar);
904 xbt_free(this_prefix);
905 xbt_free(child_prefix);
908 static void xbt_log_help_categories(void)
910 printf("Current log category hierarchy:\n");
911 xbt_log_help_categories_rec(&_XBT_LOGV(XBT_LOG_ROOT_CAT), " ");