From 40fdee19318cc9f317dc89b1ff7c8e65778cdabb Mon Sep 17 00:00:00 2001 From: Kubat <mael.martin31@gmail.com> Date: Thu, 14 May 2020 13:08:24 +0200 Subject: [PATCH] Can specify loglevel in config file --- inc/common/macro.h | 2 +- src/common.c | 2 +- src/config.c | 51 +++++++++++++++++++++++++++++++++++++++------- src/main/lkt.c | 1 + src/main/server.c | 8 ++++---- 5 files changed, 51 insertions(+), 13 deletions(-) diff --git a/inc/common/macro.h b/inc/common/macro.h index fe3ad890..99e756c1 100644 --- a/inc/common/macro.h +++ b/inc/common/macro.h @@ -55,7 +55,7 @@ enum log_level { #define LOG(format, level, section, ...) \ { \ - if (level >= log_level) \ + if (level <= log_level) \ fprintf(stderr, " %c %s%s: " format "\n", \ level == ERROR ? '!' : level == WARN ? '*' : level == INFO ? '.' : ' ', \ sizeof(section) > sizeof("") ? ("[" section "] ") : "", \ diff --git a/src/common.c b/src/common.c index 06d37add..cc7dd77b 100644 --- a/src/common.c +++ b/src/common.c @@ -8,7 +8,7 @@ #include <stdarg.h> #include <sys/stat.h> -int log_level = INFO; +int log_level = 0; /* None by default */ void __not_implemented(const char *func, char *file, int line) diff --git a/src/config.c b/src/config.c index f2ccb89a..647bb9e0 100644 --- a/src/config.c +++ b/src/config.c @@ -6,6 +6,7 @@ #include <lektor/net.h> #include <lektor/reg.h> +#include <strings.h> #include <stdlib.h> #include <errno.h> #include <string.h> @@ -46,6 +47,34 @@ handler(volatile sqlite3 *user, const char *section, const char *name, return 0; } +static inline void +__set_log_level(const char *name, const char *level) +{ + if (!STR_MATCH(name, "log")) { + LOG_WARN("CONFIG", "Invalid option '%s[:=]%s' with no section", + name, level); + return; + } + + if (!level[0]) { + LOG_WARN("CONFIG", "%s", "Invalid empty 'log' option"); + return; + } + + if (STR_MATCH(level, "error")) + log_level = ERROR; + else if (STR_MATCH(level, "warn") || STR_MATCH(level, "warning")) + log_level = WARN; + else if (STR_MATCH(level, "info")) + log_level = INFO; + else if (STR_MATCH(level, "debug")) + log_level = DEBUG; + else + log_level = strtol(level, NULL, 0); + + LOG_INFO("CONFIG", "Log level set to %d", log_level); +} + static inline int ini_parse(const char *path, volatile sqlite3 *db) { @@ -58,6 +87,9 @@ ini_parse(const char *path, volatile sqlite3 *db) return 1; } + memset(section, 0, INI_MAX_SECTION_LEN); + memset(line, 0, INI_MAX_LINE_LEN); + /* Parse the file */ while (NULL != fgets(line, INI_MAX_LINE_LEN, file)) { ++linenum; @@ -102,13 +134,18 @@ ini_parse(const char *path, volatile sqlite3 *db) value = skip(value); strip(value); - /* Handle the SECTION, NAME[:=]VALUE */ - if (handler(db, section, name, value)) { - error = 1; - LOG_ERROR("PARSER", "Failed to '[handle] %s, " - "%s{:,=}%s' at line '%d'", - section, name, value, linenum); - } + /* Handle the SECTION, NAME[:=]VALUE + The only option that has no SECTION is the log level: + log[:=]ERROR|WARN|INFO|DEBUG|\d+ */ + if (section[0]) { + if (handler(db, section, name, value)) { + error = 1; + LOG_ERROR("PARSER", "Failed to '[handle] %s, " + "%s{:,=}%s' at line '%d'", + section, name, value, linenum); + } + } else + __set_log_level(name, value); } else { diff --git a/src/main/lkt.c b/src/main/lkt.c index 58471c17..cc044f05 100644 --- a/src/main/lkt.c +++ b/src/main/lkt.c @@ -941,6 +941,7 @@ parse_args(args_t *args, int argc, const char **argv) int main(int argc, const char **argv) { + log_level = ERROR; executable_name = "lkt"; assert(NULL != setlocale(LC_ALL, "en_US.UTF-8")); /* BECAUSE! */ assert(!signal(SIGPIPE, sigpipe__)); diff --git a/src/main/server.c b/src/main/server.c index 726c83ab..5f760ed5 100644 --- a/src/main/server.c +++ b/src/main/server.c @@ -66,10 +66,6 @@ main(int argc, char *argv[]) } normal_launch: - LOG_INFO("GENERAL", "Lektor launched by user %s (shell: %s, home: %s)", - pw->pw_name, pw->pw_shell, pw->pw_dir); - if (env_get(LKT_ENV_RESTART)) - LOG_INFO("GENERAL", "%s", "Lektord has been restarted"); reg_set(server_reg); mthread_init(); pthread_create(&th, NULL, mthread_main, NULL); @@ -136,6 +132,10 @@ normal_launch: lkt_queue_send(&srv.queue, lkt_event_play_pos, (void *) (size_t) strtol(env_current, NULL, 0)); } + LOG_INFO("GENERAL", "Lektor was %s, user: %s, shell: %s, home: %s", + env_get(LKT_ENV_RESTART) ? "restarted" : "started", + pw->pw_name, pw->pw_shell, pw->pw_dir); + lkt_listen(&srv); return EXIT_FAILURE; } -- GitLab