armory/blender/arm/log.py

71 lines
1.7 KiB
Python
Raw Permalink Normal View History

import platform
2020-06-08 12:26:17 +02:00
DEBUG = 36
INFO = 37
2021-02-19 17:25:42 +01:00
WARN = 35
ERROR = 31
2020-06-08 12:26:17 +02:00
if platform.system() == "Windows":
HAS_COLOR_SUPPORT = platform.release() == "10"
if HAS_COLOR_SUPPORT:
# Enable ANSI codes. Otherwise, the ANSI sequences might not be
# evaluated correctly for the first colored print statement.
import ctypes
kernel32 = ctypes.windll.kernel32
# -11: stdout
handle_out = kernel32.GetStdHandle(-11)
console_mode = ctypes.c_long()
kernel32.GetConsoleMode(handle_out, ctypes.byref(console_mode))
# 0b100: ENABLE_VIRTUAL_TERMINAL_PROCESSING, enables ANSI codes
# see https://docs.microsoft.com/en-us/windows/console/setconsolemode
console_mode.value |= 0b100
kernel32.SetConsoleMode(handle_out, console_mode)
else:
HAS_COLOR_SUPPORT = True
2017-03-15 12:30:14 +01:00
info_text = ''
num_warnings = 0
num_errors = 0
2016-10-19 13:28:06 +02:00
2021-03-21 01:26:05 +01:00
def clear(clear_warnings=False, clear_errors=False):
global info_text, num_warnings, num_errors
2017-03-15 12:30:14 +01:00
info_text = ''
if clear_warnings:
num_warnings = 0
2021-03-21 01:26:05 +01:00
if clear_errors:
num_errors = 0
2016-10-19 13:28:06 +02:00
def format_text(text):
return (text[:80] + '..') if len(text) > 80 else text # Limit str size
2016-11-12 18:30:39 +01:00
2020-07-07 22:43:20 +02:00
def log(text, color=None):
if HAS_COLOR_SUPPORT and color is not None:
2020-06-08 12:26:17 +02:00
csi = '\033['
2020-07-07 22:43:20 +02:00
text = csi + str(color) + 'm' + text + csi + '0m'
2016-10-19 13:28:06 +02:00
print(text)
2020-06-08 12:26:17 +02:00
def debug(text):
2020-07-07 22:43:20 +02:00
log(text, DEBUG)
2020-06-08 12:26:17 +02:00
def info(text):
global info_text
2020-07-07 22:43:20 +02:00
log(text, INFO)
2017-03-15 12:30:14 +01:00
info_text = format_text(text)
2016-11-05 20:57:04 +01:00
2020-06-08 12:26:17 +02:00
def print_warn(text):
2021-02-19 16:53:01 +01:00
log('WARNING: ' + text, WARN)
2020-06-08 12:26:17 +02:00
2016-11-05 20:57:04 +01:00
def warn(text):
global num_warnings
num_warnings += 1
2020-06-08 12:26:17 +02:00
print_warn(text)
def error(text):
global num_errors
num_errors += 1
2020-07-07 22:43:20 +02:00
log('ERROR: ' + text, ERROR)