aboutsummaryrefslogtreecommitdiffstats
path: root/slack/log.py
blob: b132c0e9bdc6577113c12b7035654c5dd7cc83a0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
from __future__ import annotations

from enum import IntEnum
from typing import Set

import weechat

from slack.error import format_exception
from slack.shared import shared

printed_exceptions: Set[BaseException] = set()


class LogLevel(IntEnum):
    TRACE = 1
    DEBUG = 2
    INFO = 3
    WARN = 4
    ERROR = 5
    FATAL = 6


# 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}")


def print_exception_once(e: BaseException):
    if e not in printed_exceptions:
        print_error(format_exception(e))
        printed_exceptions.add(e)


def log(level: LogLevel, 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}")