about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorMichael Vetter <jubalh@iodoru.org>2022-06-22 13:27:12 +0200
committerGitHub <noreply@github.com>2022-06-22 13:27:12 +0200
commit2aade0db2de5a9a5e2b319982559a16e610bdf73 (patch)
tree44d6bd563595941b7d2313ab31ad9523688826d3
parent6147a1342d4cda8cb5c15d1453d48dc028c2b24d (diff)
parentc0e27f5e7296d4d293c7392724e6c4d0e25a4da9 (diff)
downloadprofani-tty-2aade0db2de5a9a5e2b319982559a16e610bdf73.tar.gz
Merge pull request #1726 from profanity-im/feat/1627-change-log-level
Switch log level while running
-rw-r--r--src/command/cmd_ac.c14
-rw-r--r--src/command/cmd_defs.c8
-rw-r--r--src/command/cmd_funcs.c14
-rw-r--r--src/log.c24
-rw-r--r--src/log.h1
-rw-r--r--src/ui/console.c4
6 files changed, 57 insertions, 8 deletions
diff --git a/src/command/cmd_ac.c b/src/command/cmd_ac.c
index d8121d65..e3661cac 100644
--- a/src/command/cmd_ac.c
+++ b/src/command/cmd_ac.c
@@ -151,6 +151,7 @@ static Autocomplete notify_trigger_ac;
 static Autocomplete prefs_ac;
 static Autocomplete sub_ac;
 static Autocomplete log_ac;
+static Autocomplete log_level_ac;
 static Autocomplete autoaway_ac;
 static Autocomplete autoaway_mode_ac;
 static Autocomplete autoaway_presence_ac;
@@ -391,6 +392,13 @@ cmd_ac_init(void)
     autocomplete_add(log_ac, "rotate");
     autocomplete_add(log_ac, "shared");
     autocomplete_add(log_ac, "where");
+    autocomplete_add(log_ac, "level");
+
+    log_level_ac = autocomplete_new();
+    autocomplete_add(log_level_ac, "WARN");
+    autocomplete_add(log_level_ac, "INFO");
+    autocomplete_add(log_level_ac, "DEBUG");
+    autocomplete_add(log_level_ac, "ERROR");
 
     autoaway_ac = autocomplete_new();
     autocomplete_add(autoaway_ac, "mode");
@@ -1353,6 +1361,7 @@ cmd_ac_reset(ProfWin* window)
     autocomplete_reset(who_roster_ac);
     autocomplete_reset(prefs_ac);
     autocomplete_reset(log_ac);
+    autocomplete_reset(log_level_ac);
     autocomplete_reset(commands_ac);
     autocomplete_reset(autoaway_ac);
     autocomplete_reset(autoaway_mode_ac);
@@ -1533,6 +1542,7 @@ cmd_ac_uninit(void)
     autocomplete_free(sub_ac);
     autocomplete_free(wintitle_ac);
     autocomplete_free(log_ac);
+    autocomplete_free(log_level_ac);
     autocomplete_free(prefs_ac);
     autocomplete_free(autoaway_ac);
     autocomplete_free(autoaway_mode_ac);
@@ -2441,6 +2451,10 @@ _log_autocomplete(ProfWin* window, const char* const input, gboolean previous)
     if (result) {
         return result;
     }
+    result = autocomplete_param_with_ac(input, "/log level", log_level_ac, TRUE, previous);
+    if (result) {
+        return result;
+    }
     result = autocomplete_param_with_ac(input, "/log", log_ac, TRUE, previous);
     if (result) {
         return result;
diff --git a/src/command/cmd_defs.c b/src/command/cmd_defs.c
index 2eaec284..98a74314 100644
--- a/src/command/cmd_defs.c
+++ b/src/command/cmd_defs.c
@@ -1884,14 +1884,16 @@ static struct cmd_t command_defs[] = {
               "/log where",
               "/log rotate on|off",
               "/log maxsize <bytes>",
-              "/log shared on|off")
+              "/log shared on|off",
+              "/log level INFO|DEBUG|WARN|ERROR")
       CMD_DESC(
               "Manage profanity log settings.")
       CMD_ARGS(
               { "where", "Show the current log file location." },
               { "rotate on|off", "Rotate log, default on. Does not take effect if you specified a filename yourself when starting Profanity." },
-              { "maxsize <bytes>", "With rotate enabled, specifies the max log size, defaults to 1048580 (1MB)." },
-              { "shared on|off", "Share logs between all instances, default: on. When off, the process id will be included in the log filename. Does not take effect if you specified a filename yourself when starting Profanity." })
+              { "maxsize <bytes>", "With rotate enabled, specifies the max log size, defaults to 10485760 (10MB)." },
+              { "shared on|off", "Share logs between all instances, default: on. When off, the process id will be included in the log filename. Does not take effect if you specified a filename yourself when starting Profanity." },
+              {"level INFO|DEBUG|WARN|EFFOR", "Set the log level. Default is INFO. Only works with default log file, not with user provided log file during startup via -f." })
       CMD_NOEXAMPLES
     },
 
diff --git a/src/command/cmd_funcs.c b/src/command/cmd_funcs.c
index 4edc7c16..fbea6bc2 100644
--- a/src/command/cmd_funcs.c
+++ b/src/command/cmd_funcs.c
@@ -6495,9 +6495,19 @@ cmd_log(ProfWin* window, const char* const command, gchar** args)
         return TRUE;
     }
 
-    cons_bad_cmd_usage(command);
+    if (strcmp(subcmd, "level") == 0) {
+        if (g_strcmp0(value, "INFO") == 0 || g_strcmp0(value, "DEBUG") == 0 || g_strcmp0(value, "WARN") == 0 || g_strcmp0(value, "ERROR") == 0) {
+
+            log_level_t prof_log_level = log_level_from_string(value);
+            log_close();
+            log_init(prof_log_level, NULL);
 
-    /* TODO: make 'level' subcommand for debug level */
+            cons_show("Log level changed to: %s.", value);
+            return TRUE;
+        }
+    }
+
+    cons_bad_cmd_usage(command);
 
     return TRUE;
 }
diff --git a/src/log.c b/src/log.c
index 867e0bb9..d2ab58ac 100644
--- a/src/log.c
+++ b/src/log.c
@@ -89,7 +89,7 @@ static void _free_chat_log(struct dated_chat_log* dated_log);
 static gboolean _key_equals(void* key1, void* key2);
 static char* _get_log_filename(const char* const other, const char* const login, GDateTime* dt, gboolean is_room);
 static void _rotate_log_file(void);
-static char* _log_string_from_level(log_level_t level);
+static char* _log_abbreviation_string_from_level(log_level_t level);
 static void _chat_log_chat(const char* const login, const char* const other, const gchar* const msg,
                            chat_log_direction_t direction, GDateTime* timestamp, const char* const resourcepart);
 static void _groupchat_log_chat(const gchar* const login, const gchar* const room, const gchar* const nick,
@@ -189,7 +189,7 @@ log_msg(log_level_t level, const char* const area, const char* const msg)
     if (level >= level_filter && logp) {
         GDateTime* dt = g_date_time_new_now_local();
 
-        char* level_str = _log_string_from_level(level);
+        char* level_str = _log_abbreviation_string_from_level(level);
 
         gchar* date_fmt = g_date_time_format_iso8601(dt);
 
@@ -225,6 +225,23 @@ log_level_from_string(char* log_level)
     }
 }
 
+const char*
+log_string_from_level(log_level_t level)
+{
+    switch (level) {
+    case PROF_LEVEL_ERROR:
+        return "ERROR";
+    case PROF_LEVEL_WARN:
+        return "WARN";
+    case PROF_LEVEL_INFO:
+        return "INFO";
+    case PROF_LEVEL_DEBUG:
+        return "DEBUG";
+    default:
+        return "LOG";
+    }
+}
+
 static void
 _rotate_log_file(void)
 {
@@ -700,8 +717,9 @@ _get_log_filename(const char* const other, const char* const login, GDateTime* d
     return logfile_path;
 }
 
+// abbreviation string is the prefix thats used in the log file
 static char*
-_log_string_from_level(log_level_t level)
+_log_abbreviation_string_from_level(log_level_t level)
 {
     switch (level) {
     case PROF_LEVEL_ERROR:
diff --git a/src/log.h b/src/log.h
index 00e40fb6..596227d2 100644
--- a/src/log.h
+++ b/src/log.h
@@ -63,6 +63,7 @@ void log_warning(const char* const msg, ...);
 void log_error(const char* const msg, ...);
 void log_msg(log_level_t level, const char* const area, const char* const msg);
 log_level_t log_level_from_string(char* log_level);
+const char* log_string_from_level(log_level_t level);
 
 void log_stderr_init(log_level_t level);
 void log_stderr_close(void);
diff --git a/src/ui/console.c b/src/ui/console.c
index 2c4a8e41..28706b55 100644
--- a/src/ui/console.c
+++ b/src/ui/console.c
@@ -2018,6 +2018,10 @@ cons_log_setting(void)
         cons_show("Shared log (/log shared)    : ON");
     else
         cons_show("Shared log (/log shared)    : OFF");
+
+    log_level_t filter = log_get_filter();
+    const gchar* level = log_string_from_level(filter);
+    cons_show("Log level (/log level)      : %s", level);
 }
 
 void