forked from Ridepad/uwu-logs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
h_debug.py
57 lines (47 loc) · 1.75 KB
/
h_debug.py
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
import logging
from time import perf_counter
from c_path import Directories
LOGGING_FORMAT_DEFAULT = '''%(asctime)s | %(levelname)-8s | %(filename)18s:%(lineno)-4s | %(message)s'''
LOGGING_FORMAT = {
"connections" : '''%(asctime)s | %(message)s''',
}
def setup_logger(logger_name):
log_file = Directories.loggers / f'{logger_name}.log'
logger = logging.getLogger(logger_name)
_format = LOGGING_FORMAT.get(logger_name, LOGGING_FORMAT_DEFAULT)
formatter = logging.Formatter(_format)
fileHandler = logging.FileHandler(log_file)
fileHandler.setFormatter(formatter)
streamHandler = logging.StreamHandler()
streamHandler.setFormatter(formatter)
logger.setLevel(logging.DEBUG)
logger.addHandler(fileHandler)
logger.addHandler(streamHandler)
return logger
class Loggers(dict[str, logging.Logger]):
__getattr__ = dict.get
connections = setup_logger('connections')
reports = setup_logger('reports')
uploads = setup_logger('uploads')
unusual_spells = setup_logger('unusual_spells')
memory = setup_logger('memory')
archives = setup_logger("archives")
player_queue = setup_logger('player_queue')
ladder = setup_logger('ladder')
ladder_parser = setup_logger('ladder_parser')
missing = setup_logger('missing')
def get_ms(timestamp):
if timestamp is None:
return -1
return int((perf_counter()-timestamp)*1000)
def get_ms_str(timestamp):
return f"{get_ms(timestamp):>7,} ms"
def running_time(f):
_logger = Loggers.reports
def running_time_inner(*args, **kwargs):
timestamp = perf_counter()
q = f(*args, **kwargs)
msg = f"{get_ms_str(timestamp)} | {f.__module__}.{f.__name__}"
_logger.debug(msg)
return q
return running_time_inner