1 /*
2 * Copyright 2004-2021 the Pacemaker project contributors
3 *
4 * The version control history for this file may have further details.
5 *
6 * This source code is licensed under the GNU General Public License version 2
7 * or later (GPLv2+) WITHOUT ANY WARRANTY.
8 */
9
10 #ifdef __cplusplus
11 extern "C" {
12 #endif
13
14 /**
15 * \file
16 * \brief Wrappers for and extensions to libqb logging
17 * \ingroup core
18 */
19
20 #ifndef CRM_LOGGING__H
21 # define CRM_LOGGING__H
22
23 # include <stdio.h>
24 # include <glib.h>
25 # include <qb/qblog.h>
26 # include <libxml/tree.h>
27
28 /* Define custom log priorities.
29 *
30 * syslog(3) uses int for priorities, but libqb's struct qb_log_callsite uses
31 * uint8_t, so make sure they fit in the latter.
32 */
33
34 // Define something even less desired than debug
35 # ifndef LOG_TRACE
36 # define LOG_TRACE (LOG_DEBUG+1)
37 # endif
38
39 // Print message to stdout instead of logging it
40 # ifndef LOG_STDOUT
41 # define LOG_STDOUT 254
42 # endif
43
44 // Don't send message anywhere
45 # ifndef LOG_NEVER
46 # define LOG_NEVER 255
47 # endif
48
49 /* "Extended information" logging support */
50 #ifdef QB_XS
51 # define CRM_XS QB_XS
52 # define crm_extended_logging(t, e) qb_log_ctl((t), QB_LOG_CONF_EXTENDED, (e))
53 #else
54 # define CRM_XS "|"
55
56 /* A caller might want to check the return value, so we can't define this as a
57 * no-op, and we can't simply define it to be 0 because gcc will then complain
58 * when the value isn't checked.
59 */
60 static inline int
61 crm_extended_logging(int t, int e)
/* ![[previous]](../icons/n_left.png)
![[next]](../icons/n_right.png)
![[first]](../icons/n_first.png)
![[last]](../icons/n_last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
62 {
63 return 0;
64 }
65 #endif
66
67 extern unsigned int crm_log_level;
68 extern unsigned int crm_trace_nonlog;
69
70 /*! \deprecated Pacemaker library functions set this when a configuration
71 * error is found, which turns on extra messages at the end of
72 * processing. It should not be used directly and will be removed
73 * from the public C API in a future release.
74 */
75 extern gboolean crm_config_error;
76
77 /*! \deprecated Pacemaker library functions set this when a configuration
78 * warning is found, which turns on extra messages at the end of
79 * processing. It should not be used directly and will be removed
80 * from the public C API in a future release.
81 */
82 extern gboolean crm_config_warning;
83
84 enum xml_log_options
85 {
86 xml_log_option_filtered = 0x0001,
87 xml_log_option_formatted = 0x0002,
88 xml_log_option_text = 0x0004, /* add this option to dump text into xml */
89 xml_log_option_full_fledged = 0x0008, // Use libxml when converting XML to text
90 xml_log_option_diff_plus = 0x0010,
91 xml_log_option_diff_minus = 0x0020,
92 xml_log_option_diff_short = 0x0040,
93 xml_log_option_diff_all = 0x0100,
94 xml_log_option_dirty_add = 0x1000,
95 xml_log_option_open = 0x2000,
96 xml_log_option_children = 0x4000,
97 xml_log_option_close = 0x8000,
98 };
99
100 void crm_enable_blackbox(int nsig);
101 void crm_disable_blackbox(int nsig);
102 void crm_write_blackbox(int nsig, struct qb_log_callsite *callsite);
103
104 void crm_update_callsites(void);
105
106 void crm_log_deinit(void);
107
108 void crm_log_preinit(const char *entity, int argc, char **argv);
109 gboolean crm_log_init(const char *entity, uint8_t level, gboolean daemon,
110 gboolean to_stderr, int argc, char **argv, gboolean quiet);
111
112 void crm_log_args(int argc, char **argv);
113 void crm_log_output_fn(const char *file, const char *function, int line, int level,
114 const char *prefix, const char *output);
115
116 // Log a block of text line by line
117 #define crm_log_output(level, prefix, output) \
118 crm_log_output_fn(__FILE__, __func__, __LINE__, level, prefix, output)
119
120 void crm_bump_log_level(int argc, char **argv);
121
122 void crm_enable_stderr(int enable);
123
124 gboolean crm_is_callsite_active(struct qb_log_callsite *cs, uint8_t level, uint32_t tags);
125
126 void log_data_element(int log_level, const char *file, const char *function, int line,
127 const char *prefix, xmlNode * data, int depth, gboolean formatted);
128
129 /* returns the old value */
130 unsigned int set_crm_log_level(unsigned int level);
131
132 unsigned int get_crm_log_level(void);
133
134 /*
135 * Throughout the macros below, note the leading, pre-comma, space in the
136 * various ' , ##args' occurrences to aid portability across versions of 'gcc'.
137 * https://gcc.gnu.org/onlinedocs/cpp/Variadic-Macros.html#Variadic-Macros
138 */
139 #if defined(__clang__)
140 # define CRM_TRACE_INIT_DATA(name)
141 # else
142 # include <assert.h> // required by QB_LOG_INIT_DATA() macro
143 # define CRM_TRACE_INIT_DATA(name) QB_LOG_INIT_DATA(name)
144 #endif
145
146 /* Using "switch" instead of "if" in these macro definitions keeps
147 * static analysis from complaining about constant evaluations
148 */
149
150 /*!
151 * \brief Log a message
152 *
153 * \param[in] level Priority at which to log the message
154 * \param[in] fmt printf-style format string literal for message
155 * \param[in] args Any arguments needed by format string
156 *
157 * \note This is a macro, and \p level may be evaluated more than once.
158 */
159 # define do_crm_log(level, fmt, args...) do { \
160 switch (level) { \
161 case LOG_STDOUT: \
162 printf(fmt "\n" , ##args); \
163 break; \
164 case LOG_NEVER: \
165 break; \
166 default: \
167 qb_log_from_external_source(__func__, __FILE__, fmt, \
168 (level), __LINE__, 0 , ##args); \
169 break; \
170 } \
171 } while (0)
172
173 /*!
174 * \brief Log a message that is likely to be filtered out
175 *
176 * \param[in] level Priority at which to log the message
177 * \param[in] fmt printf-style format string for message
178 * \param[in] args Any arguments needed by format string
179 *
180 * \note This is a macro, and \p level may be evaluated more than once.
181 * This does nothing when level is LOG_STDOUT.
182 */
183 # define do_crm_log_unlikely(level, fmt, args...) do { \
184 switch (level) { \
185 case LOG_STDOUT: case LOG_NEVER: \
186 break; \
187 default: { \
188 static struct qb_log_callsite *trace_cs = NULL; \
189 if (trace_cs == NULL) { \
190 trace_cs = qb_log_callsite_get(__func__, __FILE__, fmt, \
191 (level), __LINE__, 0); \
192 } \
193 if (crm_is_callsite_active(trace_cs, (level), 0)) { \
194 qb_log_from_external_source(__func__, __FILE__, fmt, \
195 (level), __LINE__, 0 , ##args); \
196 } \
197 } \
198 break; \
199 } \
200 } while (0)
201
202 # define CRM_LOG_ASSERT(expr) do { \
203 if (!(expr)) { \
204 static struct qb_log_callsite *core_cs = NULL; \
205 if(core_cs == NULL) { \
206 core_cs = qb_log_callsite_get(__func__, __FILE__, \
207 "log-assert", LOG_TRACE, \
208 __LINE__, 0); \
209 } \
210 crm_abort(__FILE__, __func__, __LINE__, #expr, \
211 core_cs?core_cs->targets:FALSE, TRUE); \
212 } \
213 } while(0)
214
215 /* 'failure_action' MUST NOT be 'continue' as it will apply to the
216 * macro's do-while loop
217 */
218 # define CRM_CHECK(expr, failure_action) do { \
219 if (!(expr)) { \
220 static struct qb_log_callsite *core_cs = NULL; \
221 if (core_cs == NULL) { \
222 core_cs = qb_log_callsite_get(__func__, __FILE__, \
223 "check-assert", \
224 LOG_TRACE, __LINE__, 0); \
225 } \
226 crm_abort(__FILE__, __func__, __LINE__, #expr, \
227 (core_cs? core_cs->targets: FALSE), TRUE); \
228 failure_action; \
229 } \
230 } while(0)
231
232 /*!
233 * \brief Log XML line-by-line in a formatted fashion
234 *
235 * \param[in] level Priority at which to log the messages
236 * \param[in] text Prefix for each line
237 * \param[in] xml XML to log
238 *
239 * \note This is a macro, and \p level may be evaluated more than once.
240 * This does nothing when level is LOG_STDOUT.
241 */
242 # define do_crm_log_xml(level, text, xml) do { \
243 switch (level) { \
244 case LOG_STDOUT: case LOG_NEVER: \
245 break; \
246 default: { \
247 static struct qb_log_callsite *xml_cs = NULL; \
248 if (xml_cs == NULL) { \
249 xml_cs = qb_log_callsite_get(__func__, __FILE__, \
250 "xml-blob", (level), __LINE__, 0); \
251 } \
252 if (crm_is_callsite_active(xml_cs, (level), 0)) { \
253 log_data_element((level), __FILE__, __func__, \
254 __LINE__, text, xml, 1, xml_log_option_formatted); \
255 } \
256 } \
257 break; \
258 } \
259 } while(0)
260
261 /*!
262 * \brief Log a message as if it came from a different code location
263 *
264 * \param[in] level Priority at which to log the message
265 * \param[in] file Source file name to use instead of __FILE__
266 * \param[in] function Source function name to use instead of __func__
267 * \param[in] line Source line number to use instead of __line__
268 * \param[in] fmt printf-style format string literal for message
269 * \param[in] args Any arguments needed by format string
270 *
271 * \note This is a macro, and \p level may be evaluated more than once.
272 */
273 # define do_crm_log_alias(level, file, function, line, fmt, args...) do { \
274 switch (level) { \
275 case LOG_STDOUT: \
276 printf(fmt "\n" , ##args); \
277 break; \
278 case LOG_NEVER: \
279 break; \
280 default: \
281 qb_log_from_external_source(function, file, fmt, (level), \
282 line, 0 , ##args); \
283 break; \
284 } \
285 } while (0)
286
287 /*!
288 * \brief Send a system error message to both the log and stderr
289 *
290 * \param[in] level Priority at which to log the message
291 * \param[in] fmt printf-style format string for message
292 * \param[in] args Any arguments needed by format string
293 *
294 * \deprecated One of the other logging functions should be used with
295 * pcmk_strerror() instead.
296 * \note This is a macro, and \p level may be evaluated more than once.
297 * \note Because crm_perror() adds the system error message and error number
298 * onto the end of fmt, that information will become extended information
299 * if CRM_XS is used inside fmt and will not show up in syslog.
300 */
301 # define crm_perror(level, fmt, args...) do { \
302 switch (level) { \
303 case LOG_NEVER: \
304 break; \
305 default: { \
306 const char *err = strerror(errno); \
307 /* cast to int makes coverity happy when level == 0 */ \
308 if ((level) <= (int) crm_log_level) { \
309 fprintf(stderr, fmt ": %s (%d)\n" , ##args, err, errno);\
310 } \
311 do_crm_log((level), fmt ": %s (%d)" , ##args, err, errno); \
312 } \
313 break; \
314 } \
315 } while (0)
316
317 /*!
318 * \brief Log a message with a tag (for use with PCMK_trace_tags)
319 *
320 * \param[in] level Priority at which to log the message
321 * \param[in] tag String to tag message with
322 * \param[in] fmt printf-style format string for message
323 * \param[in] args Any arguments needed by format string
324 *
325 * \note This is a macro, and \p level may be evaluated more than once.
326 * This does nothing when level is LOG_STDOUT.
327 */
328 # define crm_log_tag(level, tag, fmt, args...) do { \
329 switch (level) { \
330 case LOG_STDOUT: case LOG_NEVER: \
331 break; \
332 default: { \
333 static struct qb_log_callsite *trace_tag_cs = NULL; \
334 int converted_tag = g_quark_try_string(tag); \
335 if (trace_tag_cs == NULL) { \
336 trace_tag_cs = qb_log_callsite_get(__func__, __FILE__, \
337 fmt, (level), __LINE__, converted_tag); \
338 } \
339 if (crm_is_callsite_active(trace_tag_cs, (level), \
340 converted_tag)) { \
341 qb_log_from_external_source(__func__, __FILE__, fmt, \
342 (level), __LINE__, converted_tag , ##args); \
343 } \
344 } \
345 } \
346 } while (0)
347
348 # define crm_emerg(fmt, args...) qb_log(LOG_EMERG, fmt , ##args)
349 # define crm_crit(fmt, args...) qb_logt(LOG_CRIT, 0, fmt , ##args)
350 # define crm_err(fmt, args...) qb_logt(LOG_ERR, 0, fmt , ##args)
351 # define crm_warn(fmt, args...) qb_logt(LOG_WARNING, 0, fmt , ##args)
352 # define crm_notice(fmt, args...) qb_logt(LOG_NOTICE, 0, fmt , ##args)
353 # define crm_info(fmt, args...) qb_logt(LOG_INFO, 0, fmt , ##args)
354
355 # define crm_debug(fmt, args...) do_crm_log_unlikely(LOG_DEBUG, fmt , ##args)
356 # define crm_trace(fmt, args...) do_crm_log_unlikely(LOG_TRACE, fmt , ##args)
357
358 # define crm_log_xml_crit(xml, text) do_crm_log_xml(LOG_CRIT, text, xml)
359 # define crm_log_xml_err(xml, text) do_crm_log_xml(LOG_ERR, text, xml)
360 # define crm_log_xml_warn(xml, text) do_crm_log_xml(LOG_WARNING, text, xml)
361 # define crm_log_xml_notice(xml, text) do_crm_log_xml(LOG_NOTICE, text, xml)
362 # define crm_log_xml_info(xml, text) do_crm_log_xml(LOG_INFO, text, xml)
363 # define crm_log_xml_debug(xml, text) do_crm_log_xml(LOG_DEBUG, text, xml)
364 # define crm_log_xml_trace(xml, text) do_crm_log_xml(LOG_TRACE, text, xml)
365
366 # define crm_log_xml_explicit(xml, text) do { \
367 static struct qb_log_callsite *digest_cs = NULL; \
368 digest_cs = qb_log_callsite_get( \
369 __func__, __FILE__, text, LOG_TRACE, __LINE__, \
370 crm_trace_nonlog); \
371 if (digest_cs && digest_cs->targets) { \
372 do_crm_log_xml(LOG_TRACE, text, xml); \
373 } \
374 } while(0)
375
376 # define crm_str(x) (const char*)(x?x:"<null>")
377
378 #if !defined(PCMK_ALLOW_DEPRECATED) || (PCMK_ALLOW_DEPRECATED == 1)
379 #include <crm/common/logging_compat.h>
380 #endif
381
382 #ifdef __cplusplus
383 }
384 #endif
385
386 #endif