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;
243 typedef struct xbt_log_appender_s* xbt_log_appender_t;
244 typedef struct xbt_log_layout_s s_xbt_log_layout_t;
245 typedef struct xbt_log_layout_s* xbt_log_layout_t;
246 typedef struct xbt_log_event_s s_xbt_log_event_t;
247 typedef struct xbt_log_event_s* xbt_log_event_t;
248 typedef struct xbt_log_category_s s_xbt_log_category_t;
249 typedef struct xbt_log_category_s* xbt_log_category_t;
251 /* Do NOT access any members of this structure directly. FIXME: move to private? */
253 struct xbt_log_category_s {
254 xbt_log_category_t parent;
255 xbt_log_category_t firstChild;
256 xbt_log_category_t nextSibling;
258 const char *description;
261 int isThreshInherited;
262 xbt_log_appender_t appender;
263 xbt_log_layout_t layout;
267 struct xbt_log_event_s {
268 xbt_log_category_t cat;
269 e_xbt_log_priority_t priority;
270 const char *fileName;
271 const char *functionName;
279 * \ingroup XBT_log_implem
280 * \param cat the category (not only its name, but the variable)
281 * \param thresholdPriority the priority
283 * Programatically alters a category's threshold priority (don't use).
285 XBT_PUBLIC(void) xbt_log_threshold_set(xbt_log_category_t cat, e_xbt_log_priority_t thresholdPriority);
288 * \ingroup XBT_log_implem
289 * \param cat the category (not only its name, but the variable)
290 * \param app the appender
292 * Programatically sets the category's appender. (the preferred interface is through xbt_log_control_set())
294 XBT_PUBLIC(void) xbt_log_appender_set(xbt_log_category_t cat, xbt_log_appender_t app);
296 * \ingroup XBT_log_implem
297 * \param cat the category (not only its name, but the variable)
298 * \param lay the layout
300 * Programatically sets the category's layout. (the preferred interface is through xbt_log_control_set())
302 XBT_PUBLIC(void) xbt_log_layout_set(xbt_log_category_t cat, xbt_log_layout_t lay);
305 * \ingroup XBT_log_implem
306 * \param cat the category (not only its name, but the variable)
307 * \param additivity whether logging actions must be passed to parent.
309 * Programatically sets whether the logging actions must be passed to the parent category.
310 * (the preferred interface is through xbt_log_control_set())
312 XBT_PUBLIC(void) xbt_log_additivity_set(xbt_log_category_t cat, int additivity);
314 /** @brief create a new simple layout
316 * This layout is not as flexible as the pattern one
318 XBT_PUBLIC(xbt_log_layout_t) xbt_log_layout_simple_new(char *arg);
319 XBT_PUBLIC(xbt_log_layout_t) xbt_log_layout_format_new(char *arg);
320 XBT_PUBLIC(xbt_log_appender_t) xbt_log_appender_file_new(char *arg);
321 XBT_PUBLIC(xbt_log_appender_t) xbt_log_appender2_file_new(char *arg,int roll);
323 /* ********************************** */
324 /* Functions that you shouldn't call */
325 /* ********************************** */
326 XBT_PUBLIC(void) _xbt_log_event_log(xbt_log_event_t ev, const char *fmt, ...) XBT_ATTRIB_PRINTF(2, 3);
327 XBT_PUBLIC(int) _xbt_log_cat_init(xbt_log_category_t category, e_xbt_log_priority_t priority);
330 XBT_PUBLIC_DATA(s_xbt_log_category_t) _XBT_LOGV(XBT_LOG_ROOT_CAT);
332 // If we `dllexport` the root log category, MinGW does not want us to take its address with the error:
333 // > initializer element is not constant
334 // When using auto-import, MinGW is happy.
335 // We should handle this for non-root log categories as well.
336 extern s_xbt_log_category_t _XBT_LOGV(XBT_LOG_ROOT_CAT);
339 extern xbt_log_appender_t xbt_log_default_appender;
340 extern xbt_log_layout_t xbt_log_default_layout;
342 /* ********************** */
343 /* Public functions again */
344 /* ********************** */
348 * \param catName name of the category
349 * \param priority minimal priority to be enabled to return true (must be #e_xbt_log_priority_t)
352 * Returns true if the given priority is enabled for the category.
353 * If you have expensive expressions that are computed outside of the log command and used only within it, you should
354 * make its evaluation conditional using this macro.
356 #define XBT_LOG_ISENABLED(catName, priority) \
357 _XBT_LOG_ISENABLEDV(_XBT_LOGV(catName), priority)
360 * Helper function that implements XBT_LOG_ISENABLED.
363 * First part is a compile-time constant.
364 * Call to xbt_log_cat_init only happens once.
366 #define _XBT_LOG_ISENABLEDV(catv, priority) \
367 (priority >= XBT_LOG_STATIC_THRESHOLD \
368 && ((catv).initialized || _xbt_log_cat_init(&(catv), priority)) \
369 && priority >= (catv).threshold)
373 * Some kludge macros to ease maintenance. See how they're used below.
375 * IMPLEMENTATION NOTE: To reduce the parameter passing overhead of an enabled message, the many parameters passed to
376 * the logging function are packed in a structure. Since these values will be usually be passed to at least 3 functions,
378 * It also allows adding new values (such as a timestamp) without breaking code.
379 * Setting the LogEvent's valist member is done inside _log_logEvent.
384 #ifdef XBT_LOG_MAYDAY
385 # define XBT_CLOG(cat, prio, ...) \
386 _XBT_IF_ONE_ARG(_XBT_CLOG_ARG1, _XBT_CLOG_ARGN, __VA_ARGS__)(__VA_ARGS__)
387 # define _XBT_CLOG_ARG1(f) \
388 fprintf(stderr,"%s:%d:\n" f, __FILE__, __LINE__)
389 # define _XBT_CLOG_ARGN(f, ...) \
390 fprintf(stderr,"%s:%d:\n" f, __FILE__, __LINE__, __VA_ARGS__)
391 # define XBT_LOG(...) XBT_CLOG(0, __VA_ARGS__)
394 // This code is duplicated to remove one level of indirection, working around a MSVC bug
395 // See: http://stackoverflow.com/questions/9183993/msvc-variadic-macro-expansion
397 # define XBT_CLOG(category, prio, ...) \
399 if (_XBT_LOG_ISENABLEDV((category), prio)) { \
400 s_xbt_log_event_t _log_ev; \
401 _log_ev.cat = &(category); \
402 _log_ev.priority = (prio); \
403 _log_ev.fileName = __FILE__; \
404 _log_ev.functionName = __func__; \
405 _log_ev.lineNum = __LINE__; \
406 _xbt_log_event_log(&_log_ev, __VA_ARGS__); \
410 # define XBT_LOG(prio,...) \
412 if (_XBT_LOG_ISENABLEDV((*_simgrid_log_category__default), prio)) { \
413 s_xbt_log_event_t _log_ev; \
414 _log_ev.cat = _simgrid_log_category__default; \
415 _log_ev.priority = (prio); \
416 _log_ev.fileName = __FILE__; \
417 _log_ev.functionName = __func__; \
418 _log_ev.lineNum = __LINE__; \
419 _xbt_log_event_log(&_log_ev, __VA_ARGS__); \
426 * \param categ the category on which to log
427 * \param ... the format string and its arguments
428 * @brief Log an event at the DEBUG priority on the specified category with these args.
430 #define XBT_CDEBUG(categ, ...) \
432 if (XBT_LOG_ISENABLED (categ, xbt_log_priority_debug)) { \
433 s_xbt_log_event_t _log_ev; \
434 _log_ev.cat = &(_XBT_LOGV(categ)); \
435 _log_ev.priority = xbt_log_priority_debug; \
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 VERB priority on the specified category with these args.
447 #define XBT_CVERB(categ, ...) \
449 if (XBT_LOG_ISENABLED (categ, xbt_log_priority_verbose)) { \
450 s_xbt_log_event_t _log_ev; \
451 _log_ev.cat = &(_XBT_LOGV(categ)); \
452 _log_ev.priority = xbt_log_priority_verbose; \
453 _log_ev.fileName = __FILE__; \
454 _log_ev.functionName = __func__; \
455 _log_ev.lineNum = __LINE__; \
456 _xbt_log_event_log(&_log_ev, __VA_ARGS__); \
462 * @brief Log an event at the INFO priority on the specified category with these args.
464 #define XBT_CINFO(categ, ...) \
466 if (XBT_LOG_ISENABLED (categ, xbt_log_priority_info)) { \
467 s_xbt_log_event_t _log_ev; \
468 _log_ev.cat = &(_XBT_LOGV(categ)); \
469 _log_ev.priority = xbt_log_priority_info; \
470 _log_ev.fileName = __FILE__; \
471 _log_ev.functionName = __func__; \
472 _log_ev.lineNum = __LINE__; \
473 _xbt_log_event_log(&_log_ev, __VA_ARGS__); \
480 * @brief Log an event at the WARN priority on the specified category with these args.
482 #define XBT_CWARN(categ, ...) \
484 if (XBT_LOG_ISENABLED (categ, xbt_log_priority_warning)) { \
485 s_xbt_log_event_t _log_ev; \
486 _log_ev.cat = &(_XBT_LOGV(categ)); \
487 _log_ev.priority = xbt_log_priority_warning; \
488 _log_ev.fileName = __FILE__; \
489 _log_ev.functionName = __func__; \
490 _log_ev.lineNum = __LINE__; \
491 _xbt_log_event_log(&_log_ev, __VA_ARGS__); \
498 * @brief Log an event at the ERROR priority on the specified category with these args.
500 #define XBT_CERROR(categ, ...) \
502 if (XBT_LOG_ISENABLED (categ, xbt_log_priority_error)) { \
503 s_xbt_log_event_t _log_ev; \
504 _log_ev.cat = &(_XBT_LOGV(categ)); \
505 _log_ev.priority = xbt_log_priority_error; \
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 * @brief Log an event at the CRITICAL priority on the specified category with these args (CCRITICALn exists for any n<10).
517 #define XBT_CCRITICAL(categ, ...) \
519 if (XBT_LOG_ISENABLED (categ, xbt_log_priority_critical)) { \
520 s_xbt_log_event_t _log_ev; \
521 _log_ev.cat = &(_XBT_LOGV(categ)); \
522 _log_ev.priority = xbt_log_priority_critical; \
523 _log_ev.fileName = __FILE__; \
524 _log_ev.functionName = __func__; \
525 _log_ev.lineNum = __LINE__; \
526 _xbt_log_event_log(&_log_ev, __VA_ARGS__); \
532 * \param ... the format string and its arguments
533 * @brief Log an event at the DEBUG priority on the default category with these args.
535 #define XBT_DEBUG(...) \
537 if (_XBT_LOG_ISENABLEDV(*_simgrid_log_category__default, \
538 xbt_log_priority_debug)) { \
539 s_xbt_log_event_t _log_ev; \
540 _log_ev.cat = _simgrid_log_category__default; \
541 _log_ev.priority = xbt_log_priority_debug; \
542 _log_ev.fileName = __FILE__; \
543 _log_ev.functionName = __func__; \
544 _log_ev.lineNum = __LINE__; \
545 _xbt_log_event_log(&_log_ev, __VA_ARGS__); \
551 * @brief Log an event at the VERB priority on the default category with these args.
553 #define XBT_VERB(...) \
555 if (_XBT_LOG_ISENABLEDV(*_simgrid_log_category__default, \
556 xbt_log_priority_verbose)) { \
557 s_xbt_log_event_t _log_ev; \
558 _log_ev.cat = _simgrid_log_category__default; \
559 _log_ev.priority = xbt_log_priority_verbose; \
560 _log_ev.fileName = __FILE__; \
561 _log_ev.functionName = __func__; \
562 _log_ev.lineNum = __LINE__; \
563 _xbt_log_event_log(&_log_ev, __VA_ARGS__); \
569 * @brief Log an event at the INFO priority on the default category with these args.
571 #define XBT_INFO(...) \
573 if (_XBT_LOG_ISENABLEDV(*_simgrid_log_category__default, \
574 xbt_log_priority_info)) { \
575 s_xbt_log_event_t _log_ev; \
576 _log_ev.cat = _simgrid_log_category__default; \
577 _log_ev.priority = xbt_log_priority_info; \
578 _log_ev.fileName = __FILE__; \
579 _log_ev.functionName = __func__; \
580 _log_ev.lineNum = __LINE__; \
581 _xbt_log_event_log(&_log_ev, __VA_ARGS__); \
587 * @brief Log an event at the WARN priority on the default category with these args.
589 #define XBT_WARN(...) \
591 if (_XBT_LOG_ISENABLEDV(*_simgrid_log_category__default, \
592 xbt_log_priority_warning)) { \
593 s_xbt_log_event_t _log_ev; \
594 _log_ev.cat = _simgrid_log_category__default; \
595 _log_ev.priority = xbt_log_priority_warning; \
596 _log_ev.fileName = __FILE__; \
597 _log_ev.functionName = __func__; \
598 _log_ev.lineNum = __LINE__; \
599 _xbt_log_event_log(&_log_ev, __VA_ARGS__); \
605 * @brief Log an event at the ERROR priority on the default category with these args.
607 #define XBT_ERROR(...) \
609 if (_XBT_LOG_ISENABLEDV(*_simgrid_log_category__default, \
610 xbt_log_priority_error)) { \
611 s_xbt_log_event_t _log_ev; \
612 _log_ev.cat = _simgrid_log_category__default; \
613 _log_ev.priority = xbt_log_priority_error; \
614 _log_ev.fileName = __FILE__; \
615 _log_ev.functionName = __func__; \
616 _log_ev.lineNum = __LINE__; \
617 _xbt_log_event_log(&_log_ev, __VA_ARGS__); \
623 * @brief Log an event at the CRITICAL priority on the default category with these args.
625 #define XBT_CRITICAL(...) \
627 if (_XBT_LOG_ISENABLEDV(*_simgrid_log_category__default, \
628 xbt_log_priority_critical)) { \
629 s_xbt_log_event_t _log_ev; \
630 _log_ev.cat = _simgrid_log_category__default; \
631 _log_ev.priority = xbt_log_priority_critical; \
632 _log_ev.fileName = __FILE__; \
633 _log_ev.functionName = __func__; \
634 _log_ev.lineNum = __LINE__; \
635 _xbt_log_event_log(&_log_ev, __VA_ARGS__); \
639 #define _XBT_IN_OUT(...) \
640 _XBT_IF_ONE_ARG(_XBT_IN_OUT_ARG1, _XBT_IN_OUT_ARGN, __VA_ARGS__)(__VA_ARGS__)
641 #define _XBT_IN_OUT_ARG1(fmt) \
642 XBT_LOG(xbt_log_priority_trace, fmt, __func__)
643 #define _XBT_IN_OUT_ARGN(fmt, ...) \
644 XBT_LOG(xbt_log_priority_trace, fmt, __func__, __VA_ARGS__)
648 * @brief Log at TRACE priority that we entered in current function, appending a user specified format.
650 #define XBT_IN(...) _XBT_IN_OUT(">> begin of %s" __VA_ARGS__)
654 * @brief Log at TRACE priority that we exited the current function, appending a user specified format.
656 #define XBT_OUT(...) _XBT_IN_OUT("<< end of %s" __VA_ARGS__)
660 * @brief Log at TRACE priority a message indicating that we reached that point, appending a user specified format.
662 #define XBT_HERE(...) XBT_LOG(xbt_log_priority_trace, "-- was here" __VA_ARGS__)
665 #endif /* ! _XBT_LOG_H_ */