root/include/crm/common/logging.h

/* [previous][next][first][last][top][bottom][index][help] */

INCLUDED FROM


DEFINITIONS

This source file includes following definitions.
  1. crm_extended_logging

   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][next][first][last][top][bottom][index][help] */
  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 /*!
 109  * \brief Initializes the logging system and defaults to the least verbose output level
 110  *
 111  * \param[in] entity  If not NULL, will be used as the identity for logging purposes
 112  * \param[in] argc    The number of command line parameters
 113  * \param[in] argv    The command line parameter values
 114  */
 115 void crm_log_preinit(const char *entity, int argc, char **argv);
 116 gboolean crm_log_init(const char *entity, uint8_t level, gboolean daemon,
 117                       gboolean to_stderr, int argc, char **argv, gboolean quiet);
 118 
 119 void crm_log_args(int argc, char **argv);
 120 void crm_log_output_fn(const char *file, const char *function, int line, int level,
 121                        const char *prefix, const char *output);
 122 
 123 // Log a block of text line by line
 124 #define crm_log_output(level, prefix, output)   \
 125     crm_log_output_fn(__FILE__, __func__, __LINE__, level, prefix, output)
 126 
 127 void crm_bump_log_level(int argc, char **argv);
 128 
 129 void crm_enable_stderr(int enable);
 130 
 131 gboolean crm_is_callsite_active(struct qb_log_callsite *cs, uint8_t level, uint32_t tags);
 132 
 133 void log_data_element(int log_level, const char *file, const char *function, int line,
 134                       const char *prefix, xmlNode * data, int depth, gboolean formatted);
 135 
 136 /* returns the old value */
 137 unsigned int set_crm_log_level(unsigned int level);
 138 
 139 unsigned int get_crm_log_level(void);
 140 
 141 /*
 142  * Throughout the macros below, note the leading, pre-comma, space in the
 143  * various ' , ##args' occurrences to aid portability across versions of 'gcc'.
 144  * https://gcc.gnu.org/onlinedocs/cpp/Variadic-Macros.html#Variadic-Macros
 145  */
 146 #if defined(__clang__)
 147 #    define CRM_TRACE_INIT_DATA(name)
 148 #  else
 149 #    include <assert.h> // required by QB_LOG_INIT_DATA() macro
 150 #    define CRM_TRACE_INIT_DATA(name) QB_LOG_INIT_DATA(name)
 151 #endif
 152 
 153 /* Using "switch" instead of "if" in these macro definitions keeps
 154  * static analysis from complaining about constant evaluations
 155  */
 156 
 157 /*!
 158  * \brief Log a message
 159  *
 160  * \param[in] level  Priority at which to log the message
 161  * \param[in] fmt    printf-style format string literal for message
 162  * \param[in] args   Any arguments needed by format string
 163  *
 164  * \note This is a macro, and \p level may be evaluated more than once.
 165  */
 166 #  define do_crm_log(level, fmt, args...) do {                              \
 167         switch (level) {                                                    \
 168             case LOG_STDOUT:                                                \
 169                 printf(fmt "\n" , ##args);                                  \
 170                 break;                                                      \
 171             case LOG_NEVER:                                                 \
 172                 break;                                                      \
 173             default:                                                        \
 174                 qb_log_from_external_source(__func__, __FILE__, fmt,        \
 175                     (level),   __LINE__, 0 , ##args);                       \
 176                 break;                                                      \
 177         }                                                                   \
 178     } while (0)
 179 
 180 /*!
 181  * \brief Log a message that is likely to be filtered out
 182  *
 183  * \param[in] level  Priority at which to log the message
 184  * \param[in] fmt    printf-style format string for message
 185  * \param[in] args   Any arguments needed by format string
 186  *
 187  * \note This is a macro, and \p level may be evaluated more than once.
 188  *       This does nothing when level is LOG_STDOUT.
 189  */
 190 #  define do_crm_log_unlikely(level, fmt, args...) do {                     \
 191         switch (level) {                                                    \
 192             case LOG_STDOUT: case LOG_NEVER:                                \
 193                 break;                                                      \
 194             default: {                                                      \
 195                 static struct qb_log_callsite *trace_cs = NULL;             \
 196                 if (trace_cs == NULL) {                                     \
 197                     trace_cs = qb_log_callsite_get(__func__, __FILE__, fmt, \
 198                                                    (level), __LINE__, 0);   \
 199                 }                                                           \
 200                 if (crm_is_callsite_active(trace_cs, (level), 0)) {         \
 201                     qb_log_from_external_source(__func__, __FILE__, fmt,    \
 202                         (level), __LINE__, 0 , ##args);                     \
 203                 }                                                           \
 204             }                                                               \
 205             break;                                                          \
 206         }                                                                   \
 207     } while (0)
 208 
 209 #  define CRM_LOG_ASSERT(expr) do {                                     \
 210         if (!(expr)) {                                                  \
 211             static struct qb_log_callsite *core_cs = NULL;              \
 212             if(core_cs == NULL) {                                       \
 213                 core_cs = qb_log_callsite_get(__func__, __FILE__,       \
 214                                               "log-assert", LOG_TRACE,  \
 215                                               __LINE__, 0);             \
 216             }                                                           \
 217             crm_abort(__FILE__, __func__, __LINE__, #expr,              \
 218                       core_cs?core_cs->targets:FALSE, TRUE);            \
 219         }                                                               \
 220     } while(0)
 221 
 222 /* 'failure_action' MUST NOT be 'continue' as it will apply to the
 223  * macro's do-while loop
 224  */
 225 #  define CRM_CHECK(expr, failure_action) do {                                      \
 226         if (!(expr)) {                                                  \
 227             static struct qb_log_callsite *core_cs = NULL;              \
 228             if (core_cs == NULL) {                                      \
 229                 core_cs = qb_log_callsite_get(__func__, __FILE__,       \
 230                                               "check-assert",           \
 231                                               LOG_TRACE, __LINE__, 0);  \
 232             }                                                           \
 233                 crm_abort(__FILE__, __func__, __LINE__, #expr,              \
 234                         (core_cs? core_cs->targets: FALSE), TRUE);              \
 235                 failure_action;                                                                 \
 236             }                                                                                           \
 237     } while(0)
 238 
 239 /*!
 240  * \brief Log XML line-by-line in a formatted fashion
 241  *
 242  * \param[in] level  Priority at which to log the messages
 243  * \param[in] text   Prefix for each line
 244  * \param[in] xml    XML to log
 245  *
 246  * \note This is a macro, and \p level may be evaluated more than once.
 247  *       This does nothing when level is LOG_STDOUT.
 248  */
 249 #  define do_crm_log_xml(level, text, xml) do {                             \
 250         switch (level) {                                                    \
 251             case LOG_STDOUT: case LOG_NEVER:                                \
 252                 break;                                                      \
 253             default: {                                                      \
 254                 static struct qb_log_callsite *xml_cs = NULL;               \
 255                 if (xml_cs == NULL) {                                       \
 256                     xml_cs = qb_log_callsite_get(__func__, __FILE__,        \
 257                                         "xml-blob", (level), __LINE__, 0);  \
 258                 }                                                           \
 259                 if (crm_is_callsite_active(xml_cs, (level), 0)) {           \
 260                     log_data_element((level), __FILE__, __func__,           \
 261                          __LINE__, text, xml, 1, xml_log_option_formatted); \
 262                 }                                                           \
 263             }                                                               \
 264             break;                                                          \
 265         }                                                                   \
 266     } while(0)
 267 
 268 /*!
 269  * \brief Log a message as if it came from a different code location
 270  *
 271  * \param[in] level     Priority at which to log the message
 272  * \param[in] file      Source file name to use instead of __FILE__
 273  * \param[in] function  Source function name to use instead of __func__
 274  * \param[in] line      Source line number to use instead of __line__
 275  * \param[in] fmt       printf-style format string literal for message
 276  * \param[in] args      Any arguments needed by format string
 277  *
 278  * \note This is a macro, and \p level may be evaluated more than once.
 279  */
 280 #  define do_crm_log_alias(level, file, function, line, fmt, args...) do {  \
 281         switch (level) {                                                    \
 282             case LOG_STDOUT:                                                \
 283                 printf(fmt "\n" , ##args);                                  \
 284                 break;                                                      \
 285             case LOG_NEVER:                                                 \
 286                 break;                                                      \
 287             default:                                                        \
 288                 qb_log_from_external_source(function, file, fmt, (level),   \
 289                                             line, 0 , ##args);              \
 290                 break;                                                      \
 291         }                                                                   \
 292     } while (0)
 293 
 294 /*!
 295  * \brief Send a system error message to both the log and stderr
 296  *
 297  * \param[in] level  Priority at which to log the message
 298  * \param[in] fmt    printf-style format string for message
 299  * \param[in] args   Any arguments needed by format string
 300  *
 301  * \deprecated One of the other logging functions should be used with
 302  *             pcmk_strerror() instead.
 303  * \note This is a macro, and \p level may be evaluated more than once.
 304  * \note Because crm_perror() adds the system error message and error number
 305  *       onto the end of fmt, that information will become extended information
 306  *       if CRM_XS is used inside fmt and will not show up in syslog.
 307  */
 308 #  define crm_perror(level, fmt, args...) do {                              \
 309         switch (level) {                                                    \
 310             case LOG_NEVER:                                                 \
 311                 break;                                                      \
 312             default: {                                                      \
 313                 const char *err = strerror(errno);                          \
 314                 /* cast to int makes coverity happy when level == 0 */      \
 315                 if ((level) <= (int) crm_log_level) {                       \
 316                     fprintf(stderr, fmt ": %s (%d)\n" , ##args, err, errno);\
 317                 }                                                           \
 318                 do_crm_log((level), fmt ": %s (%d)" , ##args, err, errno);  \
 319             }                                                               \
 320             break;                                                          \
 321         }                                                                   \
 322     } while (0)
 323 
 324 /*!
 325  * \brief Log a message with a tag (for use with PCMK_trace_tags)
 326  *
 327  * \param[in] level  Priority at which to log the message
 328  * \param[in] tag    String to tag message with
 329  * \param[in] fmt    printf-style format string for message
 330  * \param[in] args   Any arguments needed by format string
 331  *
 332  * \note This is a macro, and \p level may be evaluated more than once.
 333  *       This does nothing when level is LOG_STDOUT.
 334  */
 335 #  define crm_log_tag(level, tag, fmt, args...)    do {                     \
 336         switch (level) {                                                    \
 337             case LOG_STDOUT: case LOG_NEVER:                                \
 338                 break;                                                      \
 339             default: {                                                      \
 340                 static struct qb_log_callsite *trace_tag_cs = NULL;         \
 341                 int converted_tag = g_quark_try_string(tag);                \
 342                 if (trace_tag_cs == NULL) {                                 \
 343                     trace_tag_cs = qb_log_callsite_get(__func__, __FILE__,  \
 344                                     fmt, (level), __LINE__, converted_tag); \
 345                 }                                                           \
 346                 if (crm_is_callsite_active(trace_tag_cs, (level),           \
 347                                            converted_tag)) {                \
 348                     qb_log_from_external_source(__func__, __FILE__, fmt,    \
 349                                 (level), __LINE__, converted_tag , ##args); \
 350                 }                                                           \
 351             }                                                               \
 352         }                                                                   \
 353     } while (0)
 354 
 355 #  define crm_emerg(fmt, args...)   qb_log(LOG_EMERG,       fmt , ##args)
 356 #  define crm_crit(fmt, args...)    qb_logt(LOG_CRIT,    0, fmt , ##args)
 357 #  define crm_err(fmt, args...)     qb_logt(LOG_ERR,     0, fmt , ##args)
 358 #  define crm_warn(fmt, args...)    qb_logt(LOG_WARNING, 0, fmt , ##args)
 359 #  define crm_notice(fmt, args...)  qb_logt(LOG_NOTICE,  0, fmt , ##args)
 360 #  define crm_info(fmt, args...)    qb_logt(LOG_INFO,    0, fmt , ##args)
 361 
 362 #  define crm_debug(fmt, args...)   do_crm_log_unlikely(LOG_DEBUG, fmt , ##args)
 363 #  define crm_trace(fmt, args...)   do_crm_log_unlikely(LOG_TRACE, fmt , ##args)
 364 
 365 #  define crm_log_xml_crit(xml, text)    do_crm_log_xml(LOG_CRIT,    text, xml)
 366 #  define crm_log_xml_err(xml, text)     do_crm_log_xml(LOG_ERR,     text, xml)
 367 #  define crm_log_xml_warn(xml, text)    do_crm_log_xml(LOG_WARNING, text, xml)
 368 #  define crm_log_xml_notice(xml, text)  do_crm_log_xml(LOG_NOTICE,  text, xml)
 369 #  define crm_log_xml_info(xml, text)    do_crm_log_xml(LOG_INFO,    text, xml)
 370 #  define crm_log_xml_debug(xml, text)   do_crm_log_xml(LOG_DEBUG,   text, xml)
 371 #  define crm_log_xml_trace(xml, text)   do_crm_log_xml(LOG_TRACE,   text, xml)
 372 
 373 #  define crm_log_xml_explicit(xml, text)  do {                 \
 374         static struct qb_log_callsite *digest_cs = NULL;        \
 375         digest_cs = qb_log_callsite_get(                        \
 376             __func__, __FILE__, text, LOG_TRACE, __LINE__,      \
 377             crm_trace_nonlog);                                  \
 378         if (digest_cs && digest_cs->targets) {                  \
 379             do_crm_log_xml(LOG_TRACE,   text, xml);             \
 380         }                                                       \
 381     } while(0)
 382 
 383 #  define crm_str(x)    (const char*)(x?x:"<null>")
 384 
 385 #if !defined(PCMK_ALLOW_DEPRECATED) || (PCMK_ALLOW_DEPRECATED == 1)
 386 #include <crm/common/logging_compat.h>
 387 #endif
 388 
 389 #ifdef __cplusplus
 390 }
 391 #endif
 392 
 393 #endif

/* [previous][next][first][last][top][bottom][index][help] */