feat: redirect qt logs to nim-chronicles

This commit is contained in:
Igor Sirotin 2024-07-04 12:38:28 +01:00
parent 7146d5af5b
commit 264f92747f
4 changed files with 48 additions and 27 deletions

View File

@ -85,6 +85,28 @@ proc ensureDirectories*(dataDir, tmpDir, logDir: string) =
createDir(tmpDir)
createDir(logDir)
proc logHandlerCallback(messageType: cint, message: cstring, category: cstring, file: cstring, function: cstring, line: cint) {.cdecl, exportc.} =
logScope:
topics = "qt"
text = $message
file = $file & ":" & $line
function = $function
category = $category
case int(messageType):
of 0: # QtDebugMsg
debug "qt message"
of 1: # QtWarningMsg
warn "qt warning"
of 2: # QtCriticalMsg
error "qt error"
of 3: # QtFatalMsg
fatal "qt fatal error"
of 4: # QtInfoMsg
info "qt message"
else:
warn "qt message of unknown type", messageType = int(messageType)
proc mainProc() =
when defined(macosx) and defined(arm64):
@ -160,6 +182,7 @@ proc mainProc() =
app.icon(app.applicationDirPath & statusAppIconPath)
prepareLogging()
installMessageHandler(logHandlerCallback)
singletonInstance.engine.addImportPath("qrc:/")
singletonInstance.engine.addImportPath("qrc:/./imports")
@ -203,9 +226,7 @@ proc mainProc() =
keycardServiceQObjPointer = cast[pointer](appController.keycardService.vptr)
setupRemoteSignalsHandling()
info fmt("Version: {APP_VERSION}")
info fmt("Commit: {GIT_COMMIT}")
info "Current date:", currentDateTime=now()
info "app info", version=APP_VERSION, commit=GIT_COMMIT, currentDateTime=now()
info "starting application controller..."
appController.start()

View File

@ -64,6 +64,10 @@ DOS_API void DOS_CALL dos_qtwebview_initialize(void);
DOS_API void DOS_CALL dos_qguiapplication_try_enable_threaded_renderer();
typedef void (DOS_CALL *MessageHandlerCallback)(int type, const char* data, const char* category, const char* file, const char* func, int line);
DOS_API void dos_installMessageHandler(MessageHandlerCallback logHandler);
/// \brief Create a QGuiApplication
/// \note The created QGuiApplication should be freed by calling dos_qguiapplication_delete()
DOS_API void DOS_CALL dos_qguiapplication_create();

View File

@ -187,33 +187,30 @@ void dos_qguiapplication_try_enable_threaded_renderer()
qputenv("QSG_RENDER_LOOP", QByteArrayLiteral("threaded"));
}
static MessageHandlerCallback messageHandlerCallback = nullptr;
// This catches the QT and QML logs and outputs them.
// This is necessary on Windows, because otherwise we do not get any logs at all
void myMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
QByteArray localMsg = msg.toLocal8Bit();
const char *file = context.file ? context.file : "";
const char *function = context.function ? context.function : "";
switch (type) {
case QtDebugMsg:
fprintf(stderr, "Debug: %s (%s:%u, %s)\n", localMsg.constData(), file, context.line, function);
break;
case QtInfoMsg:
fprintf(stderr, "Info: %s (%s:%u, %s)\n", localMsg.constData(), file, context.line, function);
break;
case QtWarningMsg:
fprintf(stderr, "Warning: %s (%s:%u, %s)\n", localMsg.constData(), file, context.line, function);
break;
case QtCriticalMsg:
fprintf(stderr, "Critical: %s (%s:%u, %s)\n", localMsg.constData(), file, context.line, function);
break;
case QtFatalMsg:
fprintf(stderr, "Fatal: %s (%s:%u, %s)\n", localMsg.constData(), file, context.line, function);
break;
default:
fprintf(stderr, "Default: %s (%s:%u, %s)\n", localMsg.constData(), file, context.line, function);
break;
if (messageHandlerCallback == nullptr) {
return;
}
QByteArray localMsg = msg.toLocal8Bit();
const char* category = context.category ? context.category : "";
const char* file = context.file ? context.file : "";
const char* function = context.function ? context.function : "";
int messageType = int(type);
const char* message = localMsg.constData();
messageHandlerCallback(messageType, message, category, file, function, context.line);
}
void dos_installMessageHandler(MessageHandlerCallback messageHandler)
{
messageHandlerCallback = messageHandler;
qInstallMessageHandler(myMessageOutput);
}
void dos_qguiapplication_create()
@ -240,7 +237,6 @@ void dos_qguiapplication_create()
// We increase js stack size to prevent "Maximum call stack size exceeded" on UI loading.
qputenv("QV4_JS_MAX_STACK_SIZE", "10485760");
qputenv("QT_QUICK_CONTROLS_HOVER_ENABLED", "1");
qInstallMessageHandler(myMessageOutput);
new QGuiApplication(argc, argv);
#ifdef Q_OS_MACOS

2
vendor/nimqml vendored

@ -1 +1 @@
Subproject commit 20d4db12bb40dcf1c549ab3149ca0b4b791e90b7
Subproject commit 10f8dc4ca2d67acbf6bc7e0f49f4c0c8ff2737a0