Logo AND Algorithmique Numérique Distribuée

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