pacemaker  2.1.0-7c3f660
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-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 
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 unsigned int crm_trace_nonlog;
69 
75 extern gboolean crm_config_error;
76 
82 extern gboolean crm_config_warning;
83 
85 {
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
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 
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 
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 
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 
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 
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 
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)
380 #endif
381 
382 #ifdef __cplusplus
383 }
384 #endif
385 
386 #endif
void crm_write_blackbox(int nsig, struct qb_log_callsite *callsite)
Definition: logging.c:465
void crm_enable_stderr(int enable)
Definition: logging.c:955
char data[0]
Definition: cpg.c:55
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:796
unsigned int crm_trace_nonlog
Definition: logging.c:46
Deprecated Pacemaker logging API.
void crm_bump_log_level(int argc, char **argv)
Make logging more verbose.
Definition: logging.c:976
void crm_log_deinit(void)
Definition: logging.c:125
gboolean crm_config_error
Definition: utils.c:52
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:1017
void crm_enable_blackbox(int nsig)
Definition: logging.c:443
xml_log_options
Definition: logging.h:84
void crm_log_args(int argc, char **argv)
Log the command line (once)
Definition: logging.c:1002
unsigned int set_crm_log_level(unsigned int level)
Definition: logging.c:941
void crm_log_preinit(const char *entity, int argc, char **argv)
Definition: logging.c:740
gboolean crm_is_callsite_active(struct qb_log_callsite *cs, uint8_t level, uint32_t tags)
Definition: logging.c:626
unsigned int get_crm_log_level(void)
Definition: logging.c:987
unsigned int crm_log_level
Definition: logging.c:45
void crm_disable_blackbox(int nsig)
Definition: logging.c:449
gboolean crm_config_warning
Definition: utils.c:53
void crm_update_callsites(void)
Definition: logging.c:655