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
25 /* XBT_LOG_MAYDAY: define this to replace the logging facilities with basic
26 printf function. Useful to debug the logging facilities themselves, or to not make source analysis tools mad */
27 //#define XBT_LOG_MAYDAY
33 #include <stddef.h> /* NULL */
36 /**@brief Log priorities
39 * The different existing priorities.
43 xbt_log_priority_none = 0, /** used internally (don't poke with)*/
45 xbt_log_priority_trace = 1, /**< enter and return of some functions */
46 xbt_log_priority_debug = 2, /**< crufty output */
47 xbt_log_priority_verbose = 3, /**< verbose output for the user wanting more */
48 xbt_log_priority_info = 4, /**< output about the regular functionning */
49 xbt_log_priority_warning = 5, /**< minor issue encountered */
50 xbt_log_priority_error = 6, /**< issue encountered */
51 xbt_log_priority_critical = 7, /**< major issue encountered */
53 xbt_log_priority_infinite = 8, /**< value for XBT_LOG_STATIC_THRESHOLD to not log */
56 xbt_log_priority_uninitialized = -1 /* used internally (don't poke with) */
58 } e_xbt_log_priority_t;
61 * define NLOG to disable at compilation time any logging request
62 * define NDEBUG to disable at compilation time any logging request of priority below VERBOSE
66 * @def XBT_LOG_STATIC_THRESHOLD
69 * All logging requests with priority < XBT_LOG_STATIC_THRESHOLD are disabled at compile time, i.e., compiled out.
72 # define XBT_LOG_STATIC_THRESHOLD xbt_log_priority_infinite
76 # define XBT_LOG_STATIC_THRESHOLD xbt_log_priority_verbose
77 # else /* !NLOG && !NDEBUG */
79 # ifndef XBT_LOG_STATIC_THRESHOLD
80 # define XBT_LOG_STATIC_THRESHOLD xbt_log_priority_none
81 # endif /* !XBT_LOG_STATIC_THRESHOLD */
83 #endif /* !defined(NLOG) */
85 /* Transforms a category name to a global variable name. */
86 #define _XBT_LOGV(cat) _XBT_LOG_CONCAT(_simgrid_log_category__, cat)
87 #define _XBT_LOGV_CTOR(cat) _XBT_LOG_CONCAT2(_XBT_LOGV(cat), __constructor__)
88 #define _XBT_LOG_CONCAT(x, y) x ## y
89 #define _XBT_LOG_CONCAT2(x, y) _XBT_LOG_CONCAT(x, y)
91 /* The root of the category hierarchy. */
92 #define XBT_LOG_ROOT_CAT root
94 /* The whole tree of categories is connected by setting the address of the parent category as a field of the child one.
95 * This is normally done at the first use of the category.
97 * It is however necessary to make this connections as early as possible, if we want the category to be listed by
98 * --help-log-categories. We use constructor attributes for these initializations to take place automatically before the
102 /* XBT_LOG_NEW_SUBCATEGORY_helper:
103 * Implementation of XBT_LOG_NEW_SUBCATEGORY, which must declare "extern parent" in addition to avoid an extra
104 * declaration of root when XBT_LOG_NEW_SUBCATEGORY is called by XBT_LOG_NEW_CATEGORY */
105 #define XBT_LOG_NEW_SUBCATEGORY_helper(catName, parent, desc) \
107 extern void _XBT_LOGV_CTOR(catName)(void) XBT_ATTRIB_CONSTRUCTOR(600); \
108 void _XBT_LOGV_CTOR(catName)(void) \
110 XBT_LOG_EXTERNAL_CATEGORY(catName); \
111 if (!_XBT_LOGV(catName).initialized) { \
112 _xbt_log_cat_init(&_XBT_LOGV(catName), xbt_log_priority_uninitialized); \
116 XBT_EXPORT_NO_IMPORT s_xbt_log_category_t _XBT_LOGV(catName) = { \
117 &_XBT_LOGV(parent), \
118 NULL /* firstChild */, \
119 NULL /* nextSibling */, \
122 0 /*initialized */, \
123 xbt_log_priority_uninitialized /* threshold */, \
124 1 /* isThreshInherited */, \
125 NULL /* appender */, \
132 * @param catName name of new category
133 * @param parent father of the new category in the tree
134 * @param desc string describing the purpose of this category
137 * Defines a new subcategory of the parent.
139 #define XBT_LOG_NEW_SUBCATEGORY(catName, parent, desc) \
140 XBT_LOG_EXTERNAL_CATEGORY(parent); \
141 XBT_LOG_NEW_SUBCATEGORY_helper(catName, parent, desc)
145 * @param catName name of new category
146 * @param desc string describing the purpose of this category
149 * Creates a new subcategory of the root category.
151 # define XBT_LOG_NEW_CATEGORY(catName,desc) \
152 XBT_LOG_NEW_SUBCATEGORY_helper(catName, XBT_LOG_ROOT_CAT, desc)
156 * @param cname name of the cat
159 * Indicates which category is the default one.
162 #if defined(XBT_LOG_MAYDAY) /*|| defined (NLOG) * turning logging off */
163 # define XBT_LOG_DEFAULT_CATEGORY(cname)
165 #define XBT_LOG_DEFAULT_CATEGORY(cname) \
166 XBT_ATTRIB_UNUSED static xbt_log_category_t _XBT_LOGV(default) = &_XBT_LOGV(cname)
171 * @param cname name of the cat
172 * @param desc string describing the purpose of this category
175 * Creates a new subcategory of the root category and makes it the default (used by macros that don't explicitly
176 * specify a category).
178 # define XBT_LOG_NEW_DEFAULT_CATEGORY(cname,desc) \
179 XBT_LOG_NEW_CATEGORY(cname,desc); \
180 XBT_LOG_DEFAULT_CATEGORY(cname)
184 * @param cname name of the cat
185 * @param parent name of the parent
186 * @param desc string describing the purpose of this category
189 * Creates a new subcategory of the parent category and makes it the default
190 * (used by macros that don't explicitly specify a category).
192 #define XBT_LOG_NEW_DEFAULT_SUBCATEGORY(cname, parent, desc) \
193 XBT_LOG_NEW_SUBCATEGORY(cname, parent, desc); \
194 XBT_LOG_DEFAULT_CATEGORY(cname)
198 * @param cname name of the cat
201 * Indicates that a category you'll use in this file (e.g., to get subcategories of it) really lives in another file.
204 #define XBT_LOG_EXTERNAL_CATEGORY(cname) \
205 extern s_xbt_log_category_t _XBT_LOGV(cname)
209 * @param cname name of the cat
212 * Indicates that the default category of this file was declared in another file.
215 #define XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(cname) \
216 XBT_LOG_EXTERNAL_CATEGORY(cname);\
217 XBT_LOG_DEFAULT_CATEGORY(cname)
219 /* Functions you may call */
221 XBT_PUBLIC void xbt_log_control_set(const char* cs);
223 /* Forward declarations */
224 typedef struct xbt_log_appender_s s_xbt_log_appender_t;
225 typedef s_xbt_log_appender_t* xbt_log_appender_t;
226 typedef struct xbt_log_layout_s s_xbt_log_layout_t;
227 typedef s_xbt_log_layout_t* xbt_log_layout_t;
228 typedef struct xbt_log_event_s s_xbt_log_event_t;
229 typedef s_xbt_log_event_t* xbt_log_event_t;
230 typedef struct xbt_log_category_s s_xbt_log_category_t;
231 typedef s_xbt_log_category_t* xbt_log_category_t;
233 /* Do NOT access any members of this structure directly. FIXME: move to private? */
235 struct xbt_log_category_s {
236 xbt_log_category_t parent;
237 xbt_log_category_t firstChild;
238 xbt_log_category_t nextSibling;
240 const char *description;
243 int isThreshInherited;
244 xbt_log_appender_t appender;
245 xbt_log_layout_t layout;
249 struct xbt_log_event_s {
250 xbt_log_category_t cat;
251 e_xbt_log_priority_t priority;
252 const char *fileName;
253 const char *functionName;
261 * @ingroup XBT_log_implem
262 * @param cat the category (not only its name, but the variable)
263 * @param thresholdPriority the priority
265 * Programatically alters a category's threshold priority (don't use).
267 XBT_PUBLIC void xbt_log_threshold_set(xbt_log_category_t cat, e_xbt_log_priority_t thresholdPriority);
270 * @ingroup XBT_log_implem
271 * @param cat the category (not only its name, but the variable)
272 * @param app the appender
274 * Programatically sets the category's appender. (the preferred interface is through xbt_log_control_set())
276 XBT_PUBLIC void xbt_log_appender_set(xbt_log_category_t cat, xbt_log_appender_t app);
278 * @ingroup XBT_log_implem
279 * @param cat the category (not only its name, but the variable)
280 * @param lay the layout
282 * Programatically sets the category's layout. (the preferred interface is through xbt_log_control_set())
284 XBT_PUBLIC void xbt_log_layout_set(xbt_log_category_t cat, xbt_log_layout_t lay);
287 * @ingroup XBT_log_implem
288 * @param cat the category (not only its name, but the variable)
289 * @param additivity whether logging actions must be passed to parent.
291 * Programatically sets whether the logging actions must be passed to the parent category.
292 * (the preferred interface is through xbt_log_control_set())
294 XBT_PUBLIC void xbt_log_additivity_set(xbt_log_category_t cat, int additivity);
296 /** @brief create a new simple layout
298 * This layout is not as flexible as the pattern one
300 XBT_PUBLIC xbt_log_layout_t xbt_log_layout_simple_new(char* arg);
301 XBT_PUBLIC xbt_log_layout_t xbt_log_layout_format_new(char* arg);
302 XBT_PUBLIC xbt_log_appender_t xbt_log_appender_file_new(char* arg);
303 XBT_PUBLIC xbt_log_appender_t xbt_log_appender2_file_new(char* arg, int roll);
305 /* ********************************** */
306 /* Functions that you shouldn't call */
307 /* ********************************** */
308 XBT_PUBLIC void xbt_log_init(int* argc, char** argv);
309 XBT_PUBLIC void _xbt_log_event_log(xbt_log_event_t ev, const char* fmt, ...) XBT_ATTRIB_PRINTF(2, 3);
310 XBT_PUBLIC int _xbt_log_cat_init(xbt_log_category_t category, e_xbt_log_priority_t priority);
313 XBT_PUBLIC_DATA s_xbt_log_category_t _XBT_LOGV(XBT_LOG_ROOT_CAT);
315 // If we `dllexport` the root log category, MinGW does not want us to take its address with the error:
316 // > initializer element is not constant
317 // When using auto-import, MinGW is happy.
318 // We should handle this for non-root log categories as well.
319 extern s_xbt_log_category_t _XBT_LOGV(XBT_LOG_ROOT_CAT);
322 extern xbt_log_appender_t xbt_log_default_appender;
323 extern xbt_log_layout_t xbt_log_default_layout;
325 /* ********************** */
326 /* Public functions again */
327 /* ********************** */
331 * @param catName name of the category
332 * @param priority minimal priority to be enabled to return true (must be #e_xbt_log_priority_t)
335 * Returns true if the given priority is enabled for the category.
336 * If you have expensive expressions that are computed outside of the log command and used only within it, you should
337 * make its evaluation conditional using this macro.
339 #define XBT_LOG_ISENABLED(catName, priority) \
340 _XBT_LOG_ISENABLEDV(_XBT_LOGV(catName), priority)
343 * Helper function that implements XBT_LOG_ISENABLED.
346 * First part is a compile-time constant.
347 * Call to xbt_log_cat_init only happens once.
349 #define _XBT_LOG_ISENABLEDV(catv, priority) \
350 (priority >= XBT_LOG_STATIC_THRESHOLD \
351 && ((catv).initialized || _xbt_log_cat_init(&(catv), priority)) \
352 && priority >= (catv).threshold)
356 * Some kludge macros to ease maintenance. See how they're used below.
358 * IMPLEMENTATION NOTE: To reduce the parameter passing overhead of an enabled message, the many parameters passed to
359 * the logging function are packed in a structure. Since these values will be usually be passed to at least 3 functions,
361 * It also allows adding new values (such as a timestamp) without breaking code.
362 * Setting the LogEvent's valist member is done inside _log_logEvent.
367 #ifdef XBT_LOG_MAYDAY
368 # define XBT_CLOG(cat, prio, ...) \
369 _XBT_IF_ONE_ARG(_XBT_CLOG_ARG1, _XBT_CLOG_ARGN, __VA_ARGS__)(__VA_ARGS__)
370 # define _XBT_CLOG_ARG1(f) \
371 fprintf(stderr,"%s:%d:\n" f, __FILE__, __LINE__)
372 # define _XBT_CLOG_ARGN(f, ...) \
373 fprintf(stderr,"%s:%d:\n" f, __FILE__, __LINE__, __VA_ARGS__)
374 # define XBT_LOG(...) XBT_CLOG(0, __VA_ARGS__)
377 // This code is duplicated to remove one level of indirection, working around a MSVC bug
378 // See: http://stackoverflow.com/questions/9183993/msvc-variadic-macro-expansion
380 # define XBT_CLOG(category, prio, ...) \
382 if (_XBT_LOG_ISENABLEDV((category), prio)) { \
383 s_xbt_log_event_t _log_ev; \
384 _log_ev.cat = &(category); \
385 _log_ev.priority = (prio); \
386 _log_ev.fileName = __FILE__; \
387 _log_ev.functionName = __func__; \
388 _log_ev.lineNum = __LINE__; \
389 _xbt_log_event_log(&_log_ev, __VA_ARGS__); \
393 # define XBT_LOG(prio,...) \
395 if (_XBT_LOG_ISENABLEDV((*_simgrid_log_category__default), prio)) { \
396 s_xbt_log_event_t _log_ev; \
397 _log_ev.cat = _simgrid_log_category__default; \
398 _log_ev.priority = (prio); \
399 _log_ev.fileName = __FILE__; \
400 _log_ev.functionName = __func__; \
401 _log_ev.lineNum = __LINE__; \
402 _xbt_log_event_log(&_log_ev, __VA_ARGS__); \
409 * @param categ the category on which to log
410 * @param ... the format string and its arguments
411 * @brief Log an event at the DEBUG priority on the specified category with these args.
413 #define XBT_CDEBUG(categ, ...) \
415 if (XBT_LOG_ISENABLED (categ, xbt_log_priority_debug)) { \
416 s_xbt_log_event_t _log_ev; \
417 _log_ev.cat = &(_XBT_LOGV(categ)); \
418 _log_ev.priority = xbt_log_priority_debug; \
419 _log_ev.fileName = __FILE__; \
420 _log_ev.functionName = __func__; \
421 _log_ev.lineNum = __LINE__; \
422 _xbt_log_event_log(&_log_ev, __VA_ARGS__); \
428 * @brief Log an event at the VERB priority on the specified category with these args.
430 #define XBT_CVERB(categ, ...) \
432 if (XBT_LOG_ISENABLED (categ, xbt_log_priority_verbose)) { \
433 s_xbt_log_event_t _log_ev; \
434 _log_ev.cat = &(_XBT_LOGV(categ)); \
435 _log_ev.priority = xbt_log_priority_verbose; \
436 _log_ev.fileName = __FILE__; \
437 _log_ev.functionName = __func__; \
438 _log_ev.lineNum = __LINE__; \
439 _xbt_log_event_log(&_log_ev, __VA_ARGS__); \
445 * @brief Log an event at the INFO priority on the specified category with these args.
447 #define XBT_CINFO(categ, ...) \
449 if (XBT_LOG_ISENABLED (categ, xbt_log_priority_info)) { \
450 s_xbt_log_event_t _log_ev; \
451 _log_ev.cat = &(_XBT_LOGV(categ)); \
452 _log_ev.priority = xbt_log_priority_info; \
453 _log_ev.fileName = __FILE__; \
454 _log_ev.functionName = __func__; \
455 _log_ev.lineNum = __LINE__; \
456 _xbt_log_event_log(&_log_ev, __VA_ARGS__); \
463 * @brief Log an event at the WARN priority on the specified category with these args.
465 #define XBT_CWARN(categ, ...) \
467 if (XBT_LOG_ISENABLED (categ, xbt_log_priority_warning)) { \
468 s_xbt_log_event_t _log_ev; \
469 _log_ev.cat = &(_XBT_LOGV(categ)); \
470 _log_ev.priority = xbt_log_priority_warning; \
471 _log_ev.fileName = __FILE__; \
472 _log_ev.functionName = __func__; \
473 _log_ev.lineNum = __LINE__; \
474 _xbt_log_event_log(&_log_ev, __VA_ARGS__); \
481 * @brief Log an event at the ERROR priority on the specified category with these args.
483 #define XBT_CERROR(categ, ...) \
485 if (XBT_LOG_ISENABLED (categ, xbt_log_priority_error)) { \
486 s_xbt_log_event_t _log_ev; \
487 _log_ev.cat = &(_XBT_LOGV(categ)); \
488 _log_ev.priority = xbt_log_priority_error; \
489 _log_ev.fileName = __FILE__; \
490 _log_ev.functionName = __func__; \
491 _log_ev.lineNum = __LINE__; \
492 _xbt_log_event_log(&_log_ev, __VA_ARGS__); \
498 * @brief Log an event at the CRITICAL priority on the specified category with these args (CCRITICALn exists for any n<10).
500 #define XBT_CCRITICAL(categ, ...) \
502 if (XBT_LOG_ISENABLED (categ, xbt_log_priority_critical)) { \
503 s_xbt_log_event_t _log_ev; \
504 _log_ev.cat = &(_XBT_LOGV(categ)); \
505 _log_ev.priority = xbt_log_priority_critical; \
506 _log_ev.fileName = __FILE__; \
507 _log_ev.functionName = __func__; \
508 _log_ev.lineNum = __LINE__; \
509 _xbt_log_event_log(&_log_ev, __VA_ARGS__); \
515 * @param ... the format string and its arguments
516 * @brief Log an event at the DEBUG priority on the default category with these args.
518 #define XBT_DEBUG(...) \
520 if (_XBT_LOG_ISENABLEDV(*_simgrid_log_category__default, \
521 xbt_log_priority_debug)) { \
522 s_xbt_log_event_t _log_ev; \
523 _log_ev.cat = _simgrid_log_category__default; \
524 _log_ev.priority = xbt_log_priority_debug; \
525 _log_ev.fileName = __FILE__; \
526 _log_ev.functionName = __func__; \
527 _log_ev.lineNum = __LINE__; \
528 _xbt_log_event_log(&_log_ev, __VA_ARGS__); \
534 * @brief Log an event at the VERB priority on the default category with these args.
536 #define XBT_VERB(...) \
538 if (_XBT_LOG_ISENABLEDV(*_simgrid_log_category__default, \
539 xbt_log_priority_verbose)) { \
540 s_xbt_log_event_t _log_ev; \
541 _log_ev.cat = _simgrid_log_category__default; \
542 _log_ev.priority = xbt_log_priority_verbose; \
543 _log_ev.fileName = __FILE__; \
544 _log_ev.functionName = __func__; \
545 _log_ev.lineNum = __LINE__; \
546 _xbt_log_event_log(&_log_ev, __VA_ARGS__); \
552 * @brief Log an event at the INFO priority on the default category with these args.
554 #define XBT_INFO(...) \
556 if (_XBT_LOG_ISENABLEDV(*_simgrid_log_category__default, \
557 xbt_log_priority_info)) { \
558 s_xbt_log_event_t _log_ev; \
559 _log_ev.cat = _simgrid_log_category__default; \
560 _log_ev.priority = xbt_log_priority_info; \
561 _log_ev.fileName = __FILE__; \
562 _log_ev.functionName = __func__; \
563 _log_ev.lineNum = __LINE__; \
564 _xbt_log_event_log(&_log_ev, __VA_ARGS__); \
570 * @brief Log an event at the WARN priority on the default category with these args.
572 #define XBT_WARN(...) \
574 if (_XBT_LOG_ISENABLEDV(*_simgrid_log_category__default, \
575 xbt_log_priority_warning)) { \
576 s_xbt_log_event_t _log_ev; \
577 _log_ev.cat = _simgrid_log_category__default; \
578 _log_ev.priority = xbt_log_priority_warning; \
579 _log_ev.fileName = __FILE__; \
580 _log_ev.functionName = __func__; \
581 _log_ev.lineNum = __LINE__; \
582 _xbt_log_event_log(&_log_ev, __VA_ARGS__); \
588 * @brief Log an event at the ERROR priority on the default category with these args.
590 #define XBT_ERROR(...) \
592 if (_XBT_LOG_ISENABLEDV(*_simgrid_log_category__default, \
593 xbt_log_priority_error)) { \
594 s_xbt_log_event_t _log_ev; \
595 _log_ev.cat = _simgrid_log_category__default; \
596 _log_ev.priority = xbt_log_priority_error; \
597 _log_ev.fileName = __FILE__; \
598 _log_ev.functionName = __func__; \
599 _log_ev.lineNum = __LINE__; \
600 _xbt_log_event_log(&_log_ev, __VA_ARGS__); \
606 * @brief Log an event at the CRITICAL priority on the default category with these args.
608 #define XBT_CRITICAL(...) \
610 if (_XBT_LOG_ISENABLEDV(*_simgrid_log_category__default, \
611 xbt_log_priority_critical)) { \
612 s_xbt_log_event_t _log_ev; \
613 _log_ev.cat = _simgrid_log_category__default; \
614 _log_ev.priority = xbt_log_priority_critical; \
615 _log_ev.fileName = __FILE__; \
616 _log_ev.functionName = __func__; \
617 _log_ev.lineNum = __LINE__; \
618 _xbt_log_event_log(&_log_ev, __VA_ARGS__); \
622 #define _XBT_IN_OUT(...) \
623 _XBT_IF_ONE_ARG(_XBT_IN_OUT_ARG1, _XBT_IN_OUT_ARGN, __VA_ARGS__)(__VA_ARGS__)
624 #define _XBT_IN_OUT_ARG1(fmt) \
625 XBT_LOG(xbt_log_priority_trace, fmt, __func__)
626 #define _XBT_IN_OUT_ARGN(fmt, ...) \
627 XBT_LOG(xbt_log_priority_trace, fmt, __func__, __VA_ARGS__)
631 * @brief Log at TRACE priority that we entered in current function, appending a user specified format.
633 #define XBT_IN(...) _XBT_IN_OUT(">> begin of %s" __VA_ARGS__)
637 * @brief Log at TRACE priority that we exited the current function, appending a user specified format.
639 #define XBT_OUT(...) _XBT_IN_OUT("<< end of %s" __VA_ARGS__)
643 * @brief Log at TRACE priority a message indicating that we reached that point, appending a user specified format.
645 #define XBT_HERE(...) XBT_LOG(xbt_log_priority_trace, "-- was here" __VA_ARGS__)
648 #endif /* ! _XBT_LOG_H_ */