1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-07-21 18:24:14 -04:00

Merge pull request #1726 from profanity-im/feat/1627-change-log-level

Switch log level while running
This commit is contained in:
Michael Vetter 2022-06-22 13:27:12 +02:00 committed by GitHub
commit 2aade0db2d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 57 additions and 8 deletions

View File

@ -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;

View File

@ -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
},

View File

@ -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;
}

View File

@ -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:

View File

@ -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);

View File

@ -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