Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
partial revert of e16e0233
[simgrid.git] / include / xbt / log.h
1 /* log - a generic logging facility in the spirit of log4j                  */
2
3 /* Copyright (c) 2004-2019. The SimGrid Team. All rights reserved.          */
4
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. */
7
8 /** @addtogroup XBT_log
9  *  @brief A generic logging facility in the spirit of log4j (grounding feature)
10  *
11  */
12
13 /** @defgroup XBT_log_cats Existing log categories
14  *  @ingroup XBT_log
15  *  @brief (automatically extracted)
16  *
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.
19  *
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
22  * simulator.
23  */
24
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
28
29 #ifndef XBT_LOG_H
30 #define XBT_LOG_H
31
32 #include <stdarg.h>
33 #include <stddef.h> /* NULL */
34 #include <stdio.h>  /* FILE */
35 #include <xbt/misc.h>
36 SG_BEGIN_DECL
37 /**@brief Log priorities
38  * @ingroup XBT_log
39  *
40  * The different existing priorities.
41  */
42 typedef enum {
43   //! @cond
44   xbt_log_priority_none = 0,           /** used internally (don't poke with)*/
45   //! @endcond
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 */
53
54   xbt_log_priority_infinite = 8,       /**< value for XBT_LOG_STATIC_THRESHOLD to not log */
55
56   //! @cond
57   xbt_log_priority_uninitialized = -1  /* used internally (don't poke with) */
58   //! @endcond
59 } e_xbt_log_priority_t;
60
61 /*
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
64  */
65
66 /**
67  * @def XBT_LOG_STATIC_THRESHOLD
68  * @ingroup XBT_log
69  *
70  * All logging requests with priority < XBT_LOG_STATIC_THRESHOLD are disabled at compile time, i.e., compiled out.
71  */
72 #ifdef NLOG
73 #  define XBT_LOG_STATIC_THRESHOLD xbt_log_priority_infinite
74 #else
75
76 #  ifdef NDEBUG
77 #    define XBT_LOG_STATIC_THRESHOLD xbt_log_priority_verbose
78 #  else                         /* !NLOG && !NDEBUG */
79
80 #    ifndef XBT_LOG_STATIC_THRESHOLD
81 #      define XBT_LOG_STATIC_THRESHOLD xbt_log_priority_none
82 #    endif                      /* !XBT_LOG_STATIC_THRESHOLD */
83 #  endif                        /* NDEBUG */
84 #endif                          /* !defined(NLOG) */
85
86 /* Transforms a category name to a global variable name. */
87 #define _XBT_LOGV(cat) _XBT_CONCAT(_simgrid_log_category__, cat)
88 #define _XBT_LOGV_CTOR(cat) _XBT_CONCAT2(_XBT_LOGV(cat), __constructor__)
89
90 /* The root of the category hierarchy. */
91 #define XBT_LOG_ROOT_CAT   root
92
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.
95  *
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. We use constructor attributes for these initializations to take place automatically before the
98  * start of main().
99  */
100
101 /* XBT_LOG_NEW_SUBCATEGORY_helper:
102  * Implementation of XBT_LOG_NEW_SUBCATEGORY, which must declare "extern parent" in addition to avoid an extra
103  * declaration of root when XBT_LOG_NEW_SUBCATEGORY is called by XBT_LOG_NEW_CATEGORY */
104 #define XBT_LOG_NEW_SUBCATEGORY_helper(catName, parent, desc)                                                          \
105   SG_BEGIN_DECL                                                                                                        \
106   extern void _XBT_LOGV_CTOR(catName)(void) XBT_ATTRIB_CONSTRUCTOR(600);                                               \
107   void _XBT_LOGV_CTOR(catName)(void)                                                                                   \
108   {                                                                                                                    \
109     XBT_LOG_EXTERNAL_CATEGORY(catName);                                                                                \
110     if (!_XBT_LOGV(catName).initialized) {                                                                             \
111       _xbt_log_cat_init(&_XBT_LOGV(catName), xbt_log_priority_uninitialized);                                          \
112     }                                                                                                                  \
113   }                                                                                                                    \
114   SG_END_DECL                                                                                                          \
115   XBT_EXPORT_NO_IMPORT s_xbt_log_category_t _XBT_LOGV(catName) = {                                                     \
116       &_XBT_LOGV(parent),                                                                                              \
117       NULL /* firstChild */,                                                                                           \
118       NULL /* nextSibling */,                                                                                          \
119       _XBT_STRINGIFY(catName),                                                                                         \
120       (desc),                                                                                                          \
121       0 /*initialized */,                                                                                              \
122       xbt_log_priority_uninitialized /* threshold */,                                                                  \
123       1 /* isThreshInherited */,                                                                                       \
124       NULL /* appender */,                                                                                             \
125       NULL /* layout */,                                                                                               \
126       1 /* additivity */                                                                                               \
127   }
128
129 /**
130  * @ingroup XBT_log
131  * @param catName name of new category
132  * @param parent father of the new category in the tree
133  * @param desc string describing the purpose of this category
134  * @hideinitializer
135  *
136  * Defines a new subcategory of the parent.
137  */
138 #define XBT_LOG_NEW_SUBCATEGORY(catName, parent, desc)                                                                 \
139   XBT_LOG_EXTERNAL_CATEGORY(parent);                                                                                   \
140   XBT_LOG_NEW_SUBCATEGORY_helper(catName, parent, (desc))
141
142 /**
143  * @ingroup XBT_log
144  * @param catName name of new category
145  * @param desc string describing the purpose of this category
146  * @hideinitializer
147  *
148  * Creates a new subcategory of the root category.
149  */
150 #define XBT_LOG_NEW_CATEGORY(catName, desc) XBT_LOG_NEW_SUBCATEGORY_helper(catName, XBT_LOG_ROOT_CAT, (desc))
151
152 /**
153  * @ingroup XBT_log
154  * @param cname name of the cat
155  * @hideinitializer
156  *
157  * Indicates which category is the default one.
158  */
159
160 #if defined(XBT_LOG_MAYDAY) /*|| defined (NLOG) * turning logging off */
161 # define XBT_LOG_DEFAULT_CATEGORY(cname)
162 #else
163 #define XBT_LOG_DEFAULT_CATEGORY(cname)                                                                                \
164   XBT_ATTRIB_UNUSED static xbt_log_category_t _XBT_LOGV(default) = &_XBT_LOGV(cname)
165 #endif
166
167 /**
168  * @ingroup XBT_log
169  * @param cname name of the cat
170  * @param desc string describing the purpose of this category
171  * @hideinitializer
172  *
173  * Creates a new subcategory of the root category and makes it the default (used by macros that don't explicitly
174  * specify a category).
175  */
176 #define XBT_LOG_NEW_DEFAULT_CATEGORY(cname, desc)                                                                      \
177   XBT_LOG_NEW_CATEGORY(cname, (desc));                                                                                 \
178   XBT_LOG_DEFAULT_CATEGORY(cname)
179
180 /**
181  * @ingroup XBT_log
182  * @param cname name of the cat
183  * @param parent name of the parent
184  * @param desc string describing the purpose of this category
185  * @hideinitializer
186  *
187  * Creates a new subcategory of the parent category and makes it the default
188  * (used by macros that don't explicitly specify a category).
189  */
190 #define XBT_LOG_NEW_DEFAULT_SUBCATEGORY(cname, parent, desc)                                                           \
191   XBT_LOG_NEW_SUBCATEGORY(cname, parent, (desc));                                                                      \
192   XBT_LOG_DEFAULT_CATEGORY(cname)
193
194 /**
195  * @ingroup XBT_log
196  * @param cname name of the cat
197  * @hideinitializer
198  *
199  * Indicates that a category you'll use in this file (e.g., to get subcategories of it) really lives in another file.
200  */
201
202 #define XBT_LOG_EXTERNAL_CATEGORY(cname) \
203    extern s_xbt_log_category_t _XBT_LOGV(cname)
204
205 /**
206  * @ingroup XBT_log
207  * @param cname name of the cat
208  * @hideinitializer
209  *
210  * Indicates that the default category of this file was declared in another file.
211  */
212
213 #define XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(cname) \
214    XBT_LOG_EXTERNAL_CATEGORY(cname);\
215    XBT_LOG_DEFAULT_CATEGORY(cname)
216
217 /* Functions you may call */
218
219 XBT_PUBLIC void xbt_log_control_set(const char* cs);
220
221 /* Forward declarations */
222 typedef struct xbt_log_appender_s  s_xbt_log_appender_t;
223 typedef s_xbt_log_appender_t* xbt_log_appender_t;
224 typedef struct xbt_log_layout_s  s_xbt_log_layout_t;
225 typedef s_xbt_log_layout_t* xbt_log_layout_t;
226 typedef struct xbt_log_event_s  s_xbt_log_event_t;
227 typedef s_xbt_log_event_t* xbt_log_event_t;
228 typedef struct xbt_log_category_s  s_xbt_log_category_t;
229 typedef s_xbt_log_category_t* xbt_log_category_t;
230
231 /* Do NOT access any members of this structure directly. FIXME: move to private? */
232
233 struct xbt_log_category_s {
234   xbt_log_category_t parent;
235   xbt_log_category_t firstChild;
236   xbt_log_category_t nextSibling;
237   const char *name;
238   const char *description;
239   int initialized;
240   int threshold;
241   int isThreshInherited;
242   xbt_log_appender_t appender;
243   xbt_log_layout_t layout;
244   int additivity;
245 };
246
247 struct xbt_log_event_s {
248   xbt_log_category_t cat;
249   e_xbt_log_priority_t priority;
250   const char *fileName;
251   const char *functionName;
252   int lineNum;
253   va_list ap;
254   char *buffer;
255   int buffer_size;
256 };
257
258 /**
259  * @ingroup XBT_log_implem
260  * @param cat the category (not only its name, but the variable)
261  * @param thresholdPriority the priority
262  *
263  * Programmatically alters a category's threshold priority (don't use).
264  */
265 XBT_PUBLIC void xbt_log_threshold_set(xbt_log_category_t cat, e_xbt_log_priority_t thresholdPriority);
266
267 /**
268  * @ingroup XBT_log_implem
269  * @param cat the category (not only its name, but the variable)
270  * @param app the appender
271  *
272  * Programmatically sets the category's appender. (the preferred interface is through xbt_log_control_set())
273  */
274 XBT_PUBLIC void xbt_log_appender_set(xbt_log_category_t cat, xbt_log_appender_t app);
275 /**
276  * @ingroup XBT_log_implem
277  * @param cat the category (not only its name, but the variable)
278  * @param lay the layout
279  *
280  * Programmatically sets the category's layout. (the preferred interface is through xbt_log_control_set())
281  */
282 XBT_PUBLIC void xbt_log_layout_set(xbt_log_category_t cat, xbt_log_layout_t lay);
283
284 /**
285  * @ingroup XBT_log_implem
286  * @param cat the category (not only its name, but the variable)
287  * @param additivity whether logging actions must be passed to parent.
288  *
289  * Programmatically sets whether the logging actions must be passed to the parent category.
290  * (the preferred interface is through xbt_log_control_set())
291  */
292 XBT_PUBLIC void xbt_log_additivity_set(xbt_log_category_t cat, int additivity);
293
294 /** @brief create a new simple layout
295  *
296  * This layout is not as flexible as the pattern one
297  */
298 XBT_PUBLIC xbt_log_layout_t xbt_log_layout_simple_new(const char* arg);
299 XBT_PUBLIC xbt_log_layout_t xbt_log_layout_format_new(const char* arg);
300 XBT_PUBLIC xbt_log_appender_t xbt_log_appender_stream(FILE* f);
301 XBT_PUBLIC xbt_log_appender_t xbt_log_appender_file_new(const char* arg);
302 XBT_PUBLIC xbt_log_appender_t xbt_log_appender2_file_new(const char* arg, int roll);
303
304 /* ********************************** */
305 /* Functions that you shouldn't call  */
306 /* ********************************** */
307 XBT_PUBLIC void xbt_log_init(int* argc, char** argv);
308 XBT_PUBLIC void _xbt_log_event_log(xbt_log_event_t ev, const char* fmt, ...) XBT_ATTRIB_PRINTF(2, 3);
309 XBT_PUBLIC int _xbt_log_cat_init(xbt_log_category_t category, e_xbt_log_priority_t priority);
310
311 #ifdef DLL_EXPORT
312 XBT_PUBLIC_DATA s_xbt_log_category_t _XBT_LOGV(XBT_LOG_ROOT_CAT);
313 #else
314 // If we `dllexport` the root log category, MinGW does not want us to take its address with the error:
315 // > initializer element is not constant
316 // When using auto-import, MinGW is happy.
317 // We should handle this for non-root log categories as well.
318 extern s_xbt_log_category_t _XBT_LOGV(XBT_LOG_ROOT_CAT);
319 #endif
320
321 extern xbt_log_appender_t xbt_log_default_appender;
322 extern xbt_log_layout_t xbt_log_default_layout;
323
324 /* ********************** */
325 /* Public functions again */
326 /* ********************** */
327
328 /**
329  * @ingroup XBT_log
330  * @param catName name of the category
331  * @param priority minimal priority to be enabled to return true (must be #e_xbt_log_priority_t)
332  * @hideinitializer
333  *
334  * Returns true if the given priority is enabled for the category.
335  * If you have expensive expressions that are computed outside of the log command and used only within it, you should
336  * make its evaluation conditional using this macro.
337  */
338 #define XBT_LOG_ISENABLED(catName, priority) _XBT_LOG_ISENABLEDV(_XBT_LOGV(catName), (priority))
339
340 /*
341  * Helper function that implements XBT_LOG_ISENABLED.
342  *
343  * NOTES
344  * First part is a compile-time constant.
345  * Call to xbt_log_cat_init only happens once.
346  */
347 #define _XBT_LOG_ISENABLEDV(catv, priority)                                                                            \
348   ((priority) >= XBT_LOG_STATIC_THRESHOLD && ((catv).initialized || _xbt_log_cat_init(&(catv), (priority))) &&         \
349    (priority) >= (catv).threshold)
350
351 /*
352  * Internal Macros
353  * Some kludge macros to ease maintenance. See how they're used below.
354  *
355  * IMPLEMENTATION NOTE: To reduce the parameter passing overhead of an enabled message, the many parameters passed to
356  * the logging function are packed in a structure. Since these values will be usually be passed to at least 3 functions,
357  * this is a win.
358  * It also allows adding new values (such as a timestamp) without breaking code.
359  * Setting the LogEvent's valist member is done inside _log_logEvent.
360  */
361
362 /* Logging Macros */
363
364 #ifdef XBT_LOG_MAYDAY
365 # define XBT_CLOG(cat, prio, ...) \
366   _XBT_IF_ONE_ARG(_XBT_CLOG_ARG1, _XBT_CLOG_ARGN, __VA_ARGS__)(__VA_ARGS__)
367 # define _XBT_CLOG_ARG1(f) \
368   fprintf(stderr,"%s:%d:\n" f, __FILE__, __LINE__)
369 # define _XBT_CLOG_ARGN(f, ...) \
370   fprintf(stderr,"%s:%d:\n" f, __FILE__, __LINE__, __VA_ARGS__)
371 # define XBT_LOG(...) XBT_CLOG(0, __VA_ARGS__)
372 #else
373
374 #define XBT_CLOG(category, prio, ...)                                                                                  \
375   do {                                                                                                                 \
376     if (_XBT_LOG_ISENABLEDV((category), (prio))) {                                                                     \
377       s_xbt_log_event_t _log_ev;                                                                                       \
378       _log_ev.cat          = &(category);                                                                              \
379       _log_ev.priority     = (prio);                                                                                   \
380       _log_ev.fileName     = __FILE__;                                                                                 \
381       _log_ev.functionName = __func__;                                                                                 \
382       _log_ev.lineNum      = __LINE__;                                                                                 \
383       _xbt_log_event_log(&_log_ev, __VA_ARGS__);                                                                       \
384     }                                                                                                                  \
385   } while (0)
386
387 #define XBT_LOG(prio, ...) XBT_CLOG(*_simgrid_log_category__default, (prio), __VA_ARGS__)
388
389 #endif
390
391 /** @ingroup XBT_log
392  *  @hideinitializer
393  * @param categ the category on which to log
394  * @param ... the format string and its arguments
395  *  @brief Log an event at the DEBUG priority on the specified category with these args.
396  */
397 #define XBT_CDEBUG(categ, ...) XBT_CLOG(_XBT_LOGV(categ), xbt_log_priority_debug, __VA_ARGS__)
398
399 /** @ingroup XBT_log
400  *  @hideinitializer
401  *  @brief Log an event at the VERB priority on the specified category with these args.
402  */
403 #define XBT_CVERB(categ, ...) XBT_CLOG(_XBT_LOGV(categ), xbt_log_priority_verbose, __VA_ARGS__)
404
405 /** @ingroup XBT_log
406  *  @hideinitializer
407  *  @brief Log an event at the INFO priority on the specified category with these args.
408  */
409 #define XBT_CINFO(categ, ...) XBT_CLOG(_XBT_LOGV(categ), xbt_log_priority_info, __VA_ARGS__)
410
411 /** @ingroup XBT_log
412  *  @hideinitializer
413  *  @brief Log an event at the WARN priority on the specified category with these args.
414  */
415 #define XBT_CWARN(categ, ...) XBT_CLOG(_XBT_LOGV(categ), xbt_log_priority_warning, __VA_ARGS__)
416
417 /** @ingroup XBT_log
418  *  @hideinitializer
419  *  @brief Log an event at the ERROR priority on the specified category with these args.
420  */
421 #define XBT_CERROR(categ, ...) XBT_CLOG(_XBT_LOGV(categ), xbt_log_priority_error, __VA_ARGS__)
422
423 /** @ingroup XBT_log
424  *  @hideinitializer
425  *  @brief Log an event at the CRITICAL priority on the specified category with these args.
426  */
427 #define XBT_CCRITICAL(categ, ...) XBT_CLOG(_XBT_LOGV(categ), xbt_log_priority_critical, __VA_ARGS__)
428
429 /** @ingroup XBT_log
430  *  @hideinitializer
431  *  @param ... the format string and its arguments
432  *  @brief Log an event at the DEBUG priority on the default category with these args.
433  */
434 #define XBT_DEBUG(...) XBT_LOG(xbt_log_priority_debug, __VA_ARGS__)
435
436 /** @ingroup XBT_log
437  *  @hideinitializer
438  *  @brief Log an event at the VERB priority on the default category with these args.
439  */
440 #define XBT_VERB(...) XBT_LOG(xbt_log_priority_verbose, __VA_ARGS__)
441
442 /** @ingroup XBT_log
443  *  @hideinitializer
444  *  @brief Log an event at the INFO priority on the default category with these args.
445  */
446 #define XBT_INFO(...) XBT_LOG(xbt_log_priority_info, __VA_ARGS__)
447
448 /** @ingroup XBT_log
449  *  @hideinitializer
450  *  @brief Log an event at the WARN priority on the default category with these args.
451  */
452 #define XBT_WARN(...) XBT_LOG(xbt_log_priority_warning, __VA_ARGS__)
453
454 /** @ingroup XBT_log
455  *  @hideinitializer
456  *  @brief Log an event at the ERROR priority on the default category with these args.
457  */
458 #define XBT_ERROR(...) XBT_LOG(xbt_log_priority_error, __VA_ARGS__)
459
460 /** @ingroup XBT_log
461  *  @hideinitializer
462  *  @brief Log an event at the CRITICAL priority on the default category with these args.
463  */
464 #define XBT_CRITICAL(...) XBT_LOG(xbt_log_priority_critical, __VA_ARGS__)
465
466 #define _XBT_IN_OUT(...) \
467   _XBT_IF_ONE_ARG(_XBT_IN_OUT_ARG1, _XBT_IN_OUT_ARGN, __VA_ARGS__)(__VA_ARGS__)
468 #define _XBT_IN_OUT_ARG1(fmt) XBT_LOG(xbt_log_priority_trace, (fmt), __func__)
469 #define _XBT_IN_OUT_ARGN(fmt, ...) XBT_LOG(xbt_log_priority_trace, (fmt), __func__, __VA_ARGS__)
470
471 /** @ingroup XBT_log
472  *  @hideinitializer
473  *  @brief Log at TRACE priority that we entered in current function, appending a user specified format.
474  */
475 #define XBT_IN(...) _XBT_IN_OUT(">> begin of %s" __VA_ARGS__)
476
477 /** @ingroup XBT_log
478  *  @hideinitializer
479  *  @brief Log at TRACE priority that we exited the current function, appending a user specified format.
480  */
481 #define XBT_OUT(...) _XBT_IN_OUT("<< end of %s" __VA_ARGS__)
482
483 /** @ingroup XBT_log
484  *  @hideinitializer
485  *  @brief Log at TRACE priority a message indicating that we reached that point, appending a user specified format.
486  */
487 #define XBT_HERE(...) XBT_LOG(xbt_log_priority_trace, "-- was here" __VA_ARGS__)
488
489 /** @ingroup XBT_log
490  *  @hideinitializer
491  *  @brief Log help messages through category xbt.xbt_help.
492  */
493 #define XBT_HELP(...) XBT_CINFO(xbt_help, __VA_ARGS__)
494
495 SG_END_DECL
496 #endif                          /* ! _XBT_LOG_H_ */