diff --git a/main.py b/main.py index 7cf457f..1b9066d 100644 --- a/main.py +++ b/main.py @@ -455,7 +455,6 @@ if __name__ == "__main__": handler = WeechatHandler() handler.format_string = "{record.channel}: {record.message}" handler.push_application() - nio.http.logger.level = logbook.DEBUG # TODO if this fails we should abort and unload the script. matrix.globals.CONFIG = W.config_new("matrix", diff --git a/matrix/config.py b/matrix/config.py index 4bc8850..e259d75 100644 --- a/matrix/config.py +++ b/matrix/config.py @@ -16,6 +16,9 @@ from __future__ import unicode_literals +import nio +import logbook + from matrix.plugin_options import (Option, RedactType, ServerBufferType) import matrix.globals @@ -30,6 +33,19 @@ def matrix_config_reload_cb(data, config_file): return W.WEECHAT_RC_OK +def change_log_level(category, level): + if category == "all": + nio.logger_group.level = level + elif category == "http": + nio.http.logger.level = level + elif category == "client": + nio.client.logger.level = level + elif category == "events": + nio.events.logger.level = level + elif category == "responses": + nio.responses.logger.level = level + + @utf8_decode def matrix_config_change_cb(data, option): option_name = key_from_value(OPTIONS.options, option) @@ -49,6 +65,36 @@ def matrix_config_change_cb(data, option): elif option_name == "max_backlog_sync_events": OPTIONS.backlog_limit = W.config_integer(option) + elif option_name == "debug_level": + value = W.config_integer(option) + if value == 0: + OPTIONS.debug_level = logbook.ERROR + elif value == 1: + OPTIONS.debug_level = logbook.WARNING + elif value == 2: + OPTIONS.debug_level = logbook.INFO + elif value == 3: + OPTIONS.debug_level = logbook.DEBUG + + change_log_level(OPTIONS.debug_category, OPTIONS.debug_level) + + elif option_name == "debug_category": + value = W.config_integer(option) + change_log_level(OPTIONS.debug_category, logbook.ERROR) + + if value == 0: + OPTIONS.debug_category = "all" + elif value == 1: + OPTIONS.debug_category = "http" + elif value == 2: + OPTIONS.debug_category = "client" + elif value == 3: + OPTIONS.debug_category = "events" + elif value == 4: + OPTIONS.debug_category = "responses" + + change_log_level(OPTIONS.debug_category, OPTIONS.debug_level) + elif option_name == "fetch_backlog_on_pgup": OPTIONS.enable_backlog = W.config_boolean(option) @@ -80,7 +126,11 @@ def matrix_config_init(config_file): Option("max_backlog_sync_events", "integer", "", 1, 100, "10", ("How many events to fetch during backlog fetching")), Option("fetch_backlog_on_pgup", "boolean", "", 0, 0, "on", - ("Fetch messages in the backlog on a window page up event")) + ("Fetch messages in the backlog on a window page up event")), + Option("debug_level", "integer", "error|warn|info|debug", 0, 0, + "off", "Enable network protocol debugging."), + Option("debug_category", "integer", "all|http|client|events|responses", + 0, 0, "all", "Debugging category") ] def add_global_options(section, options): @@ -101,8 +151,8 @@ def matrix_config_init(config_file): add_global_options(section, look_options) - section = W.config_new_section(config_file, "network", 0, 0, "", "", "", "", - "", "", "", "", "", "") + section = W.config_new_section(config_file, "network", 0, 0, "", "", "", + "", "", "", "", "", "", "") add_global_options(section, network_options) diff --git a/matrix/plugin_options.py b/matrix/plugin_options.py index fc0ecf5..a872a85 100644 --- a/matrix/plugin_options.py +++ b/matrix/plugin_options.py @@ -15,6 +15,9 @@ # CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. from __future__ import unicode_literals + +import logbook + from collections import namedtuple from enum import Enum, unique @@ -49,7 +52,8 @@ class PluginOptions: def __init__(self): self.redaction_type = RedactType.STRIKETHROUGH # type: RedactType - self.look_server_buf = ServerBufferType.MERGE_CORE # type: ServerBufferType + self.look_server_buf = ServerBufferType.MERGE_CORE \ + # type: ServerBufferType self.sync_limit = 30 # type: int self.backlog_limit = 10 # type: int @@ -59,4 +63,6 @@ class PluginOptions: self.redaction_comp_len = 50 # type: int self.options = dict() # type: Dict[str, weechat.config_option] - self.debug = [] # type: List[DebugType] + self.debug = [] + self.debug_level = logbook.ERROR + self.debug_category = "all"