|
a |
|
b/src/multivelo/mv_logging.py |
|
|
1 |
import os |
|
|
2 |
import sys |
|
|
3 |
|
|
|
4 |
current_path = os.path.dirname(__file__) |
|
|
5 |
src_path = os.path.join(current_path, "..") |
|
|
6 |
sys.path.append(src_path) |
|
|
7 |
|
|
|
8 |
from multivelo import settings |
|
|
9 |
from numba import jit |
|
|
10 |
|
|
|
11 |
msg_codes = {1: "update", 2: "warning", 3: "error"} |
|
|
12 |
|
|
|
13 |
|
|
|
14 |
# msg = the message to print |
|
|
15 |
# code = what message code this runs under |
|
|
16 |
# @jit(nopython=True, fastmath=True, debug=True) |
|
|
17 |
def _msg(msg, code): |
|
|
18 |
|
|
|
19 |
if code == 2 or code == 3: |
|
|
20 |
msg = msg_codes[code] + ": " + msg |
|
|
21 |
|
|
|
22 |
if settings.GENE is not None: |
|
|
23 |
msg = str(settings.GENE) + " - " + msg |
|
|
24 |
|
|
|
25 |
msg = str(msg) + "\n" |
|
|
26 |
|
|
|
27 |
return msg |
|
|
28 |
|
|
|
29 |
|
|
|
30 |
# msg: the message to output |
|
|
31 |
# v: at what minimum verbosity level do we output this? |
|
|
32 |
# filename: the filename to output the log to |
|
|
33 |
# @jit(nopython=True, fastmath=True, debug=True) |
|
|
34 |
def _log(msg, code, v=0): |
|
|
35 |
|
|
|
36 |
# if the current verbosity is less than the minimum |
|
|
37 |
# verbosity needed for this message to print, |
|
|
38 |
# don't bother printing it |
|
|
39 |
if settings.VERBOSITY < v: |
|
|
40 |
return |
|
|
41 |
|
|
|
42 |
msg = _msg(msg, code=code) |
|
|
43 |
|
|
|
44 |
print(msg) |
|
|
45 |
|
|
|
46 |
if settings.LOG_FILENAME is not None: |
|
|
47 |
log_path = settings.LOG_FOLDER + "/" + settings.LOG_FILENAME |
|
|
48 |
|
|
|
49 |
if not os.path.isdir(settings.LOG_FOLDER): |
|
|
50 |
os.mkdir(settings.LOG_FOLDER) |
|
|
51 |
|
|
|
52 |
with open(log_path, "a") as logfile: |
|
|
53 |
logfile.write(msg) |
|
|
54 |
|
|
|
55 |
logfile.close() |
|
|
56 |
|
|
|
57 |
# @jit(nopython=True, fastmath=True, debug=True) |
|
|
58 |
def update(msg, v): |
|
|
59 |
_log(msg, code=1, v=v) |
|
|
60 |
|
|
|
61 |
# @jit(nopython=True, fastmath=True, debug=True) |
|
|
62 |
def warn(msg, v): |
|
|
63 |
_log(msg, code=2, v=v) |
|
|
64 |
|
|
|
65 |
# @jit(nopython=True, fastmath=True, debug=True) |
|
|
66 |
def error(msg, v): |
|
|
67 |
_log(msg, code=3, v=v) |