1 /* log - a generic logging facility in the spirit of log4j */
3 /* Copyright (c) 2004-2018. The SimGrid Team. All rights reserved. */
5 /* This program is free software; you can redistribute it and/or modify it
6 * under the terms of the license (GNU LGPL) which comes with this package. */
8 /** @addtogroup XBT_log
9 * @brief A generic logging facility in the spirit of log4j (grounding feature)
13 /** \defgroup XBT_log_cats Existing log categories
15 * \brief (automatically extracted)
17 * This is the list of all existing log categories in SimGrid.
18 * This list is automatically extracted from the source code by the tools/doxygen/xbt_log_extract_hierarchy.pl utility.
20 * It should thus contain every categories that are defined in the SimGrid library.
21 * If you want to see the one defined in your code in addition, provide `--help-logs` on the command line of your simulator.
24 /* XBT_LOG_MAYDAY: define this to replace the logging facilities with basic
25 printf function. Useful to debug the logging facilities themselves, or to not make source analysis tools mad */
26 //#define XBT_LOG_MAYDAY
33 #include <stddef.h> /* NULL */
35 /**\brief Log priorities
38 * The different existing priorities.
42 xbt_log_priority_none = 0, /** used internally (don't poke with)*/
44 xbt_log_priority_trace = 1, /**< enter and return of some functions */
45 xbt_log_priority_debug = 2, /**< crufty output */
46 xbt_log_priority_verbose = 3, /**< verbose output for the user wanting more */
47 xbt_log_priority_info = 4, /**< output about the regular functionning */
48 xbt_log_priority_warning = 5, /**< minor issue encountered */
49 xbt_log_priority_error = 6, /**< issue encountered */
50 xbt_log_priority_critical = 7, /**< major issue encountered */
52 xbt_log_priority_infinite = 8, /**< value for XBT_LOG_STATIC_THRESHOLD to not log */
55 xbt_log_priority_uninitialized = -1 /* used internally (don't poke with) */
57 } e_xbt_log_priority_t;
60 * define NLOG to disable at compilation time any logging request
61 * define NDEBUG to disable at compilation time any logging request of priority below VERBOSE
65 * @def XBT_LOG_STATIC_THRESHOLD
68 * All logging requests with priority < XBT_LOG_STATIC_THRESHOLD are disabled at compile time, i.e., compiled out.
71 # define XBT_LOG_STATIC_THRESHOLD xbt_log_priority_infinite
75 # define XBT_LOG_STATIC_THRESHOLD xbt_log_priority_verbose
76 # else /* !NLOG && !NDEBUG */
78 # ifndef XBT_LOG_STATIC_THRESHOLD
79 # define XBT_LOG_STATIC_THRESHOLD xbt_log_priority_none
80 # endif /* !XBT_LOG_STATIC_THRESHOLD */
82 #endif /* !defined(NLOG) */
84 /* Transforms a category name to a global variable name. */
85 #define _XBT_LOGV(cat) _XBT_LOG_CONCAT(_simgrid_log_category__, cat)
86 #define _XBT_LOGV_CTOR(cat) _XBT_LOG_CONCAT2(_XBT_LOGV(cat), __constructor__)
87 #define _XBT_LOG_CONCAT(x, y) x ## y
88 #define _XBT_LOG_CONCAT2(x, y) _XBT_LOG_CONCAT(x, y)
90 /* The root of the category hierarchy. */
91 #define XBT_LOG_ROOT_CAT root
93 /* The whole tree of categories is connected by setting the address of the parent category as a field of the child one.
94 * This is normally done at the first use of the category.
96 * It is however necessary to make this connections as early as possible, if we want the category to be listed by
97 * --help-log-categories.
99 * When possible, the initializations takes place automatically before the start of main(). It's the case when
100 * compiling with gcc.
102 * For the other cases, you can use the XBT_LOG_CONNECT(cat) macro to force early initialization. See, for example,
103 * in xbt/log.c, the function xbt_log_connect_categories().
106 #define XBT_LOG_CONNECT(cat) \
108 extern void _XBT_LOGV_CTOR(cat)(void); \
109 _XBT_LOGV_CTOR(cat)(); \
112 /* XBT_LOG_NEW_SUBCATEGORY_helper:
113 * Implementation of XBT_LOG_NEW_SUBCATEGORY, which must declare "extern parent" in addition to avoid an extra
114 * declaration of root when XBT_LOG_NEW_SUBCATEGORY is called by XBT_LOG_NEW_CATEGORY */
115 #define XBT_LOG_NEW_SUBCATEGORY_helper(catName, parent, desc) \
117 extern void _XBT_LOGV_CTOR(catName)(void) XBT_ATTRIB_CONSTRUCTOR(600); \
118 void _XBT_LOGV_CTOR(catName)(void) \
120 XBT_LOG_EXTERNAL_CATEGORY(catName); \
121 if (!_XBT_LOGV(catName).initialized) { \
122 _xbt_log_cat_init(&_XBT_LOGV(catName), xbt_log_priority_uninitialized); \
126 XBT_EXPORT_NO_IMPORT(s_xbt_log_category_t) \
127 _XBT_LOGV(catName) = { \
128 &_XBT_LOGV(parent), \
129 NULL /* firstChild */, \
130 NULL /* nextSibling */, \
133 0 /*initialized */, \
134 xbt_log_priority_uninitialized /* threshold */, \
135 1 /* isThreshInherited */, \
136 NULL /* appender */, \
143 * \param catName name of new category
144 * \param parent father of the new category in the tree
145 * \param desc string describing the purpose of this category
148 * Defines a new subcategory of the parent.
150 #define XBT_LOG_NEW_SUBCATEGORY(catName, parent, desc) \
151 XBT_LOG_EXTERNAL_CATEGORY(parent); \
152 XBT_LOG_NEW_SUBCATEGORY_helper(catName, parent, desc) \
156 * \param catName name of new category
157 * \param desc string describing the purpose of this category
160 * Creates a new subcategory of the root category.
162 # define XBT_LOG_NEW_CATEGORY(catName,desc) \
163 XBT_LOG_NEW_SUBCATEGORY_helper(catName, XBT_LOG_ROOT_CAT, desc)
167 * \param cname name of the cat
170 * Indicates which category is the default one.
173 #if defined(XBT_LOG_MAYDAY) /*|| defined (NLOG) * turning logging off */
174 # define XBT_LOG_DEFAULT_CATEGORY(cname)
176 #define XBT_LOG_DEFAULT_CATEGORY(cname) \
177 XBT_ATTRIB_UNUSED static xbt_log_category_t _XBT_LOGV(default) = &_XBT_LOGV(cname)
182 * \param cname name of the cat
183 * \param desc string describing the purpose of this category
186 * Creates a new subcategory of the root category and makes it the default (used by macros that don't explicitly
187 * specify a category).
189 # define XBT_LOG_NEW_DEFAULT_CATEGORY(cname,desc) \
190 XBT_LOG_NEW_CATEGORY(cname,desc); \
191 XBT_LOG_DEFAULT_CATEGORY(cname)
195 * \param cname name of the cat
196 * \param parent name of the parent
197 * \param desc string describing the purpose of this category
200 * Creates a new subcategory of the parent category and makes it the default
201 * (used by macros that don't explicitly specify a category).
203 #define XBT_LOG_NEW_DEFAULT_SUBCATEGORY(cname, parent, desc) \
204 XBT_LOG_NEW_SUBCATEGORY(cname, parent, desc); \
205 XBT_LOG_DEFAULT_CATEGORY(cname)
209 * \param cname name of the cat
212 * Indicates that a category you'll use in this file (e.g., to get subcategories of it) really lives in another file.
215 #define XBT_LOG_EXTERNAL_CATEGORY(cname) \
216 extern s_xbt_log_category_t _XBT_LOGV(cname)
220 * \param cname name of the cat
223 * Indicates that the default category of this file was declared in another file.
226 #define XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(cname) \
227 XBT_LOG_EXTERNAL_CATEGORY(cname);\
228 XBT_LOG_DEFAULT_CATEGORY(cname)
230 /* Functions you may call */
232 XBT_PUBLIC(void) xbt_log_control_set(const char *cs);
234 /* Forward declarations */
235 typedef struct xbt_log_appender_s s_xbt_log_appender_t;
236 typedef struct xbt_log_appender_s* xbt_log_appender_t;
237 typedef struct xbt_log_layout_s s_xbt_log_layout_t;
238 typedef struct xbt_log_layout_s* xbt_log_layout_t;
239 typedef struct xbt_log_event_s s_xbt_log_event_t;
240 typedef struct xbt_log_event_s* xbt_log_event_t;
241 typedef struct xbt_log_category_s s_xbt_log_category_t;
242 typedef struct xbt_log_category_s* xbt_log_category_t;
244 /* Do NOT access any members of this structure directly. FIXME: move to private? */
246 struct xbt_log_category_s {
247 xbt_log_category_t parent;
248 xbt_log_category_t firstChild;
249 xbt_log_category_t nextSibling;
251 const char *description;
254 int isThreshInherited;
255 xbt_log_appender_t appender;
256 xbt_log_layout_t layout;
260 struct xbt_log_event_s {
261 xbt_log_category_t cat;
262 e_xbt_log_priority_t priority;
263 const char *fileName;
264 const char *functionName;
272 * \ingroup XBT_log_implem
273 * \param cat the category (not only its name, but the variable)
274 * \param thresholdPriority the priority
276 * Programatically alters a category's threshold priority (don't use).
278 XBT_PUBLIC(void) xbt_log_threshold_set(xbt_log_category_t cat, e_xbt_log_priority_t thresholdPriority);
281 * \ingroup XBT_log_implem
282 * \param cat the category (not only its name, but the variable)
283 * \param app the appender
285 * Programatically sets the category's appender. (the preferred interface is through xbt_log_control_set())
287 XBT_PUBLIC(void) xbt_log_appender_set(xbt_log_category_t cat, xbt_log_appender_t app);
289 * \ingroup XBT_log_implem
290 * \param cat the category (not only its name, but the variable)
291 * \param lay the layout
293 * Programatically sets the category's layout. (the preferred interface is through xbt_log_control_set())
295 XBT_PUBLIC(void) xbt_log_layout_set(xbt_log_category_t cat, xbt_log_layout_t lay);
298 * \ingroup XBT_log_implem
299 * \param cat the category (not only its name, but the variable)
300 * \param additivity whether logging actions must be passed to parent.
302 * Programatically sets whether the logging actions must be passed to the parent category.
303 * (the preferred interface is through xbt_log_control_set())
305 XBT_PUBLIC(void) xbt_log_additivity_set(xbt_log_category_t cat, int additivity);
307 /** @brief create a new simple layout
309 * This layout is not as flexible as the pattern one
311 XBT_PUBLIC(xbt_log_layout_t) xbt_log_layout_simple_new(char *arg);
312 XBT_PUBLIC(xbt_log_layout_t) xbt_log_layout_format_new(char *arg);
313 XBT_PUBLIC(xbt_log_appender_t) xbt_log_appender_file_new(char *arg);
314 XBT_PUBLIC(xbt_log_appender_t) xbt_log_appender2_file_new(char *arg,int roll);
316 /* ********************************** */
317 /* Functions that you shouldn't call */
318 /* ********************************** */
319 XBT_PUBLIC(void) xbt_log_init(int *argc, char **argv);
320 XBT_PUBLIC(void) _xbt_log_event_log(xbt_log_event_t ev, const char *fmt, ...) XBT_ATTRIB_PRINTF(2, 3);
321 XBT_PUBLIC(int) _xbt_log_cat_init(xbt_log_category_t category, e_xbt_log_priority_t priority);
324 XBT_PUBLIC_DATA(s_xbt_log_category_t) _XBT_LOGV(XBT_LOG_ROOT_CAT);
326 // If we `dllexport` the root log category, MinGW does not want us to take its address with the error:
327 // > initializer element is not constant
328 // When using auto-import, MinGW is happy.
329 // We should handle this for non-root log categories as well.
330 extern s_xbt_log_category_t _XBT_LOGV(XBT_LOG_ROOT_CAT);
333 extern xbt_log_appender_t xbt_log_default_appender;
334 extern xbt_log_layout_t xbt_log_default_layout;
336 /* ********************** */
337 /* Public functions again */
338 /* ********************** */
342 * \param catName name of the category
343 * \param priority minimal priority to be enabled to return true (must be #e_xbt_log_priority_t)
346 * Returns true if the given priority is enabled for the category.
347 * If you have expensive expressions that are computed outside of the log command and used only within it, you should
348 * make its evaluation conditional using this macro.
350 #define XBT_LOG_ISENABLED(catName, priority) \
351 _XBT_LOG_ISENABLEDV(_XBT_LOGV(catName), priority)
354 * Helper function that implements XBT_LOG_ISENABLED.
357 * First part is a compile-time constant.
358 * Call to xbt_log_cat_init only happens once.
360 #define _XBT_LOG_ISENABLEDV(catv, priority) \
361 (priority >= XBT_LOG_STATIC_THRESHOLD \
362 && ((catv).initialized || _xbt_log_cat_init(&(catv), priority)) \
363 && priority >= (catv).threshold)
367 * Some kludge macros to ease maintenance. See how they're used below.
369 * IMPLEMENTATION NOTE: To reduce the parameter passing overhead of an enabled message, the many parameters passed to
370 * the logging function are packed in a structure. Since these values will be usually be passed to at least 3 functions,
372 * It also allows adding new values (such as a timestamp) without breaking code.
373 * Setting the LogEvent's valist member is done inside _log_logEvent.
378 #ifdef XBT_LOG_MAYDAY
379 # define XBT_CLOG(cat, prio, ...) \
380 _XBT_IF_ONE_ARG(_XBT_CLOG_ARG1, _XBT_CLOG_ARGN, __VA_ARGS__)(__VA_ARGS__)
381 # define _XBT_CLOG_ARG1(f) \
382 fprintf(stderr,"%s:%d:\n" f, __FILE__, __LINE__)
383 # define _XBT_CLOG_ARGN(f, ...) \
384 fprintf(stderr,"%s:%d:\n" f, __FILE__, __LINE__, __VA_ARGS__)
385 # define XBT_LOG(...) XBT_CLOG(0, __VA_ARGS__)
388 // This code is duplicated to remove one level of indirection, working around a MSVC bug
389 // See: http://stackoverflow.com/questions/9183993/msvc-variadic-macro-expansion
391 # define XBT_CLOG(category, prio, ...) \
393 if (_XBT_LOG_ISENABLEDV((category), prio)) { \
394 s_xbt_log_event_t _log_ev; \
395 _log_ev.cat = &(category); \
396 _log_ev.priority = (prio); \
397 _log_ev.fileName = __FILE__; \
398 _log_ev.functionName = __func__; \
399 _log_ev.lineNum = __LINE__; \
400 _xbt_log_event_log(&_log_ev, __VA_ARGS__); \
404 # define XBT_LOG(prio,...) \
406 if (_XBT_LOG_ISENABLEDV((*_simgrid_log_category__default), prio)) { \
407 s_xbt_log_event_t _log_ev; \
408 _log_ev.cat = _simgrid_log_category__default; \
409 _log_ev.priority = (prio); \
410 _log_ev.fileName = __FILE__; \
411 _log_ev.functionName = __func__; \
412 _log_ev.lineNum = __LINE__; \
413 _xbt_log_event_log(&_log_ev, __VA_ARGS__); \
420 * \param categ the category on which to log
421 * \param ... the format string and its arguments
422 * @brief Log an event at the DEBUG priority on the specified category with these args.
424 #define XBT_CDEBUG(categ, ...) \
426 if (XBT_LOG_ISENABLED (categ, xbt_log_priority_debug)) { \
427 s_xbt_log_event_t _log_ev; \
428 _log_ev.cat = &(_XBT_LOGV(categ)); \
429 _log_ev.priority = xbt_log_priority_debug; \
430 _log_ev.fileName = __FILE__; \
431 _log_ev.functionName = __func__; \
432 _log_ev.lineNum = __LINE__; \
433 _xbt_log_event_log(&_log_ev, __VA_ARGS__); \
439 * @brief Log an event at the VERB priority on the specified category with these args.
441 #define XBT_CVERB(categ, ...) \
443 if (XBT_LOG_ISENABLED (categ, xbt_log_priority_verbose)) { \
444 s_xbt_log_event_t _log_ev; \
445 _log_ev.cat = &(_XBT_LOGV(categ)); \
446 _log_ev.priority = xbt_log_priority_verbose; \
447 _log_ev.fileName = __FILE__; \
448 _log_ev.functionName = __func__; \
449 _log_ev.lineNum = __LINE__; \
450 _xbt_log_event_log(&_log_ev, __VA_ARGS__); \
456 * @brief Log an event at the INFO priority on the specified category with these args.
458 #define XBT_CINFO(categ, ...) \
460 if (XBT_LOG_ISENABLED (categ, xbt_log_priority_info)) { \
461 s_xbt_log_event_t _log_ev; \
462 _log_ev.cat = &(_XBT_LOGV(categ)); \
463 _log_ev.priority = xbt_log_priority_info; \
464 _log_ev.fileName = __FILE__; \
465 _log_ev.functionName = __func__; \
466 _log_ev.lineNum = __LINE__; \
467 _xbt_log_event_log(&_log_ev, __VA_ARGS__); \
474 * @brief Log an event at the WARN priority on the specified category with these args.
476 #define XBT_CWARN(categ, ...) \
478 if (XBT_LOG_ISENABLED (categ, xbt_log_priority_warning)) { \
479 s_xbt_log_event_t _log_ev; \
480 _log_ev.cat = &(_XBT_LOGV(categ)); \
481 _log_ev.priority = xbt_log_priority_warning; \
482 _log_ev.fileName = __FILE__; \
483 _log_ev.functionName = __func__; \
484 _log_ev.lineNum = __LINE__; \
485 _xbt_log_event_log(&_log_ev, __VA_ARGS__); \
492 * @brief Log an event at the ERROR priority on the specified category with these args.
494 #define XBT_CERROR(categ, ...) \
496 if (XBT_LOG_ISENABLED (categ, xbt_log_priority_error)) { \
497 s_xbt_log_event_t _log_ev; \
498 _log_ev.cat = &(_XBT_LOGV(categ)); \
499 _log_ev.priority = xbt_log_priority_error; \
500 _log_ev.fileName = __FILE__; \
501 _log_ev.functionName = __func__; \
502 _log_ev.lineNum = __LINE__; \
503 _xbt_log_event_log(&_log_ev, __VA_ARGS__); \
509 * @brief Log an event at the CRITICAL priority on the specified category with these args (CCRITICALn exists for any n<10).
511 #define XBT_CCRITICAL(categ, ...) \
513 if (XBT_LOG_ISENABLED (categ, xbt_log_priority_critical)) { \
514 s_xbt_log_event_t _log_ev; \
515 _log_ev.cat = &(_XBT_LOGV(categ)); \
516 _log_ev.priority = xbt_log_priority_critical; \
517 _log_ev.fileName = __FILE__; \
518 _log_ev.functionName = __func__; \
519 _log_ev.lineNum = __LINE__; \
520 _xbt_log_event_log(&_log_ev, __VA_ARGS__); \
526 * \param ... the format string and its arguments
527 * @brief Log an event at the DEBUG priority on the default category with these args.
529 #define XBT_DEBUG(...) \
531 if (_XBT_LOG_ISENABLEDV(*_simgrid_log_category__default, \
532 xbt_log_priority_debug)) { \
533 s_xbt_log_event_t _log_ev; \
534 _log_ev.cat = _simgrid_log_category__default; \
535 _log_ev.priority = xbt_log_priority_debug; \
536 _log_ev.fileName = __FILE__; \
537 _log_ev.functionName = __func__; \
538 _log_ev.lineNum = __LINE__; \
539 _xbt_log_event_log(&_log_ev, __VA_ARGS__); \
545 * @brief Log an event at the VERB priority on the default category with these args.
547 #define XBT_VERB(...) \
549 if (_XBT_LOG_ISENABLEDV(*_simgrid_log_category__default, \
550 xbt_log_priority_verbose)) { \
551 s_xbt_log_event_t _log_ev; \
552 _log_ev.cat = _simgrid_log_category__default; \
553 _log_ev.priority = xbt_log_priority_verbose; \
554 _log_ev.fileName = __FILE__; \
555 _log_ev.functionName = __func__; \
556 _log_ev.lineNum = __LINE__; \
557 _xbt_log_event_log(&_log_ev, __VA_ARGS__); \
563 * @brief Log an event at the INFO priority on the default category with these args.
565 #define XBT_INFO(...) \
567 if (_XBT_LOG_ISENABLEDV(*_simgrid_log_category__default, \
568 xbt_log_priority_info)) { \
569 s_xbt_log_event_t _log_ev; \
570 _log_ev.cat = _simgrid_log_category__default; \
571 _log_ev.priority = xbt_log_priority_info; \
572 _log_ev.fileName = __FILE__; \
573 _log_ev.functionName = __func__; \
574 _log_ev.lineNum = __LINE__; \
575 _xbt_log_event_log(&_log_ev, __VA_ARGS__); \
581 * @brief Log an event at the WARN priority on the default category with these args.
583 #define XBT_WARN(...) \
585 if (_XBT_LOG_ISENABLEDV(*_simgrid_log_category__default, \
586 xbt_log_priority_warning)) { \
587 s_xbt_log_event_t _log_ev; \
588 _log_ev.cat = _simgrid_log_category__default; \
589 _log_ev.priority = xbt_log_priority_warning; \
590 _log_ev.fileName = __FILE__; \
591 _log_ev.functionName = __func__; \
592 _log_ev.lineNum = __LINE__; \
593 _xbt_log_event_log(&_log_ev, __VA_ARGS__); \
599 * @brief Log an event at the ERROR priority on the default category with these args.
601 #define XBT_ERROR(...) \
603 if (_XBT_LOG_ISENABLEDV(*_simgrid_log_category__default, \
604 xbt_log_priority_error)) { \
605 s_xbt_log_event_t _log_ev; \
606 _log_ev.cat = _simgrid_log_category__default; \
607 _log_ev.priority = xbt_log_priority_error; \
608 _log_ev.fileName = __FILE__; \
609 _log_ev.functionName = __func__; \
610 _log_ev.lineNum = __LINE__; \
611 _xbt_log_event_log(&_log_ev, __VA_ARGS__); \
617 * @brief Log an event at the CRITICAL priority on the default category with these args.
619 #define XBT_CRITICAL(...) \
621 if (_XBT_LOG_ISENABLEDV(*_simgrid_log_category__default, \
622 xbt_log_priority_critical)) { \
623 s_xbt_log_event_t _log_ev; \
624 _log_ev.cat = _simgrid_log_category__default; \
625 _log_ev.priority = xbt_log_priority_critical; \
626 _log_ev.fileName = __FILE__; \
627 _log_ev.functionName = __func__; \
628 _log_ev.lineNum = __LINE__; \
629 _xbt_log_event_log(&_log_ev, __VA_ARGS__); \
633 #define _XBT_IN_OUT(...) \
634 _XBT_IF_ONE_ARG(_XBT_IN_OUT_ARG1, _XBT_IN_OUT_ARGN, __VA_ARGS__)(__VA_ARGS__)
635 #define _XBT_IN_OUT_ARG1(fmt) \
636 XBT_LOG(xbt_log_priority_trace, fmt, __func__)
637 #define _XBT_IN_OUT_ARGN(fmt, ...) \
638 XBT_LOG(xbt_log_priority_trace, fmt, __func__, __VA_ARGS__)
642 * @brief Log at TRACE priority that we entered in current function, appending a user specified format.
644 #define XBT_IN(...) _XBT_IN_OUT(">> begin of %s" __VA_ARGS__)
648 * @brief Log at TRACE priority that we exited the current function, appending a user specified format.
650 #define XBT_OUT(...) _XBT_IN_OUT("<< end of %s" __VA_ARGS__)
654 * @brief Log at TRACE priority a message indicating that we reached that point, appending a user specified format.
656 #define XBT_HERE(...) XBT_LOG(xbt_log_priority_trace, "-- was here" __VA_ARGS__)
659 #endif /* ! _XBT_LOG_H_ */