Welcome to mirror list, hosted at ThFree Co, Russian Federation.

logging_helpers.py « common « gajim - dev.gajim.org/gajim/gajim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3e0fc5565e490c20bd80c64d82b252a590f1a3b9 (plain)
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# Copyright (C) 2009 Bruno Tarquini <btarquini AT gmail.com>
#
# This file is part of Gajim.
#
# Gajim is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published
# by the Free Software Foundation; version 3 only.
#
# Gajim is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Gajim. If not, see <http://www.gnu.org/licenses/>.

import logging
import os
import sys
import time
from collections.abc import Callable
from datetime import datetime

from gajim.common import app
from gajim.common import configpaths
from gajim.common.i18n import _

LogCallback = Callable[[str], None]


def parseLogLevel(arg: str) -> int:
    '''
    Either numeric value or level name from logging module
    '''
    if arg.isdigit():
        return int(arg)
    if arg.isupper() and hasattr(logging, arg):
        return getattr(logging, arg)
    print(_('%s is not a valid loglevel') % repr(arg), file=sys.stderr)
    return 0


def parseLogTarget(arg: str) -> str:
    '''
    [gajim.]c.x.y  ->  gajim.c.x.y
    .other_logger  ->  other_logger
    <None>         ->  gajim
    '''
    arg = arg.lower()
    if not arg:
        return 'gajim'
    if arg.startswith('.'):
        return arg[1:]
    if arg.startswith('gajim'):
        return arg
    return 'gajim.' + arg


def parseAndSetLogLevels(arg: str) -> None:
    '''
    [=]LOGLEVEL     ->  gajim=LOGLEVEL
    gajim=LOGLEVEL  ->  gajim=LOGLEVEL
    .other=10       ->  other=10
    .=10            ->  <nothing>
    c.x.y=c.z=20    ->  gajim.c.x.y=20
                        gajim.c.z=20
    gajim=10,c.x=20 ->  gajim=10
                        gajim.c.x=20
    '''
    for directive in arg.split(','):
        directive = directive.strip()
        if not directive:
            continue
        if '=' not in directive:
            directive = '=' + directive
        targets, level = directive.rsplit('=', 1)
        level = parseLogLevel(level.strip())
        for target in targets.split('='):
            target = parseLogTarget(target.strip())
            if target:
                logging.getLogger(target).setLevel(level)
                print('Logger %s level set to %d' % (target, level),
                      file=sys.stderr)


class Colors:
    NONE = chr(27) + '[0m'
    BLACK = chr(27) + '[30m'
    RED = chr(27) + '[31m'
    GREEN = chr(27) + '[32m'
    BROWN = chr(27) + '[33m'
    BLUE = chr(27) + '[34m'
    MAGENTA = chr(27) + '[35m'
    CYAN = chr(27) + '[36m'
    LIGHT_GRAY = chr(27) + '[37m'
    DARK_GRAY = chr(27) + '[30;1m'
    BRIGHT_RED = chr(27) + '[31;1m'
    BRIGHT_GREEN = chr(27) + '[32;1m'
    YELLOW = chr(27) + '[33;1m'
    BRIGHT_BLUE = chr(27) + '[34;1m'
    PURPLE = chr(27) + '[35;1m'
    BRIGHT_CYAN = chr(27) + '[36;1m'
    WHITE = chr(27) + '[37;1m'


def colorize(text: str, color: str) -> str:
    return color + text + Colors.NONE


class LogConsoleHandler(logging.StreamHandler):  # pyright: ignore
    def __init__(self) -> None:
        super().__init__()  # pyright: ignore
        self._callback: LogCallback | None = None

    def emit(self, record: logging.LogRecord) -> None:
        if record.levelno < logging.WARNING:
            return

        msg = self.format(record)
        app.logging_records.append(msg)
        if self._callback is not None:
            self._callback(msg)

    def set_callback(self,
                     func: LogCallback | None
                     ) -> None:
        self._callback = func


class FancyFormatter(logging.Formatter):
    '''
    An eye-candy formatter with Colors
    '''
    colors_mapping = {
        'DEBUG': Colors.BLUE,
        'INFO': Colors.GREEN,
        'WARNING': Colors.BROWN,
        'ERROR': Colors.RED,
        'CRITICAL': Colors.BRIGHT_RED,
    }

    def __init__(self,
                 fmt: str | None = None,
                 datefmt: str | None = None,
                 use_color: bool = False) -> None:
        logging.Formatter.__init__(self, fmt, datefmt)
        self.use_color = use_color

    def format(self, record: logging.LogRecord) -> str:
        level = record.levelname
        record.levelname = '(%s)' % level[0]

        if self.use_color:
            c = FancyFormatter.colors_mapping.get(level, '')
            record.levelname = colorize(record.levelname, c)
            record.name = '%-25s' % colorize(record.name, Colors.CYAN)
        else:
            record.name = '%-25s|' % record.name

        return logging.Formatter.format(self, record)


def init() -> None:
    '''
    Initialize the logging system
    '''

    if app.get_debug_mode():
        _cleanup_debug_logs()
        _redirect_output()

    use_color = False
    if os.name != 'nt':
        use_color = sys.stderr.isatty()

    stream_handler = logging.StreamHandler()
    stream_handler.setFormatter(
        FancyFormatter(
            '%(asctime)s %(levelname)s %(name)-35s %(message)s',
            '%Y-%m-%dT%H:%M:%S',
            use_color
        )
    )

    root_log = logging.getLogger('gajim')
    root_log.setLevel(logging.WARNING)
    root_log.addHandler(_log_console_handler)
    root_log.addHandler(stream_handler)
    root_log.propagate = False

    root_log = logging.getLogger('nbxmpp')
    root_log.setLevel(logging.WARNING)
    root_log.addHandler(_log_console_handler)
    root_log.addHandler(stream_handler)
    root_log.propagate = False

    root_log = logging.getLogger('gnupg')
    root_log.setLevel(logging.WARNING)
    root_log.addHandler(_log_console_handler)
    root_log.addHandler(stream_handler)
    root_log.propagate = False

    root_log = logging.getLogger('omemo_dr')
    root_log.setLevel(logging.WARNING)
    root_log.addHandler(_log_console_handler)
    root_log.addHandler(stream_handler)
    root_log.propagate = False

    # GAJIM_DEBUG is set only on Windows when using Gajim-Debug.exe
    # Gajim-Debug.exe shows a command line prompt and we want to redirect
    # log output to it
    if app.get_debug_mode() or os.environ.get('GAJIM_DEBUG', False):
        set_verbose()


def set_loglevels(loglevels_string: str) -> None:
    parseAndSetLogLevels(loglevels_string)


def set_verbose() -> None:
    parseAndSetLogLevels('gajim=DEBUG')
    parseAndSetLogLevels('.nbxmpp=INFO')
    parseAndSetLogLevels('.omemo_dr=DEBUG')


def set_quiet() -> None:
    parseAndSetLogLevels('gajim=CRITICAL')
    parseAndSetLogLevels('.nbxmpp=CRITICAL')
    parseAndSetLogLevels('.omemo_dr=CRITICAL')


def get_log_console_handler() -> LogConsoleHandler:
    return _log_console_handler


def _redirect_output() -> None:
    debug_folder = configpaths.get('DEBUG')
    date = datetime.today().strftime('%d%m%Y-%H%M%S')
    filename = '%s-debug.log' % date
    fd = open(debug_folder / filename, 'a', encoding='utf8')
    sys.stderr = sys.stdout = fd


def _cleanup_debug_logs() -> None:
    debug_folder = configpaths.get('DEBUG')
    debug_files = list(debug_folder.glob('*-debug.log*'))
    now = time.time()
    for file in debug_files:
        # Delete everything older than 3 days
        if file.stat().st_ctime < now - 259200:
            file.unlink()


_log_console_handler = LogConsoleHandler()
_log_console_handler.setFormatter(
    logging.Formatter(
        '%(asctime)s (%(levelname).1s) %(name)-35s | %(message)s\n',
        '%x %H:%M:%S'
    )
)


if __name__ == '__main__':
    init()

    set_loglevels('gajim.c=DEBUG,INFO')

    log = logging.getLogger('gajim')
    log.debug('debug')
    log.info('info')
    log.warning('warn')
    log.error('error')
    log.critical('critical')

    log = logging.getLogger('gajim.c.x.dispatcher')
    log.debug('debug')
    log.info('info')
    log.warning('warn')
    log.error('error')
    log.critical('critical')