-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathqpc_logging.py
101 lines (71 loc) · 2.41 KB
/
qpc_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
import os
import sys
import platform
from qpc_args import args
from enum import Enum
_win32_legacy_con = False
_win32_handle = None
if os.name == "nt":
if platform.release().startswith("10"):
# hack to enter virtual terminal mode,
# could do it properly, but that's a lot of lines and this works just fine
import subprocess
subprocess.call('', shell=True)
else:
import ctypes
_win32_handle = ctypes.windll.kernel32.GetStdHandle(-11)
_win32_legacy_con = True
class Color(Enum):
if _win32_legacy_con:
RED = "4"
DGREEN = "2"
GREEN = "10"
YELLOW = "6"
BLUE = "1"
MAGENTA = "13"
CYAN = "3" # or 9
DEFAULT = "7"
else: # ansi escape chars
RED = "\033[0;31m"
DGREEN = "\033[0;32m"
GREEN = "\033[1;32m"
YELLOW = "\033[0;33m"
BLUE = "\033[0;34m"
MAGENTA = "\033[1;35m"
CYAN = "\033[0;36m"
DEFAULT = "\033[0m"
class Severity(Enum):
WARNING = Color.YELLOW
ERROR = Color.RED
WARNING_COUNT = 0
def warning(*text):
warning_no_line(*text[:-1], text[-1] + "\n")
def warning_no_line(*text):
if not args.hide_warnings:
_print_severity(Severity.WARNING, "\n ", *text)
global WARNING_COUNT
WARNING_COUNT += 1
def error(*text):
_print_severity(Severity.ERROR, "\n ", *text, "\n")
quit(1)
def verbose(*text):
if args.verbose:
print("".join(text))
def verbose_color(color: Color, *text):
if args.verbose:
print_color(color, "".join(text))
def _print_severity(level: Severity, spacing: str, *text):
print_color(level.value, f"[{level.name}] {spacing.join(text)}")
def win32_set_fore_color(color: int):
if not ctypes.windll.kernel32.SetConsoleTextAttribute(_win32_handle, color):
print(f"[ERROR] WIN32 Changing Colors Failed, Error Code: {str(ctypes.GetLastError())},"
f" color: {color}, handle: {str(_win32_handle)}")
def stdout_color(color: Color, *text):
if _win32_legacy_con:
win32_set_fore_color(int(color.value))
sys.stdout.write("".join(text))
win32_set_fore_color(int(Color.DEFAULT.value))
else:
sys.stdout.write(color.value + "".join(text) + Color.DEFAULT.value)
def print_color(color: Color, *text):
stdout_color(color, *text, "\n")