X-Git-Url: http://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=ccan%2Fpr_log%2Fpr_log.c;fp=ccan%2Fpr_log%2Fpr_log.c;h=1de8cb9e5c25cb833d01486bf048e85b659c68c3;hp=0000000000000000000000000000000000000000;hb=1f7028e9d7b87a95613c9db6a64a2111d4899395;hpb=6aaca17e07588997417a73fac19dcf0ff17ed81b diff --git a/ccan/pr_log/pr_log.c b/ccan/pr_log/pr_log.c new file mode 100644 index 00000000..1de8cb9e --- /dev/null +++ b/ccan/pr_log/pr_log.c @@ -0,0 +1,48 @@ +/* Licensed under LGPLv2.1+ - see LICENSE file for details */ +#include "pr_log.h" + +#include +#include +#include +#include +#include +#include + +#include + +#define DEBUG_NEED_INIT INT_MIN +static int debug = DEBUG_NEED_INIT; + +bool debug_is(int lvl) +{ + return lvl <= debug_level(); +} + +int debug_level(void) +{ + if (debug != DEBUG_NEED_INIT) + return debug; + char *c = getenv("DEBUG"); + if (!c) { + debug = CCAN_PR_LOG_DEFAULT_LEVEL; + return debug; + } + + debug = atoi(c); + return debug; +} + +void pr_log_(char const *fmt, ...) +{ + int level = INT_MIN; + if (fmt[0] == '<' && cisdigit(fmt[1]) && fmt[2] == '>') + level = fmt[1] - '0'; + + if (!debug_is(level)) + return; + + va_list va; + va_start(va, fmt); + vfprintf(stderr, fmt, va); + va_end(va); +}