wip: refactor: better (maybe) logger

This commit is contained in:
2025-12-01 01:13:08 +01:00
parent fc3306ea55
commit c93c58a988
3 changed files with 97 additions and 128 deletions
+82 -85
View File
@@ -1,67 +1,28 @@
/* /*
* @Author: Uyanide pywang0608@foxmail.com * @Author: Uyanide pywang0608@foxmail.com
* @Date: 2025-08-07 01:12:37 * @Date: 2025-08-07 01:12:37
* @LastEditTime: 2025-08-07 22:24:27 * @LastEditTime: 2025-12-01 01:12:49
* @Description: Implementation of logger. * @Description: Implementation of logger.
*/ */
#include "logger.h" #include "logger.h"
#include <qnamespace.h>
#ifndef GENERAL_LOGGER_DISABLED
#include <unistd.h> #include <unistd.h>
#include <QCoreApplication> #include <QCoreApplication>
#include <QObject> #include <QDateTime>
#include <QMutex>
#include <QProcessEnvironment> #include <QProcessEnvironment>
#include <QString> #include <cstdio>
#include <QTextStream>
#include <QThreadPool>
Logger* Logger::instance(FILE* stream, Q_LOGGING_CATEGORY(logMain, "wallpaper.carousel")
GeneralLogger::LogIndent indent,
QObject* parent) {
static Logger* logger{};
if (!logger) {
if (!stream) {
stream = stderr; // Default to stderr if no stream provided
}
logger = new Logger(stream, indent, parent);
// Ensure logger runs in the main thread
logger->moveToThread(QCoreApplication::instance()->thread());
}
return logger;
}
Logger::Logger(FILE* stream, static FILE* g_logStream = stderr;
GeneralLogger::LogIndent indent, static GeneralLogger::LogIndent g_maxIndent = GeneralLogger::DETAIL;
QObject* parent) static bool g_isColored = false;
: QObject(parent), m_stream(stream), m_logStream(stream), m_indent(indent) { static QMutex g_logMutex;
connect(this,
&Logger::logSig,
this,
&Logger::_log,
Qt::AutoConnection);
}
void Logger::_log( static bool checkIsColored(FILE* stream) {
const QString& msg, if (!stream || !isatty(fileno(stream))) {
const QString& levelString,
const QString& levelColorString,
const QString& textColorString,
const GeneralLogger::LogIndent indent) {
if (indent > m_indent) return;
m_logStream << levelColorString << levelString << ' ';
for (qint32 i = 0; i < indent; i++) m_logStream << " ";
m_logStream << textColorString << msg << (textColorString.isEmpty() ? "\n" : "\033[0m\n");
m_logStream.flush();
}
bool Logger::isColored() {
const auto stream = Logger::instance()->m_stream;
if (!isatty(fileno(stream))) {
return false; return false;
} }
QProcessEnvironment env = QProcessEnvironment::systemEnvironment(); QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
@@ -72,44 +33,80 @@ bool Logger::isColored() {
return true; return true;
} }
#endif // GENERAL_LOGGER_DISABLED void myMessageOutput(QtMsgType type, const QMessageLogContext& context, const QString& msg) {
Q_UNUSED(context);
void GeneralLogger::info( QMutexLocker locker(&g_logMutex);
const QString& msg,
const GeneralLogger::LogIndent indent) { QString levelTag;
#ifndef GENERAL_LOGGER_DISABLED QString colorTag;
constexpr const char* colorInfoMsg[]{"\033[32m", "\033[0m", "\033[0m"}; QString colorText;
const auto color = Logger::isColored(); QString resetColor = g_isColored ? "\033[0m" : "";
emit Logger::instance() -> logSig(msg,
"[INFO]", switch (type) {
color ? "\033[92m" : "", case QtDebugMsg:
color ? colorInfoMsg[indent] : "", levelTag = "[DEBUG]";
indent); colorTag = g_isColored ? "\033[36m" : ""; // Cyan
#endif // GENERAL_LOGGER_DISABLED break;
case QtInfoMsg:
levelTag = "[INFO]";
colorTag = g_isColored ? "\033[92m" : ""; // Light Green
colorText = g_isColored ? "\033[32m" : ""; // Green
break;
case QtWarningMsg:
levelTag = "[WARN]";
colorTag = g_isColored ? "\033[93m" : ""; // Light Yellow
colorText = g_isColored ? "\033[33m" : ""; // Yellow
break;
case QtCriticalMsg:
levelTag = "[ERROR]";
colorTag = g_isColored ? "\033[91m" : ""; // Light Red
colorText = g_isColored ? "\033[31m" : ""; // Red
break;
case QtFatalMsg:
levelTag = "[FATAL]";
colorTag = g_isColored ? "\033[95m" : ""; // Magenta
break;
} }
void GeneralLogger::warn( if (g_logStream) {
const QString& msg, fprintf(g_logStream, "%s%s %s%s%s\n", qPrintable(colorTag), qPrintable(levelTag), qPrintable(colorText), qPrintable(msg), qPrintable(resetColor));
const GeneralLogger::LogIndent indent) { fflush(g_logStream);
#ifndef GENERAL_LOGGER_DISABLED }
const auto color = Logger::isColored();
emit Logger::instance() -> logSig(msg,
"[WARN]",
color ? "\033[93m" : "",
color ? "\033[33m" : "",
indent);
#endif // GENERAL_LOGGER_DISABLED
} }
void GeneralLogger::error( void Logger::init(FILE* stream, GeneralLogger::LogIndent maxIndent) {
const QString& msg, if (stream) {
const GeneralLogger::LogIndent indent) { g_logStream = stream;
#ifndef GENERAL_LOGGER_DISABLED }
const auto color = Logger::isColored(); g_maxIndent = maxIndent;
emit Logger::instance() -> logSig(msg, g_isColored = checkIsColored(g_logStream);
"[ERROR]",
color ? "\033[91m" : "", qInstallMessageHandler(myMessageOutput);
color ? "\033[31m" : "", }
indent);
#endif // GENERAL_LOGGER_DISABLED void Logger::setMaxIndent(GeneralLogger::LogIndent indent) {
g_maxIndent = indent;
}
GeneralLogger::LogIndent Logger::maxIndent() {
return g_maxIndent;
}
void GeneralLogger::info(const QString& msg, const GeneralLogger::LogIndent indent) {
if (indent > g_maxIndent) return;
QString indentedMsg = QString(" ").repeated(indent) + msg;
qCInfo(logMain).noquote() << indentedMsg;
}
void GeneralLogger::warn(const QString& msg, const GeneralLogger::LogIndent indent) {
if (indent > g_maxIndent) return;
QString indentedMsg = QString(" ").repeated(indent) + msg;
qCWarning(logMain).noquote() << indentedMsg;
}
void GeneralLogger::error(const QString& msg, const GeneralLogger::LogIndent indent) {
QString indentedMsg = QString(" ").repeated(indent) + msg;
qCCritical(logMain).noquote() << indentedMsg;
} }
+11 -37
View File
@@ -1,15 +1,17 @@
/* /*
* @Author: Uyanide pywang0608@foxmail.com * @Author: Uyanide pywang0608@foxmail.com
* @Date: 2025-08-05 10:43:31 * @Date: 2025-08-05 10:43:31
* @LastEditTime: 2025-08-07 21:26:09 * @LastEditTime: 2025-12-01 01:12:53
* @Description: A simple thread-safe logger for general use. * @Description: A simple thread-safe logger.
*/ */
#ifndef GENERAL_LOGGER_H #ifndef GENERAL_LOGGER_H
#define GENERAL_LOGGER_H #define GENERAL_LOGGER_H
#include <QObject> #include <QLoggingCategory>
#include <QString> #include <QString>
#include <QTextStream> #include <cstdio>
Q_DECLARE_LOGGING_CATEGORY(logMain)
namespace GeneralLogger { namespace GeneralLogger {
@@ -29,41 +31,13 @@ void error(const QString& msg,
const LogIndent indent = GENERAL); const LogIndent indent = GENERAL);
} // namespace GeneralLogger } // namespace GeneralLogger
#ifndef GENERAL_LOGGER_DISABLED class Logger {
class Logger : public QObject {
Q_OBJECT
public: public:
static Logger* instance(FILE* stream = nullptr, static void init(FILE* stream = stderr,
GeneralLogger::LogIndent indent = GeneralLogger::DETAIL, GeneralLogger::LogIndent maxIndent = GeneralLogger::DETAIL);
QObject* parent = nullptr);
static bool isColored(); static void setMaxIndent(GeneralLogger::LogIndent indent);
static GeneralLogger::LogIndent maxIndent();
private:
explicit Logger(FILE* stream,
GeneralLogger::LogIndent indent,
QObject* parent);
private slots:
void _log(const QString& msg,
const QString& levelString,
const QString& levelColorString,
const QString& textColorString,
const GeneralLogger::LogIndent indent);
private:
FILE* m_stream;
QTextStream m_logStream;
GeneralLogger::LogIndent m_indent;
signals:
void logSig(const QString& msg,
const QString& levelString,
const QString& levelColorString,
const QString& textColorString,
const GeneralLogger::LogIndent indent);
}; };
#endif // GENERAL_LOGGER_DISABLED
#endif // GENERAL_LOGGER_H #endif // GENERAL_LOGGER_H
+2 -4
View File
@@ -1,7 +1,7 @@
/* /*
* @Author: Uyanide pywang0608@foxmail.com * @Author: Uyanide pywang0608@foxmail.com
* @Date: 2025-08-05 00:37:58 * @Date: 2025-08-05 00:37:58
* @LastEditTime: 2025-08-07 22:21:29 * @LastEditTime: 2025-12-01 01:07:34
* @Description: Entry point. * @Description: Entry point.
*/ */
#include <qapplication.h> #include <qapplication.h>
@@ -27,9 +27,7 @@ static QString getConfigDir() {
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
QApplication a(argc, argv); QApplication a(argc, argv);
#ifndef GENERAL_LOGGER_DISABLED Logger::init(stderr, GeneralLogger::DETAIL);
Logger::instance(stderr, GeneralLogger::LogIndent::DETAIL, &a);
#endif // GENERAL_LOGGER_DISABLED
Config config(getConfigDir()); Config config(getConfigDir());