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
19static QFile* _logFile;
20static bool _toFile = false;
21static VerboseLvl _verboseLevel = Debug;
22
23
24#define MESSAGE_PATTERN \
25"[%{time MM-dd h:mm:ss.zzz} %{threadid} " \
26 "%{if-debug}Debug%{endif}%{if-info}Info%{endif}%{if-warning}Warning%{endif}%{if-critical}Error%{endif}%{if-fatal}Fatal%{endif}] " \
27 "%{message}"
28
29
31 _logFile = new QFile();
32}
33
35 _logFile->close();
36 delete _logFile;
37}
38
39
41 switch (type) {
42 case QtDebugMsg: return lvl >= Debug;
43 case QtInfoMsg: return lvl >= Info;
44 case QtWarningMsg: return lvl >= Warning;
45
46 case QtCriticalMsg:
47 case QtFatalMsg:
48 return true;
49 }
50
51 return true;
52}
54
55
56
57 if (checkLogType(type, _verboseLevel)) {
58 if (_toFile && _logFile) {
59 QTextStream stream(_logFile);
60 stream << qFormatLogMessage(type, context, msg) << Qt::endl;
61 _logFile->flush();
62 }
63
64 switch (type) {
65 case QtMsgType::QtFatalMsg: {
67 break;
68 }
69
70 case QtMsgType::QtCriticalMsg:
71 case QtMsgType::QtWarningMsg: {
72 std::cerr << qFormatLogMessage(type, context, msg).toStdString() << std::endl;
73 break;
74 }
75 case QtMsgType::QtDebugMsg:
76 case QtMsgType::QtInfoMsg:
77 default: {
78 std::cout << qFormatLogMessage(type, context, msg).toStdString() << std::endl;
79
80 break;
81 }
82
83 }
84 }
85}
86
87
91
92
93 _verboseLevel = Params::getVerboseLvl();
94
95 if (Params::isEndable("fileLog")) {
96 _toFile = true;
97 QString path = QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation);
98 QString filePath = path + "/" + QCoreApplication::applicationName() + ".log";
99 auto file = Params::getArg("fileLog");
100 if (file.size()) {
101 filePath = file;
102 }
103
104 QDir().mkpath(path);
105
106 _logFile->setFileName(filePath);
107
108 if (!_logFile->open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text)) {
109 qFatal() << "Can't open log file";
110 }
111
112 }
113
114}
115
116}
void init()
init This method initialize logging of all qt message into file.
Definition qalogger.cpp:88
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:40
void messageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
Definition qalogger.cpp:53
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:24