Skip to content

Commit

Permalink
Log Qt message handler messages
Browse files Browse the repository at this point in the history
  • Loading branch information
oblivioncth committed Nov 7, 2023
1 parent 9c77db2 commit 285f3d9
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ endif()

include(OB/FetchQx)
ob_fetch_qx(
REF "757ed90239489811311c91f74ab842c2628b2e8b"
REF "708fbeb800d0591976e83c6b7a69f844a734b68c"
COMPONENTS
${CLIFP_QX_COMPONENTS}
)
Expand Down
66 changes: 65 additions & 1 deletion app/src/kernel/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,34 @@ Core::Core(QObject* parent) :
mCriticalErrorOccurred(false),
mStatusHeading(u"Initializing"_s),
mStatusMessage(u"..."_s)
{}
{
establishCanonCore(*this); // Ignore return value as there should never be more than one Core with current design
}

//-Class Functions------------------------------------------------------------------------------------------------------
//Private:
bool Core::establishCanonCore(Core& cc)
{
if(!smDefaultMessageHandler)
smDefaultMessageHandler = qInstallMessageHandler(qtMessageHandler);

if(smCanonCore)
return false;

smCanonCore = &cc;
return true;
}

void Core::qtMessageHandler(QtMsgType type, const QMessageLogContext& context, const QString& msg)
{
// Log messages
if(smCanonCore && smCanonCore->isLogOpen())
smCanonCore->logQtMessage(type, context, msg);

// Defer to default behavior
if(smDefaultMessageHandler)
smDefaultMessageHandler(type, context, msg);
}

//-Instance Functions-------------------------------------------------------------
//Private:
Expand Down Expand Up @@ -225,6 +252,41 @@ Qx::Error Core::searchAndFilterEntity(QUuid& returnBuffer, QString name, bool ex
}
}

void Core::logQtMessage(QtMsgType type, const QMessageLogContext& context, const QString& msg)
{
#if defined QT_NO_MESSAGELOGCONTEXT || !defined QT_MESSAGELOGCONTEXT
QString msgWithContext = msg;
#else
static const QString cTemplate = u"(%1:%2, %3) %4"_s;
static const QString unk = u"Unk."_s;
QString msgWithContext = cTemplate.arg(
context.file ? QString(context.file) : unk,
context.line >= 0 ? QString::number(context.line) : unk,
context.function ? QString(context.function) : unk,
msg
);
#endif

switch (type)
{
case QtDebugMsg:
logEvent(NAME, u"SYSTEM DEBUG) "_s + msgWithContext);
break;
case QtInfoMsg:
logEvent(NAME, u"SYSTEM INFO) "_s + msgWithContext);
break;
case QtWarningMsg:
logError(NAME, CoreError(CoreError::InternalError, msgWithContext, Qx::Warning));
break;
case QtCriticalMsg:
logError(NAME, CoreError(CoreError::InternalError, msgWithContext, Qx::Err));
break;
case QtFatalMsg:
logError(NAME, CoreError(CoreError::InternalError, msgWithContext, Qx::Critical));
break;
}
}

//Public:
Qx::Error Core::initialize(QStringList& commandLine)
{
Expand Down Expand Up @@ -734,6 +796,8 @@ Qx::Error Core::enqueueDataPackTasks(const Fp::GameData& gameData)
void Core::enqueueSingleTask(Task* task) { mTaskQueue.push(task); logTask(NAME, task); }
void Core::clearTaskQueue() { mTaskQueue = {}; }

bool Core::isLogOpen() const { return mLogger->isOpen(); }

void Core::logCommand(QString src, QString commandName)
{
Qx::IoOpReport logReport = mLogger->recordGeneralEvent(src, COMMAND_LABEL.arg(commandName));
Expand Down
14 changes: 14 additions & 0 deletions app/src/kernel/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class QX_ERROR_TYPE(CoreError, "CoreError", 1200)
enum Type
{
NoError,
InternalError,
InvalidOptions,
TitleNotFound,
TooManyResults,
Expand All @@ -48,6 +49,7 @@ class QX_ERROR_TYPE(CoreError, "CoreError", 1200)
private:
static inline const QHash<Type, QString> ERR_STRINGS{
{NoError, u""_s},
{InternalError, u"Internal system error."_s},
{InvalidOptions, u"Invalid global options provided."_s},
{TitleNotFound, u"Could not find the title in the Flashpoint database."_s},
{TooManyResults, u"More results than can be presented were returned in a search."_s},
Expand Down Expand Up @@ -233,6 +235,10 @@ class Core : public QObject
// Meta
static inline const QString NAME = u"core"_s;

// Qt Message Handling
static inline constinit QtMessageHandler smDefaultMessageHandler = nullptr;
static inline QPointer<Core> smCanonCore;

//-Instance Variables------------------------------------------------------------------------------------------------------
private:
// Handles
Expand All @@ -255,6 +261,12 @@ class Core : public QObject
public:
explicit Core(QObject* parent);

//-Class Functions------------------------------------------------------------------------------------------------------
private:
// Qt Message Handling - NOTE: Storing a static instance of core is required due to the C-function pointer interface of qInstallMessageHandler()
static bool establishCanonCore(Core& cc);
static void qtMessageHandler(QtMsgType type, const QMessageLogContext& context, const QString& msg);

//-Instance Functions------------------------------------------------------------------------------------------------------
private:
bool isActionableOptionSet(const QCommandLineParser& clParser) const;
Expand All @@ -263,6 +275,7 @@ class Core : public QObject

// Helper
Qx::Error searchAndFilterEntity(QUuid& returnBuffer, QString name, bool exactName, QUuid parent = QUuid());
void logQtMessage(QtMsgType type, const QMessageLogContext& context, const QString& msg);

public:
// Setup
Expand All @@ -286,6 +299,7 @@ class Core : public QObject
void clearTaskQueue(); // TODO: See if this can be done away with, it's awkward (i.e. not fill queue in first place). Think I tried to before though.

// Notifications/Logging
bool isLogOpen() const;
void logCommand(QString src, QString commandName);
void logCommandOptions(QString src, QString commandOptions);
void logError(QString src, Qx::Error error);
Expand Down

0 comments on commit 285f3d9

Please sign in to comment.