1 /* log - a generic logging facility in the spirit of log4j */
3 /* Copyright (c) 2004-2015. The SimGrid Team.
4 * All rights reserved. */
6 /* This program is free software; you can redistribute it and/or modify it
7 * under the terms of the license (GNU LGPL) which comes with this package. */
9 /** @addtogroup XBT_log
10 * @brief A generic logging facility in the spirit of log4j (grounding feature)
14 /** \defgroup XBT_log_cats Existing log categories
16 * \brief (automatically extracted)
18 * This is the list of all existing log categories in SimGrid.
19 * This list is automatically extracted from the source code by the tools/doxygen/xbt_log_extract_hierarchy.pl utility.
21 * You can thus be certain that it is uptodate, but it may somehow lack a final manual touch.
22 * Anyway, nothing's perfect ;)
25 /* XBT_LOG_MAYDAY: define this to replace the logging facilities with basic
26 printf function. Useful to debug the logging facilities themselves */
28 //#define XBT_LOG_MAYDAY
35 #include <stddef.h> /* NULL */
37 /**\brief Log priorities
40 * The different existing priorities.
44 xbt_log_priority_none = 0, /** used internally (don't poke with)*/
46 xbt_log_priority_trace = 1, /**< enter and return of some functions */
47 xbt_log_priority_debug = 2, /**< crufty output */
48 xbt_log_priority_verbose = 3, /**< verbose output for the user wanting more */
49 xbt_log_priority_info = 4, /**< output about the regular functionning */
50 xbt_log_priority_warning = 5, /**< minor issue encountered */
51 xbt_log_priority_error = 6, /**< issue encountered */
52 xbt_log_priority_critical = 7, /**< major issue encountered */
54 xbt_log_priority_infinite = 8, /**< value for XBT_LOG_STATIC_THRESHOLD to not log */
57 xbt_log_priority_uninitialized = -1 /* used internally (don't poke with) */
59 } e_xbt_log_priority_t;
62 * define NLOG to disable at compilation time any logging request
63 * define NDEBUG to disable at compilation time any logging request of priority below VERBOSE
67 * @def XBT_LOG_STATIC_THRESHOLD
70 * All logging requests with priority < XBT_LOG_STATIC_THRESHOLD are disabled at compile time, i.e., compiled out.
73 # define XBT_LOG_STATIC_THRESHOLD xbt_log_priority_infinite
77 # define XBT_LOG_STATIC_THRESHOLD xbt_log_priority_verbose
78 # else /* !NLOG && !NDEBUG */
80 # ifndef XBT_LOG_STATIC_THRESHOLD
81 # define XBT_LOG_STATIC_THRESHOLD xbt_log_priority_none
82 # endif /* !XBT_LOG_STATIC_THRESHOLD */
84 #endif /* !defined(NLOG) */
86 /* Transforms a category name to a global variable name. */
87 #define _XBT_LOGV(cat) _XBT_LOG_CONCAT(_simgrid_log_category__, cat)
88 #define _XBT_LOGV_CTOR(cat) _XBT_LOG_CONCAT2(_XBT_LOGV(cat), __constructor__)
89 #define _XBT_LOG_CONCAT(x, y) x ## y
90 #define _XBT_LOG_CONCAT2(x, y) _XBT_LOG_CONCAT(x, y)
91 /* Apparently, constructor priorities are not supported by gcc on Macs */
92 #if defined(__GNUC__) && defined(__APPLE__)
93 # define _XBT_LOGV_CTOR_ATTRIBUTE
95 # define _XBT_LOGV_CTOR_ATTRIBUTE _XBT_GNUC_CONSTRUCTOR(600)
98 /* The root of the category hierarchy. */
99 #define XBT_LOG_ROOT_CAT root
101 /* The whole tree of categories is connected by setting the address of the parent category as a field of the child one.
102 * This is normally done at the first use of the category.
104 * It is however necessary to make this connections as early as possible, if we want the category to be listed by
105 * --help-log-categories.
107 * When possible, the initializations takes place automatically before the start of main(). It's the case when
108 * compiling with gcc.
110 * For the other cases, you can use the XBT_LOG_CONNECT(cat) macro to force early initialization. See, for example,
111 * in xbt/log.c, the function xbt_log_connect_categories().
114 #define XBT_LOG_CONNECT(cat) \
116 extern void _XBT_LOGV_CTOR(cat)(void); \
117 _XBT_LOGV_CTOR(cat)(); \
120 /* XBT_LOG_NEW_SUBCATEGORY_helper:
121 * Implementation of XBT_LOG_NEW_SUBCATEGORY, which must declare "extern parent" in addition to avoid an extra
122 * declaration of root when XBT_LOG_NEW_SUBCATEGORY is called by XBT_LOG_NEW_CATEGORY */
123 #define XBT_LOG_NEW_SUBCATEGORY_helper(catName, parent, desc) \
125 extern void _XBT_LOGV_CTOR(catName)(void) _XBT_LOGV_CTOR_ATTRIBUTE; \
126 void _XBT_LOGV_CTOR(catName)(void) \
128 XBT_LOG_EXTERNAL_CATEGORY(catName); \
129 if (!_XBT_LOGV(catName).initialized) { \
130 _xbt_log_cat_init(&_XBT_LOGV(catName), xbt_log_priority_uninitialized); \
134 XBT_EXPORT_NO_IMPORT(s_xbt_log_category_t) _XBT_LOGV(catName) = { \
135 &_XBT_LOGV(parent), \
136 NULL /* firstChild */, \
137 NULL /* nextSibling */, \
140 0 /*initialized */, \
141 xbt_log_priority_uninitialized /* threshold */, \
142 1 /* isThreshInherited */, \
143 NULL /* appender */, \
150 * \param catName name of new category
151 * \param parent father of the new category in the tree
152 * \param desc string describing the purpose of this category
155 * Defines a new subcategory of the parent.
157 #define XBT_LOG_NEW_SUBCATEGORY(catName, parent, desc) \
158 XBT_LOG_EXTERNAL_CATEGORY(parent); \
159 XBT_LOG_NEW_SUBCATEGORY_helper(catName, parent, desc) \
163 * \param catName name of new category
164 * \param desc string describing the purpose of this category
167 * Creates a new subcategory of the root category.
169 # define XBT_LOG_NEW_CATEGORY(catName,desc) \
170 XBT_LOG_NEW_SUBCATEGORY_helper(catName, XBT_LOG_ROOT_CAT, desc)
174 * \param cname name of the cat
177 * Indicates which category is the default one.
180 #if defined(XBT_LOG_MAYDAY) /*|| defined (NLOG) * turning logging off */
181 # define XBT_LOG_DEFAULT_CATEGORY(cname)
183 # define XBT_LOG_DEFAULT_CATEGORY(cname) \
184 static xbt_log_category_t _XBT_LOGV(default) XBT_ATTRIB_UNUSED = &_XBT_LOGV(cname)
189 * \param cname name of the cat
190 * \param desc string describing the purpose of this category
193 * Creates a new subcategory of the root category and makes it the default (used by macros that don't explicitly
194 * specify a category).
196 # define XBT_LOG_NEW_DEFAULT_CATEGORY(cname,desc) \
197 XBT_LOG_NEW_CATEGORY(cname,desc); \
198 XBT_LOG_DEFAULT_CATEGORY(cname)
202 * \param cname name of the cat
203 * \param parent name of the parent
204 * \param desc string describing the purpose of this category
207 * Creates a new subcategory of the parent category and makes it the default
208 * (used by macros that don't explicitly specify a category).
210 #define XBT_LOG_NEW_DEFAULT_SUBCATEGORY(cname, parent, desc) \
211 XBT_LOG_NEW_SUBCATEGORY(cname, parent, desc); \
212 XBT_LOG_DEFAULT_CATEGORY(cname)
216 * \param cname name of the cat
219 * Indicates that a category you'll use in this file (e.g., to get subcategories of it) really lives in another file.
222 #define XBT_LOG_EXTERNAL_CATEGORY(cname) \
223 extern s_xbt_log_category_t _XBT_LOGV(cname)
227 * \param cname name of the cat
230 * Indicates that the default category of this file was declared in another file.
233 #define XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(cname) \
234 XBT_LOG_EXTERNAL_CATEGORY(cname);\
235 XBT_LOG_DEFAULT_CATEGORY(cname)
237 /* Functions you may call */
239 XBT_PUBLIC(void) xbt_log_control_set(const char *cs);
241 /* Forward declarations */
242 typedef struct xbt_log_appender_s s_xbt_log_appender_t, *xbt_log_appender_t;
243 typedef struct xbt_log_layout_s s_xbt_log_layout_t, *xbt_log_layout_t;
244 typedef struct xbt_log_event_s s_xbt_log_event_t, *xbt_log_event_t;
245 typedef struct xbt_log_category_s s_xbt_log_category_t, *xbt_log_category_t;
247 /* Do NOT access any members of this structure directly. FIXME: move to private? */
249 struct xbt_log_category_s {
250 xbt_log_category_t parent;
251 xbt_log_category_t firstChild;
252 xbt_log_category_t nextSibling;
254 const char *description;
257 int isThreshInherited;
258 xbt_log_appender_t appender;
259 xbt_log_layout_t layout;
263 struct xbt_log_event_s {
264 xbt_log_category_t cat;
265 e_xbt_log_priority_t priority;
266 const char *fileName;
267 const char *functionName;
275 * \ingroup XBT_log_implem
276 * \param cat the category (not only its name, but the variable)
277 * \param thresholdPriority the priority
279 * Programatically alters a category's threshold priority (don't use).
281 XBT_PUBLIC(void) xbt_log_threshold_set(xbt_log_category_t cat, e_xbt_log_priority_t thresholdPriority);
284 * \ingroup XBT_log_implem
285 * \param cat the category (not only its name, but the variable)
286 * \param app the appender
288 * Programatically sets the category's appender. (the preferred interface is through xbt_log_control_set())
290 XBT_PUBLIC(void) xbt_log_appender_set(xbt_log_category_t cat, xbt_log_appender_t app);
292 * \ingroup XBT_log_implem
293 * \param cat the category (not only its name, but the variable)
294 * \param lay the layout
296 * Programatically sets the category's layout. (the preferred interface is through xbt_log_control_set())
298 XBT_PUBLIC(void) xbt_log_layout_set(xbt_log_category_t cat, xbt_log_layout_t lay);
301 * \ingroup XBT_log_implem
302 * \param cat the category (not only its name, but the variable)
303 * \param additivity whether logging actions must be passed to parent.
305 * Programatically sets whether the logging actions must be passed to the parent category.
306 * (the preferred interface is through xbt_log_control_set())
308 XBT_PUBLIC(void) xbt_log_additivity_set(xbt_log_category_t cat, int additivity);
310 /** @brief create a new simple layout
312 * This layout is not as flexible as the pattern one
314 XBT_PUBLIC(xbt_log_layout_t) xbt_log_layout_simple_new(char *arg);
315 XBT_PUBLIC(xbt_log_layout_t) xbt_log_layout_format_new(char *arg);
316 XBT_PUBLIC(xbt_log_appender_t) xbt_log_appender_file_new(char *arg);
317 XBT_PUBLIC(xbt_log_appender_t) xbt_log_appender2_file_new(char *arg,int roll);
319 /* ********************************** */
320 /* Functions that you shouldn't call */
321 /* ********************************** */
322 XBT_PUBLIC(void) _xbt_log_event_log(xbt_log_event_t ev, const char *fmt, ...) XBT_ATTRIB_PRINTF(2, 3);
323 XBT_PUBLIC(int) _xbt_log_cat_init(xbt_log_category_t category, e_xbt_log_priority_t priority);
326 XBT_PUBLIC_DATA(s_xbt_log_category_t) _XBT_LOGV(XBT_LOG_ROOT_CAT);
328 // If we `dllexport` the root log category, MinGW does not want us to take its address with the error:
329 // > initializer element is not constant
330 // When using auto-import, MinGW is happy.
331 // We should handle this for non-root log categories as well.
332 extern s_xbt_log_category_t _XBT_LOGV(XBT_LOG_ROOT_CAT);
335 extern xbt_log_appender_t xbt_log_default_appender;
336 extern xbt_log_layout_t xbt_log_default_layout;
338 /* ********************** */
339 /* Public functions again */
340 /* ********************** */
344 * \param catName name of the category
345 * \param priority minimal priority to be enabled to return true (must be #e_xbt_log_priority_t)
348 * Returns true if the given priority is enabled for the category.
349 * If you have expensive expressions that are computed outside of the log command and used only within it, you should
350 * make its evaluation conditional using this macro.
352 #define XBT_LOG_ISENABLED(catName, priority) \
353 _XBT_LOG_ISENABLEDV(_XBT_LOGV(catName), priority)
356 * Helper function that implements XBT_LOG_ISENABLED.
359 * First part is a compile-time constant.
360 * Call to xbt_log_cat_init only happens once.
362 #define _XBT_LOG_ISENABLEDV(catv, priority) \
363 (priority >= XBT_LOG_STATIC_THRESHOLD \
364 && ((catv).initialized || _xbt_log_cat_init(&(catv), priority)) \
365 && priority >= (catv).threshold)
369 * Some kludge macros to ease maintenance. See how they're used below.
371 * IMPLEMENTATION NOTE: To reduce the parameter passing overhead of an enabled message, the many parameters passed to
372 * the logging function are packed in a structure. Since these values will be usually be passed to at least 3 functions,
374 * It also allows adding new values (such as a timestamp) without breaking code.
375 * Setting the LogEvent's valist member is done inside _log_logEvent.
380 #ifdef XBT_LOG_MAYDAY
381 # define XBT_CLOG(cat, prio, ...) \
382 _XBT_IF_ONE_ARG(_XBT_CLOG_ARG1, _XBT_CLOG_ARGN, __VA_ARGS__)(__VA_ARGS__)
383 # define _XBT_CLOG_ARG1(f) \
384 fprintf(stderr,"%s:%d:\n" f, __FILE__, __LINE__)
385 # define _XBT_CLOG_ARGN(f, ...) \
386 fprintf(stderr,"%s:%d:\n" f, __FILE__, __LINE__, __VA_ARGS__)
387 # define XBT_LOG(...) XBT_CLOG(0, __VA_ARGS__)
390 // This code is duplicated to remove one level of indirection, working around a MSVC bug
391 // See: http://stackoverflow.com/questions/9183993/msvc-variadic-macro-expansion
393 # define XBT_CLOG(category, prio, ...) \
395 if (_XBT_LOG_ISENABLEDV((category), prio)) { \
396 s_xbt_log_event_t _log_ev; \
397 _log_ev.cat = &(category); \
398 _log_ev.priority = (prio); \
399 _log_ev.fileName = __FILE__; \
400 _log_ev.functionName = _XBT_FUNCTION; \
401 _log_ev.lineNum = __LINE__; \
402 _xbt_log_event_log(&_log_ev, __VA_ARGS__); \
406 # define XBT_LOG(prio,...) \
408 if (_XBT_LOG_ISENABLEDV((*_simgrid_log_category__default), prio)) { \
409 s_xbt_log_event_t _log_ev; \
410 _log_ev.cat = _simgrid_log_category__default; \
411 _log_ev.priority = (prio); \
412 _log_ev.fileName = __FILE__; \
413 _log_ev.functionName = _XBT_FUNCTION; \
414 _log_ev.lineNum = __LINE__; \
415 _xbt_log_event_log(&_log_ev, __VA_ARGS__); \
422 * \param categ the category on which to log
423 * \param ... the format string and its arguments
424 * @brief Log an event at the DEBUG priority on the specified category with these args.
426 #define XBT_CDEBUG(categ, ...) \
428 if (XBT_LOG_ISENABLED (categ, xbt_log_priority_debug)) { \
429 s_xbt_log_event_t _log_ev; \
430 _log_ev.cat = &(_XBT_LOGV(categ)); \
431 _log_ev.priority = xbt_log_priority_debug; \
432 _log_ev.fileName = __FILE__; \
433 _log_ev.functionName = _XBT_FUNCTION; \
434 _log_ev.lineNum = __LINE__; \
435 _xbt_log_event_log(&_log_ev, __VA_ARGS__); \
441 * @brief Log an event at the VERB priority on the specified category with these args.
443 #define XBT_CVERB(categ, ...) \
445 if (XBT_LOG_ISENABLED (categ, xbt_log_priority_verbose)) { \
446 s_xbt_log_event_t _log_ev; \
447 _log_ev.cat = &(_XBT_LOGV(categ)); \
448 _log_ev.priority = xbt_log_priority_verbose; \
449 _log_ev.fileName = __FILE__; \
450 _log_ev.functionName = _XBT_FUNCTION; \
451 _log_ev.lineNum = __LINE__; \
452 _xbt_log_event_log(&_log_ev, __VA_ARGS__); \
458 * @brief Log an event at the INFO priority on the specified category with these args.
460 #define XBT_CINFO(categ, ...) \
462 if (XBT_LOG_ISENABLED (categ, xbt_log_priority_info)) { \
463 s_xbt_log_event_t _log_ev; \
464 _log_ev.cat = &(_XBT_LOGV(categ)); \
465 _log_ev.priority = xbt_log_priority_info; \
466 _log_ev.fileName = __FILE__; \
467 _log_ev.functionName = _XBT_FUNCTION; \
468 _log_ev.lineNum = __LINE__; \
469 _xbt_log_event_log(&_log_ev, __VA_ARGS__); \
476 * @brief Log an event at the WARN priority on the specified category with these args.
478 #define XBT_CWARN(categ, ...) \
480 if (XBT_LOG_ISENABLED (categ, xbt_log_priority_warning)) { \
481 s_xbt_log_event_t _log_ev; \
482 _log_ev.cat = &(_XBT_LOGV(categ)); \
483 _log_ev.priority = xbt_log_priority_warning; \
484 _log_ev.fileName = __FILE__; \
485 _log_ev.functionName = _XBT_FUNCTION; \
486 _log_ev.lineNum = __LINE__; \
487 _xbt_log_event_log(&_log_ev, __VA_ARGS__); \
494 * @brief Log an event at the ERROR priority on the specified category with these args.
496 #define XBT_CERROR(categ, ...) \
498 if (XBT_LOG_ISENABLED (categ, xbt_log_priority_error)) { \
499 s_xbt_log_event_t _log_ev; \
500 _log_ev.cat = &(_XBT_LOGV(categ)); \
501 _log_ev.priority = xbt_log_priority_error; \
502 _log_ev.fileName = __FILE__; \
503 _log_ev.functionName = _XBT_FUNCTION; \
504 _log_ev.lineNum = __LINE__; \
505 _xbt_log_event_log(&_log_ev, __VA_ARGS__); \
511 * @brief Log an event at the CRITICAL priority on the specified category with these args (CCRITICALn exists for any n<10).
513 #define XBT_CCRITICAL(categ, ...) \
515 if (XBT_LOG_ISENABLED (categ, xbt_log_priority_critical)) { \
516 s_xbt_log_event_t _log_ev; \
517 _log_ev.cat = &(_XBT_LOGV(categ)); \
518 _log_ev.priority = xbt_log_priority_critical; \
519 _log_ev.fileName = __FILE__; \
520 _log_ev.functionName = _XBT_FUNCTION; \
521 _log_ev.lineNum = __LINE__; \
522 _xbt_log_event_log(&_log_ev, __VA_ARGS__); \
528 * \param ... the format string and its arguments
529 * @brief Log an event at the DEBUG priority on the default category with these args.
531 #define XBT_DEBUG(...) \
533 if (_XBT_LOG_ISENABLEDV(*_simgrid_log_category__default, \
534 xbt_log_priority_debug)) { \
535 s_xbt_log_event_t _log_ev; \
536 _log_ev.cat = _simgrid_log_category__default; \
537 _log_ev.priority = xbt_log_priority_debug; \
538 _log_ev.fileName = __FILE__; \
539 _log_ev.functionName = _XBT_FUNCTION; \
540 _log_ev.lineNum = __LINE__; \
541 _xbt_log_event_log(&_log_ev, __VA_ARGS__); \
547 * @brief Log an event at the VERB priority on the default category with these args.
549 #define XBT_VERB(...) \
551 if (_XBT_LOG_ISENABLEDV(*_simgrid_log_category__default, \
552 xbt_log_priority_verbose)) { \
553 s_xbt_log_event_t _log_ev; \
554 _log_ev.cat = _simgrid_log_category__default; \
555 _log_ev.priority = xbt_log_priority_verbose; \
556 _log_ev.fileName = __FILE__; \
557 _log_ev.functionName = _XBT_FUNCTION; \
558 _log_ev.lineNum = __LINE__; \
559 _xbt_log_event_log(&_log_ev, __VA_ARGS__); \
565 * @brief Log an event at the INFO priority on the default category with these args.
567 #define XBT_INFO(...) \
569 if (_XBT_LOG_ISENABLEDV(*_simgrid_log_category__default, \
570 xbt_log_priority_info)) { \
571 s_xbt_log_event_t _log_ev; \
572 _log_ev.cat = _simgrid_log_category__default; \
573 _log_ev.priority = xbt_log_priority_info; \
574 _log_ev.fileName = __FILE__; \
575 _log_ev.functionName = _XBT_FUNCTION; \
576 _log_ev.lineNum = __LINE__; \
577 _xbt_log_event_log(&_log_ev, __VA_ARGS__); \
583 * @brief Log an event at the WARN priority on the default category with these args.
585 #define XBT_WARN(...) \
587 if (_XBT_LOG_ISENABLEDV(*_simgrid_log_category__default, \
588 xbt_log_priority_warning)) { \
589 s_xbt_log_event_t _log_ev; \
590 _log_ev.cat = _simgrid_log_category__default; \
591 _log_ev.priority = xbt_log_priority_warning; \
592 _log_ev.fileName = __FILE__; \
593 _log_ev.functionName = _XBT_FUNCTION; \
594 _log_ev.lineNum = __LINE__; \
595 _xbt_log_event_log(&_log_ev, __VA_ARGS__); \
601 * @brief Log an event at the ERROR priority on the default category with these args.
603 #define XBT_ERROR(...) \
605 if (_XBT_LOG_ISENABLEDV(*_simgrid_log_category__default, \
606 xbt_log_priority_error)) { \
607 s_xbt_log_event_t _log_ev; \
608 _log_ev.cat = _simgrid_log_category__default; \
609 _log_ev.priority = xbt_log_priority_error; \
610 _log_ev.fileName = __FILE__; \
611 _log_ev.functionName = _XBT_FUNCTION; \
612 _log_ev.lineNum = __LINE__; \
613 _xbt_log_event_log(&_log_ev, __VA_ARGS__); \
619 * @brief Log an event at the CRITICAL priority on the default category with these args.
621 #define XBT_CRITICAL(...) \
623 if (_XBT_LOG_ISENABLEDV(*_simgrid_log_category__default, \
624 xbt_log_priority_critical)) { \
625 s_xbt_log_event_t _log_ev; \
626 _log_ev.cat = _simgrid_log_category__default; \
627 _log_ev.priority = xbt_log_priority_critical; \
628 _log_ev.fileName = __FILE__; \
629 _log_ev.functionName = _XBT_FUNCTION; \
630 _log_ev.lineNum = __LINE__; \
631 _xbt_log_event_log(&_log_ev, __VA_ARGS__); \
635 #define _XBT_IN_OUT(...) \
636 _XBT_IF_ONE_ARG(_XBT_IN_OUT_ARG1, _XBT_IN_OUT_ARGN, __VA_ARGS__)(__VA_ARGS__)
637 #define _XBT_IN_OUT_ARG1(fmt) \
638 XBT_LOG(xbt_log_priority_trace, fmt, _XBT_FUNCTION)
639 #define _XBT_IN_OUT_ARGN(fmt, ...) \
640 XBT_LOG(xbt_log_priority_trace, fmt, _XBT_FUNCTION, __VA_ARGS__)
644 * @brief Log at TRACE priority that we entered in current function, appending a user specified format.
646 #define XBT_IN(...) _XBT_IN_OUT(">> begin of %s" __VA_ARGS__)
650 * @brief Log at TRACE priority that we exited the current function, appending a user specified format.
652 #define XBT_OUT(...) _XBT_IN_OUT("<< end of %s" __VA_ARGS__)
656 * @brief Log at TRACE priority a message indicating that we reached that point, appending a user specified format.
658 #define XBT_HERE(...) XBT_LOG(xbt_log_priority_trace, "-- was here" __VA_ARGS__)
661 #endif /* ! _XBT_LOG_H_ */