pacemaker  2.0.3-4b1f869f0f
Scalable High-Availability cluster resource manager
logging.h
Go to the documentation of this file.
1 /*
2  * Copyright 2004-2019 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 <qb/qblog.h>
24 
25 # ifndef LOG_TRACE
26 # define LOG_TRACE LOG_DEBUG+1
27 # endif
28 
29 /* "Extended information" logging support */
30 #ifdef QB_XS
31 # define CRM_XS QB_XS
32 # define crm_extended_logging(t, e) qb_log_ctl((t), QB_LOG_CONF_EXTENDED, (e))
33 #else
34 # define CRM_XS "|"
35 
36 /* A caller might want to check the return value, so we can't define this as a
37  * no-op, and we can't simply define it to be 0 because gcc will then complain
38  * when the value isn't checked.
39  */
40 static inline int
41 crm_extended_logging(int t, int e)
42 {
43  return 0;
44 }
45 #endif
46 
47 extern unsigned int crm_log_level;
48 extern gboolean crm_config_error;
49 extern gboolean crm_config_warning;
50 extern unsigned int crm_trace_nonlog;
51 
53 {
56  xml_log_option_text = 0x0004, /* add this option to dump text into xml */
57  xml_log_option_full_fledged = 0x0008, /* crm_xml_dump: serialize using libxml */
66 };
67 
68 void crm_enable_blackbox(int nsig);
69 void crm_disable_blackbox(int nsig);
70 void crm_write_blackbox(int nsig, struct qb_log_callsite *callsite);
71 
72 void crm_update_callsites(void);
73 
74 void crm_log_deinit(void);
75 
76 gboolean crm_log_cli_init(const char *entity);
77 
78 void crm_log_preinit(const char *entity, int argc, char **argv);
79 gboolean crm_log_init(const char *entity, uint8_t level, gboolean daemon,
80  gboolean to_stderr, int argc, char **argv, gboolean quiet);
81 
82 void crm_log_args(int argc, char **argv);
83 void crm_log_output_fn(const char *file, const char *function, int line, int level,
84  const char *prefix, const char *output);
85 # define crm_log_output(level, prefix, output) crm_log_output_fn(__FILE__, __FUNCTION__, __LINE__, level, prefix, output)
86 
87 gboolean crm_add_logfile(const char *filename);
88 
89 void crm_bump_log_level(int argc, char **argv);
90 
91 void crm_enable_stderr(int enable);
92 
93 gboolean crm_is_callsite_active(struct qb_log_callsite *cs, uint8_t level, uint32_t tags);
94 
95 void log_data_element(int log_level, const char *file, const char *function, int line,
96  const char *prefix, xmlNode * data, int depth, gboolean formatted);
97 
98 /* returns the old value */
99 unsigned int set_crm_log_level(unsigned int level);
100 
101 unsigned int get_crm_log_level(void);
102 
103 /*
104  * Throughout the macros below, note the leading, pre-comma, space in the
105  * various ' , ##args' occurrences to aid portability across versions of 'gcc'.
106  * https://gcc.gnu.org/onlinedocs/cpp/Variadic-Macros.html#Variadic-Macros
107  */
108 #if defined(__clang__)
109 # define CRM_TRACE_INIT_DATA(name)
110 # else
111 # include <assert.h> // required by QB_LOG_INIT_DATA() macro
112 # define CRM_TRACE_INIT_DATA(name) QB_LOG_INIT_DATA(name)
113 #endif
114 
122 # define do_crm_log(level, fmt, args...) \
123  qb_log_from_external_source(__func__, __FILE__, fmt, level, __LINE__, 0 , ##args)
124 
132 # define do_crm_log_unlikely(level, fmt, args...) do { \
133  static struct qb_log_callsite *trace_cs = NULL; \
134  if(trace_cs == NULL) { \
135  trace_cs = qb_log_callsite_get(__func__, __FILE__, fmt, level, __LINE__, 0); \
136  } \
137  if (crm_is_callsite_active(trace_cs, level, 0)) { \
138  qb_log_from_external_source( \
139  __func__, __FILE__, fmt, level, __LINE__, 0 , ##args); \
140  } \
141  } while(0)
142 
143 # define CRM_LOG_ASSERT(expr) do { \
144  if(__unlikely((expr) == FALSE)) { \
145  static struct qb_log_callsite *core_cs = NULL; \
146  if(core_cs == NULL) { \
147  core_cs = qb_log_callsite_get(__func__, __FILE__, "log-assert", LOG_TRACE, __LINE__, 0); \
148  } \
149  crm_abort(__FILE__, __FUNCTION__, __LINE__, #expr, \
150  core_cs?core_cs->targets:FALSE, TRUE); \
151  } \
152  } while(0)
153 
154 /* 'failure_action' MUST NOT be 'continue' as it will apply to the
155  * macro's do-while loop
156  */
157 # define CRM_CHECK(expr, failure_action) do { \
158  if(__unlikely((expr) == FALSE)) { \
159  static struct qb_log_callsite *core_cs = NULL; \
160  if(core_cs == NULL) { \
161  core_cs = qb_log_callsite_get(__func__, __FILE__, "check-assert", LOG_TRACE, __LINE__, 0); \
162  } \
163  crm_abort(__FILE__, __FUNCTION__, __LINE__, #expr, \
164  core_cs?core_cs->targets:FALSE, TRUE); \
165  failure_action; \
166  } \
167  } while(0)
168 
169 # define do_crm_log_xml(level, text, xml) do { \
170  static struct qb_log_callsite *xml_cs = NULL; \
171  if(xml_cs == NULL) { \
172  xml_cs = qb_log_callsite_get(__func__, __FILE__, "xml-blob", level, __LINE__, 0); \
173  } \
174  if (crm_is_callsite_active(xml_cs, level, 0)) { \
175  log_data_element(level, __FILE__, __FUNCTION__, __LINE__, text, xml, 1, xml_log_option_formatted); \
176  } \
177  } while(0)
178 
189 # define do_crm_log_alias(level, file, function, line, fmt, args...) do { \
190  if(level > 0) { \
191  qb_log_from_external_source(function, file, fmt, level, line, 0 , ##args); \
192  } else { \
193  printf(fmt "\n" , ##args); \
194  } \
195  } while(0)
196 
206 # define do_crm_log_always(level, fmt, args...) qb_log(level, fmt , ##args)
207 
219 # define crm_perror(level, fmt, args...) do { \
220  const char *err = strerror(errno); \
221  /* cast to int makes coverity happy when level == 0 */ \
222  if (level <= (int)crm_log_level) { \
223  fprintf(stderr, fmt ": %s (%d)\n" , ##args, err, errno); \
224  } \
225  do_crm_log(level, fmt ": %s (%d)" , ##args, err, errno); \
226  } while(0)
227 
228 # define crm_log_tag(level, tag, fmt, args...) do { \
229  static struct qb_log_callsite *trace_tag_cs = NULL; \
230  int converted_tag = g_quark_try_string(tag); \
231  if(trace_tag_cs == NULL) { \
232  trace_tag_cs = qb_log_callsite_get(__func__, __FILE__, fmt, level, __LINE__, converted_tag); \
233  } \
234  if (crm_is_callsite_active(trace_tag_cs, level, converted_tag)) { \
235  qb_log_from_external_source(__func__, __FILE__, fmt, level, \
236  __LINE__, converted_tag , ##args); \
237  } \
238  } while(0)
239 
240 # define crm_crit(fmt, args...) qb_logt(LOG_CRIT, 0, fmt , ##args)
241 # define crm_err(fmt, args...) qb_logt(LOG_ERR, 0, fmt , ##args)
242 # define crm_warn(fmt, args...) qb_logt(LOG_WARNING, 0, fmt , ##args)
243 # define crm_notice(fmt, args...) qb_logt(LOG_NOTICE, 0, fmt , ##args)
244 # define crm_info(fmt, args...) qb_logt(LOG_INFO, 0, fmt , ##args)
245 
246 # define crm_debug(fmt, args...) do_crm_log_unlikely(LOG_DEBUG, fmt , ##args)
247 # define crm_trace(fmt, args...) do_crm_log_unlikely(LOG_TRACE, fmt , ##args)
248 
249 # define crm_log_xml_crit(xml, text) do_crm_log_xml(LOG_CRIT, text, xml)
250 # define crm_log_xml_err(xml, text) do_crm_log_xml(LOG_ERR, text, xml)
251 # define crm_log_xml_warn(xml, text) do_crm_log_xml(LOG_WARNING, text, xml)
252 # define crm_log_xml_notice(xml, text) do_crm_log_xml(LOG_NOTICE, text, xml)
253 # define crm_log_xml_info(xml, text) do_crm_log_xml(LOG_INFO, text, xml)
254 # define crm_log_xml_debug(xml, text) do_crm_log_xml(LOG_DEBUG, text, xml)
255 # define crm_log_xml_trace(xml, text) do_crm_log_xml(LOG_TRACE, text, xml)
256 
257 # define crm_log_xml_explicit(xml, text) do { \
258  static struct qb_log_callsite *digest_cs = NULL; \
259  digest_cs = qb_log_callsite_get( \
260  __func__, __FILE__, text, LOG_TRACE, __LINE__, \
261  crm_trace_nonlog); \
262  if (digest_cs && digest_cs->targets) { \
263  do_crm_log_xml(LOG_TRACE, text, xml); \
264  } \
265  } while(0)
266 
267 # define crm_str(x) (const char*)(x?x:"<null>")
268 
269 #ifdef __cplusplus
270 }
271 #endif
272 
273 #endif
xml_log_option_open
@ xml_log_option_open
Definition: logging.h:63
crm_add_logfile
gboolean crm_add_logfile(const char *filename)
Definition: logging.c:229
data
char data[0]
Definition: internal.h:12
crm_write_blackbox
void crm_write_blackbox(int nsig, struct qb_log_callsite *callsite)
Definition: logging.c:457
xml_log_option_formatted
@ xml_log_option_formatted
Definition: logging.h:55
crm_is_callsite_active
gboolean crm_is_callsite_active(struct qb_log_callsite *cs, uint8_t level, uint32_t tags)
Definition: logging.c:624
xml_log_option_diff_plus
@ xml_log_option_diff_plus
Definition: logging.h:58
crm_config_error
gboolean crm_config_error
Definition: utils.c:59
crm_update_callsites
void crm_update_callsites(void)
Definition: logging.c:653
crm_bump_log_level
void crm_bump_log_level(int argc, char **argv)
Definition: logging.c:961
set_crm_log_level
unsigned int set_crm_log_level(unsigned int level)
Definition: logging.c:938
crm_trace_nonlog
unsigned int crm_trace_nonlog
Definition: logging.c:39
crm_log_preinit
void crm_log_preinit(const char *entity, int argc, char **argv)
Definition: logging.c:739
crm_log_output_fn
void crm_log_output_fn(const char *file, const char *function, int line, int level, const char *prefix, const char *output)
Definition: logging.c:1018
xml_log_option_text
@ xml_log_option_text
Definition: logging.h:56
crm_log_init
gboolean crm_log_init(const char *entity, uint8_t level, gboolean daemon, gboolean to_stderr, int argc, char **argv, gboolean quiet)
Definition: logging.c:795
crm_log_level
unsigned int crm_log_level
Definition: logging.c:37
get_crm_log_level
unsigned int get_crm_log_level(void)
Definition: logging.c:979
crm_enable_stderr
void crm_enable_stderr(int enable)
Definition: logging.c:949
crm_log_deinit
void crm_log_deinit(void)
Definition: logging.c:177
log_data_element
void log_data_element(int log_level, const char *file, const char *function, int line, const char *prefix, xmlNode *data, int depth, gboolean formatted)
crm_log_args
void crm_log_args(int argc, char **argv)
Definition: logging.c:986
crm_log_cli_init
gboolean crm_log_cli_init(const char *entity)
Definition: logging.c:514
xml_log_options
xml_log_options
Definition: logging.h:52
xml_log_option_full_fledged
@ xml_log_option_full_fledged
Definition: logging.h:57
xml_log_option_close
@ xml_log_option_close
Definition: logging.h:65
daemon
int daemon(int nochdir, int noclose)
xml_log_option_filtered
@ xml_log_option_filtered
Definition: logging.h:54
xml_log_option_dirty_add
@ xml_log_option_dirty_add
Definition: logging.h:62
crm_enable_blackbox
void crm_enable_blackbox(int nsig)
Definition: logging.c:435
crm_config_warning
gboolean crm_config_warning
Definition: utils.c:60
xml_log_option_children
@ xml_log_option_children
Definition: logging.h:64
crm_disable_blackbox
void crm_disable_blackbox(int nsig)
Definition: logging.c:441
xml_log_option_diff_short
@ xml_log_option_diff_short
Definition: logging.h:60
xml_log_option_diff_minus
@ xml_log_option_diff_minus
Definition: logging.h:59
xml_log_option_diff_all
@ xml_log_option_diff_all
Definition: logging.h:61