X-Git-Url: https://git.ozlabs.org/?a=blobdiff_plain;ds=sidebyside;f=ccan%2Fpr_log%2Fpr_log.h;fp=ccan%2Fpr_log%2Fpr_log.h;h=8e125b82084b31cc1b0ed6e49a078d618aaa369d;hb=1f7028e9d7b87a95613c9db6a64a2111d4899395;hp=0000000000000000000000000000000000000000;hpb=6aaca17e07588997417a73fac19dcf0ff17ed81b;p=ccan diff --git a/ccan/pr_log/pr_log.h b/ccan/pr_log/pr_log.h new file mode 100644 index 00000000..8e125b82 --- /dev/null +++ b/ccan/pr_log/pr_log.h @@ -0,0 +1,72 @@ +/* Licensed under LGPLv2.1+ - see LICENSE file for details */ +#ifndef CCAN_PR_LOG_H_ +#define CCAN_PR_LOG_H_ + +#include +#include + +/* + * pr_emerg, pr_alert, pr_crit, pr_error, pr_warn, pr_notice, pr_info, pr_debug + * + * Each of these prints a format string only in the case where we're running at + * the selected debug level. + * + * Note that using these functions also causes a pre-pended log level to be + * included in the printed string. + * + * Log levels correspond to those used in syslog(3) . + */ +#define pr_emerg(...) pr_log_(LOG_EMERG __VA_ARGS__) +#define pr_alert(...) pr_log_(LOG_ALERT __VA_ARGS__) +#define pr_crit(...) pr_log_(LOG_CRIT __VA_ARGS__) +#define pr_error(...) pr_log_(LOG_ERROR __VA_ARGS__) +#define pr_warn(...) pr_log_(LOG_WARN __VA_ARGS__) +#define pr_notice(...) pr_log_(LOG_NOTICE __VA_ARGS__) +#define pr_info(...) pr_log_(LOG_INFO __VA_ARGS__) +#define pr_debug(...) pr_log_(LOG_DEBUG __VA_ARGS__) + +#ifdef DEBUG +# define pr_devel(...) pr_debug(__VA_ARGS__) +#else +static PRINTF_FMT(1,2) inline void pr_check_printf_args(const char *fmt, ...) +{ + (void)fmt; +} +# define pr_devel(...) pr_check_printf_args(__VA_ARGS__) +#endif + +#ifndef CCAN_PR_LOG_DEFAULT_LEVEL +# define CCAN_PR_LOG_DEFAULT_LEVEL 6 +#endif + +#define LOG_EMERG "<0>" +#define LOG_ALERT "<1>" +#define LOG_CRIT "<2>" +#define LOG_ERROR "<3>" +#define LOG_WARN "<4>" +#define LOG_NOTICE "<5>" +#define LOG_INFO "<6>" +#define LOG_DEBUG "<7>" + +#ifndef CCAN_PR_LOG_DISABLE +/** + * pr_log_ - print output based on the given logging level + * + * Example: + * + * pr_log_(LOG_EMERG "something went terribly wrong\n"); + * pr_log_(LOG_DEBUG "everything is fine\n"); + */ +void PRINTF_FMT(1,2) pr_log_(char const *fmt, ...); +bool debug_is(int lvl); +int debug_level(void); +#else +static PRINTF_FMT(1,2) inline void pr_log_(char const *fmt, ...) +{ + (void)fmt; +} +static inline bool debug_is(int lvl) { (void)lvl; return false; } +static inline int debug_level(void) { return -1; } +#endif + +#endif