GCC Code Coverage Report


Directory: ./
File: lib/log.cpp
Date: 2023-04-20 22:59:23
Exec Total Coverage
Lines: 53 59 89.8%
Branches: 24 34 70.6%

Line Branch Exec Source
1 /*************************************************************************************
2 * Copyright 2016 by Sebastian K├╝gler <sebas@kde.org> *
3 * *
4 * This library is free software; you can redistribute it and/or *
5 * modify it under the terms of the GNU Lesser General Public *
6 * License as published by the Free Software Foundation; either *
7 * version 2.1 of the License, or (at your option) any later version. *
8 * *
9 * This library is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
12 * Lesser General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU Lesser General Public *
15 * License along with this library; if not, write to the Free Software *
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
17 *************************************************************************************/
18 #include "log.h"
19
20 #include <QDateTime>
21 #include <QDir>
22 #include <QFile>
23 #include <QFileInfo>
24 #include <QStandardPaths>
25
26 namespace Disman
27 {
28
29 Log* Log::sInstance = nullptr;
30 QtMessageHandler sDefaultMessageHandler = nullptr;
31
32 2 void dismanLogOutput(QtMsgType type, const QMessageLogContext& context, const QString& msg)
33 {
34 2 auto category = QString::fromLocal8Bit(context.category);
35
1/2
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 if (category.startsWith(QLatin1String("disman"))) {
36 2 Log::log(msg, category);
37 }
38 2 sDefaultMessageHandler(type, context, msg);
39 2 }
40
41 void log(const QString& msg)
42 {
43 Log::log(msg);
44 }
45
46 27 Log* Log::instance()
47 {
48
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 10 times.
27 if (!sInstance) {
49 17 sInstance = new Log();
50 }
51
52 27 return sInstance;
53 }
54
55 using namespace Disman;
56 class Q_DECL_HIDDEN Log::Private
57 {
58 public:
59 QString context;
60 bool enabled = false;
61 QString file;
62 };
63
64 17 Log::Log()
65 17 : d(new Private)
66 {
67 17 const char* logging_env = "DISMAN_LOGGING";
68
69
2/2
✓ Branch 1 taken 16 times.
✓ Branch 2 taken 1 times.
17 if (qEnvironmentVariableIsSet(logging_env)) {
70 16 const QString logging_env_value = QString::fromUtf8(qgetenv(logging_env));
71 16 if (logging_env_value != QLatin1Char('0')
72
6/8
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
✓ Branch 6 taken 3 times.
✓ Branch 7 taken 13 times.
✓ Branch 8 taken 16 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 3 times.
✓ Branch 12 taken 13 times.
32 && logging_env_value.toLower() != QLatin1String("false")) {
73 3 d->enabled = true;
74 }
75 16 }
76
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 3 times.
17 if (!d->enabled) {
77 14 return;
78 }
79 6 d->file = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation)
80 9 + QLatin1String("/disman/disman.log");
81
82 3 QLoggingCategory::setFilterRules(QStringLiteral("disman.*=true"));
83 3 QFileInfo fi(d->file);
84
1/2
✗ Branch 7 not taken.
✓ Branch 8 taken 3 times.
3 if (!QDir().mkpath(fi.absolutePath())) {
85 qWarning() << "Failed to create logging dir" << fi.absolutePath();
86 }
87
88
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
3 if (!sDefaultMessageHandler) {
89 1 sDefaultMessageHandler = qInstallMessageHandler(dismanLogOutput);
90 }
91 3 }
92
93 Log::Log(Log::Private* dd)
94 : d(dd)
95 {
96 }
97
98 28 Log::~Log()
99 {
100
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
14 delete d;
101 14 sInstance = nullptr;
102 28 }
103
104 3 QString Log::context() const
105 {
106 3 return d->context;
107 }
108
109 1 void Log::set_context(const QString& context)
110 {
111 1 d->context = context;
112 1 }
113
114 8 bool Log::enabled() const
115 {
116 8 return d->enabled;
117 }
118
119 5 QString Log::file() const
120 {
121 5 return d->file;
122 }
123
124 4 void Log::log(const QString& msg, const QString& category)
125 {
126
2/2
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 2 times.
4 if (!instance()->enabled()) {
127 2 return;
128 }
129 2 auto _cat = category;
130 2 _cat.remove(QStringLiteral("disman."));
131 const QString timestamp
132 4 = QDateTime::currentDateTime().toString(QStringLiteral("dd.MM.yyyy hh:mm:ss.zzz"));
133 QString logMessage
134 2 = QStringLiteral("\n%1 ; %2 ; %3 : %4").arg(timestamp, _cat, instance()->context(), msg);
135 2 QFile file(instance()->file());
136
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2 if (!file.open(QIODevice::Append | QIODevice::Text)) {
137 return;
138 }
139 2 file.write(logMessage.toUtf8());
140
4/8
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 2 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 2 times.
✗ Branch 11 not taken.
2 }
141
142 } // ns
143