From 27917d12ec58357749bae5fbd08ca05e087a6a4c Mon Sep 17 00:00:00 2001 From: nick black Date: Sun, 17 Oct 2021 17:12:52 -0400 Subject: [PATCH] add set_loglevel_from_env() #2277 --- src/lib/direct.c | 1 + src/lib/internal.h | 4 ++++ src/lib/notcurses.c | 1 + src/lib/util.c | 16 ++++++++++++++++ 4 files changed, 22 insertions(+) diff --git a/src/lib/direct.c b/src/lib/direct.c index f74cb1184..74818ad56 100644 --- a/src/lib/direct.c +++ b/src/lib/direct.c @@ -909,6 +909,7 @@ ncdirect* ncdirect_core_init(const char* termtype, FILE* outfp, uint64_t flags){ }else{ loglevel = NCLOGLEVEL_SILENT; } + set_loglevel_from_env(&loglevel); int cursor_y = -1; int cursor_x = -1; if(interrogate_terminfo(&ret->tcache, ret->ttyfp, utf8, 1, diff --git a/src/lib/internal.h b/src/lib/internal.h index 3b88b463c..2b0bc612b 100644 --- a/src/lib/internal.h +++ b/src/lib/internal.h @@ -1746,6 +1746,10 @@ emit_scrolls(const tinfo* ti, int count, fbuf* f){ // replace or populate the TERM environment variable with 'termname' int putenv_term(const char* termname) __attribute__ ((nonnull (1))); +// check environment for NOTCURSES_LOGLEVEL, and use it if defined. +int set_loglevel_from_env(ncloglevel_e* loglevel) + __attribute__ ((nonnull (1))); + #undef API #undef ALLOC diff --git a/src/lib/notcurses.c b/src/lib/notcurses.c index 8cbb92992..b0ad23cd7 100644 --- a/src/lib/notcurses.c +++ b/src/lib/notcurses.c @@ -1002,6 +1002,7 @@ notcurses* notcurses_core_init(const notcurses_options* opts, FILE* outfp){ ret->rstate.f.used = 0; ret->rstate.f.size = 0; ret->loglevel = opts->loglevel; + set_loglevel_from_env(&ret->loglevel); if(!(opts->flags & NCOPTION_INHIBIT_SETLOCALE)){ init_lang(); } diff --git a/src/lib/util.c b/src/lib/util.c index c5e5bd18a..704ae8f8d 100644 --- a/src/lib/util.c +++ b/src/lib/util.c @@ -8,6 +8,22 @@ #endif #include "internal.h" +int set_loglevel_from_env(ncloglevel_e* llptr){ + const char* ll = getenv("NOTCURSES_LOGLEVEL"); + if(ll == NULL){ + return 0; + } + char* endl; + long l = strtol(ll, &endl, 10); + if(l < NCLOGLEVEL_PANIC || l > NCLOGLEVEL_TRACE){ + logpanic("Illegal NOTCURSES_LOGLEVEL: %s\n", ll); + return -1; + } + *llptr = l; + loginfo("Got loglevel from environment: %ld\n", l); + return 0; +} + char* notcurses_accountname(void){ #ifndef __MINGW64__ const char* un;