comparison sat/core/log.py @ 3911:8289ac1b34f4

plugin XEP-0384: Fully reworked to adjust to the reworked python-omemo: - support for both (modern) OMEMO under the `urn:xmpp:omemo:2` namespace and (legacy) OMEMO under the `eu.siacs.conversations.axolotl` namespace - maintains one identity across both versions of OMEMO - migrates data from the old plugin - includes more features for protocol stability - uses SCE for modern OMEMO - fully type-checked, linted and format-checked - added type hints to various pieces of backend code used by the plugin - added stubs for some Twisted APIs used by the plugin under stubs/ (use `export MYPYPATH=stubs/` before running mypy) - core (xmpp): enabled `send` trigger and made it an asyncPoint fix 375
author Syndace <me@syndace.dev>
date Tue, 23 Aug 2022 21:06:24 +0200
parents 7550ae9cfbac
children 760f563b1243
comparison
equal deleted inserted replaced
3910:199598223f82 3911:8289ac1b34f4
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. 18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 19
20 """High level logging functions""" 20 """High level logging functions"""
21 # XXX: this module use standard logging module when possible, but as SàT can work in different cases where logging is not the best choice (twisted, pyjamas, etc), it is necessary to have a dedicated module. Additional feature like environment variables and colors are also managed. 21 # XXX: this module use standard logging module when possible, but as SàT can work in different cases where logging is not the best choice (twisted, pyjamas, etc), it is necessary to have a dedicated module. Additional feature like environment variables and colors are also managed.
22 # TODO: change formatting from "%s" style to "{}" when moved to Python 3 22 # TODO: change formatting from "%s" style to "{}" when moved to Python 3
23
24 from typing import TYPE_CHECKING, Any, Optional
25 from typing_extensions import TypedDict
26
27 if TYPE_CHECKING:
28 from logging import _ExcInfoType
29 else:
30 _ExcInfoType = Any
23 31
24 from sat.core.constants import Const as C 32 from sat.core.constants import Const as C
25 from sat.tools.common.ansi import ANSI as A 33 from sat.tools.common.ansi import ANSI as A
26 from sat.core import exceptions 34 from sat.core import exceptions
27 import traceback 35 import traceback
33 COLOR_END = '%(color_end)s' 41 COLOR_END = '%(color_end)s'
34 42
35 43
36 class Filtered(Exception): 44 class Filtered(Exception):
37 pass 45 pass
46
47
48 class KWArgs(TypedDict):
49 exc_info: _ExcInfoType
38 50
39 51
40 class Logger: 52 class Logger:
41 """High level logging class""" 53 """High level logging class"""
42 fmt = None # format option as given by user (e.g. SAT_LOG_LOGGER) 54 fmt = None # format option as given by user (e.g. SAT_LOG_LOGGER)
58 70
59 def addTraceback(self, message): 71 def addTraceback(self, message):
60 tb = traceback.format_exc() 72 tb = traceback.format_exc()
61 return message + "\n==== traceback ====\n" + tb 73 return message + "\n==== traceback ====\n" + tb
62 74
63 def out(self, message, level=None, **kwargs): 75 def out(self, message: object, level: Optional[str] = None, **kwargs: KWArgs) -> None:
64 """Actually log the message 76 """Actually log the message
65 77
66 @param message: formatted message 78 @param message: formatted message
67 """ 79 """
68 if kwargs.get('exc_info', False): 80 if kwargs.get('exc_info', False):
69 message = self.addTraceback(message) 81 message = self.addTraceback(message)
70 print(message) 82 print(message)
71 83
72 def log(self, level, message, **kwargs): 84 def log(self, level: str, message: object, **kwargs: KWArgs) -> None:
73 """Print message 85 """Print message
74 86
75 @param level: one of C.LOG_LEVELS 87 @param level: one of C.LOG_LEVELS
76 @param message: message to format and print 88 @param message: message to format and print
77 """ 89 """
82 else: 94 else:
83 self.out(self.post_treat(level, formatted), level, **kwargs) 95 self.out(self.post_treat(level, formatted), level, **kwargs)
84 except Filtered: 96 except Filtered:
85 pass 97 pass
86 98
87 def format(self, level, message): 99 def format(self, level: str, message: object) -> object:
88 """Format message according to Logger.fmt 100 """Format message according to Logger.fmt
89 101
90 @param level: one of C.LOG_LEVELS 102 @param level: one of C.LOG_LEVELS
91 @param message: message to format 103 @param message: message to format
92 @return: formatted message 104 @return: formatted message
115 record['profile'] = configure_cls[backend].getProfile() 127 record['profile'] = configure_cls[backend].getProfile()
116 return self.fmt % record 128 return self.fmt % record
117 else: 129 else:
118 raise e 130 raise e
119 131
120 def debug(self, msg, **kwargs): 132 def debug(self, msg: object, **kwargs: KWArgs) -> None:
121 self.log(C.LOG_LVL_DEBUG, msg, **kwargs) 133 self.log(C.LOG_LVL_DEBUG, msg, **kwargs)
122 134
123 def info(self, msg, **kwargs): 135 def info(self, msg: object, **kwargs: KWArgs) -> None:
124 self.log(C.LOG_LVL_INFO, msg, **kwargs) 136 self.log(C.LOG_LVL_INFO, msg, **kwargs)
125 137
126 def warning(self, msg, **kwargs): 138 def warning(self, msg: object, **kwargs: KWArgs) -> None:
127 self.log(C.LOG_LVL_WARNING, msg, **kwargs) 139 self.log(C.LOG_LVL_WARNING, msg, **kwargs)
128 140
129 def error(self, msg, **kwargs): 141 def error(self, msg: object, **kwargs: KWArgs) -> None:
130 self.log(C.LOG_LVL_ERROR, msg, **kwargs) 142 self.log(C.LOG_LVL_ERROR, msg, **kwargs)
131 143
132 def critical(self, msg, **kwargs): 144 def critical(self, msg: object, **kwargs: KWArgs) -> None:
133 self.log(C.LOG_LVL_CRITICAL, msg, **kwargs) 145 self.log(C.LOG_LVL_CRITICAL, msg, **kwargs)
134 146
135 147
136 class FilterName(object): 148 class FilterName(object):
137 """Filter on logger name according to a regex""" 149 """Filter on logger name according to a regex"""