QuasarAppLib 1.0.530.c9fba11
qalogger.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2024-2026 QuasarApp.
3 * Distributed under the lgplv3 software license, see the accompanying
4 * Everyone is permitted to copy and distribute verbatim copies
5 * of this license document, but changing it is not allowed.
6*/
7
8#include "qalogger.h"
9#include "params.h"
10#include <iostream>
11
12#include <QCoreApplication>
13#include <QDir>
14#include <QFile>
15#include <QStandardPaths>
16
17namespace QuasarAppUtils {
18
19Q_GLOBAL_STATIC(QString, _logFile)
20
21static bool _toFile = false;
22static VerboseLvl _verboseLevel = Debug;
23
24
25#define MESSAGE_PATTERN \
26"[%{time MM-dd h:mm:ss.zzz} %{threadid} " \
27 "%{if-debug}Debug%{endif}%{if-info}Info%{endif}%{if-warning}Warning%{endif}%{if-critical}Error%{endif}%{if-fatal}Fatal%{endif}] " \
28 "%{message}"
29
30
33
36
37
38bool checkLogType(QtMsgType type, VerboseLvl lvl) {
39 switch (type) {
40 case QtDebugMsg: return lvl >= Debug;
41 case QtInfoMsg: return lvl >= Info;
42 case QtWarningMsg: return lvl >= Warning;
43
44 case QtCriticalMsg:
45 case QtFatalMsg:
46 return true;
47 }
48
49 return true;
50}
51void messageHandler(QtMsgType type, const QMessageLogContext & context, const QString &msg) {
52
53
54
55 if (checkLogType(type, _verboseLevel)) {
56 if (_toFile && !_logFile.isDestroyed() && _logFile->size()) {
57 QFile logFile(*_logFile);
58 if (logFile.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text)) {
59 QTextStream stream(&logFile);
60 stream << qFormatLogMessage(type, context, msg) << Qt::endl;
61 }
62
63 }
64
65 switch (type) {
66 case QtMsgType::QtFatalMsg:
67 case QtMsgType::QtCriticalMsg:
68 case QtMsgType::QtWarningMsg: {
69 std::cerr << qFormatLogMessage(type, context, msg).toStdString() << std::endl;
70 break;
71 }
72 case QtMsgType::QtDebugMsg:
73 case QtMsgType::QtInfoMsg:
74 default: {
75 std::cout << qFormatLogMessage(type, context, msg).toStdString() << std::endl;
76
77 break;
78 }
79
80 }
81 }
82}
83
84
86 qSetMessagePattern(MESSAGE_PATTERN);
87 qInstallMessageHandler(messageHandler);
88
89
90 _verboseLevel = Params::getVerboseLvl();
91
92 if (Params::isEndable("fileLog")) {
93 _toFile = true;
94 QString path = QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation);
95 QString filePath = path + "/" + QCoreApplication::applicationName() +
96 " " + QDate::currentDate().toString(Qt::DateFormat::ISODate) + ".log";
97 auto file = Params::getArg("fileLog");
98 if (file.size()) {
99 filePath = file;
100 }
101
102 QDir().mkpath(path);
103
104 *_logFile = filePath;
105
106 }
107
108}
109
111 return *_logFile;
112}
113
114}
static bool isEndable(const QString &key)
isEndable This method check if enable a key argument.
Definition params.cpp:35
static VerboseLvl getVerboseLvl()
getVerboseLvl This method return the verbose log level.
Definition params.cpp:71
static QString getArg(const QString &key, const QString &def={})
getArg return string value of a key if key is exits else return a def value. If a def value not defin...
Definition params.cpp:348
static QString getLogFilePath()
getLogFilePath This method returns the path of the log file that will be used by the logger....
Definition qalogger.cpp:110
void init()
init This method initialize logging of all qt message into file.
Definition qalogger.cpp:85
QALogger()
QALogger This is the default constructor of the QALogger object. After the construction call the init...
Definition qalogger.cpp:31
~QALogger()
~QALogger This is the destructor of the QALogger object. It removes the message handler and closes th...
Definition qalogger.cpp:34
The QuasarAppUtils namespace contains the base functions for all applications of the QuasarApp group....
bool checkLogType(QtMsgType type, VerboseLvl lvl)
Definition qalogger.cpp:38
void messageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
Definition qalogger.cpp:51
VerboseLvl
The VerboseLvl enum uses for sets log level.
Definition params.h:23
@ Warning
Warning message. This logs will marked as a Warning and printing if the verbose lvl >= 1.
Definition params.h:27
@ Info
General information. This logs will marked as a Info and and printing if the verbose lvl >= 2.
Definition params.h:29
@ Debug
Debug message. This logs will marked as a Debug and printing if the verbose lvl >= 3.
Definition params.h:31
#define MESSAGE_PATTERN
Definition qalogger.cpp:25