pacemaker 2.1.5-a3f44794f94
Scalable High-Availability cluster resource manager
logging.h
Go to the documentation of this file.
1/*
2 * Copyright 2004-2022 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#ifndef PCMK__CRM_COMMON_LOGGING__H
11# define PCMK__CRM_COMMON_LOGGING__H
12
13# include <stdio.h>
14# include <glib.h>
15# include <qb/qblog.h>
16# include <libxml/tree.h>
17
18#ifdef __cplusplus
19extern "C" {
20#endif
21
29/* Define custom log priorities.
30 *
31 * syslog(3) uses int for priorities, but libqb's struct qb_log_callsite uses
32 * uint8_t, so make sure they fit in the latter.
33 */
34
35// Define something even less desired than debug
36# ifndef LOG_TRACE
37# define LOG_TRACE (LOG_DEBUG+1)
38# endif
39
40// Print message to stdout instead of logging it
41# ifndef LOG_STDOUT
42# define LOG_STDOUT 254
43# endif
44
45// Don't send message anywhere
46# ifndef LOG_NEVER
47# define LOG_NEVER 255
48# endif
49
50/* "Extended information" logging support */
51#ifdef QB_XS
52# define CRM_XS QB_XS
53# define crm_extended_logging(t, e) qb_log_ctl((t), QB_LOG_CONF_EXTENDED, (e))
54#else
55# define CRM_XS "|"
56
57/* A caller might want to check the return value, so we can't define this as a
58 * no-op, and we can't simply define it to be 0 because gcc will then complain
59 * when the value isn't checked.
60 */
61static inline int
62crm_extended_logging(int t, int e)
63{
64 return 0;
65}
66#endif
67
68extern unsigned int crm_log_level;
69extern unsigned int crm_trace_nonlog;
70
76extern gboolean crm_config_error;
77
83extern gboolean crm_config_warning;
84
86{
89 xml_log_option_text = 0x0004, /* add this option to dump text into xml */
90 xml_log_option_full_fledged = 0x0008, // Use libxml when converting XML to text
99};
100
101void crm_enable_blackbox(int nsig);
102void crm_disable_blackbox(int nsig);
103void crm_write_blackbox(int nsig, const struct qb_log_callsite *callsite);
104
105void crm_update_callsites(void);
106
107void crm_log_deinit(void);
108
116void crm_log_preinit(const char *entity, int argc, char *const *argv);
117gboolean crm_log_init(const char *entity, uint8_t level, gboolean daemon,
118 gboolean to_stderr, int argc, char **argv, gboolean quiet);
119
120void crm_log_args(int argc, char **argv);
121void crm_log_output_fn(const char *file, const char *function, int line, int level,
122 const char *prefix, const char *output);
123
124// Log a block of text line by line
125#define crm_log_output(level, prefix, output) \
126 crm_log_output_fn(__FILE__, __func__, __LINE__, level, prefix, output)
127
128void crm_bump_log_level(int argc, char **argv);
129
130void crm_enable_stderr(int enable);
131
132gboolean crm_is_callsite_active(struct qb_log_callsite *cs, uint8_t level, uint32_t tags);
133
134void log_data_element(int log_level, const char *file, const char *function,
135 int line, const char *prefix, const xmlNode *data,
136 int depth, gboolean formatted);
137
138/* returns the old value */
139unsigned int set_crm_log_level(unsigned int level);
140
141unsigned int get_crm_log_level(void);
142
143/*
144 * Throughout the macros below, note the leading, pre-comma, space in the
145 * various ' , ##args' occurrences to aid portability across versions of 'gcc'.
146 * https://gcc.gnu.org/onlinedocs/cpp/Variadic-Macros.html#Variadic-Macros
147 */
148#if defined(__clang__)
149# define CRM_TRACE_INIT_DATA(name)
150# else
151# include <assert.h> // required by QB_LOG_INIT_DATA() macro
152# define CRM_TRACE_INIT_DATA(name) QB_LOG_INIT_DATA(name)
153#endif
154
155/* Using "switch" instead of "if" in these macro definitions keeps
156 * static analysis from complaining about constant evaluations
157 */
158
168# define do_crm_log(level, fmt, args...) do { \
169 switch (level) { \
170 case LOG_STDOUT: \
171 printf(fmt "\n" , ##args); \
172 break; \
173 case LOG_NEVER: \
174 break; \
175 default: \
176 qb_log_from_external_source(__func__, __FILE__, fmt, \
177 (level), __LINE__, 0 , ##args); \
178 break; \
179 } \
180 } while (0)
181
192# define do_crm_log_unlikely(level, fmt, args...) do { \
193 switch (level) { \
194 case LOG_STDOUT: case LOG_NEVER: \
195 break; \
196 default: { \
197 static struct qb_log_callsite *trace_cs = NULL; \
198 if (trace_cs == NULL) { \
199 trace_cs = qb_log_callsite_get(__func__, __FILE__, fmt, \
200 (level), __LINE__, 0); \
201 } \
202 if (crm_is_callsite_active(trace_cs, (level), 0)) { \
203 qb_log_from_external_source(__func__, __FILE__, fmt, \
204 (level), __LINE__, 0 , ##args); \
205 } \
206 } \
207 break; \
208 } \
209 } while (0)
210
211# define CRM_LOG_ASSERT(expr) do { \
212 if (!(expr)) { \
213 static struct qb_log_callsite *core_cs = NULL; \
214 if(core_cs == NULL) { \
215 core_cs = qb_log_callsite_get(__func__, __FILE__, \
216 "log-assert", LOG_TRACE, \
217 __LINE__, 0); \
218 } \
219 crm_abort(__FILE__, __func__, __LINE__, #expr, \
220 core_cs?core_cs->targets:FALSE, TRUE); \
221 } \
222 } while(0)
223
224/* 'failure_action' MUST NOT be 'continue' as it will apply to the
225 * macro's do-while loop
226 */
227# define CRM_CHECK(expr, failure_action) do { \
228 if (!(expr)) { \
229 static struct qb_log_callsite *core_cs = NULL; \
230 if (core_cs == NULL) { \
231 core_cs = qb_log_callsite_get(__func__, __FILE__, \
232 "check-assert", \
233 LOG_TRACE, __LINE__, 0); \
234 } \
235 crm_abort(__FILE__, __func__, __LINE__, #expr, \
236 (core_cs? core_cs->targets: FALSE), TRUE); \
237 failure_action; \
238 } \
239 } while(0)
240
251# define do_crm_log_xml(level, text, xml) do { \
252 switch (level) { \
253 case LOG_STDOUT: case LOG_NEVER: \
254 break; \
255 default: { \
256 static struct qb_log_callsite *xml_cs = NULL; \
257 if (xml_cs == NULL) { \
258 xml_cs = qb_log_callsite_get(__func__, __FILE__, \
259 "xml-blob", (level), __LINE__, 0); \
260 } \
261 if (crm_is_callsite_active(xml_cs, (level), 0)) { \
262 log_data_element((level), __FILE__, __func__, \
263 __LINE__, text, xml, 1, xml_log_option_formatted); \
264 } \
265 } \
266 break; \
267 } \
268 } while(0)
269
282# define do_crm_log_alias(level, file, function, line, fmt, args...) do { \
283 switch (level) { \
284 case LOG_STDOUT: \
285 printf(fmt "\n" , ##args); \
286 break; \
287 case LOG_NEVER: \
288 break; \
289 default: \
290 qb_log_from_external_source(function, file, fmt, (level), \
291 line, 0 , ##args); \
292 break; \
293 } \
294 } while (0)
295
310# define crm_perror(level, fmt, args...) do { \
311 switch (level) { \
312 case LOG_NEVER: \
313 break; \
314 default: { \
315 const char *err = strerror(errno); \
316 /* cast to int makes coverity happy when level == 0 */ \
317 if ((level) <= (int) crm_log_level) { \
318 fprintf(stderr, fmt ": %s (%d)\n" , ##args, err, errno);\
319 } \
320 do_crm_log((level), fmt ": %s (%d)" , ##args, err, errno); \
321 } \
322 break; \
323 } \
324 } while (0)
325
337# define crm_log_tag(level, tag, fmt, args...) do { \
338 switch (level) { \
339 case LOG_STDOUT: case LOG_NEVER: \
340 break; \
341 default: { \
342 static struct qb_log_callsite *trace_tag_cs = NULL; \
343 int converted_tag = g_quark_try_string(tag); \
344 if (trace_tag_cs == NULL) { \
345 trace_tag_cs = qb_log_callsite_get(__func__, __FILE__, \
346 fmt, (level), __LINE__, converted_tag); \
347 } \
348 if (crm_is_callsite_active(trace_tag_cs, (level), \
349 converted_tag)) { \
350 qb_log_from_external_source(__func__, __FILE__, fmt, \
351 (level), __LINE__, converted_tag , ##args); \
352 } \
353 } \
354 } \
355 } while (0)
356
357# define crm_emerg(fmt, args...) qb_log(LOG_EMERG, fmt , ##args)
358# define crm_crit(fmt, args...) qb_logt(LOG_CRIT, 0, fmt , ##args)
359# define crm_err(fmt, args...) qb_logt(LOG_ERR, 0, fmt , ##args)
360# define crm_warn(fmt, args...) qb_logt(LOG_WARNING, 0, fmt , ##args)
361# define crm_notice(fmt, args...) qb_logt(LOG_NOTICE, 0, fmt , ##args)
362# define crm_info(fmt, args...) qb_logt(LOG_INFO, 0, fmt , ##args)
363
364# define crm_debug(fmt, args...) do_crm_log_unlikely(LOG_DEBUG, fmt , ##args)
365# define crm_trace(fmt, args...) do_crm_log_unlikely(LOG_TRACE, fmt , ##args)
366
367# define crm_log_xml_crit(xml, text) do_crm_log_xml(LOG_CRIT, text, xml)
368# define crm_log_xml_err(xml, text) do_crm_log_xml(LOG_ERR, text, xml)
369# define crm_log_xml_warn(xml, text) do_crm_log_xml(LOG_WARNING, text, xml)
370# define crm_log_xml_notice(xml, text) do_crm_log_xml(LOG_NOTICE, text, xml)
371# define crm_log_xml_info(xml, text) do_crm_log_xml(LOG_INFO, text, xml)
372# define crm_log_xml_debug(xml, text) do_crm_log_xml(LOG_DEBUG, text, xml)
373# define crm_log_xml_trace(xml, text) do_crm_log_xml(LOG_TRACE, text, xml)
374
375# define crm_log_xml_explicit(xml, text) do { \
376 static struct qb_log_callsite *digest_cs = NULL; \
377 digest_cs = qb_log_callsite_get( \
378 __func__, __FILE__, text, LOG_TRACE, __LINE__, \
379 crm_trace_nonlog); \
380 if (digest_cs && digest_cs->targets) { \
381 do_crm_log_xml(LOG_TRACE, text, xml); \
382 } \
383 } while(0)
384
385#if !defined(PCMK_ALLOW_DEPRECATED) || (PCMK_ALLOW_DEPRECATED == 1)
387#endif
388
389#ifdef __cplusplus
390}
391#endif
392
393#endif
char data[0]
Definition: cpg.c:10
gboolean crm_is_callsite_active(struct qb_log_callsite *cs, uint8_t level, uint32_t tags)
Definition: logging.c:635
unsigned int get_crm_log_level(void)
Definition: logging.c:1030
void crm_log_deinit(void)
Definition: logging.c:125
gboolean crm_log_init(const char *entity, uint8_t level, gboolean daemon, gboolean to_stderr, int argc, char **argv, gboolean quiet)
Definition: logging.c:850
void crm_write_blackbox(int nsig, const struct qb_log_callsite *callsite)
Definition: logging.c:474
void crm_disable_blackbox(int nsig)
Definition: logging.c:458
void crm_update_callsites(void)
Definition: logging.c:664
gboolean crm_config_warning
Definition: utils.c:50
void crm_enable_stderr(int enable)
Definition: logging.c:998
void crm_enable_blackbox(int nsig)
Definition: logging.c:452
unsigned int crm_trace_nonlog
Definition: logging.c:46
void crm_log_output_fn(const char *file, const char *function, int line, int level, const char *prefix, const char *output)
Definition: logging.c:1060
unsigned int set_crm_log_level(unsigned int level)
Definition: logging.c:984
xml_log_options
Definition: logging.h:86
@ xml_log_option_diff_minus
Definition: logging.h:92
@ xml_log_option_filtered
Definition: logging.h:87
@ xml_log_option_diff_all
Definition: logging.h:94
@ xml_log_option_diff_short
Definition: logging.h:93
@ xml_log_option_text
Definition: logging.h:89
@ xml_log_option_diff_plus
Definition: logging.h:91
@ xml_log_option_dirty_add
Definition: logging.h:95
@ xml_log_option_close
Definition: logging.h:98
@ xml_log_option_full_fledged
Definition: logging.h:90
@ xml_log_option_formatted
Definition: logging.h:88
@ xml_log_option_children
Definition: logging.h:97
@ xml_log_option_open
Definition: logging.h:96
void crm_log_args(int argc, char **argv)
Log the command line (once)
Definition: logging.c:1045
gboolean crm_config_error
Definition: utils.c:49
void crm_bump_log_level(int argc, char **argv)
Make logging more verbose.
Definition: logging.c:1019
unsigned int crm_log_level
Definition: logging.c:45
void log_data_element(int log_level, const char *file, const char *function, int line, const char *prefix, const xmlNode *data, int depth, gboolean formatted)
void crm_log_preinit(const char *entity, int argc, char *const *argv)
Initializes the logging system and defaults to the least verbose output level.
Definition: logging.c:765
Deprecated Pacemaker logging API.
int daemon(int nochdir, int noclose)