From 2eee382a62a909d5a3f2f5eda94f30fe68fd5335 Mon Sep 17 00:00:00 2001 From: "kr.angelov" Date: Fri, 20 Jan 2012 13:41:10 +0000 Subject: initial import of the C runtime --- src/runtime/c/gu/log.c | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 src/runtime/c/gu/log.c (limited to 'src/runtime/c/gu/log.c') diff --git a/src/runtime/c/gu/log.c b/src/runtime/c/gu/log.c new file mode 100644 index 000000000..399646c50 --- /dev/null +++ b/src/runtime/c/gu/log.c @@ -0,0 +1,79 @@ +#include +#include + +#include +#include +#include +#include + +static int gu_log_depth = 0; + +static bool +gu_log_match(const char* pat, size_t patlen, const char* str) +{ + if (patlen > 0 && pat[patlen-1] == '*') { + return strncmp(pat, str, patlen-1) == 0; + } else if (strlen(str) == patlen) { + return strncmp(pat, str, patlen) == 0; + } + return false; +} + +static bool +gu_log_enabled(const char* func, const char* file) +{ + const char* cfg = getenv("GU_LOG"); + if (cfg == NULL) { + return false; + } + const char* p = cfg; + while (true) { + size_t len = strcspn(p, ","); + if (gu_log_match(p, len, func)) { + return true; + } + if (gu_log_match(p, len, file)) { + return true; + } + if (p[len] == '\0') { + break; + } + p = &p[len + 1]; + } + return false; +} + + +void +gu_log_full_v(GuLogKind kind, const char* func, const char* file, int line, + const char* fmt, va_list args) +{ + (void) (kind && line); + if (!gu_log_enabled(func, file)) { + return; + } + if (kind == GU_LOG_KIND_EXIT) { + gu_log_depth--; + } + if (fmt) { + int indent = gu_min(32 + gu_log_depth, 48); + fprintf(stderr, "%-*s: ", indent, func); + vfprintf(stderr, fmt, args); + fputc('\n', stderr); + fflush(stderr); + } + if (kind == GU_LOG_KIND_ENTER) { + gu_log_depth++; + } +} + +void +gu_log_full(GuLogKind kind, const char* func, const char* file, int line, + const char* fmt, ...) +{ + va_list args; + va_start(args, fmt); + gu_log_full_v(kind, func, file, line, fmt, args); + va_end(args); +} + -- cgit v1.2.3