diff --git a/README.md b/README.md index a10741a..bf56ce7 100644 --- a/README.md +++ b/README.md @@ -238,7 +238,7 @@ Use `trace` only while debugging a concrete problem. It may include submitted bi The `nwwebui` service has its own numeric log level: ```perl -$nw_log_level = 1; +$nw_log_level = 'info'; ``` Typical meanings: @@ -327,7 +327,7 @@ $smart_cups_print_command_template = '/usr/bin/lp -d %p -'; # nwwebui listener settings $nw_bind_ip = '0.0.0.0'; -$nw_log_level = 1; +$nw_log_level = 'info'; $nw_daemonize = 0; $nw_pid_file = '/run/mars_nwe/nwwebui.pid'; $nw_log_file = '/var/log/mars_nwe/nwwebui.log'; diff --git a/config.h.cmake b/config.h.cmake index 2701a2c..7acbe57 100644 --- a/config.h.cmake +++ b/config.h.cmake @@ -13,8 +13,10 @@ #define LOG_PATH_DEFAULT "@MARS_NWE_LOG_DIR@/nwwebui.log" #define LOG_LEVEL_ERROR 0 -#define LOG_LEVEL_INFO 1 -#define LOG_LEVEL_DEBUG 2 +#define LOG_LEVEL_WARNING 1 +#define LOG_LEVEL_INFO 2 +#define LOG_LEVEL_DEBUG 3 +#define LOG_LEVEL_TRACE 4 #define LOG_LEVEL_DEFAULT LOG_LEVEL_INFO #define DEFAULT_BIND_IP "0.0.0.0" diff --git a/nwwebui.c b/nwwebui.c index dffbfe0..037bef6 100644 --- a/nwwebui.c +++ b/nwwebui.c @@ -91,12 +91,86 @@ static void log_reopen(const char *path) log_open(); } + +static int parse_log_level(const char *value) +{ + char buf[64]; + size_t i; + + if (!value || !*value) { + return LOG_LEVEL_DEFAULT; + } + + while (*value && isspace((unsigned char)*value)) { + value++; + } + + snprintf(buf, sizeof(buf), "%s", value); + + for (i = 0; buf[i]; i++) { + buf[i] = (char)tolower((unsigned char)buf[i]); + } + + while (i > 0 && isspace((unsigned char)buf[i - 1])) { + buf[i - 1] = '\0'; + i--; + } + + if (strcmp(buf, "error") == 0 || strcmp(buf, "err") == 0 || strcmp(buf, "0") == 0) { + return LOG_LEVEL_ERROR; + } + if (strcmp(buf, "warning") == 0 || strcmp(buf, "warn") == 0 || strcmp(buf, "1") == 0) { + return LOG_LEVEL_WARNING; + } + if (strcmp(buf, "info") == 0 || strcmp(buf, "2") == 0) { + return LOG_LEVEL_INFO; + } + if (strcmp(buf, "debug") == 0 || strcmp(buf, "3") == 0) { + return LOG_LEVEL_DEBUG; + } + if (strcmp(buf, "trace") == 0 || strcmp(buf, "4") == 0) { + return LOG_LEVEL_TRACE; + } + + /* + Compatibility with the old numeric values: + 0 = error + 1 = info + 2 = debug + + Named levels are preferred for new configurations. + */ + if (strcmp(buf, "old-info") == 0) { + return LOG_LEVEL_INFO; + } + + return LOG_LEVEL_DEFAULT; +} + +static const char *log_level_name(int level) +{ + if (level <= LOG_LEVEL_ERROR) { + return "ERROR"; + } + if (level == LOG_LEVEL_WARNING) { + return "WARNING"; + } + if (level == LOG_LEVEL_DEBUG) { + return "DEBUG"; + } + if (level >= LOG_LEVEL_TRACE) { + return "TRACE"; + } + + return "INFO"; +} + static void log_msg(int level, const char *fmt, ...) { time_t now; struct tm tm_now; char tbuf[64]; - const char *lvl = "INFO"; + const char *lvl; va_list ap; if (level > g_log_level) { @@ -109,11 +183,7 @@ static void log_msg(int level, const char *fmt, ...) localtime_r(&now, &tm_now); strftime(tbuf, sizeof(tbuf), "%Y-%m-%d %H:%M:%S", &tm_now); - if (level == LOG_LEVEL_ERROR) { - lvl = "ERROR"; - } else if (level == LOG_LEVEL_DEBUG) { - lvl = "DEBUG"; - } + lvl = log_level_name(level); fprintf(g_log_fp, "[%s] [%s] ", tbuf, lvl); @@ -471,7 +541,7 @@ static void load_smart_conf(nw_config_t *cfg) if (strcmp(key, "nw_bind_ip") == 0) { snprintf(cfg->bind_ip, sizeof(cfg->bind_ip), "%s", val); } else if (strcmp(key, "nw_log_level") == 0) { - g_log_level = atoi(val); + g_log_level = parse_log_level(val); } else if (strcmp(key, "nw_log_file") == 0) { snprintf(cfg->log_file, sizeof(cfg->log_file), "%s", val); } else if (strcmp(key, "nw_ssl_enable") == 0) { diff --git a/smart.conf.cmake b/smart.conf.cmake index 30a9c0b..004cdd3 100644 --- a/smart.conf.cmake +++ b/smart.conf.cmake @@ -125,11 +125,19 @@ $smart_cups_print_command_template = '@CUPS_LP_EXECUTABLE@ -d %p -'; # Use 127.0.0.1 for local-only testing. $nw_bind_ip = '0.0.0.0'; -# Log level used by nwwebui. -# 0 = errors only -# 1 = informational messages -# 2 = debug messages -$nw_log_level = 1; +# Log level used by the native nwwebui frontend service. +# +# Supported values, from quiet to verbose: +# +# error - only real errors +# warning - errors and warnings +# info - normal operational messages, default +# debug - additional diagnostic information +# trace - very verbose request/connection tracing +# +# Older numeric values are still accepted for compatibility, but named values +# are preferred for new configurations. +$nw_log_level = 'info'; # Run nwwebui in daemon mode by default. # 0 = stay in foreground