From 9f02b0bf16972dfb7a18610e5096d4576e447a48 Mon Sep 17 00:00:00 2001 From: "S. Christoffer Eliesen" Date: Mon, 26 Oct 2015 13:41:45 +0100 Subject: [PATCH] commands: Learn 'debuglog'. Replicates i3 option. Verbosity level given as command line argument becomes default log level, and using 'debuglog toggle' switches back and forth between default and debug (or L_ERROR and debug if default is also L_DEBUG). --- include/log.h | 4 ++++ sway.5.txt | 5 +++++ sway/commands.c | 24 ++++++++++++++++++++++++ sway/log.c | 19 +++++++++++++++++++ 4 files changed, 52 insertions(+) diff --git a/include/log.h b/include/log.h index 806725a6a..4b769c93a 100644 --- a/include/log.h +++ b/include/log.h @@ -11,6 +11,10 @@ typedef enum { } log_importance_t; void init_log(log_importance_t verbosity); +void set_log_level(log_importance_t verbosity); +void reset_log_level(void); +// returns whether debug logging is on after switching. +bool toggle_debug_logging(void); void sway_log_colors(int mode); void sway_log(log_importance_t verbosity, const char* format, ...) __attribute__((format(printf,2,3))); void sway_log_errno(log_importance_t verbosity, char* format, ...) __attribute__((format(printf,2,3))); diff --git a/sway.5.txt b/sway.5.txt index d97720c54..69a6909f1 100644 --- a/sway.5.txt +++ b/sway.5.txt @@ -28,6 +28,11 @@ Commands execute Firefox if the alt, shift, and F keys are pressed together. Any valid sway command is eligible to be bound to a key combo. +**debuglog** :: + Turn debug log output _on_ or _off_. This will override any command line + option given. _toggle_ will toggle between debug and log level given as + command line option. + **exec** :: Executes _shell command_ with sh. diff --git a/sway/commands.c b/sway/commands.c index 0102fc5a2..c81379fd5 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -30,6 +30,7 @@ struct cmd_handler { static sway_cmd cmd_bindsym; static sway_cmd cmd_orientation; +static sway_cmd cmd_debuglog; static sway_cmd cmd_exec; static sway_cmd cmd_exec_always; static sway_cmd cmd_exit; @@ -229,6 +230,28 @@ static struct cmd_results *cmd_exec_always(int argc, char **argv) { return cmd_results_new(CMD_SUCCESS, NULL, NULL); } +static struct cmd_results *cmd_debuglog(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "debuglog", EXPECTED_EQUAL_TO, 1))) { + return error; + } else if (strcasecmp(argv[0], "toggle") == 0) { + if (config->reading) { + return cmd_results_new(CMD_FAILURE, "debuglog toggle", "Can't be used in config file."); + } + if (toggle_debug_logging()) { + sway_log(L_DEBUG, "Debuglog turned on."); + } + } else if (strcasecmp(argv[0], "on") == 0) { + set_log_level(L_DEBUG); + sway_log(L_DEBUG, "Debuglog turned on."); + } else if (strcasecmp(argv[0], "off") == 0) { + reset_log_level(); + } else { + return cmd_results_new(CMD_FAILURE, "debuglog", "Expected 'debuglog on|off|toggle'"); + } + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} + static struct cmd_results *cmd_exec(int argc, char **argv) { if (!config->active) return cmd_results_new(CMD_DEFER, "exec", NULL); if (config->reloading) { @@ -1162,6 +1185,7 @@ static struct cmd_results *cmd_ws_auto_back_and_forth(int argc, char **argv) { /* Keep alphabetized */ static struct cmd_handler handlers[] = { { "bindsym", cmd_bindsym }, + { "debuglog", cmd_debuglog }, { "default_orientation", cmd_orientation }, { "exec", cmd_exec }, { "exec_always", cmd_exec_always }, diff --git a/sway/log.c b/sway/log.c index 62be73e56..2ad9657df 100644 --- a/sway/log.c +++ b/sway/log.c @@ -11,6 +11,7 @@ #include int colored = 1; +log_importance_t loglevel_default = L_ERROR; log_importance_t v = L_SILENT; static const char *verbosity_colors[] = { @@ -21,11 +22,29 @@ static const char *verbosity_colors[] = { }; void init_log(log_importance_t verbosity) { + if (verbosity != L_DEBUG) { + // command "debuglog" needs to know the user specified log level when + // turning off debug logging. + loglevel_default = verbosity; + } v = verbosity; signal(SIGSEGV, error_handler); signal(SIGABRT, error_handler); } +void set_log_level(log_importance_t verbosity) { + v = verbosity; +} + +void reset_log_level(void) { + v = loglevel_default; +} + +bool toggle_debug_logging(void) { + v = (v == L_DEBUG) ? loglevel_default : L_DEBUG; + return (v == L_DEBUG); +} + void sway_log_colors(int mode) { colored = (mode == 1) ? 1 : 0; }