From 3eb728148c7912a426ddae8b7e61ea51177b8ee5 Mon Sep 17 00:00:00 2001
From: Kubat <mael.martin31@gmail.com>
Date: Mon, 25 Jan 2021 09:53:19 +0100
Subject: [PATCH] MISC: Add the 'FATAL' log, for when things go reaaeeallly
 wrong

---
 inc/lektor/common.h | 3 ++-
 src/base/commands.c | 3 +--
 src/base/config.c   | 6 ++----
 src/main/server.c   | 7 ++-----
 src/net/listen.c    | 6 ++----
 5 files changed, 9 insertions(+), 16 deletions(-)

diff --git a/inc/lektor/common.h b/inc/lektor/common.h
index 68ff134f..5bef0740 100644
--- a/inc/lektor/common.h
+++ b/inc/lektor/common.h
@@ -88,10 +88,11 @@ enum log_level {
 };
 extern enum log_level __log_level;
 void __lkt_log(enum log_level, const char *section, const char *func, const char *format, ...);
+#define LOG_DEBUG(section, ...) __lkt_log(DEBUG, section, __func__, __VA_ARGS__)
 #define LOG_INFO( section, ...) __lkt_log(INFO,  section, __func__, __VA_ARGS__)
 #define LOG_WARN( section, ...) __lkt_log(WARN,  section, __func__, __VA_ARGS__)
 #define LOG_ERROR(section, ...) __lkt_log(ERROR, section, __func__, __VA_ARGS__)
-#define LOG_DEBUG(section, ...) __lkt_log(DEBUG, section, __func__, __VA_ARGS__)
+#define LOG_FATAL(...)          { __lkt_log(ERROR, "FATAL", __func__, __VA_ARGS__); exit(1); }
 
 #define SELF_EXECUTABLE_LINUX       "/proc/self/exe"
 #define SELF_EXECUTABLE_FREEBSD     "/proc/curproc/file"
diff --git a/src/base/commands.c b/src/base/commands.c
index 8a58e442..8dc78f9a 100644
--- a/src/base/commands.c
+++ b/src/base/commands.c
@@ -89,8 +89,7 @@ command_kill(struct lkt_state *srv, size_t c, char UNUSED *argv[LKT_MESSAGE_ARGS
 {
     RETURN_UNLESS(lkt_client_auth(srv, c, false), "Failed to authentificate user", false);
     srv->__signal_INT(srv);
-    LOG_ERROR("FATAL", "The SIGINT handler returned, now exiting");
-    exit(EXIT_FAILURE);
+    LOG_FATAL("The SIGINT handler returned, now exiting");
 }
 
 bool
diff --git a/src/base/config.c b/src/base/config.c
index 274b4f1f..3dd1b39d 100644
--- a/src/base/config.c
+++ b/src/base/config.c
@@ -175,10 +175,8 @@ config_default_file(char *dest, size_t len)
     if (NULL == home || strlen(home) >= len) {
         LOG_DEBUG("CONFIG", "No env variable XDG_CONFIG_HOME");
         home = getenv("HOME");
-        if (NULL == home) {
-            LOG_ERROR("FATAL", "Failed to get home folder for user, will now exit");
-            exit(EXIT_FAILURE);
-        }
+        if (NULL == home)
+            LOG_FATAL("Failed to get home folder for user, will now exit");
         safe_strncpy(dest, home, len);
         strncat(dest, "/.config/lektor/lektor.ini", len - 1);
         LOG_DEBUG("CONFIG", "Use '%s' insted of XDG_CONFIG_HOME", home);
diff --git a/src/main/server.c b/src/main/server.c
index bc09e26d..310ed274 100644
--- a/src/main/server.c
+++ b/src/main/server.c
@@ -160,11 +160,8 @@ retry_config:
             config_default_file(conf_file, PATH_MAX);
             __mkdir(conf_file); /* Create the folder for the file. */
             FILE *file_desc = fopen(conf_file, "w+");
-            if (NULL == file_desc) {
-                LOG_ERROR("FATAL", "Failed to open default config file '%s' to initialize it",
-                          conf_file);
-                exit(EXIT_FAILURE);
-            }
+            if (NULL == file_desc)
+                LOG_FATAL("Failed to open default config file '%s' to initialize it", conf_file);
             config_default(file_desc);
             fclose(file_desc);
             LOG_INFO("INIT", "Default configuration file has been writen to %s", conf_file);
diff --git a/src/net/listen.c b/src/net/listen.c
index f73458c5..73744330 100644
--- a/src/net/listen.c
+++ b/src/net/listen.c
@@ -980,10 +980,8 @@ lkt_listen(struct lkt_state *srv)
     srv->fds_len       = 1;
     srv->start_date    = time(NULL);
 
-    if (srv->start_date == (time_t) -1) {
-        LOG_ERROR("FATAL", "Overflow or error on call to time function");
-        exit(EXIT_FAILURE);
-    }
+    if (srv->start_date == (time_t) -1)
+        LOG_FATAL("Overflow or error on call to time function");
 
     /* Listen */
     for (;;) {
-- 
GitLab