File: log.py

package info (click to toggle)
patool 4.0.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,400 kB
  • sloc: python: 5,517; makefile: 177; sh: 122; vhdl: 1
file content (114 lines) | stat: -rw-r--r-- 3,414 bytes parent folder | download
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
# Copyright (C) 2010-2023 Bastian Kleineidam
#
# This program 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, either version 3 of the License, or
# (at your option) any later version.
#
# This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
"""Logging functions."""

import os
import sys
import time
import locale
import logging
import platform
from . import configuration


# the global logging.Logger object, initialized by init_logging()
logger: logging.Logger | None = None
default_encoding = locale.getpreferredencoding()


def init_logging(stream=sys.stderr) -> None:
    """Initialize the global logger. All log messages will be
    sent to the given stream, default is sys.stderr.
    """
    global logger  # noqa PLW0603
    logger = logging.getLogger(configuration.AppName)
    handler = logging.StreamHandler(stream=stream)
    format = f"%(levelname)s {configuration.AppName}: %(message)s"
    handler.setFormatter(logging.Formatter(format))
    logger.addHandler(handler)
    logger.setLevel(logging.INFO)


def encode_safe(*args, encoding=default_encoding) -> str:
    """Replacing unknown characters in args for the given encoding.
    @return: a space-separated string that will not have encoding errors
    with the given encoding
    """
    return " ".join(
        [str(arg).encode(encoding, errors="replace").decode(encoding) for arg in args]
    )


def log_error(msg) -> None:
    """Log error message."""
    if logger is not None:
        logger.error(encode_safe(msg))


def log_warning(msg) -> None:
    """Log warning message."""
    if logger is not None:
        logger.warning(encode_safe(msg))


def log_info(msg) -> None:
    """Log info message."""
    if logger is not None:
        logger.info(encode_safe(msg))


# environment keys to print for internal error info
EnvKeys = ("LANGUAGE", "LC_ALL", "LC_CTYPE", "LANG")


def log_internal_error() -> None:
    """Print internal error message."""
    now = time.localtime()
    env = os.linesep.join(
        [f"{key}={os.getenv(key)!r}" for key in EnvKeys if os.getenv(key) is not None]
    )
    if logger is not None:
        logger.exception(
            encode_safe(
                f"""********** Oops, I did it again. *************

You have found an internal error in {configuration.AppName}.
Please write a bug report at
{configuration.SupportUrl}
and include at least the information below:

Not disclosing some of the information below due to privacy reasons is ok.
I will try to help you nonetheless, but you have to give me something
I can work with ;) .

{configuration.App}
Python {sys.version} on {sys.platform}
Platform: {platform.platform()}
Local time: {strtime(now)}
sys.orig_argv: {sys.orig_argv}
Environment:
{env}
******** {configuration.AppName} internal error, over and out ********
"""
            )
        )


def strtime(t: time.struct_time) -> str:
    """Return ISO 8601 formatted time."""
    return time.strftime("%Y-%m-%d %H:%M:%S%z", t)


init_logging()