From 3f26dd6be98eca05d429693fe54eb38713af72ff Mon Sep 17 00:00:00 2001 From: Michael Vetter Date: Wed, 22 Jun 2022 12:39:44 +0200 Subject: [PATCH 1/6] Let user change log level while running `/log level INFO|DEBUG|WARN|ERROR` is now available. Looks like this solves a TODO (see removed comment in source) from 2013 :-) Works only with default log file. Not with user provided log file during start up via the -f parameter. Fix https://github.com/profanity-im/profanity/issues/1627 --- src/command/cmd_ac.c | 1 + src/command/cmd_defs.c | 6 ++++-- src/command/cmd_funcs.c | 14 ++++++++++++-- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/command/cmd_ac.c b/src/command/cmd_ac.c index d8121d65..b945528a 100644 --- a/src/command/cmd_ac.c +++ b/src/command/cmd_ac.c @@ -391,6 +391,7 @@ cmd_ac_init(void) autocomplete_add(log_ac, "rotate"); autocomplete_add(log_ac, "shared"); autocomplete_add(log_ac, "where"); + autocomplete_add(log_ac, "level"); autoaway_ac = autocomplete_new(); autocomplete_add(autoaway_ac, "mode"); diff --git a/src/command/cmd_defs.c b/src/command/cmd_defs.c index 2eaec284..8c8143fe 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 ", - "/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 ", "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." }) + { "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) { - /* TODO: make 'level' subcommand for debug level */ + log_level_t prof_log_level = log_level_from_string(value); + log_close(); + log_init(prof_log_level, NULL); + + cons_show("Log level changed to: %s.", value); + return TRUE; + } + } + + cons_bad_cmd_usage(command); return TRUE; } From 5c0c386ffa8cc0580f1e92250081f730ff3dc930 Mon Sep 17 00:00:00 2001 From: Michael Vetter Date: Wed, 22 Jun 2022 12:45:25 +0200 Subject: [PATCH 2/6] Correct default log maxsize in help This was changed in c4d7f1c29 from 1MB to 10MB. --- src/command/cmd_defs.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/command/cmd_defs.c b/src/command/cmd_defs.c index 8c8143fe..98a74314 100644 --- a/src/command/cmd_defs.c +++ b/src/command/cmd_defs.c @@ -1891,7 +1891,7 @@ static struct cmd_t command_defs[] = { 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 ", "With rotate enabled, specifies the max log size, defaults to 1048580 (1MB)." }, + { "maxsize ", "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 From ff8065eaca84b26d8d6de7bbaa03420204b36764 Mon Sep 17 00:00:00 2001 From: Michael Vetter Date: Wed, 22 Jun 2022 12:52:30 +0200 Subject: [PATCH 3/6] Rename _log_string_from_level to _log_abbreviation_string_from_level --- src/log.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/log.c b/src/log.c index 867e0bb9..6aeb6ecf 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); @@ -700,8 +700,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: From b302c604ab0a84204dd99083f04c505db851002f Mon Sep 17 00:00:00 2001 From: Michael Vetter Date: Wed, 22 Jun 2022 12:59:50 +0200 Subject: [PATCH 4/6] Display log level information in preferences --- src/log.c | 17 +++++++++++++++++ src/log.h | 1 + src/ui/console.c | 4 ++++ 3 files changed, 22 insertions(+) diff --git a/src/log.c b/src/log.c index 6aeb6ecf..d2ab58ac 100644 --- a/src/log.c +++ b/src/log.c @@ -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) { 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..b2a8a053 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 From 4da322db3b26d4ef774821090066f060373dafda Mon Sep 17 00:00:00 2001 From: Michael Vetter Date: Wed, 22 Jun 2022 13:03:38 +0200 Subject: [PATCH 5/6] Add autocompletion for `/log level` --- src/command/cmd_ac.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/command/cmd_ac.c b/src/command/cmd_ac.c index b945528a..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; @@ -393,6 +394,12 @@ cmd_ac_init(void) 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"); autocomplete_add(autoaway_ac, "time"); @@ -1354,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); @@ -1534,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); @@ -2442,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; From c0e27f5e7296d4d293c7392724e6c4d0e25a4da9 Mon Sep 17 00:00:00 2001 From: Michael Vetter Date: Wed, 22 Jun 2022 13:10:42 +0200 Subject: [PATCH 6/6] Fix formatting --- src/ui/console.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ui/console.c b/src/ui/console.c index b2a8a053..28706b55 100644 --- a/src/ui/console.c +++ b/src/ui/console.c @@ -2020,7 +2020,7 @@ cons_log_setting(void) cons_show("Shared log (/log shared) : OFF"); log_level_t filter = log_get_filter(); - const gchar *level = log_string_from_level(filter); + const gchar* level = log_string_from_level(filter); cons_show("Log level (/log level) : %s", level); }