aboutsummaryrefslogtreecommitdiffstats
path: root/slack/log.py
diff options
context:
space:
mode:
Diffstat (limited to 'slack/log.py')
-rw-r--r--slack/log.py63
1 files changed, 59 insertions, 4 deletions
diff --git a/slack/log.py b/slack/log.py
index 4b8bbe1..9abc1ad 100644
--- a/slack/log.py
+++ b/slack/log.py
@@ -1,14 +1,15 @@
from __future__ import annotations
+import time
+from dataclasses import dataclass
from enum import IntEnum
-from typing import Set
+from typing import List, Set
import weechat
from slack.error import store_and_format_exception
from slack.shared import shared
-
-printed_exceptions: Set[BaseException] = set()
+from slack.util import get_callback_name
class LogLevel(IntEnum):
@@ -20,6 +21,25 @@ class LogLevel(IntEnum):
FATAL = 6
+class DebugMessageType(IntEnum):
+ WEBSOCKET_SEND = 1
+ WEBSOCKET_RECV = 2
+ HTTP_REQUEST = 3
+ LOG = 4
+
+
+@dataclass
+class DebugMessage:
+ time: float
+ level: LogLevel
+ message_type: DebugMessageType
+ message: str
+
+
+debug_messages: List[DebugMessage] = []
+printed_exceptions: Set[BaseException] = set()
+
+
# TODO: Figure out what to do with print_error vs log
def print_error(message: str):
weechat.prnt("", f"{weechat.prefix('error')}{shared.SCRIPT_NAME}: {message}")
@@ -31,7 +51,42 @@ def print_exception_once(e: BaseException):
printed_exceptions.add(e)
-def log(level: LogLevel, message: str):
+def log(level: LogLevel, message_type: DebugMessageType, message: str):
if level >= LogLevel.INFO:
prefix = weechat.prefix("error") if level >= LogLevel.ERROR else "\t"
weechat.prnt("", f"{prefix}{shared.SCRIPT_NAME} {level.name}: {message}")
+
+ debug_message = DebugMessage(time.time(), level, message_type, message)
+ debug_messages.append(debug_message)
+ print_debug_buffer(debug_message)
+
+
+def _close_debug_buffer_cb(data: str, buffer: str):
+ shared.debug_buffer_pointer = None
+ return weechat.WEECHAT_RC_OK
+
+
+def open_debug_buffer():
+ if shared.debug_buffer_pointer:
+ weechat.buffer_set(shared.debug_buffer_pointer, "display", "1")
+ return
+
+ name = f"{shared.SCRIPT_NAME}.debug"
+ shared.debug_buffer_pointer = weechat.buffer_new_props(
+ name,
+ {"display": "1"},
+ "",
+ "",
+ get_callback_name(_close_debug_buffer_cb),
+ "",
+ )
+ for message in debug_messages:
+ print_debug_buffer(message)
+
+
+def print_debug_buffer(debug_message: DebugMessage):
+ if shared.debug_buffer_pointer:
+ message = f"{debug_message.level.name} - {debug_message.message_type.name}\t{debug_message.message}"
+ weechat.prnt_date_tags(
+ shared.debug_buffer_pointer, int(debug_message.time), "", message
+ )