This commit is contained in:
parent
f9af0c34dd
commit
309b0fafb0
3 changed files with 132 additions and 34 deletions
|
@ -16,15 +16,15 @@
|
|||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
"""
|
||||
|
||||
from PySide6.QtCore import QtMsgType, qInstallMessageHandler
|
||||
from PySide6.QtCore import QtMsgType, qInstallMessageHandler, QMessageLogContext
|
||||
from math import ceil, log10
|
||||
from sourcemap import loads
|
||||
from os import path
|
||||
|
||||
CURRENT_PATH = path.dirname(path.realpath(__file__))
|
||||
SOURECMAP_PATH = path.realpath(f"{CURRENT_PATH}/../qml/eu/ad5001/LogarithmPlotter/js/index.mjs.map")
|
||||
SOURCEMAP_PATH = path.realpath(f"{CURRENT_PATH}/../qml/eu/ad5001/LogarithmPlotter/js/index.mjs.map")
|
||||
SOURCEMAP_INDEX = None
|
||||
|
||||
|
||||
class LOG_COLORS:
|
||||
GRAY = "\033[90m"
|
||||
BLUE = "\033[94m"
|
||||
|
@ -35,7 +35,6 @@ class LOG_COLORS:
|
|||
RESET = "\033[0m"
|
||||
|
||||
|
||||
|
||||
MODES = {
|
||||
QtMsgType.QtInfoMsg: ['info', LOG_COLORS.BLUE],
|
||||
QtMsgType.QtWarningMsg: ['warning', LOG_COLORS.ORANGE],
|
||||
|
@ -45,36 +44,59 @@ MODES = {
|
|||
|
||||
DEFAULT_MODE = ['debug', LOG_COLORS.GRAY]
|
||||
|
||||
def log_qt_debug(mode, context, message):
|
||||
|
||||
def map_javascript_source(source_file: str, line: str) -> tuple[str, str]:
|
||||
"""
|
||||
Parses and renders qt log messages.
|
||||
Maps a line from the compiled javascript to its source.
|
||||
"""
|
||||
if mode in MODES:
|
||||
mode = MODES[mode]
|
||||
else:
|
||||
mode = DEFAULT_MODE
|
||||
line = context.line
|
||||
source_file = context.file
|
||||
# Remove source and line from emssage
|
||||
if source_file is not None:
|
||||
if message.startswith(source_file):
|
||||
message = message[len(source_file) + 1:]
|
||||
source_file = source_file.split("/qml")[-1] # We're only interested in that part.
|
||||
if line is not None and message.startswith(str(line)):
|
||||
line_length = ceil(log10((line+1) if line > 0 else 1))
|
||||
message = message[line_length + 2:]
|
||||
# Check MJS
|
||||
if line is not None and source_file is not None and source_file.endswith("index.mjs"):
|
||||
try:
|
||||
try:
|
||||
if SOURCEMAP_INDEX is not None:
|
||||
token = SOURCEMAP_INDEX.lookup(line, 20)
|
||||
source_file = source_file[:-len("index.mjs")] + token.src
|
||||
line = token.src_line
|
||||
except IndexError:
|
||||
pass # Unable to find source, leave as is.
|
||||
print(f"{LOG_COLORS.INVERT}{mode[1]}[{mode[0].upper()}]{LOG_COLORS.RESET_INVERT} {message}{LOG_COLORS.RESET} ({context.function} at {source_file}:{line})")
|
||||
except IndexError:
|
||||
pass # Unable to find source, leave as is.
|
||||
return source_file, line
|
||||
|
||||
|
||||
def create_log_terminal_message(mode: QtMsgType, context: QMessageLogContext, message: str):
|
||||
"""
|
||||
Parses a qt log message and returns it.
|
||||
"""
|
||||
mode = MODES[mode] if mode in MODES else DEFAULT_MODE
|
||||
line = context.line
|
||||
source_file = context.file
|
||||
# Remove source and line from message
|
||||
if source_file is not None:
|
||||
if message.startswith(source_file):
|
||||
message = message[len(source_file) + 1:]
|
||||
source_file = "LogarithmPlotter/qml/" + source_file.split("/qml/")[-1] # We're only interested in that part.
|
||||
if line is not None and message.startswith(str(line)):
|
||||
line_length = ceil(log10((line + 1) if line > 0 else 1))
|
||||
message = message[line_length + 2:]
|
||||
# Check MJS
|
||||
if line is not None and source_file is not None and source_file.endswith("index.mjs"):
|
||||
source_file, line = map_javascript_source(source_file, line)
|
||||
prefix = f"{LOG_COLORS.INVERT}{mode[1]}[{mode[0].upper()}]{LOG_COLORS.RESET_INVERT}"
|
||||
message = message + LOG_COLORS.RESET
|
||||
context = f"{context.function} at {source_file}:{line}"
|
||||
return f"{prefix} {message} ({context})"
|
||||
|
||||
|
||||
def log_qt_debug(mode: QtMsgType, context: QMessageLogContext, message: str):
|
||||
"""
|
||||
Parses and renders qt log messages.
|
||||
"""
|
||||
print(create_log_terminal_message(mode, context, message))
|
||||
|
||||
|
||||
def setup():
|
||||
global SOURCEMAP_INDEX
|
||||
with open(SOURECMAP_PATH, "r") as f:
|
||||
SOURCEMAP_INDEX = loads(f.read())
|
||||
qInstallMessageHandler(log_qt_debug)
|
||||
try:
|
||||
with open(SOURCEMAP_PATH, "r") as f:
|
||||
from sourcemap import loads
|
||||
SOURCEMAP_INDEX = loads(f.read())
|
||||
except Exception as e:
|
||||
log_qt_debug(QtMsgType.QtWarningMsg, QMessageLogContext(),
|
||||
f"Could not setup JavaScript source mapper in logs: {repr(e)}")
|
||||
qInstallMessageHandler(log_qt_debug)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue