QuasarAppLib
qalogger.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2024-2025 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
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
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}
52
53
54
55 if (checkLogType(type, _verboseLevel)) {
56 if (_toFile && _logFile->size()) {
58 if (logFile.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text)) {
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
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
105
106 }
107
108}
109
113
114}
static QString getLogFilePath()
setVerboseLevel This method set verbose level of the logger.
Definition qalogger.cpp:110
void init()
init This method initialize logging of all qt message into file.
Definition qalogger.cpp:85
The QuasaraAppUtils class This lib include base functions for the all applications of QuasarApp group...
Definition helpdata.cpp:18
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
void gen(int size, QByteArray &result)
#define MESSAGE_PATTERN
Definition qalogger.cpp:25