-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathlogging.py
119 lines (102 loc) · 4.16 KB
/
logging.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
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
115
116
117
118
119
# Copyright vanous
#
# This file is part of BlenderDMX.
#
# BlenderDMX 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.
#
# BlenderDMX 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 <https://www.gnu.org/licenses/>.
import logging
import os
from logging.handlers import RotatingFileHandler
import bpy
class DMX_Log:
def __init__(self):
super(DMX_Log, self).__init__()
self.log = None
dmx = bpy.context.window_manager.dmx
self.logging_filter_dmx_in = dmx.logging_filter_dmx_in
self.logging_filter_mvr_xchange = dmx.logging_filter_mvr_xchange
self.logging_filter_fixture = dmx.logging_filter_fixture
@staticmethod
def enable(level):
log = logging.getLogger("blenderDMX")
log.setLevel(level)
# file log
class CustomStreamHandler(logging.StreamHandler):
def emit(self, record):
super().emit(record)
# self.stream.write("\n")
# this allow us to add a break between log lines, to make long logs more readable
self.flush()
# console log
log_formatter = logging.Formatter(
fmt="%(asctime)s.%(msecs)03d %(levelname)s %(module)s - %(funcName)s %(lineno)d: %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
if not log.handlers: # logger is global, prevent duplicate registrations
dmx = bpy.context.scene.dmx
ADDON_PATH = dmx.get_addon_path()
path = os.path.join(ADDON_PATH, "blenderDMX.log")
console_log_handler = CustomStreamHandler()
console_log_handler.setFormatter(log_formatter)
file_log_handler = RotatingFileHandler(
path, backupCount=5, maxBytes=8000000, encoding="utf-8", mode="a"
)
file_log_handler.setFormatter(log_formatter)
log.addHandler(file_log_handler)
log.addHandler(console_log_handler)
DMX_Log.log = log
DMX_Log.update_filters()
@staticmethod
def set_level(level):
DMX_Log.log.critical(f"Update logging level to {level}")
DMX_Log.log.setLevel(level)
@staticmethod
def update_filters():
DMX_Log.log.debug("Update logging filters")
dmx = bpy.context.window_manager.dmx
log = DMX_Log.log
# cache these:
DMX_Log.logging_filter_dmx_in = dmx.logging_filter_dmx_in
DMX_Log.logging_filter_mvr_xchange = dmx.logging_filter_mvr_xchange
DMX_Log.logging_filter_fixture = dmx.logging_filter_fixture
for filter in log.filters:
log.filters.remove(filter)
def custom_filter(record):
if DMX_Log.logging_filter_dmx_in:
if any(
[x in record.filename for x in ["data", "artnet", "acn", "logging"]]
):
return True
if DMX_Log.logging_filter_mvr_xchange:
if any(
[
x in record.pathname
for x in [
"mdns",
"mvr_xchange",
"mvrx_protocol",
"mvrxchange",
"logging",
]
]
):
return True
if DMX_Log.logging_filter_fixture:
if any([x in record.filename for x in ["gdtf", "fixture"]]):
return True
return not (
DMX_Log.logging_filter_dmx_in
or DMX_Log.logging_filter_mvr_xchange
or DMX_Log.logging_filter_fixture
)
log.addFilter(custom_filter)