[5d6472]: / src / multivelo / mv_logging.py

Download this file

68 lines (46 with data), 1.6 kB

 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
import os
import sys
current_path = os.path.dirname(__file__)
src_path = os.path.join(current_path, "..")
sys.path.append(src_path)
from multivelo import settings
from numba import jit
msg_codes = {1: "update", 2: "warning", 3: "error"}
# msg = the message to print
# code = what message code this runs under
# @jit(nopython=True, fastmath=True, debug=True)
def _msg(msg, code):
if code == 2 or code == 3:
msg = msg_codes[code] + ": " + msg
if settings.GENE is not None:
msg = str(settings.GENE) + " - " + msg
msg = str(msg) + "\n"
return msg
# msg: the message to output
# v: at what minimum verbosity level do we output this?
# filename: the filename to output the log to
# @jit(nopython=True, fastmath=True, debug=True)
def _log(msg, code, v=0):
# if the current verbosity is less than the minimum
# verbosity needed for this message to print,
# don't bother printing it
if settings.VERBOSITY < v:
return
msg = _msg(msg, code=code)
print(msg)
if settings.LOG_FILENAME is not None:
log_path = settings.LOG_FOLDER + "/" + settings.LOG_FILENAME
if not os.path.isdir(settings.LOG_FOLDER):
os.mkdir(settings.LOG_FOLDER)
with open(log_path, "a") as logfile:
logfile.write(msg)
logfile.close()
# @jit(nopython=True, fastmath=True, debug=True)
def update(msg, v):
_log(msg, code=1, v=v)
# @jit(nopython=True, fastmath=True, debug=True)
def warn(msg, v):
_log(msg, code=2, v=v)
# @jit(nopython=True, fastmath=True, debug=True)
def error(msg, v):
_log(msg, code=3, v=v)