forked from william-murray1204/stable-diffusion-cpp-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_logger.py
More file actions
42 lines (31 loc) · 940 Bytes
/
_logger.py
File metadata and controls
42 lines (31 loc) · 940 Bytes
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
import sys
import ctypes
import logging
import stable_diffusion_cpp
# enum sd_log_level_t {
# SD_LOG_DEBUG = 0,
# SD_LOG_INFO = 1,
# SD_LOG_WARN = 2,
# SD_LOG_ERROR = 3
# };
SD_LOG_LEVEL_TO_LOGGING_LEVEL = {
0: logging.ERROR,
1: logging.WARNING,
2: logging.INFO,
3: logging.DEBUG,
}
logger = logging.getLogger("stable-diffusion-cpp-python")
@stable_diffusion_cpp.sd_log_callback
def sd_log_callback(
level: int,
text: bytes,
data: ctypes.c_void_p,
):
if logger.level <= SD_LOG_LEVEL_TO_LOGGING_LEVEL[level]:
print(text.decode("utf-8"), end="", flush=True, file=sys.stderr)
stable_diffusion_cpp.sd_set_log_callback(sd_log_callback, ctypes.c_void_p(0))
def set_verbose(verbose: bool):
logger.setLevel(logging.DEBUG if verbose else logging.ERROR)
def log_event(level: int, message: str):
if logger.level <= SD_LOG_LEVEL_TO_LOGGING_LEVEL[level]:
print(message)