From 3785cd0beec742951bfd0aebf89f354174b968cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Sat, 21 Jul 2018 19:54:36 +0200 Subject: [PATCH] matrix: Enable nio logger. --- main.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/main.py b/main.py index 98905c2..7cf457f 100644 --- a/main.py +++ b/main.py @@ -31,7 +31,10 @@ from future.utils import bytes_to_native_str as n # pylint: disable=unused-import from typing import (List, Set, Dict, Tuple, Text, Optional, AnyStr, Deque, Any) +import logbook +from logbook import Logger, StderrHandler, StreamHandler +import nio from nio import TransportType from matrix.colors import Formatted @@ -92,6 +95,9 @@ WEECHAT_SCRIPT_LICENSE = "ISC" # type: str # yapf: enable +logger = Logger("matrix-cli") + + def print_certificate_info(buff, sock, cert): cert_pem = ssl.DER_cert_to_PEM_cert(sock.getpeercert(True)) @@ -419,6 +425,23 @@ def autoconnect(servers): server.connect() +class WeechatHandler(StreamHandler): + def __init__(self, level=logbook.NOTSET, format_string=None, filter=None, + bubble=False): + StreamHandler.__init__( + self, + object(), + level, + format_string, + None, + filter, + bubble + ) + + def write(self, item): + W.prnt("", item) + + if __name__ == "__main__": if W.register(WEECHAT_SCRIPT_NAME, WEECHAT_SCRIPT_AUTHOR, WEECHAT_SCRIPT_VERSION, WEECHAT_SCRIPT_LICENSE, @@ -429,6 +452,11 @@ if __name__ == "__main__": "directory").format(prefix=W.prefix("error")) W.prnt("", message) + 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", "matrix_config_reload_cb", "")