Add early logging setup to catch Qt init problems

This commit is contained in:
Petr Mrázek 2023-06-01 09:49:45 +02:00
parent 5cbcbc6be9
commit dd18819c6a
3 changed files with 65 additions and 52 deletions

View File

@ -79,14 +79,6 @@
#include <Secrets.h>
#if defined Q_OS_WIN32
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#include <stdio.h>
#endif
#define STRINGIFY(x) #x
#define TOSTRING(x) STRINGIFY(x)
@ -162,30 +154,6 @@ QString getIdealPlatform(QString currentPlatform) {
Application::Application(int &argc, char **argv) : QApplication(argc, argv)
{
#if defined Q_OS_WIN32
// attach the parent console
if(AttachConsole(ATTACH_PARENT_PROCESS))
{
// if attach succeeds, reopen and sync all the i/o
if(freopen("CON", "w", stdout))
{
std::cout.sync_with_stdio();
}
if(freopen("CON", "w", stderr))
{
std::cerr.sync_with_stdio();
}
if(freopen("CON", "r", stdin))
{
std::cin.sync_with_stdio();
}
auto out = GetStdHandle (STD_OUTPUT_HANDLE);
DWORD written;
const char * endline = "\n";
WriteConsole(out, endline, strlen(endline), &written, NULL);
consoleAttached = true;
}
#endif
setOrganizationName(BuildConfig.LAUNCHER_NAME);
setOrganizationDomain(BuildConfig.LAUNCHER_DOMAIN);
setApplicationName(BuildConfig.LAUNCHER_NAME);
@ -1096,17 +1064,6 @@ Application::~Application()
{
// Shut down logger by setting the logger function to nothing
qInstallMessageHandler(nullptr);
#if defined Q_OS_WIN32
// Detach from Windows console
if(consoleAttached)
{
fclose(stdout);
fclose(stdin);
fclose(stderr);
FreeConsole();
}
#endif
}
void Application::messageReceived(const QByteArray& message)

View File

@ -206,11 +206,6 @@ private:
QString m_rootPath;
Status m_status = Application::StartingUp;
#if defined Q_OS_WIN32
// used on Windows to attach the standard IO streams
bool consoleAttached = false;
#endif
// FIXME: attach to instances instead.
struct InstanceXtras {
InstanceWindow * window = nullptr;

View File

@ -9,6 +9,26 @@
#include <chrono>
#endif
#if defined Q_OS_WIN32
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#endif
#include <iostream>
namespace {
void earlyDebugOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
const char *levels = "DWCFIS";
const QString format("EARLY %1 %2\n");
QString out = format.arg(levels[type]).arg(msg);
QTextStream(stderr) << out.toLocal8Bit();
fflush(stderr);
}
}
int main(int argc, char *argv[])
{
#ifdef BREAK_INFINITE_LOOP
@ -24,9 +44,40 @@ int main(int argc, char *argv[])
return 42;
#endif
#if defined Q_OS_WIN32
// used on Windows to attach the standard IO streams
bool consoleAttached = false;
// attach the parent console
if(AttachConsole(ATTACH_PARENT_PROCESS))
{
// if attach succeeds, reopen and sync all the i/o
if(freopen("CON", "w", stdout))
{
std::cout.sync_with_stdio();
}
if(freopen("CON", "w", stderr))
{
std::cerr.sync_with_stdio();
}
if(freopen("CON", "r", stdin))
{
std::cin.sync_with_stdio();
}
auto out = GetStdHandle (STD_OUTPUT_HANDLE);
DWORD written;
const char * endline = "\n";
WriteConsole(out, endline, strlen(endline), &written, NULL);
consoleAttached = true;
}
#endif
qInstallMessageHandler(earlyDebugOutput);
// initialize Qt
Application app(argc, argv);
int result = 1;
switch (app.status())
{
case Application::StartingUp:
@ -44,12 +95,22 @@ int main(int argc, char *argv[])
Q_INIT_RESOURCE(OSX);
Q_INIT_RESOURCE(iOS);
Q_INIT_RESOURCE(flat);
return app.exec();
result = app.exec();
}
case Application::Failed:
return 1;
result = 1;
case Application::Succeeded:
return 0;
result = 0;
}
return 1;
#if defined Q_OS_WIN32
// Detach from Windows console
if(consoleAttached)
{
fclose(stdout);
fclose(stdin);
fclose(stderr);
FreeConsole();
}
#endif
return result;
}