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