3 /* log - a generic logging facility in the spirit of log4j */
5 /* Copyright (c) 2003, 2004 Martin Quinson. All rights reserved. */
7 /* This program is free software; you can redistribute it and/or modify it
8 * under the terms of the license (GNU LGPL) which comes with this package. */
13 #include <stdio.h> /* snprintf */
14 #include <stdlib.h> /* snprintf */
16 #include "portable.h" /* to get a working stdarg.h */
18 #include "xbt_modinter.h"
22 #include "xbt/sysdep.h"
24 #include "xbt/dynar.h"
26 /** \addtogroup XBT_log
28 * This section describes the API to the log functions used
29 * everywhere in this project.
31 \section XBT_log_toc Table of contents
43 - \ref log_API_example
52 \section log_overview 1. Introduction
54 This module is in charge of handling the log messages of every SimGrid
55 program. The main design goal are:
57 - <b>configurability</b>: the user can choose <i>at runtime</i> what messages to show and
58 what to hide, as well as how messages get displayed.
59 - <b>ease of use</b>: both to the programmer (using preprocessor macros black magic)
60 and to the user (with command line options)
61 - <b>performances</b>: logging shouldn't slow down the program when turned off, for example
62 - deal with <b>distributed settings</b>: SimGrid programs are [often] distributed ones,
63 and the logging mecanism allows to syndicate each and every log source into the same place.
64 At least, its design would allow to, once we write the last missing pieces
66 There is three main concepts in SimGrid's logging mecanism: <i>category</i>,
67 <i>priority</i> and <i>appender</i>. These three concepts work together to
68 enable developers to log messages according to message type and priority, and
69 to control at runtime how these messages are formatted and where they are
72 \subsection log_cat 1.1 Category hierarchy
74 The first and foremost advantage of any logging API over plain printf()
75 resides in its ability to disable certain log statements while allowing
76 others to print unhindered. This capability assumes that the logging space,
77 that is, the space of all possible logging statements, is categorized
78 according to some developer-chosen criteria.
80 This observation led to choosing category as the central concept of the
81 system. In a certain sense, they can be considered as logging topics or
84 \subsection log_pri 1.2 Logging priorities
86 The user can naturally declare interest into this or that logging category, but
87 he also can specify the desired level of details for each of them. This is
88 controled by the <i>priority</i> concept (which should maybe be renamed to
91 Empirically, the user can specify that he wants to see every debuging message
92 of GRAS while only being interested into the messages at level "error" or
93 higher about the XBT internals.
95 \subsection log_app 1.3 Message appenders
97 The message appenders are the elements in charge of actually displaying the
98 message to the user. For now, there is only one appender: the one able to print
99 stuff on stderr. But everything is in place internally to write new ones, such
100 as the one able to send the strings to a central server in charge of
101 syndicating the logs of every distributed daemons on a well known location.
103 It should also be possible to pass configuration informations to the appenders,
104 specifying for example that the message location (file and line number) is only
105 relevant to debugging information, not to critical error messages.
109 \subsection log_lay 1.4 Message layouts
111 The message layouts are the elements in charge of choosing how each message
112 will look like. Their result is a string which is then passed to the appender
113 attached to the category to be displayed.
115 For now, there is two layouts: The simple one, which is good for most cases,
116 and another one allowing users to specify the format they want.
118 Here are the existing format directives:
121 - %n: platform-dependant line separator (LOG4J compliant)
122 - %e: plain old space (SimGrid extension)
124 - %m: user-provided message
126 - %c: Category name (LOG4J compliant)
127 - %p: Priority name (LOG4J compliant)
129 - %h: Hostname (SimGrid extension)
130 - %t: Process name (LOG4J compliant -- thread name in LOG4J)
131 - %I: Process PID (SimGrid extension)
133 - %F: file name where the log event was raised (LOG4J compliant)
134 - %l: location where the log event was raised (LOG4J compliant, like '%F:%L')
135 - %L: line number where the log event was raised (LOG4J compliant)
136 - %M: function name (LOG4J compliant -- called method name here of course).
137 Defined only when using gcc because there is no __FUNCTION__ elsewhere.
139 - %b: full backtrace (Called %throwable in LOG4J).
140 Defined only when using the GNU libc because backtrace() is not defined
142 - %B: short backtrace (only the first line of the %b).
143 Called %throwable{short} in LOG4J, defined where %b is.
145 - %d: date (UNIX epoch)
146 - %r: application age (time elapsed since the beginning of the application)
149 If you want to mimick the simple layout with the format one, you would use this
150 format: '[%h:%i:(%I) %r] %l: [%c/%p] %m%n'. This is not completely correct
151 because the simple layout do not display the message location for messages at
152 priority INFO (thus, the fmt is '[%h:%i:(%I) %r] %l: [%c/%p] %m%n' in this
153 case). Moreover, if there is no process name (ie, messages comming from the
154 library itself, or test programs doing strange things) do not display the
155 process identity (thus, fmt is '[%r] %l: [%c/%p] %m%n' in that case, and '[%r]
156 [%c/%p] %m%n' if they are at priority INFO).
158 \subsection log_hist 1.5 History of this module
160 Historically, this module is an adaptation of the log4c project, which is dead
161 upstream, and which I was given the permission to fork under the LGPL licence
162 by the log4c's authors. The log4c project itself was loosely based on the
163 Apache project's Log4J, which also inspired Log4CC, Log4py and so on. Our work
164 differs somehow from these projects anyway, because the C programming language
165 is not object oriented.
167 \section log_API 2. Programmer interface
169 \subsection log_API_cat 2.1 Constructing the category hierarchy
171 Every category is declared by providing a name and an optional
172 parent. If no parent is explicitly named, the root category, LOG_ROOT_CAT is
173 the category's parent.
175 A category is created by a macro call at the top level of a file. A
176 category can be created with any one of the following macros:
178 - \ref XBT_LOG_NEW_CATEGORY(MyCat,desc); Create a new root
179 - \ref XBT_LOG_NEW_SUBCATEGORY(MyCat, ParentCat,desc);
180 Create a new category being child of the category ParentCat
181 - \ref XBT_LOG_NEW_DEFAULT_CATEGORY(MyCat,desc);
182 Like XBT_LOG_NEW_CATEGORY, but the new category is the default one
184 - \ref XBT_LOG_NEW_DEFAULT_SUBCATEGORY(MyCat, ParentCat,desc);
185 Like XBT_LOG_NEW_SUBCATEGORY, but the new category is the default one
188 The parent cat can be defined in the same file or in another file (in
189 which case you want to use the \ref XBT_LOG_EXTERNAL_CATEGORY macro to make
190 it visible in the current file), but each category may have only one
193 Typically, there will be a Category for each module and sub-module, so you
194 can independently control logging for each module.
196 For a list of all existing categories, please refer to the \ref XBT_log_cats
197 section. This file is generated automatically from the SimGrid source code, so
198 it should be complete and accurate.
200 \section log_API_pri 2.2 Declaring message priority
202 A category may be assigned a threshold priorty. The set of priorites are
203 defined by the \ref e_xbt_log_priority_t enum. All logging request under
204 this priority will be discarded.
206 If a given category is not assigned a threshold priority, then it inherits
207 one from its closest ancestor with an assigned threshold. To ensure that all
208 categories can eventually inherit a threshold, the root category always has
209 an assigned threshold priority.
211 Logging requests are made by invoking a logging macro on a category. All of
212 the macros have a printf-style format string followed by arguments. If you
213 compile with the -Wall option, gcc will warn you for unmatched arguments, ie
214 when you pass a pointer to a string where an integer was specified by the
215 format. This is usualy a good idea.
217 Because some C compilers do not support vararg macros, there is a version of
218 the macro for any number of arguments from 0 to 6. The macro name ends with
219 the total number of arguments.
221 Here is an example of the most basic type of macro. This is a logging
222 request with priority <i>warning</i>.
224 <code>CLOG5(MyCat, gras_log_priority_warning, "Values are: %d and '%s'", 5,
227 A logging request is said to be enabled if its priority is higher than or
228 equal to the threshold priority of its category. Otherwise, the request is
229 said to be disabled. A category without an assigned priority will inherit
230 one from the hierarchy.
232 It is possible to use any non-negative integer as a priority. If, as in the
233 example, one of the standard priorites is used, then there is a convenience
234 macro that is typically used instead. For example, the above example is
235 equivalent to the shorter:
237 <code>CWARN4(MyCat, "Values are: %d and '%s'", 5, "oops");</code>
239 \section log_API_subcat 2.3 Using a default category (the easy interface)
241 If \ref XBT_LOG_NEW_DEFAULT_SUBCATEGORY(MyCat, Parent) or
242 \ref XBT_LOG_NEW_DEFAULT_CATEGORY(MyCat) is used to create the
243 category, then the even shorter form can be used:
245 <code>WARN3("Values are: %d and '%s'", 5, "oops");</code>
247 Only one default category can be created per file, though multiple
248 non-defaults can be created and used.
250 \section log_API_easy 2.4 Putting all together: the easy interface
252 First of all, each module should register its own category into the categories
253 tree using \ref XBT_LOG_NEW_DEFAULT_SUBCATEGORY.
255 Then, logging should be done with the DEBUG<n>, VERB<n>, INFO<n>, WARN<n>,
256 ERROR<n> or CRITICAL<n> macro families (such as #DEBUG10, #VERB6,
257 #INFO8, #WARN6, #ERROR6 and #CRITICAL6). For each group, there is at
258 least 6 different macros (like DEBUG0, DEBUG1, DEBUG2, DEBUG3, DEBUG4 and
259 DEBUG5), only differing in the number of arguments passed along the format.
260 This is because we want SimGrid itself to keep compilable on ancient
261 compiler not supporting variable number of arguments to macros. But we
262 should provide a macro simpler to use for the users not interested in SP3
265 Under GCC, these macro check there arguments the same way than printf does. So,
266 if you compile with -Wall, the folliwing code will issue a warning:
267 <code>DEBUG2("Found %s (id %f)", some_string, a_double)</code>
269 If you want to specify the category to log onto (for example because you
270 have more than one category per file, add a C before the name of the log
271 producing macro (ie, use #CDEBUG10, #CVERB6, #CINFO8, #CWARN6, #CERROR6 and
272 #CCRITICAL6 and friends), and pass the category name as first argument.
274 The TRACE priority is not used the same way than the other. You should use
275 the #XBT_IN, XBT_IN<n> (up to #XBT_IN5), #XBT_OUT and #XBT_HERE macros
278 \section log_API_example 2.5 Example of use
280 Here is a more complete example:
285 / * create a category and a default subcategory * /
286 XBT_LOG_NEW_CATEGORY(VSS);
287 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(SA, VSS);
290 / * Now set the parent's priority. (the string would typcially be a runtime option) * /
291 xbt_log_control_set("SA.thresh:3");
293 / * This request is enabled, because WARNING >= INFO. * /
294 CWARN2(VSS, "Low fuel level.");
296 / * This request is disabled, because DEBUG < INFO. * /
297 CDEBUG2(VSS, "Starting search for nearest gas station.");
299 / * The default category SA inherits its priority from VSS. Thus,
300 the following request is enabled because INFO >= INFO. * /
301 INFO1("Located nearest gas station.");
303 / * This request is disabled, because DEBUG < INFO. * /
304 DEBUG1("Exiting gas station search");
309 \section log_user 3. User interface
311 \section log_use_conf 3.1 Configuration
312 Configuration is typically done during program initialization by invoking
313 the xbt_log_control_set() method. The control string passed to it typically
314 comes from the command line. Look at the documentation for that function for
315 the format of the control string.
317 Any SimGrid program can furthermore be configured at run time by passing a
318 --xbt-log argument on the command line (--gras-log, --msg-log and --surf-log
319 are synonyms provided by aestheticism). You can provide several of those
320 arguments to change the setting of several categories, they will be applied
321 from left to right. So,
322 \verbatim --xbt-log="root.thres:debug root.thres:critical"\endverbatim
323 should disable any logging.
325 Note that the quotes on above line are mandatory because there is a space in
326 the argument, so we are protecting ourselves from the shell, not from SimGrid.
327 We could also reach the same effect with this:
328 \verbatim --xbt-log=root.thres:debug --xbt-log=root.thres:critical\endverbatim
330 \section log_use_misc 3.2 Misc and Caveats
332 - Do not use any of the macros that start with '_'.
333 - Log4J has a 'rolling file appender' which you can select with a run-time
334 option and specify the max file size. This would be a nice default for
335 non-kernel applications.
336 - Careful, category names are global variables.
338 \section log_internals 4. Internal considerations
340 This module is a mess of macro black magic, and when it goes wrong, SimGrid
341 studently loose its ability to explain its problems. When messing around this
342 module, I often find useful to define XBT_LOG_MAYDAY (which turns it back to
343 good old printf) for the time of finding what's going wrong.
345 \section log_in_perf 4.1 Performance
347 Except for the first invocation of a given category, a disabled logging request
348 requires an a single comparison of a static variable to a constant.
350 There is also compile time constant, \ref XBT_LOG_STATIC_THRESHOLD, which
351 causes all logging requests with a lower priority to be optimized to 0 cost
352 by the compiler. By setting it to gras_log_priority_infinite, all logging
353 requests are statically disabled and cost nothing. Released executables
354 might be compiled with
355 \verbatim-DXBT_LOG_STATIC_THRESHOLD=gras_log_priority_infinite\endverbatim
357 Compiling with the \verbatim-DNLOG\endverbatim option disables all logging
358 requests at compilation time while the \verbatim-DNDEBUG\endverbatim disables
359 the requests of priority below INFO.
361 \todo Logging performance *may* be improved further by improving the message
362 propagation from appender to appender in the category tree.
364 \section log_in_app 4.2 Appenders
366 Each category has an optional appender. An appender is a pointer to a
367 structure which starts with a pointer to a doAppend() function. DoAppend()
368 prints a message to a log.
370 When a category is passed a message by one of the logging macros, the
371 category performs the following actions:
373 - if the category has an appender, the message is passed to the
374 appender's doAppend() function,
375 - if additivity is true for the category (which is the case by
376 default, and can be controlled by xbt_log_additivity_set()), the
377 message is passed to the category's parent.
379 By default, only the root category have an appender, and any other category has
380 its additivity set to true. This causes all messages to be logged by the root
383 The default appender function currently prints to stderr, and no other one
384 exist, even if more would be needed, like the one able to send the logs to a
385 remote dedicated server, or other ones offering different output formats.
386 This is on our TODO list for quite a while now, but your help would be
393 xbt_log_appender_t xbt_log_default_appender = NULL; /* set in log_init */
394 xbt_log_layout_t xbt_log_default_layout = NULL; /* set in log_init */
399 e_xbt_log_priority_t thresh;
402 } s_xbt_log_setting_t,*xbt_log_setting_t;
404 static xbt_dynar_t xbt_log_settings=NULL;
406 static void _free_setting(void *s) {
407 xbt_log_setting_t set=*(xbt_log_setting_t*)s;
416 const char *xbt_log_priority_names[8] = {
427 XBT_PUBLIC_DATA(s_xbt_log_category_t) _XBT_LOGV(XBT_LOG_ROOT_CAT) = {
429 "root", xbt_log_priority_uninitialized, 0,
433 XBT_LOG_NEW_CATEGORY(xbt,"All XBT categories (simgrid toolbox)");
434 XBT_LOG_NEW_CATEGORY(surf,"All SURF categories");
435 XBT_LOG_NEW_CATEGORY(msg,"All MSG categories");
436 XBT_LOG_NEW_CATEGORY(simix,"All SIMIX categories");
437 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(log,xbt,"Loggings from the logging mechanism itself");
439 /** @brief Get all logging settings from the command line
441 * xbt_log_control_set() is called on each string we got from cmd line
443 void xbt_log_init(int *argc,char **argv) {
447 /* create the default appender and install it in the root category,
448 which were already created (damnit. Too slow little beetle)*/
449 xbt_log_default_appender = xbt_log_appender_file_new(NULL);
450 xbt_log_default_layout = xbt_log_layout_simple_new(NULL);
451 _XBT_LOGV(XBT_LOG_ROOT_CAT).appender = xbt_log_default_appender;
452 _XBT_LOGV(XBT_LOG_ROOT_CAT).layout = xbt_log_default_layout;
455 /* Set logs and init log submodule */
456 for (i=1; i<*argc; i++){
457 if (!strncmp(argv[i],"--log=",strlen("--log=")) ||
458 !strncmp(argv[i],"--gras-log=",strlen("--gras-log=")) ||
459 !strncmp(argv[i],"--surf-log=",strlen("--surf-log=")) ||
460 !strncmp(argv[i],"--msg-log=",strlen("--msg-log=")) ||
461 !strncmp(argv[i],"--simix-log=",strlen("--simix-log=")) ||
462 !strncmp(argv[i],"--xbt-log=",strlen("--xbt-log="))){
464 if (strncmp(argv[i],"--log=",strlen("--log=")))
465 WARN2("Option %.*s is deprecated and will disapear in the future. Use --log instead.",
466 strchr(argv[i],'=')-argv[i],argv[i]);
468 opt=strchr(argv[i],'=');
470 xbt_log_control_set(opt);
471 DEBUG1("Did apply '%s' as log setting",opt);
472 /*remove this from argv*/
474 for (j=i+1; j<*argc; j++){
480 i--; /* compensate effect of next loop incrementation */
485 static void log_cat_exit(xbt_log_category_t cat) {
486 xbt_log_category_t child;
489 if (cat->appender->free_)
490 cat->appender->free_(cat->appender);
494 if (cat->layout->free_)
495 cat->layout->free_(cat->layout);
499 for(child=cat->firstChild ; child != NULL; child = child->nextSibling)
503 void xbt_log_exit(void) {
504 VERB0("Exiting log");
505 xbt_dynar_free(&xbt_log_settings);
506 log_cat_exit(&_XBT_LOGV(XBT_LOG_ROOT_CAT));
510 void _xbt_log_event_log( xbt_log_event_t ev, const char *fmt, ...) {
512 xbt_log_category_t cat = ev->cat;
514 fprintf(stderr,"XXXXXXXXXXXXXXXXXXX\nXXX Warning, logs not usable here. Either before xbt_init() or after xbt_exit().\nXXXXXXXXXXXXXXXXXXX\n");
515 va_start(ev->ap, fmt);
516 vfprintf(stderr,fmt,ev->ap);
518 xbt_backtrace_display();
522 va_start(ev->ap, fmt);
524 xbt_log_appender_t appender = cat->appender;
525 if (appender != NULL) {
526 xbt_assert1(cat->layout,"No valid layout for the appender of category %s",cat->name);
527 char *str= cat->layout->do_layout(cat->layout, ev, fmt);
528 appender->do_append(appender, str);
530 if (!cat->additivity)
539 * This gets called the first time a category is referenced and performs the
541 * Also resets threshold to inherited!
543 int _xbt_log_cat_init(xbt_log_category_t category,
544 e_xbt_log_priority_t priority) {
546 xbt_log_setting_t setting=NULL;
548 s_xbt_log_event_t _log_ev;
550 if(category == &_XBT_LOGV(XBT_LOG_ROOT_CAT)){
551 category->threshold = xbt_log_priority_info;/* xbt_log_priority_debug*/;
552 category->appender = xbt_log_default_appender;
553 category->layout = xbt_log_default_layout;
556 if (!category->parent)
557 category->parent = &_XBT_LOGV(XBT_LOG_ROOT_CAT);
559 xbt_log_parent_set(category, category->parent);
562 /* Apply the control */
563 if (!xbt_log_settings)
564 return priority >= category->threshold;
566 xbt_assert0(category,"NULL category");
567 xbt_assert(category->name);
569 xbt_dynar_foreach(xbt_log_settings,cursor,setting) {
570 xbt_assert0(setting,"Damnit, NULL cat in the list");
571 xbt_assert1(setting->catname,"NULL setting(=%p)->catname",(void*)setting);
573 if (!strcmp(setting->catname,category->name)) {
577 if (setting->thresh != xbt_log_priority_uninitialized) {
578 xbt_log_threshold_set(category, setting->thresh);
580 if (category->threshold <= xbt_log_priority_debug) {
581 _log_ev.cat = category;
582 _log_ev.priority = xbt_log_priority_debug;
583 _log_ev.fileName = __FILE__ ;
584 _log_ev.functionName = _XBT_FUNCTION ;
585 _log_ev.lineNum = __LINE__ ;
587 _xbt_log_event_log(&_log_ev,
588 "Apply settings for category '%s': set threshold to %s (=%d)",
590 xbt_log_priority_names[category->threshold],
591 category->threshold);
596 xbt_log_layout_set(category,xbt_log_layout_format_new(setting->fmt));
598 if (category->threshold <= xbt_log_priority_debug) {
599 _log_ev.cat = category;
600 _log_ev.priority = xbt_log_priority_debug;
601 _log_ev.fileName = __FILE__ ;
602 _log_ev.functionName = _XBT_FUNCTION ;
603 _log_ev.lineNum = __LINE__ ;
605 _xbt_log_event_log(&_log_ev,
606 "Apply settings for category '%s': set format to %s",
612 if (setting->additivity != -1) {
613 xbt_log_additivity_set(category,setting->additivity);
615 if (category->threshold <= xbt_log_priority_debug) {
616 _log_ev.cat = category;
617 _log_ev.priority = xbt_log_priority_debug;
618 _log_ev.fileName = __FILE__ ;
619 _log_ev.functionName = _XBT_FUNCTION ;
620 _log_ev.lineNum = __LINE__ ;
622 _xbt_log_event_log(&_log_ev,
623 "Apply settings for category '%s': set additivity to %s",
625 (setting->additivity?"on":"off"));
629 xbt_dynar_cursor_rm(xbt_log_settings,&cursor);
633 if (!found && category->threshold <= xbt_log_priority_verbose) {
635 _log_ev.cat = category;
636 _log_ev.priority = xbt_log_priority_verbose;
637 _log_ev.fileName = __FILE__ ;
638 _log_ev.functionName = _XBT_FUNCTION ;
639 _log_ev.lineNum = __LINE__ ;
641 _xbt_log_event_log(&_log_ev,
642 "Category '%s': inherited threshold = %s (=%d)",
644 xbt_log_priority_names[category->threshold], category->threshold);
647 return priority >= category->threshold;
650 void xbt_log_parent_set(xbt_log_category_t cat,xbt_log_category_t parent)
653 xbt_assert0(cat,"NULL category to be given a parent");
654 xbt_assert1(parent,"The parent category of %s is NULL",cat->name);
657 * if the threshold is initialized
658 * unlink from current parent
660 if(cat->threshold != xbt_log_priority_uninitialized){
662 xbt_log_category_t* cpp = &parent->firstChild;
664 while(*cpp != cat && *cpp != NULL) {
665 cpp = &(*cpp)->nextSibling;
668 xbt_assert(*cpp == cat);
669 *cpp = cat->nextSibling;
672 cat->parent = parent;
673 cat->nextSibling = parent->firstChild;
675 parent->firstChild = cat;
677 if (parent->threshold == xbt_log_priority_uninitialized){
679 _xbt_log_cat_init(parent,
680 xbt_log_priority_uninitialized/* ignored*/);
683 cat->threshold = parent->threshold;
685 cat->isThreshInherited = 1;
689 static void _set_inherited_thresholds(xbt_log_category_t cat) {
692 xbt_log_category_t child = cat->firstChild;
694 for( ; child != NULL; child = child->nextSibling) {
695 if (child->isThreshInherited) {
696 if (cat != &_XBT_LOGV(log))
697 VERB3("Set category threshold of %s to %s (=%d)",
698 child->name,xbt_log_priority_names[cat->threshold],cat->threshold);
699 child->threshold = cat->threshold;
700 _set_inherited_thresholds(child);
707 void xbt_log_threshold_set(xbt_log_category_t cat,
708 e_xbt_log_priority_t threshold) {
709 cat->threshold = threshold;
710 cat->isThreshInherited = 0;
712 _set_inherited_thresholds(cat);
716 static xbt_log_setting_t _xbt_log_parse_setting(const char* control_string) {
718 xbt_log_setting_t set = xbt_new(s_xbt_log_setting_t,1);
719 const char *name, *dot, *eq;
722 set->thresh = xbt_log_priority_uninitialized;
724 set->additivity = -1;
726 if (!*control_string)
728 DEBUG1("Parse log setting '%s'",control_string);
730 control_string += strspn(control_string, " ");
731 name = control_string;
732 control_string += strcspn(control_string, ".= ");
733 dot = control_string;
734 control_string += strcspn(control_string, ":= ");
736 control_string += strcspn(control_string, " ");
738 xbt_assert1(*dot == '.' && (*eq == '=' || *eq == ':'),
739 "Invalid control string '%s'",control_string);
741 if (!strncmp(dot + 1, "thresh", (size_t)(eq - dot - 1))) {
743 char *neweq=xbt_strdup(eq+1);
746 while (*(++p) != '\0') {
747 if (*p >= 'a' && *p <= 'z') {
752 DEBUG1("New priority name = %s",neweq);
753 for (i=0; i<xbt_log_priority_infinite; i++) {
754 if (!strncmp(xbt_log_priority_names[i],neweq,p-eq)) {
755 DEBUG1("This is priority %d",i);
759 if (i<xbt_log_priority_infinite) {
760 set->thresh= (e_xbt_log_priority_t) i;
763 "Unknown priority name: %s (must be one of: trace,debug,verbose,info,warning,error,critical)",eq+1);
766 } else if ( !strncmp(dot + 1, "add", (size_t)(eq - dot - 1)) ||
767 !strncmp(dot + 1, "additivity", (size_t)(eq - dot - 1)) ) {
769 char *neweq=xbt_strdup(eq+1);
772 while (*(++p) != '\0') {
773 if (*p >= 'a' && *p <= 'z') {
777 if ( !strcmp(neweq,"ON") ||
778 !strcmp(neweq,"YES") ||
779 !strcmp(neweq,"1") ) {
785 } else if (!strncmp(dot + 1, "fmt", (size_t)(eq - dot - 1))) {
786 set->fmt = xbt_strdup(eq+1);
789 snprintf(buff,min(512,eq - dot),"%s",dot+1);
790 THROW1(arg_error,0,"Unknown setting of the log category: '%s'",buff);
792 set->catname=(char*)xbt_malloc(dot - name+1);
794 memcpy(set->catname,name,dot-name);
795 set->catname[dot-name]='\0'; /* Just in case */
796 DEBUG1("This is for cat '%s'", set->catname);
801 static xbt_log_category_t _xbt_log_cat_searchsub(xbt_log_category_t cat,char *name) {
802 xbt_log_category_t child;
804 if (!strcmp(cat->name,name))
807 for(child=cat->firstChild ; child != NULL; child = child->nextSibling)
808 return _xbt_log_cat_searchsub(child,name);
810 THROW1(not_found_error,0,"No such category: %s", name);
815 * \param control_string What to parse
817 * Typically passed a command-line argument. The string has the syntax:
819 * ( [category] "." [keyword] ":" value (" ")... )...
821 * where [category] is one the category names (see \ref XBT_log_cats for
822 * a complete list of the ones defined in the SimGrid library)
823 * and keyword is one of the following:
825 * - thres: category's threshold priority. Possible values:
826 * TRACE,DEBUG,VERBOSE,INFO,WARNING,ERROR,CRITICAL
827 * - add or additivity: whether the logging actions must be passed to
828 * the parent category.
829 * Possible values: 0, 1, no, yes, on, off.
830 * Default value: yes.
831 * - fmt: the format to use. See \ref log_lay for more information.
834 void xbt_log_control_set(const char* control_string) {
835 xbt_log_setting_t set;
837 /* To split the string in commands, and the cursors */
838 xbt_dynar_t set_strings;
844 DEBUG1("Parse log settings '%s'",control_string);
846 /* some initialization if this is the first time that this get called */
847 if (xbt_log_settings == NULL)
848 xbt_log_settings = xbt_dynar_new(sizeof(xbt_log_setting_t),
851 /* split the string, and remove empty entries */
852 set_strings=xbt_str_split_quoted(control_string);
854 xbt_dynar_foreach(set_strings,cpt,str) {
855 xbt_str_trim(str,NULL);
857 xbt_dynar_cursor_rm(set_strings,&cpt);
862 if (xbt_dynar_length(set_strings) == 0) { /* vicious user! */
863 xbt_dynar_free(&set_strings);
867 /* Parse each entry and either use it right now (if the category was already
868 created), or store it for further use */
869 xbt_dynar_foreach(set_strings,cpt,str) {
870 xbt_log_category_t cat=NULL;
874 set = _xbt_log_parse_setting(str);
877 cat = _xbt_log_cat_searchsub(&_XBT_LOGV(root),set->catname);
880 if (e.category != not_found_error)
887 DEBUG0("Apply directly");
888 xbt_log_threshold_set(cat,set->thresh);
889 _free_setting((void*)&set);
892 DEBUG0("Store for further application");
893 DEBUG1("push %p to the settings",(void*)set);
894 xbt_dynar_push(xbt_log_settings,&set);
897 xbt_dynar_free(&set_strings);
900 void xbt_log_appender_set(xbt_log_category_t cat, xbt_log_appender_t app) {
902 if (cat->appender->free_)
903 cat->appender->free_(cat->appender);
908 void xbt_log_layout_set(xbt_log_category_t cat, xbt_log_layout_t lay) {
909 if (!cat->appender) {
910 VERB1("No appender to category %s. Setting the file appender as default",
912 xbt_log_appender_set(cat,xbt_log_appender_file_new(NULL));
915 if (cat->layout->free_) {
916 cat->layout->free_(cat->layout);
921 xbt_log_additivity_set(cat,0);
924 void xbt_log_additivity_set(xbt_log_category_t cat, int additivity) {
925 cat->additivity = additivity;