1 /* log - a generic logging facility in the spirit of log4j */
3 /* Copyright (c) 2004-2017. 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_mallocator);
116 XBT_LOG_CONNECT(xbt_memory_map);
117 XBT_LOG_CONNECT(xbt_parmap);
118 XBT_LOG_CONNECT(xbt_sync);
119 XBT_LOG_CONNECT(xbt_sync_os);
121 #ifdef simgrid_EXPORTS
122 /* The following categories are only defined in libsimgrid */
126 XBT_LOG_CONNECT(lua);
127 XBT_LOG_CONNECT(lua_host);
128 XBT_LOG_CONNECT(lua_platf);
129 XBT_LOG_CONNECT(lua_debug);
133 XBT_LOG_CONNECT(instr);
134 XBT_LOG_CONNECT(instr_api);
135 XBT_LOG_CONNECT(instr_config);
136 XBT_LOG_CONNECT(instr_msg);
137 XBT_LOG_CONNECT(instr_msg_process);
138 XBT_LOG_CONNECT(instr_paje_containers);
139 XBT_LOG_CONNECT(instr_paje_header);
140 XBT_LOG_CONNECT(instr_paje_trace);
141 XBT_LOG_CONNECT(instr_paje_types);
142 XBT_LOG_CONNECT(instr_paje_values);
143 XBT_LOG_CONNECT(instr_resource);
144 XBT_LOG_CONNECT(instr_routing);
145 XBT_LOG_CONNECT(instr_surf);
148 #if SIMGRID_HAVE_JEDULE
149 XBT_LOG_CONNECT(jedule);
150 XBT_LOG_CONNECT(jed_sd);
156 XBT_LOG_CONNECT(mc_checkpoint);
157 XBT_LOG_CONNECT(mc_comm_determinism);
158 XBT_LOG_CONNECT(mc_compare);
159 XBT_LOG_CONNECT(mc_dwarf);
160 XBT_LOG_CONNECT(mc_hash);
161 XBT_LOG_CONNECT(mc_liveness);
162 XBT_LOG_CONNECT(mc_memory);
163 XBT_LOG_CONNECT(mc_page_snapshot);
164 XBT_LOG_CONNECT(mc_request);
165 XBT_LOG_CONNECT(mc_safety);
166 XBT_LOG_CONNECT(mc_VisitedState);
167 XBT_LOG_CONNECT(mc_client);
168 XBT_LOG_CONNECT(mc_client_api);
169 XBT_LOG_CONNECT(mc_comm_pattern);
170 XBT_LOG_CONNECT(mc_process);
171 XBT_LOG_CONNECT(mc_protocol);
172 XBT_LOG_CONNECT(mc_Channel);
173 XBT_LOG_CONNECT(mc_ModelChecker);
174 XBT_LOG_CONNECT(mc_RegionSnaphot);
175 XBT_LOG_CONNECT(mc_Session);
176 XBT_LOG_CONNECT(mc_state);
178 XBT_LOG_CONNECT(mc_global);
179 XBT_LOG_CONNECT(mc_config);
180 XBT_LOG_CONNECT(mc_record);
183 XBT_LOG_CONNECT(msg);
184 XBT_LOG_CONNECT(msg_action);
185 XBT_LOG_CONNECT(msg_gos);
186 XBT_LOG_CONNECT(msg_io);
187 XBT_LOG_CONNECT(msg_kernel);
188 XBT_LOG_CONNECT(msg_mailbox);
189 XBT_LOG_CONNECT(msg_process);
190 XBT_LOG_CONNECT(msg_synchro);
191 XBT_LOG_CONNECT(msg_task);
192 XBT_LOG_CONNECT(msg_vm);
195 XBT_LOG_CONNECT(s4u);
196 XBT_LOG_CONNECT(s4u_activity);
197 XBT_LOG_CONNECT(s4u_actor);
198 XBT_LOG_CONNECT(s4u_netzone);
199 XBT_LOG_CONNECT(s4u_channel);
200 XBT_LOG_CONNECT(s4u_comm);
201 XBT_LOG_CONNECT(s4u_file);
202 XBT_LOG_CONNECT(s4u_link);
203 XBT_LOG_CONNECT(s4u_vm);
206 XBT_LOG_CONNECT(sg_host);
210 XBT_LOG_CONNECT(sd_daxparse);
212 XBT_LOG_CONNECT(sd_dotparse);
214 XBT_LOG_CONNECT(sd_kernel);
215 XBT_LOG_CONNECT(sd_task);
218 XBT_LOG_CONNECT(simix);
219 XBT_LOG_CONNECT(simix_context);
220 XBT_LOG_CONNECT(simix_deployment);
221 XBT_LOG_CONNECT(simix_environment);
222 XBT_LOG_CONNECT(simix_host);
223 XBT_LOG_CONNECT(simix_io);
224 XBT_LOG_CONNECT(simix_kernel);
225 XBT_LOG_CONNECT(simix_mailbox);
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_file);
242 XBT_LOG_CONNECT(surf_kernel);
243 XBT_LOG_CONNECT(surf_lagrange);
244 XBT_LOG_CONNECT(surf_lagrange_dichotomy);
245 XBT_LOG_CONNECT(surf_maxmin);
246 XBT_LOG_CONNECT(surf_network);
248 XBT_LOG_CONNECT(ns3);
250 XBT_LOG_CONNECT(surf_parse);
251 XBT_LOG_CONNECT(surf_plugin_load);
252 XBT_LOG_CONNECT(surf_route);
253 XBT_LOG_CONNECT(surf_routing_generic);
254 XBT_LOG_CONNECT(surf_route_cluster);
255 XBT_LOG_CONNECT(surf_route_cluster_torus);
256 XBT_LOG_CONNECT(surf_route_cluster_dragonfly);
257 XBT_LOG_CONNECT(surf_route_dijkstra);
258 XBT_LOG_CONNECT(surf_route_fat_tree);
259 XBT_LOG_CONNECT(surf_route_floyd);
260 XBT_LOG_CONNECT(surf_route_full);
261 XBT_LOG_CONNECT(surf_route_none);
262 XBT_LOG_CONNECT(surf_route_vivaldi);
263 XBT_LOG_CONNECT(surf_storage);
264 XBT_LOG_CONNECT(surf_trace);
265 XBT_LOG_CONNECT(surf_vm);
266 XBT_LOG_CONNECT(surf_host);
268 #endif /* simgrid_EXPORTS */
271 static void xbt_log_help(void);
272 static void xbt_log_help_categories(void);
274 /** @brief Get all logging settings from the command line
276 * xbt_log_control_set() is called on each string we got from cmd line
278 void xbt_log_init(int *argc, char **argv)
280 unsigned help_requested = 0; /* 1: logs; 2: categories */
283 /* Set logs and init log submodule */
284 for (int i = 1; i < *argc; i++) {
285 if (!strncmp(argv[i], "--log=", strlen("--log="))) {
286 char* opt = strchr(argv[i], '=');
288 xbt_log_control_set(opt);
289 XBT_DEBUG("Did apply '%s' as log setting", opt);
290 } else if (!strcmp(argv[i], "--help-logs")) {
291 help_requested |= 1U;
292 } else if (!strcmp(argv[i], "--help-log-categories")) {
293 help_requested |= 2U;
303 if (help_requested) {
304 if (help_requested & 1)
306 if (help_requested & 2)
307 xbt_log_help_categories();
312 static void log_cat_exit(xbt_log_category_t cat)
314 xbt_log_category_t child;
317 if (cat->appender->free_)
318 cat->appender->free_(cat->appender);
322 if (cat->layout->free_)
323 cat->layout->free_(cat->layout);
327 for (child = cat->firstChild; child != NULL; child = child->nextSibling)
331 void xbt_log_postexit(void)
333 XBT_VERB("Exiting log");
334 xbt_os_mutex_destroy(log_cat_init_mutex);
335 xbt_dynar_free(&xbt_log_settings);
336 log_cat_exit(&_XBT_LOGV(XBT_LOG_ROOT_CAT));
339 /* Size of the static string in which we build the log string */
340 #define XBT_LOG_STATIC_BUFFER_SIZE 2048
341 /* Minimum size of the dynamic string in which we build the log string
342 (should be greater than XBT_LOG_STATIC_BUFFER_SIZE) */
343 #define XBT_LOG_DYNAMIC_BUFFER_SIZE 4096
345 void _xbt_log_event_log(xbt_log_event_t ev, const char *fmt, ...)
347 xbt_log_category_t cat = ev->cat;
349 xbt_assert(ev->priority >= 0, "Negative logging priority naturally forbidden");
350 xbt_assert(ev->priority < sizeof(xbt_log_priority_names), "Priority %d is greater than the biggest allowed value",
354 xbt_log_appender_t appender = cat->appender;
356 if (appender != NULL) {
357 xbt_assert(cat->layout, "No valid layout for the appender of category %s", cat->name);
359 /* First, try with a static buffer */
361 char buff[XBT_LOG_STATIC_BUFFER_SIZE];
363 ev->buffer_size = sizeof buff;
364 va_start(ev->ap, fmt);
365 done = cat->layout->do_layout(cat->layout, ev, fmt);
368 appender->do_append(appender, buff);
371 /* The static buffer was too small, use a dynamically expanded one */
372 ev->buffer_size = XBT_LOG_DYNAMIC_BUFFER_SIZE;
373 ev->buffer = xbt_malloc(ev->buffer_size);
375 va_start(ev->ap, fmt);
376 done = cat->layout->do_layout(cat->layout, ev, fmt);
380 ev->buffer_size *= 2;
381 ev->buffer = xbt_realloc(ev->buffer, ev->buffer_size);
383 appender->do_append(appender, ev->buffer);
384 xbt_free(ev->buffer);
388 if (!cat->additivity)
396 * The standard logging macros use _XBT_LOG_ISENABLED, which calls _xbt_log_cat_init(). Thus, if we want to avoid an
397 * infinite recursion, we can not use the standard logging macros in _xbt_log_cat_init(), and in all functions called
400 * To circumvent the problem, we define the macro_xbt_log_init() as (0) for the length of the affected functions, and
401 * we do not forget to undefine it at the end!
404 static void _xbt_log_cat_apply_set(xbt_log_category_t category, xbt_log_setting_t setting)
406 #define _xbt_log_cat_init(a, b) (0)
408 if (setting->thresh != xbt_log_priority_uninitialized) {
409 xbt_log_threshold_set(category, setting->thresh);
411 XBT_DEBUG("Apply settings for category '%s': set threshold to %s (=%d)",
412 category->name, xbt_log_priority_names[category->threshold], category->threshold);
416 xbt_log_layout_set(category, xbt_log_layout_format_new(setting->fmt));
418 XBT_DEBUG("Apply settings for category '%s': set format to %s", category->name, setting->fmt);
421 if (setting->additivity != -1) {
422 xbt_log_additivity_set(category, setting->additivity);
424 XBT_DEBUG("Apply settings for category '%s': set additivity to %s",
425 category->name, (setting->additivity ? "on" : "off"));
427 if (setting->appender) {
428 xbt_log_appender_set(category, setting->appender);
429 if (!category->layout)
430 xbt_log_layout_set(category, xbt_log_layout_simple_new(NULL));
431 category->additivity = 0;
432 XBT_DEBUG("Set %p as appender of category '%s'", setting->appender, category->name);
434 #undef _xbt_log_cat_init
438 * This gets called the first time a category is referenced and performs the initialization.
439 * Also resets threshold to inherited!
441 int _xbt_log_cat_init(xbt_log_category_t category, e_xbt_log_priority_t priority)
443 #define _xbt_log_cat_init(a, b) (0)
445 if (log_cat_init_mutex != NULL)
446 xbt_os_mutex_acquire(log_cat_init_mutex);
448 if (category->initialized) {
449 if (log_cat_init_mutex != NULL)
450 xbt_os_mutex_release(log_cat_init_mutex);
451 return priority >= category->threshold;
455 xbt_log_setting_t setting = NULL;
457 XBT_DEBUG("Initializing category '%s' (firstChild=%s, nextSibling=%s)", category->name,
458 (category->firstChild ? category->firstChild->name : "none"),
459 (category->nextSibling ? category->nextSibling->name : "none"));
461 if (category == &_XBT_LOGV(XBT_LOG_ROOT_CAT)) {
462 category->threshold = xbt_log_priority_info;
463 category->appender = xbt_log_default_appender;
464 category->layout = xbt_log_default_layout;
466 if (!category->parent)
467 category->parent = &_XBT_LOGV(XBT_LOG_ROOT_CAT);
469 XBT_DEBUG("Set %s (%s) as father of %s ", category->parent->name,
470 (category->parent->initialized ? xbt_log_priority_names[category->parent->threshold] : "uninited"),
472 xbt_log_parent_set(category, category->parent);
474 if (XBT_LOG_ISENABLED(log, xbt_log_priority_debug)) {
477 xbt_log_category_t cpp = category->parent->firstChild;
480 buf = bprintf("%s %s", res, cpp->name);
484 res = xbt_strdup(cpp->name);
486 cpp = cpp->nextSibling;
489 XBT_DEBUG("Children of %s: %s; nextSibling: %s", category->parent->name, res,
490 (category->parent->nextSibling ? category->parent->nextSibling->name : "none"));
496 /* Apply the control */
497 if (xbt_log_settings) {
498 xbt_assert(category, "NULL category");
499 xbt_assert(category->name);
502 xbt_dynar_foreach(xbt_log_settings, cursor, setting) {
503 xbt_assert(setting, "Damnit, NULL cat in the list");
504 xbt_assert(setting->catname, "NULL setting(=%p)->catname", (void *) setting);
506 if (!strcmp(setting->catname, category->name)) {
508 _xbt_log_cat_apply_set(category, setting);
509 xbt_dynar_cursor_rm(xbt_log_settings, &cursor);
514 XBT_DEBUG("Category '%s': inherited threshold = %s (=%d)",
515 category->name, xbt_log_priority_names[category->threshold], category->threshold);
518 category->initialized = 1;
519 if (log_cat_init_mutex != NULL)
520 xbt_os_mutex_release(log_cat_init_mutex);
521 return priority >= category->threshold;
523 #undef _xbt_log_cat_init
526 void xbt_log_parent_set(xbt_log_category_t cat, xbt_log_category_t parent)
528 xbt_assert(cat, "NULL category to be given a parent");
529 xbt_assert(parent, "The parent category of %s is NULL", cat->name);
531 /* if the category is initialized, unlink from current parent */
532 if (cat->initialized) {
533 xbt_log_category_t *cpp = &cat->parent->firstChild;
535 while (*cpp != cat && *cpp != NULL) {
536 cpp = &(*cpp)->nextSibling;
539 xbt_assert(*cpp == cat);
540 *cpp = cat->nextSibling;
543 cat->parent = parent;
544 cat->nextSibling = parent->firstChild;
546 parent->firstChild = cat;
548 if (!parent->initialized)
549 _xbt_log_cat_init(parent, xbt_log_priority_uninitialized /* ignored */ );
551 cat->threshold = parent->threshold;
553 cat->isThreshInherited = 1;
556 static void _set_inherited_thresholds(xbt_log_category_t cat)
558 xbt_log_category_t child = cat->firstChild;
560 for (; child != NULL; child = child->nextSibling) {
561 if (child->isThreshInherited) {
562 if (cat != &_XBT_LOGV(log))
563 XBT_VERB("Set category threshold of %s to %s (=%d)",
564 child->name, xbt_log_priority_names[cat->threshold], cat->threshold);
565 child->threshold = cat->threshold;
566 _set_inherited_thresholds(child);
571 void xbt_log_threshold_set(xbt_log_category_t cat, e_xbt_log_priority_t threshold)
573 cat->threshold = threshold;
574 cat->isThreshInherited = 0;
576 _set_inherited_thresholds(cat);
579 static xbt_log_setting_t _xbt_log_parse_setting(const char *control_string)
581 const char *orig_control_string = control_string;
582 xbt_log_setting_t set = xbt_new(s_xbt_log_setting_t, 1);
585 set->thresh = xbt_log_priority_uninitialized;
587 set->additivity = -1;
588 set->appender = NULL;
590 if (!*control_string)
592 XBT_DEBUG("Parse log setting '%s'", control_string);
594 control_string += strspn(control_string, " ");
595 const char *name = control_string;
596 control_string += strcspn(control_string, ".:= ");
597 const char *dot = control_string;
598 control_string += strcspn(control_string, ":= ");
599 const char *eq = control_string;
601 xbt_assert(*dot == '.' || (*eq != '=' && *eq != ':'), "Invalid control string '%s'", orig_control_string);
603 if (!strncmp(dot + 1, "threshold", (size_t) (eq - dot - 1))) {
605 char *neweq = xbt_strdup(eq + 1);
608 while (*(++p) != '\0') {
609 if (*p >= 'a' && *p <= 'z') {
614 XBT_DEBUG("New priority name = %s", neweq);
615 for (i = 0; i < xbt_log_priority_infinite; i++) {
616 if (!strncmp(xbt_log_priority_names[i], neweq, p - eq)) {
617 XBT_DEBUG("This is priority %d", i);
622 if(i<XBT_LOG_STATIC_THRESHOLD){
624 "Priority '%s' (in setting '%s') is above allowed priority '%s'.\n\n"
625 "Compiling SimGrid with -DNDEBUG forbids the levels 'trace' and 'debug'\n"
626 "while -DNLOG forbids any logging, at any level.",
627 eq + 1, name, xbt_log_priority_names[XBT_LOG_STATIC_THRESHOLD]);
629 }else if (i < xbt_log_priority_infinite) {
630 set->thresh = (e_xbt_log_priority_t) i;
633 "Unknown priority name: %s (must be one of: trace,debug,verbose,info,warning,error,critical)", eq + 1);
636 } else if (!strncmp(dot + 1, "add", (size_t) (eq - dot - 1)) ||
637 !strncmp(dot + 1, "additivity", (size_t) (eq - dot - 1))) {
638 char *neweq = xbt_strdup(eq + 1);
641 while (*(++p) != '\0') {
642 if (*p >= 'a' && *p <= 'z') {
646 if (!strcmp(neweq, "ON") || !strcmp(neweq, "YES") || !strcmp(neweq, "1")) {
652 } else if (!strncmp(dot + 1, "app", (size_t) (eq - dot - 1)) ||
653 !strncmp(dot + 1, "appender", (size_t) (eq - dot - 1))) {
654 char *neweq = xbt_strdup(eq + 1);
656 if (!strncmp(neweq, "file:", 5)) {
657 set->appender = xbt_log_appender_file_new(neweq + 5);
658 }else if (!strncmp(neweq, "rollfile:", 9)) {
659 set->appender = xbt_log_appender2_file_new(neweq + 9,1);
660 }else if (!strncmp(neweq, "splitfile:", 10)) {
661 set->appender = xbt_log_appender2_file_new(neweq + 10,0);
663 THROWF(arg_error, 0, "Unknown appender log type: '%s'", neweq);
666 } else if (!strncmp(dot + 1, "fmt", (size_t) (eq - dot - 1))) {
667 set->fmt = xbt_strdup(eq + 1);
670 snprintf(buff, MIN(512, eq - dot), "%s", dot + 1);
671 xbt_die("Unknown setting of the log category: '%s'", buff);
673 set->catname = (char *) xbt_malloc(dot - name + 1);
675 memcpy(set->catname, name, dot - name);
676 set->catname[dot - name] = '\0'; /* Just in case */
677 XBT_DEBUG("This is for cat '%s'", set->catname);
682 static xbt_log_category_t _xbt_log_cat_searchsub(xbt_log_category_t cat, char *name)
684 xbt_log_category_t child;
685 xbt_log_category_t res;
687 XBT_DEBUG("Search '%s' into '%s' (firstChild='%s'; nextSibling='%s')", name,
688 cat->name, (cat->firstChild ? cat->firstChild->name : "none"),
689 (cat->nextSibling ? cat->nextSibling->name : "none"));
690 if (!strcmp(cat->name, name))
693 for (child = cat->firstChild; child != NULL; child = child->nextSibling) {
694 XBT_DEBUG("Dig into %s", child->name);
695 res = _xbt_log_cat_searchsub(child, name);
705 * \param control_string What to parse
707 * Typically passed a command-line argument. The string has the syntax:
709 * ( [category] "." [keyword] ":" value (" ")... )...
711 * where [category] is one the category names (see \ref XBT_log_cats for a complete list of the ones defined in the
712 * SimGrid library) and keyword is one of the following:
714 * - thres: category's threshold priority. Possible values:
715 * TRACE,DEBUG,VERBOSE,INFO,WARNING,ERROR,CRITICAL
716 * - add or additivity: whether the logging actions must be passed to the parent category.
717 * Possible values: 0, 1, no, yes, on, off.
718 * Default value: yes.
719 * - fmt: the format to use. See \ref log_use_conf_fmt for more information.
720 * - app or appender: the appender to use. See \ref log_use_conf_app for more information.
722 void xbt_log_control_set(const char *control_string)
724 xbt_log_setting_t set;
726 /* To split the string in commands, and the cursors */
727 xbt_dynar_t set_strings;
733 XBT_DEBUG("Parse log settings '%s'", control_string);
735 /* Special handling of no_loc request, which asks for any file localization to be omitted (for tesh runs) */
736 if (!strcmp(control_string, "no_loc")) {
740 /* some initialization if this is the first time that this get called */
741 if (xbt_log_settings == NULL)
742 xbt_log_settings = xbt_dynar_new(sizeof(xbt_log_setting_t), _free_setting);
744 /* split the string, and remove empty entries */
745 set_strings = xbt_str_split_quoted(control_string);
747 if (xbt_dynar_is_empty(set_strings)) { /* vicious user! */
748 xbt_dynar_free(&set_strings);
752 /* Parse each entry and either use it right now (if the category was already created), or store it for further use */
753 xbt_dynar_foreach(set_strings, cpt, str) {
754 set = _xbt_log_parse_setting(str);
755 xbt_log_category_t cat = _xbt_log_cat_searchsub(&_XBT_LOGV(XBT_LOG_ROOT_CAT), set->catname);
758 XBT_DEBUG("Apply directly");
759 _xbt_log_cat_apply_set(cat, set);
760 _free_setting((void *) &set);
762 XBT_DEBUG("Store for further application");
763 XBT_DEBUG("push %p to the settings", (void *) set);
764 xbt_dynar_push(xbt_log_settings, &set);
767 xbt_dynar_free(&set_strings);
770 void xbt_log_appender_set(xbt_log_category_t cat, xbt_log_appender_t app)
773 if (cat->appender->free_)
774 cat->appender->free_(cat->appender);
780 void xbt_log_layout_set(xbt_log_category_t cat, xbt_log_layout_t lay)
782 #define _xbt_log_cat_init(a, b) (0)
783 if (!cat->appender) {
784 XBT_VERB ("No appender to category %s. Setting the file appender as default", cat->name);
785 xbt_log_appender_set(cat, xbt_log_appender_file_new(NULL));
788 if (cat->layout->free_) {
789 cat->layout->free_(cat->layout);
794 xbt_log_additivity_set(cat, 0);
795 #undef _xbt_log_cat_init
798 void xbt_log_additivity_set(xbt_log_category_t cat, int additivity)
800 cat->additivity = additivity;
803 static void xbt_log_help(void)
805 printf("Description of the logging output:\n"
807 " Threshold configuration: --log=CATEGORY_NAME.thres:PRIORITY_LEVEL\n"
808 " CATEGORY_NAME: defined in code with function 'XBT_LOG_NEW_CATEGORY'\n"
809 " PRIORITY_LEVEL: the level to print (trace,debug,verbose,info,warning,error,critical)\n"
810 " -> trace: enter and return of some functions\n"
811 " -> debug: crufty output\n"
812 " -> verbose: verbose output for the user wanting more\n"
813 " -> info: output about the regular functioning\n"
814 " -> warning: minor issue encountered\n"
815 " -> error: issue encountered\n"
816 " -> critical: major issue encountered\n"
817 " The default priority level is 'info'.\n"
819 " Format configuration: --log=CATEGORY_NAME.fmt:FORMAT\n"
820 " FORMAT string may contain:\n"
821 " -> %%%%: the %% char\n"
822 " -> %%n: platform-dependent line separator (LOG4J compatible)\n"
823 " -> %%e: plain old space (SimGrid extension)\n"
825 " -> %%m: user-provided message\n"
827 " -> %%c: Category name (LOG4J compatible)\n"
828 " -> %%p: Priority name (LOG4J compatible)\n"
830 " -> %%h: Hostname (SimGrid extension)\n"
831 " -> %%P: Process name (SimGrid extension)\n"
832 " -> %%t: Thread \"name\" (LOG4J compatible -- actually the address of the thread in memory)\n"
833 " -> %%i: Process PID (SimGrid extension -- this is a 'i' as in 'i'dea)\n"
835 " -> %%F: file name where the log event was raised (LOG4J compatible)\n"
836 " -> %%l: location where the log event was raised (LOG4J compatible, like '%%F:%%L' -- this is a l as "
838 " -> %%L: line number where the log event was raised (LOG4J compatible)\n"
839 " -> %%M: function name (LOG4J compatible -- called method name here of course).\n"
840 " Defined only when using gcc because there is no __FUNCTION__ elsewhere.\n"
842 " -> %%b: full backtrace (Called %%throwable in LOG4J). Defined only under windows or when using the "
844 " backtrace() is not defined elsewhere, and we only have a fallback for windows boxes, not "
845 "mac ones for example.\n"
846 " -> %%B: short backtrace (only the first line of the %%b). Called %%throwable{short} in LOG4J; "
847 "defined where %%b is.\n"
849 " -> %%d: date (UNIX-like epoch)\n"
850 " -> %%r: application age (time elapsed since the beginning of the application)\n"
853 " --help-log-categories Display the current hierarchy of log categories.\n"
854 " --log=no_loc Don't print file names in messages (for tesh tests).\n"
858 static int xbt_log_cat_cmp(const void *pa, const void *pb)
860 xbt_log_category_t a = *(xbt_log_category_t *)pa;
861 xbt_log_category_t b = *(xbt_log_category_t *)pb;
862 return strcmp(a->name, b->name);
865 static void xbt_log_help_categories_rec(xbt_log_category_t category, const char *prefix)
870 xbt_log_category_t cat;
875 if (category->parent) {
876 this_prefix = bprintf("%s \\_ ", prefix);
877 child_prefix = bprintf("%s | ", prefix);
879 this_prefix = xbt_strdup(prefix);
880 child_prefix = xbt_strdup(prefix);
883 xbt_dynar_t dynar = xbt_dynar_new(sizeof(xbt_log_category_t), NULL);
884 for (cat = category ; cat != NULL; cat = cat->nextSibling)
885 xbt_dynar_push_as(dynar, xbt_log_category_t, cat);
887 xbt_dynar_sort(dynar, xbt_log_cat_cmp);
889 xbt_dynar_foreach(dynar, i, cat){
890 if (i == xbt_dynar_length(dynar) - 1 && category->parent)
891 *strrchr(child_prefix, '|') = ' ';
892 printf("%s%s: %s\n", this_prefix, cat->name, cat->description);
893 xbt_log_help_categories_rec(cat->firstChild, child_prefix);
896 xbt_dynar_free(&dynar);
897 xbt_free(this_prefix);
898 xbt_free(child_prefix);
901 static void xbt_log_help_categories(void)
903 printf("Current log category hierarchy:\n");
904 xbt_log_help_categories_rec(&_XBT_LOGV(XBT_LOG_ROOT_CAT), " ");