Logo AND Algorithmique Numérique Distribuée

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