Skip to content

Commit 34f868e

Browse files
andreidanila1AlexandraTrifan
authored andcommitted
common/loggingutil: Add LoggingUtil class for enhanced logging functionality
Introduced the LoggingUtil class to provide structured logging with support for different log levels (Info, Warning, Debug). This functionality is being utilized in the package manager for improved debugging and status updates. Signed-off-by: andreidanila1 <[email protected]>
1 parent 3d8a0fe commit 34f868e

File tree

4 files changed

+94
-11
lines changed

4 files changed

+94
-11
lines changed

common/include/common/loggingutil.h

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright (c) 2025 Analog Devices Inc.
3+
*
4+
* This file is part of Scopy
5+
* (see https://www.github.com/analogdevicesinc/scopy).
6+
*
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
19+
*
20+
*/
21+
22+
#ifndef LOGGINGUTIL_H
23+
#define LOGGINGUTIL_H
24+
25+
#include <QLoggingCategory>
26+
#include <QString>
27+
#include <pluginbase/statusbarmanager.h>
28+
#include "scopy-common_export.h"
29+
30+
namespace scopy {
31+
32+
class SCOPY_COMMON_EXPORT LoggingUtil
33+
{
34+
public:
35+
enum LogLevel
36+
{
37+
Info,
38+
Warning,
39+
Debug
40+
};
41+
42+
static inline void logMessage(const QLoggingCategory &category(), const QString &message,
43+
LogLevel level = LogLevel::Debug, bool pushToStatusBar = false,
44+
bool urgent = false, int ms = DEFAULT_DISPLAY_TIME)
45+
{
46+
if(pushToStatusBar) {
47+
if(urgent) {
48+
StatusBarManager::pushUrgentMessage(message, ms);
49+
} else {
50+
StatusBarManager::pushMessage(message, ms);
51+
}
52+
}
53+
54+
switch(level) {
55+
case Info:
56+
qInfo(category) << message;
57+
break;
58+
case Warning:
59+
qWarning(category) << message;
60+
break;
61+
case Debug:
62+
qDebug(category) << message;
63+
break;
64+
}
65+
}
66+
};
67+
68+
} // namespace scopy
69+
70+
#endif // LOGGINGUTIL_H

core/include/core/pkgmanager/pkgutil.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <kzip.h>
2828

2929
#define METADATA_FILE "MANIFEST.json"
30+
#define STATUS_BAR_MS 3000
3031

3132
namespace scopy {
3233
class SCOPY_CORE_EXPORT PkgUtil

core/src/pkgmanager/pkgmanager.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <QDir>
2828
#include <QFile>
2929
#include <QJsonObject>
30+
#include <common/loggingutil.h>
3031
#include <common/scopyconfig.h>
3132
#include <QFileSystemModel>
3233
#include <QLoggingCategory>
@@ -83,6 +84,9 @@ bool PkgManager::_install(const QString &zipPath, bool performRestart)
8384
}
8485
QJsonObject metadata = PkgUtil::extractJsonMetadata(zipPath);
8586
if(!PkgUtil::validatePkg(metadata)) {
87+
LoggingUtil::logMessage(CAT_PKGMANAGER,
88+
"Could not install the package. The file appears to be invalid or corrupted.",
89+
LoggingUtil::Warning, true, false, STATUS_BAR_MS);
8690
return false;
8791
}
8892

@@ -96,7 +100,8 @@ bool PkgManager::_install(const QString &zipPath, bool performRestart)
96100
if(installed) {
97101
Q_EMIT pkgInstalled(performRestart);
98102
} else {
99-
qWarning(CAT_PKGMANAGER) << "Couldn't install:" << metadata[PkgManifest::PKG_ID];
103+
LoggingUtil::logMessage(CAT_PKGMANAGER, "Couldn't install: " + metadata[PkgManifest::PKG_ID].toString(),
104+
LoggingUtil::Warning, true, false, STATUS_BAR_MS);
100105
}
101106

102107
return installed;
@@ -111,7 +116,8 @@ bool PkgManager::_uninstall(const QString &pkgId, bool performRestart)
111116
{
112117
QString pkgPath = validPackages_[pkgId].value(PkgManifest::PKG_PATH).toString();
113118
if(pkgPath.isEmpty()) {
114-
qWarning(CAT_PKGMANAGER) << "There is no package:" << pkgId;
119+
LoggingUtil::logMessage(CAT_PKGMANAGER, "Couldn't find the path for package " + pkgId,
120+
LoggingUtil::Warning, true, false, STATUS_BAR_MS);
115121
return false;
116122
}
117123
bool removed = PkgUtil::removePkg(pkgPath);

core/src/pkgmanager/pkgutil.cpp

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@
2828
#include <QLoggingCategory>
2929
#include <plot_utils.hpp>
3030
#include <qjsonarray.h>
31-
#include <common/scopyconfig.h>
32-
31+
#include <common/loggingutil.h>
3332
#include <common/scopyconfig.h>
3433

3534
Q_LOGGING_CATEGORY(CAT_PKGUTIL, "PkgUtil")
@@ -86,12 +85,14 @@ QJsonObject PkgUtil::extractJsonMetadata(const QString &zipPath)
8685
const KArchiveDirectory *zipDir = static_cast<const KArchiveDirectory *>(zip.directory()->entry(entryName));
8786
const KArchiveFile *manifestFile = zipDir->file(METADATA_FILE);
8887
if(!manifestFile) {
89-
qWarning(CAT_PKGUTIL) << METADATA_FILE << "not found in package!";
88+
LoggingUtil::logMessage(CAT_PKGUTIL, QString(METADATA_FILE) + " not found in package!",
89+
LoggingUtil::Warning, true, true, STATUS_BAR_MS);
9090
return {};
9191
}
9292
QJsonDocument doc = QJsonDocument::fromJson(manifestFile->data());
9393
if(!doc.isObject()) {
94-
qWarning(CAT_PKGUTIL) << "QJsonDocument parsing failed:" << METADATA_FILE;
94+
LoggingUtil::logMessage(CAT_PKGUTIL, "QJsonDocument parsing failed: " + QString(METADATA_FILE),
95+
LoggingUtil::Warning, true, true, STATUS_BAR_MS);
9596
return {};
9697
}
9798
QJsonObject obj = doc.object();
@@ -116,7 +117,8 @@ QJsonObject PkgUtil::getMetadata(const QString &path)
116117
return obj;
117118
}
118119

119-
// In this method we also can make another checks
120+
// This method validates the package metadata by checking required fields.
121+
// Additional checks could include verifying field formats or ensuring compatibility with specific versions.
120122
bool PkgUtil::validatePkg(QJsonObject &metadata)
121123
{
122124
if(metadata.isEmpty()) {
@@ -128,13 +130,15 @@ bool PkgUtil::validatePkg(QJsonObject &metadata)
128130
QJsonValue value = metadata.value(field);
129131
// Check if field is missing or null
130132
if(value.isNull() || value.isUndefined()) {
131-
qWarning(CAT_PKGUTIL) << "Missing required field:" << field;
133+
LoggingUtil::logMessage(CAT_PKGUTIL, "Missing required field: " + field, LoggingUtil::Warning,
134+
true, true, STATUS_BAR_MS);
132135
return false;
133136
}
134137
// Check if field is empty (string or array)
135138
if((value.isString() && value.toString().trimmed().isEmpty()) ||
136139
(value.isArray() && value.toArray().isEmpty())) {
137-
qWarning(CAT_PKGUTIL) << "Empty required field:" << field;
140+
LoggingUtil::logMessage(CAT_PKGUTIL, "Empty required field: " + field, LoggingUtil::Warning,
141+
true, true, STATUS_BAR_MS);
138142
return false;
139143
}
140144
}
@@ -161,12 +165,14 @@ QString PkgUtil::validateArchiveEntry(const KZip &zip)
161165
}
162166
QStringList zipEntries = zipDir->entries();
163167
if(zipEntries.size() != 1) {
164-
qWarning(CAT_PKGUTIL) << "There must be a single entry in the archive!";
168+
LoggingUtil::logMessage(CAT_PKGUTIL, "There must be a single entry in the archive! (a directory)",
169+
LoggingUtil::Warning, true, false, STATUS_BAR_MS);
165170
return "";
166171
}
167172
QString entryName = zipEntries.first();
168173
if(!zipDir->entry(entryName)->isDirectory()) {
169-
qWarning(CAT_PKGUTIL) << "The archive entry must be a directory!!";
174+
LoggingUtil::logMessage(CAT_PKGUTIL, "The archive entry must be a directory!", LoggingUtil::Warning,
175+
true, false, STATUS_BAR_MS);
170176
return "";
171177
}
172178
return entryName;

0 commit comments

Comments
 (0)