forked from K0lb3/UnityPy
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcolor_logging.py
More file actions
107 lines (96 loc) · 4.23 KB
/
Copy pathcolor_logging.py
File metadata and controls
107 lines (96 loc) · 4.23 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
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
import builtins
DEFAULT_FILE = ".\\unitypy_log.txt"
LOG_FORMAT = "%(asctime)s (%(name)s): %(levelname)s - %(message)s"
NO_COLORAMA = False
try:
from colorama import Fore, Style, just_fix_windows_console
just_fix_windows_console()
except:
NO_COLORAMA = True
from . import config
# NOTE: we use ENABLE_LOGGING_MODULE = 0 to better control
# console output since logging only uses full lines
unitypy_logger = None
if config.ENABLE_LOGGING_MODULE:
import logging
unitypy_logger = logging.getLogger("UnityPy")
unitypy_logger.setLevel(logging.DEBUG if config.DEBUG else logging.INFO)
if config.ENABLE_LOGGING_MODULE == 2:
file_handler = logging.FileHandler(DEFAULT_FILE, 'w', encoding="utf-8")
file_handler.setFormatter(logging.Formatter(LOG_FORMAT))
for h in unitypy_logger.handlers[:]:
unitypy_logger.removeHandler(h)
unitypy_logger.addHandler(file_handler)
else:
if not NO_COLORAMA:
class CustomFormatter(logging.Formatter):
grey = Style.DIM
yellow = Fore.YELLOW
red = Fore.RED
reset = Style.RESET_ALL
format = LOG_FORMAT
FORMATS = {
logging.DEBUG: grey + format + reset,
logging.INFO: grey + format + reset,
logging.WARNING: yellow + format + reset,
logging.ERROR: red + format + reset,
logging.CRITICAL: bold_red + format + reset
}
def format(self, record):
log_fmt = self.FORMATS.get(record.levelno, self.format)
formatter = logging.Formatter(log_fmt)
return formatter.format(record)
for h in unitypy_logger.handlers[:]:
unitypy_logger.removeHandler(h)
stream_handler = logging.StreamHandler()
stream_handler.setFormatter(CustomFormatter())
unitypy_logger.addHandler(stream_handler)
def print_info(text1, text2='', end='\n'):
"""Prints an info text, the first parameter is a dimmed string, the second is a white prefix"""
if config.ENABLE_LOGGING_MODULE:
unitypy_logger.info(str(text2) + str(text1))
else:
print(str(text2) + str(text1), end=end)
def print_debug(text1, text2='', end='\n'):
"""Prints an info text, the first parameter is a dimmed string, the second is a white prefix"""
if config.ENABLE_LOGGING_MODULE:
unitypy_logger.debug(str(text2) + str(text1))
else:
if not config.DEBUG: return
if NO_COLORAMA:
print(str(text2) + str(text1), end=end)
else:
print(str(text2) + (Style.DIM + str(text1) + Style.RESET_ALL), end=end)
def print_error(text1, text2='', end='\n'):
"""Prints an error text, the first parameter is a red string, the second is a white prefix"""
if config.ENABLE_LOGGING_MODULE:
unitypy_logger.error(str(text2) + str(text1))
else:
if NO_COLORAMA:
print(str(text2) + str(text1), end=end)
else:
print(str(text2) + (Fore.RED + str(text1) + Style.RESET_ALL), end=end)
def print_exception(text1, text2='', end='\n'):
"""Prints an error text, the first parameter is a red string, the second is a white prefix"""
if config.ENABLE_LOGGING_MODULE:
unitypy_logger.exception(str(text2) + str(text1))
else:
if NO_COLORAMA:
print(str(text2) + str(text1), end=end)
else:
print(str(text2) + (Fore.RED + str(text1) + Style.RESET_ALL), end=end)
def print_warning(text1, text2='', end='\n'):
"""Prints a warning text, the first parameter is a yellow string, the second is a white prefix"""
if config.ENABLE_LOGGING_MODULE:
unitypy_logger.warning(str(text2) + str(text1))
else:
if NO_COLORAMA:
print(str(text2) + str(text1), end=end)
else:
print(str(text2) + (Fore.YELLOW + str(text1) + Style.RESET_ALL), end=end)
# HACK: This is so we needn't export them in every .py file of the module
builtins.print_info = print_info
builtins.print_debug = print_debug
builtins.print_exception = print_exception
builtins.print_error = print_error
builtins.print_warning = print_warning