1 /**
2 * Copyright (C) Mellanox Technologies Ltd. 2020. ALL RIGHTS RESERVED.
3 *
4 * See file LICENSE for terms.
5 */
6 
7 #ifndef UCS_LOG_DEF_H_
8 #define UCS_LOG_DEF_H_
9 
10 #ifndef UCS_MAX_LOG_LEVEL
11 #  define UCS_MAX_LOG_LEVEL  UCS_LOG_LEVEL_TRACE_LAST
12 #endif
13 
14 #include <ucs/sys/compiler_def.h>
15 #include <ucs/config/global_opts.h>
16 #include <stdarg.h>
17 #include <stdint.h>
18 
19 
20 BEGIN_C_DECLS
21 
22 /** @file log_def.h */
23 
24 #define ucs_log_component_is_enabled(_level, _comp_log_config) \
25     ucs_unlikely(((_level) <= UCS_MAX_LOG_LEVEL) && \
26                  ((_level) <= (((ucs_log_component_config_t*)(_comp_log_config))->log_level)))
27 
28 #define ucs_log_is_enabled(_level) \
29     ucs_log_component_is_enabled(_level, &ucs_global_opts.log_component)
30 
31 #define ucs_log_component(_level, _comp_log_config, _fmt, ...) \
32     do { \
33         if (ucs_log_component_is_enabled(_level, _comp_log_config)) { \
34             ucs_log_dispatch(__FILE__, __LINE__, __func__, \
35                              (ucs_log_level_t)(_level), _comp_log_config, _fmt, ## __VA_ARGS__); \
36         } \
37     } while (0)
38 
39 #define ucs_log(_level, _fmt, ...) \
40     do { \
41         ucs_log_component(_level, &ucs_global_opts.log_component, _fmt, ## __VA_ARGS__); \
42     } while (0)
43 
44 #define ucs_error(_fmt, ...)        ucs_log(UCS_LOG_LEVEL_ERROR, _fmt, ## __VA_ARGS__)
45 #define ucs_warn(_fmt, ...)         ucs_log(UCS_LOG_LEVEL_WARN, _fmt,  ## __VA_ARGS__)
46 #define ucs_diag(_fmt, ...)         ucs_log(UCS_LOG_LEVEL_DIAG, _fmt,  ## __VA_ARGS__)
47 #define ucs_info(_fmt, ...)         ucs_log(UCS_LOG_LEVEL_INFO, _fmt, ## __VA_ARGS__)
48 #define ucs_debug(_fmt, ...)        ucs_log(UCS_LOG_LEVEL_DEBUG, _fmt, ##  __VA_ARGS__)
49 #define ucs_trace(_fmt, ...)        ucs_log(UCS_LOG_LEVEL_TRACE, _fmt, ## __VA_ARGS__)
50 #define ucs_trace_req(_fmt, ...)    ucs_log(UCS_LOG_LEVEL_TRACE_REQ, _fmt, ## __VA_ARGS__)
51 #define ucs_trace_data(_fmt, ...)   ucs_log(UCS_LOG_LEVEL_TRACE_DATA, _fmt, ## __VA_ARGS__)
52 #define ucs_trace_async(_fmt, ...)  ucs_log(UCS_LOG_LEVEL_TRACE_ASYNC, _fmt, ## __VA_ARGS__)
53 #define ucs_trace_func(_fmt, ...)   ucs_log(UCS_LOG_LEVEL_TRACE_FUNC, "%s(" _fmt ")", __FUNCTION__, ## __VA_ARGS__)
54 #define ucs_trace_poll(_fmt, ...)   ucs_log(UCS_LOG_LEVEL_TRACE_POLL, _fmt, ## __VA_ARGS__)
55 
56 
57 /**
58  * Print a message regardless of current log level. Output can be
59  * enabled/disabled via environment variable/configuration settings.
60  *
61  * During debugging it can be useful to add a few prints to the code
62  * without changing a current log level. Also it is useful to be able
63  * to see messages only from specific processes. For example, one may
64  * want to see prints only from rank 0 when debugging MPI.
65  *
66  * The function is intended for debugging only. It should not be used
67  * in the real code.
68  */
69 
70 #define ucs_print(_fmt, ...) \
71     do { \
72         if (ucs_global_opts.log_print_enable) { \
73             ucs_log_dispatch(__FILE__, __LINE__, __FUNCTION__, \
74                              UCS_LOG_LEVEL_PRINT, &ucs_global_opts.log_component, _fmt, ## __VA_ARGS__); \
75         } \
76     } while(0)
77 
78 
79 typedef enum {
80     UCS_LOG_FUNC_RC_STOP,
81     UCS_LOG_FUNC_RC_CONTINUE
82 } ucs_log_func_rc_t;
83 
84 /**
85  * Function type for handling log messages.
86  *
87  * @param file      Source file name.
88  * @param line      Source line number.
89  * @param function  Function name.
90  * @param level     Log level.
91  * @param comp_conf Component specific log config.
92  * @param message   Log message - format string.
93  * @param ap        Log message format parameters.
94  *
95  * @return UCS_LOG_FUNC_RC_CONTINUE - continue to next log handler.
96  *         UCS_LOG_FUNC_RC_STOP     - don't continue.
97  */
98 typedef ucs_log_func_rc_t (*ucs_log_func_t)(const char *file, unsigned line,
99                                             const char *function, ucs_log_level_t level,
100                                             const ucs_log_component_config_t *comp_conf,
101                                             const char *message, va_list ap);
102 
103 
104 extern const char *ucs_log_level_names[];
105 extern const char *ucs_log_category_names[];
106 
107 
108 /**
109  * Dispatch a logging message.
110  *
111  * @param [in] file       Source file name.
112  * @param [in] line       Source line number.
113  * @param [in] function   Function name which generated the log.
114  * @param [in] level      Log level of the message.
115  * @param [in] comp_conf  Component log config.
116  * @param [in] message    Log format.
117  */
118 void ucs_log_dispatch(const char *file, unsigned line, const char *function,
119                       ucs_log_level_t level, ucs_log_component_config_t *comp_conf,
120                       const char *format, ...)
121     UCS_F_PRINTF(6, 7);
122 
123 
124 /**
125  * Flush logging output.
126  */
127 void ucs_log_flush();
128 
129 
130 /**
131  * @return Configured log buffer size
132  */
133 size_t ucs_log_get_buffer_size();
134 
135 
136 /**
137  * Default log handler, which prints the message to the output configured in
138  * UCS global options. See @ref ucs_log_func_t.
139  */
140 ucs_log_func_rc_t
141 ucs_log_default_handler(const char *file, unsigned line, const char *function,
142                         ucs_log_level_t level,
143                         const ucs_log_component_config_t *comp_conf,
144                         const char *format, va_list ap);
145 
146 
147 /**
148  * Show a fatal error
149  */
150 void ucs_log_fatal_error(const char *format, ...);
151 
152 
153 /**
154  * Initialize/cleanup logging subsystem.
155  */
156 void ucs_log_early_init();
157 void ucs_log_init();
158 void ucs_component_log_init();
159 void ucs_log_cleanup();
160 
161 
162 const char *ucs_log_bitmap_to_str(unsigned n, uint8_t *bitmap, size_t length);
163 
164 /**
165  * Add/remove logging handlers
166  */
167 void ucs_log_push_handler(ucs_log_func_t handler);
168 void ucs_log_pop_handler();
169 unsigned ucs_log_num_handlers();
170 
171 
172 /**
173  * Log backtrace.
174  *
175  * @param level          Log level.
176  */
177 void ucs_log_print_backtrace(ucs_log_level_t level);
178 
179 END_C_DECLS
180 
181 #endif
182