1 /* log - a generic logging facility in the spirit of log4j */
3 /* Copyright (c) 2004-2019. 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 */
34 #include <stdio.h> /* FILE */
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)
92 /* The root of the category hierarchy. */
93 #define XBT_LOG_ROOT_CAT root
95 /* The whole tree of categories is connected by setting the address of the parent category as a field of the child one.
96 * This is normally done at the first use of the category.
98 * It is however necessary to make this connections as early as possible, if we want the category to be listed by
99 * --help-log-categories. We use constructor attributes for these initializations to take place automatically before the
103 /* XBT_LOG_NEW_SUBCATEGORY_helper:
104 * Implementation of XBT_LOG_NEW_SUBCATEGORY, which must declare "extern parent" in addition to avoid an extra
105 * declaration of root when XBT_LOG_NEW_SUBCATEGORY is called by XBT_LOG_NEW_CATEGORY */
106 #define XBT_LOG_NEW_SUBCATEGORY_helper(catName, parent, desc) \
108 extern void _XBT_LOGV_CTOR(catName)(void) XBT_ATTRIB_CONSTRUCTOR(600); \
109 void _XBT_LOGV_CTOR(catName)(void) \
111 XBT_LOG_EXTERNAL_CATEGORY(catName); \
112 if (!_XBT_LOGV(catName).initialized) { \
113 _xbt_log_cat_init(&_XBT_LOGV(catName), xbt_log_priority_uninitialized); \
117 XBT_EXPORT_NO_IMPORT s_xbt_log_category_t _XBT_LOGV(catName) = { \
118 &_XBT_LOGV(parent), \
119 NULL /* firstChild */, \
120 NULL /* nextSibling */, \
123 0 /*initialized */, \
124 xbt_log_priority_uninitialized /* threshold */, \
125 1 /* isThreshInherited */, \
126 NULL /* appender */, \
133 * @param catName name of new category
134 * @param parent father of the new category in the tree
135 * @param desc string describing the purpose of this category
138 * Defines a new subcategory of the parent.
140 #define XBT_LOG_NEW_SUBCATEGORY(catName, parent, desc) \
141 XBT_LOG_EXTERNAL_CATEGORY(parent); \
142 XBT_LOG_NEW_SUBCATEGORY_helper(catName, parent, desc)
146 * @param catName name of new category
147 * @param desc string describing the purpose of this category
150 * Creates a new subcategory of the root category.
152 # define XBT_LOG_NEW_CATEGORY(catName,desc) \
153 XBT_LOG_NEW_SUBCATEGORY_helper(catName, XBT_LOG_ROOT_CAT, desc)
157 * @param cname name of the cat
160 * Indicates which category is the default one.
163 #if defined(XBT_LOG_MAYDAY) /*|| defined (NLOG) * turning logging off */
164 # define XBT_LOG_DEFAULT_CATEGORY(cname)
166 #define XBT_LOG_DEFAULT_CATEGORY(cname) \
167 XBT_ATTRIB_UNUSED static xbt_log_category_t _XBT_LOGV(default) = &_XBT_LOGV(cname)
172 * @param cname name of the cat
173 * @param desc string describing the purpose of this category
176 * Creates a new subcategory of the root category and makes it the default (used by macros that don't explicitly
177 * specify a category).
179 # define XBT_LOG_NEW_DEFAULT_CATEGORY(cname,desc) \
180 XBT_LOG_NEW_CATEGORY(cname,desc); \
181 XBT_LOG_DEFAULT_CATEGORY(cname)
185 * @param cname name of the cat
186 * @param parent name of the parent
187 * @param desc string describing the purpose of this category
190 * Creates a new subcategory of the parent category and makes it the default
191 * (used by macros that don't explicitly specify a category).
193 #define XBT_LOG_NEW_DEFAULT_SUBCATEGORY(cname, parent, desc) \
194 XBT_LOG_NEW_SUBCATEGORY(cname, parent, desc); \
195 XBT_LOG_DEFAULT_CATEGORY(cname)
199 * @param cname name of the cat
202 * Indicates that a category you'll use in this file (e.g., to get subcategories of it) really lives in another file.
205 #define XBT_LOG_EXTERNAL_CATEGORY(cname) \
206 extern s_xbt_log_category_t _XBT_LOGV(cname)
210 * @param cname name of the cat
213 * Indicates that the default category of this file was declared in another file.
216 #define XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(cname) \
217 XBT_LOG_EXTERNAL_CATEGORY(cname);\
218 XBT_LOG_DEFAULT_CATEGORY(cname)
220 /* Functions you may call */
222 XBT_PUBLIC void xbt_log_control_set(const char* cs);
224 /* Forward declarations */
225 typedef struct xbt_log_appender_s s_xbt_log_appender_t;
226 typedef s_xbt_log_appender_t* xbt_log_appender_t;
227 typedef struct xbt_log_layout_s s_xbt_log_layout_t;
228 typedef s_xbt_log_layout_t* xbt_log_layout_t;
229 typedef struct xbt_log_event_s s_xbt_log_event_t;
230 typedef s_xbt_log_event_t* xbt_log_event_t;
231 typedef struct xbt_log_category_s s_xbt_log_category_t;
232 typedef s_xbt_log_category_t* xbt_log_category_t;
234 /* Do NOT access any members of this structure directly. FIXME: move to private? */
236 struct xbt_log_category_s {
237 xbt_log_category_t parent;
238 xbt_log_category_t firstChild;
239 xbt_log_category_t nextSibling;
241 const char *description;
244 int isThreshInherited;
245 xbt_log_appender_t appender;
246 xbt_log_layout_t layout;
250 struct xbt_log_event_s {
251 xbt_log_category_t cat;
252 e_xbt_log_priority_t priority;
253 const char *fileName;
254 const char *functionName;
262 * @ingroup XBT_log_implem
263 * @param cat the category (not only its name, but the variable)
264 * @param thresholdPriority the priority
266 * Programatically alters a category's threshold priority (don't use).
268 XBT_PUBLIC void xbt_log_threshold_set(xbt_log_category_t cat, e_xbt_log_priority_t thresholdPriority);
271 * @ingroup XBT_log_implem
272 * @param cat the category (not only its name, but the variable)
273 * @param app the appender
275 * Programatically sets the category's appender. (the preferred interface is through xbt_log_control_set())
277 XBT_PUBLIC void xbt_log_appender_set(xbt_log_category_t cat, xbt_log_appender_t app);
279 * @ingroup XBT_log_implem
280 * @param cat the category (not only its name, but the variable)
281 * @param lay the layout
283 * Programatically sets the category's layout. (the preferred interface is through xbt_log_control_set())
285 XBT_PUBLIC void xbt_log_layout_set(xbt_log_category_t cat, xbt_log_layout_t lay);
288 * @ingroup XBT_log_implem
289 * @param cat the category (not only its name, but the variable)
290 * @param additivity whether logging actions must be passed to parent.
292 * Programatically sets whether the logging actions must be passed to the parent category.
293 * (the preferred interface is through xbt_log_control_set())
295 XBT_PUBLIC void xbt_log_additivity_set(xbt_log_category_t cat, int additivity);
297 /** @brief create a new simple layout
299 * This layout is not as flexible as the pattern one
301 XBT_PUBLIC xbt_log_layout_t xbt_log_layout_simple_new(const char* arg);
302 XBT_PUBLIC xbt_log_layout_t xbt_log_layout_format_new(const char* arg);
303 XBT_PUBLIC xbt_log_appender_t xbt_log_appender_stream(FILE* f);
304 XBT_PUBLIC xbt_log_appender_t xbt_log_appender_file_new(const char* arg);
305 XBT_PUBLIC xbt_log_appender_t xbt_log_appender2_file_new(const char* arg, int roll);
307 /* ********************************** */
308 /* Functions that you shouldn't call */
309 /* ********************************** */
310 XBT_PUBLIC void xbt_log_init(int* argc, char** argv);
311 XBT_PUBLIC void _xbt_log_event_log(xbt_log_event_t ev, const char* fmt, ...) XBT_ATTRIB_PRINTF(2, 3);
312 XBT_PUBLIC int _xbt_log_cat_init(xbt_log_category_t category, e_xbt_log_priority_t priority);
315 XBT_PUBLIC_DATA s_xbt_log_category_t _XBT_LOGV(XBT_LOG_ROOT_CAT);
317 // If we `dllexport` the root log category, MinGW does not want us to take its address with the error:
318 // > initializer element is not constant
319 // When using auto-import, MinGW is happy.
320 // We should handle this for non-root log categories as well.
321 extern s_xbt_log_category_t _XBT_LOGV(XBT_LOG_ROOT_CAT);
324 extern xbt_log_appender_t xbt_log_default_appender;
325 extern xbt_log_layout_t xbt_log_default_layout;
327 /* ********************** */
328 /* Public functions again */
329 /* ********************** */
333 * @param catName name of the category
334 * @param priority minimal priority to be enabled to return true (must be #e_xbt_log_priority_t)
337 * Returns true if the given priority is enabled for the category.
338 * If you have expensive expressions that are computed outside of the log command and used only within it, you should
339 * make its evaluation conditional using this macro.
341 #define XBT_LOG_ISENABLED(catName, priority) \
342 _XBT_LOG_ISENABLEDV(_XBT_LOGV(catName), priority)
345 * Helper function that implements XBT_LOG_ISENABLED.
348 * First part is a compile-time constant.
349 * Call to xbt_log_cat_init only happens once.
351 #define _XBT_LOG_ISENABLEDV(catv, priority) \
352 (priority >= XBT_LOG_STATIC_THRESHOLD \
353 && ((catv).initialized || _xbt_log_cat_init(&(catv), priority)) \
354 && priority >= (catv).threshold)
358 * Some kludge macros to ease maintenance. See how they're used below.
360 * IMPLEMENTATION NOTE: To reduce the parameter passing overhead of an enabled message, the many parameters passed to
361 * the logging function are packed in a structure. Since these values will be usually be passed to at least 3 functions,
363 * It also allows adding new values (such as a timestamp) without breaking code.
364 * Setting the LogEvent's valist member is done inside _log_logEvent.
369 #ifdef XBT_LOG_MAYDAY
370 # define XBT_CLOG(cat, prio, ...) \
371 _XBT_IF_ONE_ARG(_XBT_CLOG_ARG1, _XBT_CLOG_ARGN, __VA_ARGS__)(__VA_ARGS__)
372 # define _XBT_CLOG_ARG1(f) \
373 fprintf(stderr,"%s:%d:\n" f, __FILE__, __LINE__)
374 # define _XBT_CLOG_ARGN(f, ...) \
375 fprintf(stderr,"%s:%d:\n" f, __FILE__, __LINE__, __VA_ARGS__)
376 # define XBT_LOG(...) XBT_CLOG(0, __VA_ARGS__)
379 // This code is duplicated to remove one level of indirection, working around a MSVC bug
380 // See: http://stackoverflow.com/questions/9183993/msvc-variadic-macro-expansion
382 # define XBT_CLOG(category, prio, ...) \
384 if (_XBT_LOG_ISENABLEDV((category), prio)) { \
385 s_xbt_log_event_t _log_ev; \
386 _log_ev.cat = &(category); \
387 _log_ev.priority = (prio); \
388 _log_ev.fileName = __FILE__; \
389 _log_ev.functionName = __func__; \
390 _log_ev.lineNum = __LINE__; \
391 _xbt_log_event_log(&_log_ev, __VA_ARGS__); \
395 # define XBT_LOG(prio,...) \
397 if (_XBT_LOG_ISENABLEDV((*_simgrid_log_category__default), prio)) { \
398 s_xbt_log_event_t _log_ev; \
399 _log_ev.cat = _simgrid_log_category__default; \
400 _log_ev.priority = (prio); \
401 _log_ev.fileName = __FILE__; \
402 _log_ev.functionName = __func__; \
403 _log_ev.lineNum = __LINE__; \
404 _xbt_log_event_log(&_log_ev, __VA_ARGS__); \
411 * @param categ the category on which to log
412 * @param ... the format string and its arguments
413 * @brief Log an event at the DEBUG priority on the specified category with these args.
415 #define XBT_CDEBUG(categ, ...) \
417 if (XBT_LOG_ISENABLED (categ, xbt_log_priority_debug)) { \
418 s_xbt_log_event_t _log_ev; \
419 _log_ev.cat = &(_XBT_LOGV(categ)); \
420 _log_ev.priority = xbt_log_priority_debug; \
421 _log_ev.fileName = __FILE__; \
422 _log_ev.functionName = __func__; \
423 _log_ev.lineNum = __LINE__; \
424 _xbt_log_event_log(&_log_ev, __VA_ARGS__); \
430 * @brief Log an event at the VERB priority on the specified category with these args.
432 #define XBT_CVERB(categ, ...) \
434 if (XBT_LOG_ISENABLED (categ, xbt_log_priority_verbose)) { \
435 s_xbt_log_event_t _log_ev; \
436 _log_ev.cat = &(_XBT_LOGV(categ)); \
437 _log_ev.priority = xbt_log_priority_verbose; \
438 _log_ev.fileName = __FILE__; \
439 _log_ev.functionName = __func__; \
440 _log_ev.lineNum = __LINE__; \
441 _xbt_log_event_log(&_log_ev, __VA_ARGS__); \
447 * @brief Log an event at the INFO priority on the specified category with these args.
449 #define XBT_CINFO(categ, ...) \
451 if (XBT_LOG_ISENABLED (categ, xbt_log_priority_info)) { \
452 s_xbt_log_event_t _log_ev; \
453 _log_ev.cat = &(_XBT_LOGV(categ)); \
454 _log_ev.priority = xbt_log_priority_info; \
455 _log_ev.fileName = __FILE__; \
456 _log_ev.functionName = __func__; \
457 _log_ev.lineNum = __LINE__; \
458 _xbt_log_event_log(&_log_ev, __VA_ARGS__); \
465 * @brief Log an event at the WARN priority on the specified category with these args.
467 #define XBT_CWARN(categ, ...) \
469 if (XBT_LOG_ISENABLED (categ, xbt_log_priority_warning)) { \
470 s_xbt_log_event_t _log_ev; \
471 _log_ev.cat = &(_XBT_LOGV(categ)); \
472 _log_ev.priority = xbt_log_priority_warning; \
473 _log_ev.fileName = __FILE__; \
474 _log_ev.functionName = __func__; \
475 _log_ev.lineNum = __LINE__; \
476 _xbt_log_event_log(&_log_ev, __VA_ARGS__); \
483 * @brief Log an event at the ERROR priority on the specified category with these args.
485 #define XBT_CERROR(categ, ...) \
487 if (XBT_LOG_ISENABLED (categ, xbt_log_priority_error)) { \
488 s_xbt_log_event_t _log_ev; \
489 _log_ev.cat = &(_XBT_LOGV(categ)); \
490 _log_ev.priority = xbt_log_priority_error; \
491 _log_ev.fileName = __FILE__; \
492 _log_ev.functionName = __func__; \
493 _log_ev.lineNum = __LINE__; \
494 _xbt_log_event_log(&_log_ev, __VA_ARGS__); \
500 * @brief Log an event at the CRITICAL priority on the specified category with these args (CCRITICALn exists for any n<10).
502 #define XBT_CCRITICAL(categ, ...) \
504 if (XBT_LOG_ISENABLED (categ, xbt_log_priority_critical)) { \
505 s_xbt_log_event_t _log_ev; \
506 _log_ev.cat = &(_XBT_LOGV(categ)); \
507 _log_ev.priority = xbt_log_priority_critical; \
508 _log_ev.fileName = __FILE__; \
509 _log_ev.functionName = __func__; \
510 _log_ev.lineNum = __LINE__; \
511 _xbt_log_event_log(&_log_ev, __VA_ARGS__); \
517 * @param ... the format string and its arguments
518 * @brief Log an event at the DEBUG priority on the default category with these args.
520 #define XBT_DEBUG(...) \
522 if (_XBT_LOG_ISENABLEDV(*_simgrid_log_category__default, \
523 xbt_log_priority_debug)) { \
524 s_xbt_log_event_t _log_ev; \
525 _log_ev.cat = _simgrid_log_category__default; \
526 _log_ev.priority = xbt_log_priority_debug; \
527 _log_ev.fileName = __FILE__; \
528 _log_ev.functionName = __func__; \
529 _log_ev.lineNum = __LINE__; \
530 _xbt_log_event_log(&_log_ev, __VA_ARGS__); \
536 * @brief Log an event at the VERB priority on the default category with these args.
538 #define XBT_VERB(...) \
540 if (_XBT_LOG_ISENABLEDV(*_simgrid_log_category__default, \
541 xbt_log_priority_verbose)) { \
542 s_xbt_log_event_t _log_ev; \
543 _log_ev.cat = _simgrid_log_category__default; \
544 _log_ev.priority = xbt_log_priority_verbose; \
545 _log_ev.fileName = __FILE__; \
546 _log_ev.functionName = __func__; \
547 _log_ev.lineNum = __LINE__; \
548 _xbt_log_event_log(&_log_ev, __VA_ARGS__); \
554 * @brief Log an event at the INFO priority on the default category with these args.
556 #define XBT_INFO(...) \
558 if (_XBT_LOG_ISENABLEDV(*_simgrid_log_category__default, \
559 xbt_log_priority_info)) { \
560 s_xbt_log_event_t _log_ev; \
561 _log_ev.cat = _simgrid_log_category__default; \
562 _log_ev.priority = xbt_log_priority_info; \
563 _log_ev.fileName = __FILE__; \
564 _log_ev.functionName = __func__; \
565 _log_ev.lineNum = __LINE__; \
566 _xbt_log_event_log(&_log_ev, __VA_ARGS__); \
572 * @brief Log an event at the WARN priority on the default category with these args.
574 #define XBT_WARN(...) \
576 if (_XBT_LOG_ISENABLEDV(*_simgrid_log_category__default, \
577 xbt_log_priority_warning)) { \
578 s_xbt_log_event_t _log_ev; \
579 _log_ev.cat = _simgrid_log_category__default; \
580 _log_ev.priority = xbt_log_priority_warning; \
581 _log_ev.fileName = __FILE__; \
582 _log_ev.functionName = __func__; \
583 _log_ev.lineNum = __LINE__; \
584 _xbt_log_event_log(&_log_ev, __VA_ARGS__); \
590 * @brief Log an event at the ERROR priority on the default category with these args.
592 #define XBT_ERROR(...) \
594 if (_XBT_LOG_ISENABLEDV(*_simgrid_log_category__default, \
595 xbt_log_priority_error)) { \
596 s_xbt_log_event_t _log_ev; \
597 _log_ev.cat = _simgrid_log_category__default; \
598 _log_ev.priority = xbt_log_priority_error; \
599 _log_ev.fileName = __FILE__; \
600 _log_ev.functionName = __func__; \
601 _log_ev.lineNum = __LINE__; \
602 _xbt_log_event_log(&_log_ev, __VA_ARGS__); \
608 * @brief Log an event at the CRITICAL priority on the default category with these args.
610 #define XBT_CRITICAL(...) \
612 if (_XBT_LOG_ISENABLEDV(*_simgrid_log_category__default, \
613 xbt_log_priority_critical)) { \
614 s_xbt_log_event_t _log_ev; \
615 _log_ev.cat = _simgrid_log_category__default; \
616 _log_ev.priority = xbt_log_priority_critical; \
617 _log_ev.fileName = __FILE__; \
618 _log_ev.functionName = __func__; \
619 _log_ev.lineNum = __LINE__; \
620 _xbt_log_event_log(&_log_ev, __VA_ARGS__); \
624 #define _XBT_IN_OUT(...) \
625 _XBT_IF_ONE_ARG(_XBT_IN_OUT_ARG1, _XBT_IN_OUT_ARGN, __VA_ARGS__)(__VA_ARGS__)
626 #define _XBT_IN_OUT_ARG1(fmt) \
627 XBT_LOG(xbt_log_priority_trace, fmt, __func__)
628 #define _XBT_IN_OUT_ARGN(fmt, ...) \
629 XBT_LOG(xbt_log_priority_trace, fmt, __func__, __VA_ARGS__)
633 * @brief Log at TRACE priority that we entered in current function, appending a user specified format.
635 #define XBT_IN(...) _XBT_IN_OUT(">> begin of %s" __VA_ARGS__)
639 * @brief Log at TRACE priority that we exited the current function, appending a user specified format.
641 #define XBT_OUT(...) _XBT_IN_OUT("<< end of %s" __VA_ARGS__)
645 * @brief Log at TRACE priority a message indicating that we reached that point, appending a user specified format.
647 #define XBT_HERE(...) XBT_LOG(xbt_log_priority_trace, "-- was here" __VA_ARGS__)
651 * @brief Log help messages through category xbt.xbt_help.
653 #define XBT_HELP(...) XBT_CINFO(xbt_help, __VA_ARGS__)
656 #endif /* ! _XBT_LOG_H_ */