qt support

This commit is contained in:
mrbesen 2022-10-26 21:16:56 +02:00
parent 027f901dbe
commit 9a43d50969
Signed by: MrBesen
GPG Key ID: 596B2350DCD67504
3 changed files with 100 additions and 1 deletions

51
Log.cpp
View File

@ -10,8 +10,29 @@
#include <mutex>
#endif
#if LOG_ENABLEQT == 1
#include <QString>
#endif
namespace Log {
#if LOG_ENABLEQT == 1
void qtMessageHandler(QtMsgType type, const QMessageLogContext& context, const QString& message) {
// convert type to Log::Level
Log::Level lvl = Log::Level::off;
switch(type) {
case QtDebugMsg: lvl = Log::Level::debug; break;
case QtInfoMsg: lvl = Log::Level::info; break;
case QtWarningMsg: lvl = Log::Level::warn; break;
case QtCriticalMsg: lvl = Log::Level::error; break;
case QtFatalMsg: lvl = Log::Level::fatal; break;
}
Entry(lvl, context) << message.toStdString();
}
#endif
/*
* abstract class Output
* default implementations
@ -147,6 +168,13 @@ void log(Level lvl, std::stringbuf* strb) {
/*
* class Entry
*/
#if LOG_ENABLEQT == 1
Entry::Entry(Level lvl, const QMessageLogContext& context) : lvl{lvl}, context{&context} {
for(const auto& metafunc : entryMetaFunctions) {
metafunc(ss, *this);
}
}
#endif
Entry::Entry(Level lvl) : lvl{lvl} {
for(const auto& metafunc : entryMetaFunctions) {
@ -182,6 +210,16 @@ std::ostream& defaultEntryMetaLevel(std::ostream& os, const Entry& e) {
return os << LevelTag[static_cast<int>(e.getLevel())];
}
#if LOG_ENABLEQT == 1
std::ostream& defaultEntryMetaQtContext(std::ostream& os, const Entry& e) {
if (e.getContext() && e.getContext()->file) {
const std::string file = e.getContext()->file;
return os << file << ':' << e.getContext()->line;
}
return os << "-:-";
}
#endif
void init() {
// add default console logger
if (outputs.empty()) {
@ -196,11 +234,24 @@ void init() {
entryMetaFunctions = {
defaultEntryMetaTime,
defaultEntryMetaLevel,
#if LOG_ENABLEQT == 1
space,
defaultEntryMetaQtContext,
#endif
space
};
#if LOG_ENABLEQT == 1
qInstallMessageHandler( &Log::qtMessageHandler );
#endif
}
void stop() {
#if LOG_ENABLEQT == 1
qInstallMessageHandler( nullptr );
#endif
outputs.clear();
}

47
Log.h
View File

@ -9,9 +9,45 @@
#define LOG_USEMUTEX 0
#endif
#ifndef LOG_ENABLEQT
#define LOG_ENABLEQT 0
#endif
#if LOG_ENABLEQT == 1
#include <QMessageLogContext>
#include <QDebug>
#endif
#if LOG_ENABLEQT == 1
template<typename T>
QDebug operator<<(QDebug dbg, const T& str) {
std::ostringstream stream;
stream << str;
QString conv = QString::fromStdString(stream.str());
QDebugStateSaver saver(dbg);
dbg << conv;
return dbg;
}
/*
// string specialisation, not allowed, because multiple definitions of the same operator are not allowed
template<>
QDebug operator<<(QDebug dbg, const std::string& str) {
QDebugStateSaver saver(dbg);
QString conv = QString::fromStdString(str);
dbg << conv;
return dbg;
}
*/
#endif
namespace Log {
enum class Level { off = 0, fatal, error, warn, note, info, debug, trace };
#if LOG_ENABLEQT == 1
void qtMessageHandler(QtMsgType type, const QMessageLogContext& context, const QString& message);
#endif
// set up the logger with a ConsoleOutput
void init();
// close all output streams
@ -30,6 +66,11 @@ class Entry {
private:
std::stringstream ss;
const Level lvl;
#if LOG_ENABLEQT == 1
const QMessageLogContext* context = nullptr;
Entry(Level lvl, const QMessageLogContext&);
friend void qtMessageHandler(QtMsgType type, const QMessageLogContext& context, const QString& message);
#endif
void addMetadataHeader();
@ -45,6 +86,12 @@ public:
return lvl;
}
#if LOG_ENABLEQT == 1
constexpr const QMessageLogContext* getContext() const {
return context;
}
#endif
using MetaFunction = std::function<std::ostream&(std::ostream&, const Entry&)>;
template <typename T> Entry& operator<<(T&& msg) {

View File

@ -1,7 +1,8 @@
TARGET = Log.o
TEST = test
LOG_USEMUTEX ?= 0
CXXFLAGS = -O2 -g -std=c++11 -Wall -Wextra -pedantic-errors -MMD -DLOG_USEMUTEX=$(LOG_USEMUTEX)
LOG_ENABLEQT ?= 0
CXXFLAGS = -O2 -g -std=c++11 -Wall -Wextra -pedantic-errors -MMD -DLOG_USEMUTEX=$(LOG_USEMUTEX) -DLOG_ENABLEQT=$(LOG_ENABLEQT)
.PHONY: all clean