Skip to content

Commit 2c2b834

Browse files
committed
refactor: remove Qt logging and error handling
This change decouples the utils library from Qt dependencies and standardizes logging and error handling across the project. Signed-off-by: reddevillg <reddevillg@gmail.com>
1 parent 22fc444 commit 2c2b834

45 files changed

Lines changed: 576 additions & 1206 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

apps/ll-builder/src/main.cpp

Lines changed: 56 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99
#include "linglong/builder/config.h"
1010
#include "linglong/builder/linglong_builder.h"
1111
#include "linglong/cli/cli.h"
12+
#include "linglong/common/global/initialize.h"
1213
#include "linglong/package/architecture.h"
1314
#include "linglong/package/version.h"
1415
#include "linglong/repo/client_factory.h"
1516
#include "linglong/repo/config.h"
1617
#include "linglong/repo/migrate.h"
1718
#include "linglong/utils/error/error.h"
1819
#include "linglong/utils/gettext.h"
19-
#include "linglong/utils/global/initialize.h"
2020
#include "linglong/utils/log/log.h"
2121
#include "linglong/utils/namespace.h"
2222
#include "linglong/utils/serialize/yaml.h"
@@ -136,18 +136,18 @@ getProjectYAMLPath(const std::filesystem::path &projectDir, const std::string &u
136136

137137
int handleCreate(const CreateCommandOptions &options)
138138
{
139-
qInfo() << "Handling create for project:" << QString::fromStdString(options.projectName);
139+
LogI("Handling create for project: {}", options.projectName);
140140

141141
auto name = QString::fromStdString(options.projectName);
142142
QDir projectDir = QDir::current().absoluteFilePath(name);
143143
if (projectDir.exists()) {
144-
qCritical() << name << "project dir already exists";
144+
LogE("{} project dir already exists", options.projectName);
145145
return -1;
146146
}
147147

148148
auto ret = projectDir.mkpath(".");
149149
if (!ret) {
150-
qCritical() << "create project dir failed:" << projectDir.absolutePath();
150+
LogE("create project dir failed: {}", projectDir.absolutePath().toStdString());
151151
return -1;
152152
}
153153

@@ -156,42 +156,47 @@ int handleCreate(const CreateCommandOptions &options)
156156

157157
if (!QFileInfo::exists(templateFilePath)) {
158158
templateFilePath = ":/example.yaml"; // Use Qt resource fallback
159-
qInfo() << "Using template file from Qt resources:" << templateFilePath;
159+
LogI("Using template file from Qt resources: {}", templateFilePath);
160160
} else {
161-
qInfo() << "Using template file from system path:" << templateFilePath;
161+
LogI("Using template file from system path: {}", templateFilePath);
162162
}
163163

164164
QFile templateFile(templateFilePath);
165165
QFile configFile(configFilePath);
166166

167167
if (!templateFile.open(QIODevice::ReadOnly)) {
168-
qCritical() << "Failed to open template file:" << templateFilePath
169-
<< "Error:" << templateFile.errorString();
168+
LogE("Failed to open template file {}: {}",
169+
templateFilePath,
170+
templateFile.errorString().toStdString());
170171
return -1;
171172
}
172173

173174
if (!configFile.open(QIODevice::WriteOnly)) {
174-
qCritical() << "Failed to open config file for writing:" << configFilePath
175-
<< "Error:" << configFile.errorString();
175+
LogE("Failed to open config file {} for writing: {}",
176+
configFilePath.toStdString(),
177+
configFile.errorString().toStdString());
176178
return -1;
177179
}
178180

179181
auto rawData = templateFile.readAll();
180182
rawData.replace("@ID@", name.toUtf8());
181183

182184
if (configFile.write(rawData) <= 0) {
183-
qCritical() << "Failed to write config file:" << configFilePath
184-
<< "Error:" << configFile.errorString();
185+
LogE("Failed to write config file {}: {}",
186+
configFilePath.toStdString(),
187+
configFile.errorString().toStdString());
185188
return -1;
186189
}
187190

188-
qInfo() << "Project" << name << "created successfully at" << projectDir.absolutePath();
191+
LogI("Project {} created successfully at {}",
192+
options.projectName,
193+
projectDir.absolutePath().toStdString());
189194
return 0;
190195
}
191196

192197
int handleBuild(linglong::builder::Builder &builder, const BuildCommandOptions &options)
193198
{
194-
qInfo() << "Handling build command";
199+
LogI("Handling build command");
195200

196201
auto cfg = builder.getConfig();
197202

@@ -219,11 +224,11 @@ int handleBuild(linglong::builder::Builder &builder, const BuildCommandOptions &
219224
ret = builder.build();
220225
}
221226
if (!ret) {
222-
qCritical() << "Build failed: " << ret.error();
227+
LogE("Build failed: {}", ret.error());
223228
return ret.error().code();
224229
}
225230

226-
qInfo() << "Build completed successfully.";
231+
LogI("Build completed successfully.");
227232

228233
return 0;
229234
}
@@ -260,14 +265,14 @@ int handleExport(linglong::builder::Builder &builder, const ExportCommandOptions
260265
auto exportOpts = options.exportSpecificOptions;
261266
// layer 默认使用lz4, 保持和之前版本的兼容
262267
if (exportOpts.compressor.empty()) {
263-
qInfo() << "Compressor not specified, defaulting to lz4 for layer export.";
268+
LogI("Compressor not specified, defaulting to lz4 for layer export.");
264269
exportOpts.compressor = "lz4";
265270
}
266271

267272
if (options.layerMode) {
268273
auto result = builder.exportLayer(exportOpts);
269274
if (!result) {
270-
qCritical() << "Export layer failed: " << result.error();
275+
LogE("Export layer failed: {}", result.error());
271276
return result.error().code();
272277
}
273278

@@ -276,7 +281,7 @@ int handleExport(linglong::builder::Builder &builder, const ExportCommandOptions
276281

277282
auto result = builder.exportUAB(exportOpts, options.outputFile);
278283
if (!result) {
279-
qCritical() << "Export UAB failed: " << result.error();
284+
LogE("Export UAB failed: {}", result.error());
280285
return result.error().code();
281286
}
282287

@@ -285,22 +290,21 @@ int handleExport(linglong::builder::Builder &builder, const ExportCommandOptions
285290

286291
int handlePush(linglong::builder::Builder &builder, const PushCommandOptions &options)
287292
{
288-
qInfo() << "Handling push command";
293+
LogI("Handling push command");
289294
const auto &repoOpts = options.repoOptions;
290295

291296
for (const auto &module : options.pushModules) {
292-
qInfo() << "Pushing module:" << QString::fromStdString(module);
297+
LogI("Pushing module: {}", module);
293298

294299
auto result = builder.push(module, repoOpts.repoUrl, repoOpts.repoName);
295300
if (!result) {
296-
qCritical() << "Push failed for module" << QString::fromStdString(module) << ":"
297-
<< result.error();
301+
LogE("Push failed for module {}: {}", module, result.error());
298302
return result.error().code();
299303
}
300-
qInfo() << "Module" << QString::fromStdString(module) << "pushed successfully.";
304+
LogI("Module {} pushed successfully.", module);
301305
}
302306

303-
qInfo() << "All modules pushed successfully.";
307+
LogI("All modules pushed successfully.");
304308
return 0;
305309
}
306310

@@ -352,18 +356,20 @@ int handleImportDir(linglong::repo::OSTreeRepo &repo, const ImportDirCommandOpti
352356

353357
int handleExtract(const ExtractCommandOptions &options)
354358
{
359+
LogI("Handling extract command for layer file: {} to directory: {}",
360+
options.layerFile,
361+
options.dir);
362+
355363
QString layerFile = QString::fromStdString(options.layerFile);
356364
QString targetDir = QString::fromStdString(options.dir);
357-
qInfo() << "Handling extract command for layer file:" << layerFile
358-
<< "to directory:" << targetDir;
359365

360366
auto result = linglong::builder::Builder::extractLayer(layerFile, targetDir);
361367
if (!result) {
362-
qCritical() << "Extract layer failed: " << result.error();
368+
LogE("Extract layer failed: {}", result.error());
363369
return result.error().code();
364370
}
365371

366-
qInfo() << "Layer extraction completed successfully.";
372+
LogI("Layer extraction completed successfully.");
367373
return 0;
368374
}
369375

@@ -400,7 +406,7 @@ int handleRepoAdd(linglong::repo::OSTreeRepo &repo, linglong::cli::RepoOptions &
400406
return r.alias.value_or(r.name) == alias;
401407
});
402408
if (isExist) {
403-
std::cerr << "repo " + alias + " already exist." << std::endl;
409+
LogE("repo {} already exist.", alias);
404410
return -1;
405411
}
406412

@@ -448,7 +454,7 @@ int handleRepoRemove(linglong::repo::OSTreeRepo &repo, linglong::cli::RepoOption
448454
return -1;
449455
}
450456

451-
qInfo() << "Repository" << QString::fromStdString(alias) << "removed successfully.";
457+
LogI("Repository {} removed successfully.", alias);
452458
return 0;
453459
}
454460

@@ -480,7 +486,7 @@ int handleRepoUpdate(linglong::repo::OSTreeRepo &repo, linglong::cli::RepoOption
480486
return -1;
481487
}
482488

483-
qInfo() << "Repository" << QString::fromStdString(alias) << "updated successfully.";
489+
LogI("Repository {} updated successfully.", alias);
484490
return 0;
485491
}
486492

@@ -506,9 +512,9 @@ int handleRepoSetDefault(linglong::repo::OSTreeRepo &repo, linglong::cli::RepoOp
506512
std::cerr << ret.error().message() << std::endl;
507513
return -1;
508514
}
509-
qInfo() << "Default repository set to" << QString::fromStdString(alias) << "successfully.";
515+
LogI("Default repository set to {} successfully.", alias);
510516
} else {
511-
qInfo() << QString::fromStdString(alias) << "is already the default repository.";
517+
LogI("{} is already the default repository.", alias);
512518
}
513519

514520
return 0;
@@ -639,34 +645,35 @@ std::vector<std::string> getProjectModule(const linglong::api::types::v1::Builde
639645
std::optional<std::filesystem::path>
640646
backupFailedMigrationRepo(const std::filesystem::path &repoPath)
641647
{
642-
qWarning() << "Repository migration failed. Attempting to back up the old repository:"
643-
<< QString::fromStdString(repoPath.string());
648+
LogW("Repository migration failed. Attempting to back up the old repository {}",
649+
repoPath.string());
644650

645651
auto backupDirPattern = (repoPath.parent_path() / "linglong-builder.old-XXXXXX").string();
646652
std::error_code ec;
647653

648654
char *backupDir = ::mkdtemp(backupDirPattern.data());
649655
if (backupDir == nullptr) {
650-
qCritical() << "we couldn't generate a temporary directory for migrate, old repo will "
651-
"be removed.";
656+
LogE("we couldn't generate a temporary directory for migrate, old repo will be removed.");
652657
std::filesystem::remove_all(repoPath, ec); // Use remove_all for directories
653658
if (ec) {
654-
qCritical() << "failed to remove the old repo:" << QString::fromStdString(repoPath);
659+
LogE("failed to remove the old repo: {}", repoPath);
655660
}
656661
return std::nullopt;
657662
}
658663

659664
std::filesystem::rename(repoPath, backupDir, ec);
660665
if (ec) {
661-
qCritical() << "Failed to move the old repository to the backup location (" << backupDir
662-
<< "). Error:" << ec.message().c_str() << "Please move or remove it manually:"
663-
<< QString::fromStdString(repoPath.string());
666+
LogE("Failed to move the old repository to the backup location ({}): {}\nPlease move or "
667+
"remove it manually: {}",
668+
backupDir,
669+
ec.message(),
670+
repoPath.string());
664671
// Attempt to clean up the created backup directory if rename failed
665672
return std::nullopt;
666673
}
667674

668-
qInfo() << "Old repository successfully backed up to:" << backupDir
669-
<< ". All data will need to be pulled again.";
675+
LogI("Old repository successfully backed up to: {}. All data will need to be pulled again.",
676+
backupDir);
670677
return backupDir;
671678
}
672679

@@ -680,8 +687,8 @@ int main(int argc, char **argv)
680687
// 初始化 qt qrc
681688
Q_INIT_RESOURCE(builder_releases);
682689
// 初始化应用,builder在非tty环境也输出日志
683-
linglong::utils::global::applicationInitialize(true);
684-
linglong::utils::global::initLinyapsLogSystem(linglong::utils::log::LogBackend::Console);
690+
linglong::common::global::applicationInitialize();
691+
linglong::common::global::initLinyapsLogSystem(linglong::utils::log::LogBackend::Console);
685692

686693
CLI::App commandParser{ _("linyaps builder CLI \n"
687694
"A CLI program to build linyaps application\n") };
@@ -1008,7 +1015,7 @@ You can report bugs to the linyaps team under this project: https://github.com/O
10081015
linglong::repo::loadConfig({ QString::fromStdString(builderCfg->repo + "/config.yaml"),
10091016
LINGLONG_DATA_DIR "/config.yaml" });
10101017
if (!repoCfg) {
1011-
qCritical() << repoCfg.error();
1018+
LogE("{}", repoCfg.error());
10121019
return -1;
10131020
}
10141021

@@ -1021,7 +1028,7 @@ You can report bugs to the linyaps team under this project: https://github.com/O
10211028

10221029
auto repoRoot = QDir{ QString::fromStdString(builderCfg->repo) };
10231030
if (!repoRoot.exists() && !repoRoot.mkpath(".")) {
1024-
qCritical() << "failed to create the repository of builder.";
1031+
LogE("failed to create the repository of builder.");
10251032
return -1;
10261033
}
10271034

@@ -1063,7 +1070,7 @@ You can report bugs to the linyaps team under this project: https://github.com/O
10631070

10641071
auto path = QStandardPaths::findExecutable(ociRuntimeCLI);
10651072
if (path.isEmpty()) {
1066-
qCritical() << ociRuntimeCLI << "not found";
1073+
LogE("{} not found", ociRuntimeCLI.toStdString());
10671074
return -1;
10681075
}
10691076

0 commit comments

Comments
 (0)