pacemaker  2.0.4-2deceaa
Scalable High-Availability cluster resource manager
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
logging.h
Go to the documentation of this file.
1 /*
2  * Copyright 2004-2020 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 
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)
62 {
63  return 0;
64 }
65 #endif
66 
67 extern unsigned int crm_log_level;
68 extern gboolean crm_config_error;
69 extern gboolean crm_config_warning;
70 extern unsigned int crm_trace_nonlog;
71 
73 {
76  xml_log_option_text = 0x0004, /* add this option to dump text into xml */
77  xml_log_option_full_fledged = 0x0008, /* crm_xml_dump: serialize using libxml */
86 };
87 
88 void crm_enable_blackbox(int nsig);
89 void crm_disable_blackbox(int nsig);
90 void crm_write_blackbox(int nsig, struct qb_log_callsite *callsite);
91 
92 void crm_update_callsites(void);
93 
94 void crm_log_deinit(void);
95 
96 gboolean crm_log_cli_init(const char *entity);
97 
98 void crm_log_preinit(const char *entity, int argc, char **argv);
99 gboolean crm_log_init(const char *entity, uint8_t level, gboolean daemon,
100  gboolean to_stderr, int argc, char **argv, gboolean quiet);
101 
102 void crm_log_args(int argc, char **argv);
103 void crm_log_output_fn(const char *file, const char *function, int line, int level,
104  const char *prefix, const char *output);
105 
106 // Log a block of text line by line
107 # define crm_log_output(level, prefix, output) crm_log_output_fn(__FILE__, __FUNCTION__, __LINE__, level, prefix, output)
108 
109 gboolean crm_add_logfile(const char *filename);
110 
111 void crm_bump_log_level(int argc, char **argv);
112 
113 void crm_enable_stderr(int enable);
114 
115 gboolean crm_is_callsite_active(struct qb_log_callsite *cs, uint8_t level, uint32_t tags);
116 
117 void log_data_element(int log_level, const char *file, const char *function, int line,
118  const char *prefix, xmlNode * data, int depth, gboolean formatted);
119 
120 /* returns the old value */
121 unsigned int set_crm_log_level(unsigned int level);
122 
123 unsigned int get_crm_log_level(void);
124 
125 /*
126  * Throughout the macros below, note the leading, pre-comma, space in the
127  * various ' , ##args' occurrences to aid portability across versions of 'gcc'.
128  * https://gcc.gnu.org/onlinedocs/cpp/Variadic-Macros.html#Variadic-Macros
129  */
130 #if defined(__clang__)
131 # define CRM_TRACE_INIT_DATA(name)
132 # else
133 # include <assert.h> // required by QB_LOG_INIT_DATA() macro
134 # define CRM_TRACE_INIT_DATA(name) QB_LOG_INIT_DATA(name)
135 #endif
136 
137 /* Using "switch" instead of "if" in these macro definitions keeps
138  * static analysis from complaining about constant evaluations
139  */
140 
150 # define do_crm_log(level, fmt, args...) do { \
151  switch (level) { \
152  case LOG_STDOUT: \
153  printf(fmt "\n" , ##args); \
154  break; \
155  case LOG_NEVER: \
156  break; \
157  default: \
158  qb_log_from_external_source(__func__, __FILE__, fmt, \
159  (level), __LINE__, 0 , ##args); \
160  break; \
161  } \
162  } while (0)
163 
174 # define do_crm_log_unlikely(level, fmt, args...) do { \
175  switch (level) { \
176  case LOG_STDOUT: case LOG_NEVER: \
177  break; \
178  default: { \
179  static struct qb_log_callsite *trace_cs = NULL; \
180  if (trace_cs == NULL) { \
181  trace_cs = qb_log_callsite_get(__func__, __FILE__, fmt, \
182  (level), __LINE__, 0); \
183  } \
184  if (crm_is_callsite_active(trace_cs, (level), 0)) { \
185  qb_log_from_external_source(__func__, __FILE__, fmt, \
186  (level), __LINE__, 0 , ##args); \
187  } \
188  } \
189  break; \
190  } \
191  } while (0)
192 
207 # define pcmk__log_else(level, else_action) do { \
208  static struct qb_log_callsite *trace_cs = NULL; \
209  \
210  if (trace_cs == NULL) { \
211  trace_cs = qb_log_callsite_get(__func__, __FILE__, "log_else", \
212  level, __LINE__, 0); \
213  } \
214  if (!crm_is_callsite_active(trace_cs, level, 0)) { \
215  else_action; \
216  } \
217  } while(0)
218 
219 # define CRM_LOG_ASSERT(expr) do { \
220  if(__unlikely((expr) == FALSE)) { \
221  static struct qb_log_callsite *core_cs = NULL; \
222  if(core_cs == NULL) { \
223  core_cs = qb_log_callsite_get(__func__, __FILE__, "log-assert", LOG_TRACE, __LINE__, 0); \
224  } \
225  crm_abort(__FILE__, __FUNCTION__, __LINE__, #expr, \
226  core_cs?core_cs->targets:FALSE, TRUE); \
227  } \
228  } while(0)
229 
230 /* 'failure_action' MUST NOT be 'continue' as it will apply to the
231  * macro's do-while loop
232  */
233 # define CRM_CHECK(expr, failure_action) do { \
234  if(__unlikely((expr) == FALSE)) { \
235  static struct qb_log_callsite *core_cs = NULL; \
236  if(core_cs == NULL) { \
237  core_cs = qb_log_callsite_get(__func__, __FILE__, "check-assert", LOG_TRACE, __LINE__, 0); \
238  } \
239  crm_abort(__FILE__, __FUNCTION__, __LINE__, #expr, \
240  core_cs?core_cs->targets:FALSE, TRUE); \
241  failure_action; \
242  } \
243  } while(0)
244 
255 # define do_crm_log_xml(level, text, xml) do { \
256  switch (level) { \
257  case LOG_STDOUT: case LOG_NEVER: \
258  break; \
259  default: { \
260  static struct qb_log_callsite *xml_cs = NULL; \
261  if (xml_cs == NULL) { \
262  xml_cs = qb_log_callsite_get(__func__, __FILE__, \
263  "xml-blob", (level), __LINE__, 0); \
264  } \
265  if (crm_is_callsite_active(xml_cs, (level), 0)) { \
266  log_data_element((level), __FILE__, __FUNCTION__, \
267  __LINE__, text, xml, 1, xml_log_option_formatted); \
268  } \
269  } \
270  break; \
271  } \
272  } while(0)
273 
286 # define do_crm_log_alias(level, file, function, line, fmt, args...) do { \
287  switch (level) { \
288  case LOG_STDOUT: \
289  printf(fmt "\n" , ##args); \
290  break; \
291  case LOG_NEVER: \
292  break; \
293  default: \
294  qb_log_from_external_source(function, file, fmt, (level), \
295  line, 0 , ##args); \
296  break; \
297  } \
298  } while (0)
299 
314 # define crm_perror(level, fmt, args...) do { \
315  switch (level) { \
316  case LOG_NEVER: \
317  break; \
318  default: { \
319  const char *err = strerror(errno); \
320  /* cast to int makes coverity happy when level == 0 */ \
321  if ((level) <= (int) crm_log_level) { \
322  fprintf(stderr, fmt ": %s (%d)\n" , ##args, err, errno);\
323  } \
324  do_crm_log((level), fmt ": %s (%d)" , ##args, err, errno); \
325  } \
326  break; \
327  } \
328  } while (0)
329 
341 # define crm_log_tag(level, tag, fmt, args...) do { \
342  switch (level) { \
343  case LOG_STDOUT: case LOG_NEVER: \
344  break; \
345  default: { \
346  static struct qb_log_callsite *trace_tag_cs = NULL; \
347  int converted_tag = g_quark_try_string(tag); \
348  if (trace_tag_cs == NULL) { \
349  trace_tag_cs = qb_log_callsite_get(__func__, __FILE__, \
350  fmt, (level), __LINE__, converted_tag); \
351  } \
352  if (crm_is_callsite_active(trace_tag_cs, (level), \
353  converted_tag)) { \
354  qb_log_from_external_source(__func__, __FILE__, fmt, \
355  (level), __LINE__, converted_tag , ##args); \
356  } \
357  } \
358  } \
359  } while (0)
360 
361 # define crm_emerg(fmt, args...) qb_log(LOG_EMERG, fmt , ##args)
362 # define crm_crit(fmt, args...) qb_logt(LOG_CRIT, 0, fmt , ##args)
363 # define crm_err(fmt, args...) qb_logt(LOG_ERR, 0, fmt , ##args)
364 # define crm_warn(fmt, args...) qb_logt(LOG_WARNING, 0, fmt , ##args)
365 # define crm_notice(fmt, args...) qb_logt(LOG_NOTICE, 0, fmt , ##args)
366 # define crm_info(fmt, args...) qb_logt(LOG_INFO, 0, fmt , ##args)
367 
368 # define crm_debug(fmt, args...) do_crm_log_unlikely(LOG_DEBUG, fmt , ##args)
369 # define crm_trace(fmt, args...) do_crm_log_unlikely(LOG_TRACE, fmt , ##args)
370 
371 # define crm_log_xml_crit(xml, text) do_crm_log_xml(LOG_CRIT, text, xml)
372 # define crm_log_xml_err(xml, text) do_crm_log_xml(LOG_ERR, text, xml)
373 # define crm_log_xml_warn(xml, text) do_crm_log_xml(LOG_WARNING, text, xml)
374 # define crm_log_xml_notice(xml, text) do_crm_log_xml(LOG_NOTICE, text, xml)
375 # define crm_log_xml_info(xml, text) do_crm_log_xml(LOG_INFO, text, xml)
376 # define crm_log_xml_debug(xml, text) do_crm_log_xml(LOG_DEBUG, text, xml)
377 # define crm_log_xml_trace(xml, text) do_crm_log_xml(LOG_TRACE, text, xml)
378 
379 # define crm_log_xml_explicit(xml, text) do { \
380  static struct qb_log_callsite *digest_cs = NULL; \
381  digest_cs = qb_log_callsite_get( \
382  __func__, __FILE__, text, LOG_TRACE, __LINE__, \
383  crm_trace_nonlog); \
384  if (digest_cs && digest_cs->targets) { \
385  do_crm_log_xml(LOG_TRACE, text, xml); \
386  } \
387  } while(0)
388 
389 # define crm_str(x) (const char*)(x?x:"<null>")
390 
391 #ifndef PCMK__NO_COMPAT
392 
393 /* Everything here is deprecated and kept only for public API backward
394  * compatibility. It will be moved to compatibility.h when 2.1.0 is released.
395  */
396 
408 # define do_crm_log_always(level, fmt, args...) do { \
409  switch (level) { \
410  case LOG_STDOUT: case LOG_NEVER: \
411  break; \
412  default: \
413  qb_log((level), fmt , ##args); \
414  break; \
415  } \
416  } while (0)
417 
418 #endif // PCMK__NO_COMPAT
419 
420 #ifdef __cplusplus
421 }
422 #endif
423 
424 #endif
void crm_write_blackbox(int nsig, struct qb_log_callsite *callsite)
Definition: logging.c:394
void crm_enable_stderr(int enable)
Definition: logging.c:892
void log_data_element(int log_level, const char *file, const char *function, int line, const char *prefix, xmlNode *data, int depth, gboolean formatted)
gboolean crm_log_init(const char *entity, uint8_t level, gboolean daemon, gboolean to_stderr, int argc, char **argv, gboolean quiet)
Definition: logging.c:732
unsigned int crm_trace_nonlog
Definition: logging.c:39
gboolean crm_log_cli_init(const char *entity)
Definition: logging.c:451
void crm_bump_log_level(int argc, char **argv)
Definition: logging.c:904
void crm_log_deinit(void)
Definition: logging.c:114
gboolean crm_config_error
Definition: utils.c:50
int daemon(int nochdir, int noclose)
void crm_log_output_fn(const char *file, const char *function, int line, int level, const char *prefix, const char *output)
Definition: logging.c:963
void crm_enable_blackbox(int nsig)
Definition: logging.c:372
xml_log_options
Definition: logging.h:72
void crm_log_args(int argc, char **argv)
Definition: logging.c:929
unsigned int set_crm_log_level(unsigned int level)
Definition: logging.c:878
void crm_log_preinit(const char *entity, int argc, char **argv)
Definition: logging.c:676
gboolean crm_is_callsite_active(struct qb_log_callsite *cs, uint8_t level, uint32_t tags)
Definition: logging.c:561
unsigned int get_crm_log_level(void)
Definition: logging.c:922
unsigned int crm_log_level
Definition: logging.c:37
void crm_disable_blackbox(int nsig)
Definition: logging.c:378
gboolean crm_config_warning
Definition: utils.c:51
char data[0]
Definition: internal.h:90
gboolean crm_add_logfile(const char *filename)
Definition: logging.c:166
void crm_update_callsites(void)
Definition: logging.c:590