Logo AND Algorithmique Numérique Distribuée

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