ProfileManager: use Logger.

This commit is contained in:
Guillaume Jacquemin 2022-11-21 19:20:44 +01:00
parent 060daebe17
commit 79c97733db
1 changed files with 21 additions and 3 deletions

View File

@ -27,6 +27,8 @@
#include <zip.h> #include <zip.h>
#include "../Logger/Logger.h"
#include "ProfileManager.h" #include "ProfileManager.h"
using namespace Containers::Literals; using namespace Containers::Literals;
@ -51,6 +53,8 @@ auto ProfileManager::profiles() -> Containers::ArrayView<Profile> {
} }
auto ProfileManager::refreshProfiles() -> bool { auto ProfileManager::refreshProfiles() -> bool {
LOG_INFO("Refreshing profiles.");
_profiles = Containers::Array<Profile>{}; _profiles = Containers::Array<Profile>{};
using Utility::Path::ListFlag; using Utility::Path::ListFlag;
@ -59,6 +63,7 @@ auto ProfileManager::refreshProfiles() -> bool {
if(!files) { if(!files) {
_lastError = _saveDirectory + " can't be opened."; _lastError = _saveDirectory + " can't be opened.";
LOG_ERROR(_lastError);
return false; return false;
} }
@ -72,7 +77,7 @@ auto ProfileManager::refreshProfiles() -> bool {
Profile profile{Utility::Path::join(_saveDirectory, file)}; Profile profile{Utility::Path::join(_saveDirectory, file)};
if(!profile.valid()) { if(!profile.valid()) {
Utility::Warning{} << "Profile"_s << file << "is invalid:"_s << profile.lastError(); LOG_WARNING_FORMAT("Profile {} is invalid: {}", file, profile.lastError());
continue; continue;
} }
@ -81,6 +86,7 @@ auto ProfileManager::refreshProfiles() -> bool {
if(_profiles.isEmpty()) { if(_profiles.isEmpty()) {
_lastError = "No valid profiles were found."_s; _lastError = "No valid profiles were found."_s;
LOG_ERROR(_lastError);
return false; return false;
} }
@ -96,6 +102,7 @@ auto ProfileManager::deleteProfile(std::size_t index, bool delete_builds) -> boo
_lastError = Utility::format("Couldn't delete {} (filename: {}).", _lastError = Utility::format("Couldn't delete {} (filename: {}).",
_profiles[index].companyName(), _profiles[index].companyName(),
_profiles[index].filename()); _profiles[index].filename());
LOG_ERROR(_lastError);
refreshProfiles(); refreshProfiles();
return false; return false;
} }
@ -124,7 +131,7 @@ auto ProfileManager::backupProfile(std::size_t index, bool backup_builds) -> boo
std::tm* time = std::localtime(&timestamp); std::tm* time = std::localtime(&timestamp);
auto& profile = _profiles[index]; auto& profile = _profiles[index];
auto filename = Utility::format("{}_{}{:.2d}{:.2d}_{:.2d}{:.2d}{:.2d}.mbprofbackup", auto filename = Utility::format("{}_{}{:.2d}{:.2d}_{:.2d}{:.2d}{:.2d}.backup.mbst",
Utility::String::replaceAll(profile.companyName().data(), " ", "_").c_str(), Utility::String::replaceAll(profile.companyName().data(), " ", "_").c_str(),
time->tm_year + 1900, time->tm_mon + 1, time->tm_mday, time->tm_year + 1900, time->tm_mon + 1, time->tm_mday,
time->tm_hour, time->tm_min, time->tm_sec); time->tm_hour, time->tm_min, time->tm_sec);
@ -135,18 +142,21 @@ auto ProfileManager::backupProfile(std::size_t index, bool backup_builds) -> boo
if(zip == nullptr) { if(zip == nullptr) {
zip_error_init_with_code(&error, error_code); zip_error_init_with_code(&error, error_code);
_lastError = zip_error_strerror(&error); _lastError = zip_error_strerror(&error);
LOG_ERROR(_lastError);
return false; return false;
} }
zip_source_t* profile_source = zip_source_file(zip, Utility::Path::toNativeSeparators(Utility::Path::join(_saveDirectory, profile.filename())).data(), 0, 0); zip_source_t* profile_source = zip_source_file(zip, Utility::Path::toNativeSeparators(Utility::Path::join(_saveDirectory, profile.filename())).data(), 0, 0);
if(profile_source == nullptr) { if(profile_source == nullptr) {
_lastError = zip_strerror(zip); _lastError = zip_strerror(zip);
LOG_ERROR(_lastError);
zip_source_free(profile_source); zip_source_free(profile_source);
return false; return false;
} }
if(zip_file_add(zip, profile.filename().data(), profile_source, ZIP_FL_ENC_UTF_8) == -1) { if(zip_file_add(zip, profile.filename().data(), profile_source, ZIP_FL_ENC_UTF_8) == -1) {
_lastError = zip_strerror(zip); _lastError = zip_strerror(zip);
LOG_ERROR(_lastError);
zip_source_free(profile_source); zip_source_free(profile_source);
return false; return false;
} }
@ -183,6 +193,7 @@ auto ProfileManager::backupProfile(std::size_t index, bool backup_builds) -> boo
if(zip_close(zip) == -1) { if(zip_close(zip) == -1) {
_lastError = zip_strerror(zip); _lastError = zip_strerror(zip);
LOG_ERROR(_lastError);
return false; return false;
} }
@ -204,11 +215,12 @@ void ProfileManager::refreshBackups() {
if(!files) { if(!files) {
_lastError = _backupsDirectory + " can't be opened."; _lastError = _backupsDirectory + " can't be opened.";
LOG_ERROR(_lastError);
return; return;
} }
auto predicate = [](Containers::StringView file)->bool{ auto predicate = [](Containers::StringView file)->bool{
return !file.hasSuffix(".mbprofbackup"_s); return !(file.hasSuffix(".mbprofbackup"_s) || file.hasSuffix(".backup.mbst"));
}; };
auto files_view = files->exceptSuffix(files->end() - std::remove_if(files->begin(), files->end(), predicate)); auto files_view = files->exceptSuffix(files->end() - std::remove_if(files->begin(), files->end(), predicate));
@ -281,6 +293,7 @@ void ProfileManager::refreshBackups() {
auto ProfileManager::deleteBackup(std::size_t index) -> bool { auto ProfileManager::deleteBackup(std::size_t index) -> bool {
if(!Utility::Path::remove(Utility::Path::join(_backupsDirectory, _backups[index].filename))) { if(!Utility::Path::remove(Utility::Path::join(_backupsDirectory, _backups[index].filename))) {
_lastError = "Couldn't delete " + _backups[index].filename; _lastError = "Couldn't delete " + _backups[index].filename;
LOG_ERROR(_lastError);
return false; return false;
} }
@ -307,6 +320,7 @@ auto ProfileManager::restoreBackup(std::size_t index) -> bool {
zip_error_t error; zip_error_t error;
zip_error_init_with_code(&error, error_code); zip_error_init_with_code(&error, error_code);
_lastError = zip_error_strerror(&error); _lastError = zip_error_strerror(&error);
LOG_ERROR(_lastError);
return false; return false;
} }
@ -316,6 +330,7 @@ auto ProfileManager::restoreBackup(std::size_t index) -> bool {
FILE* out = std::fopen(Utility::Path::join(_saveDirectory, file).data(), "wb"); FILE* out = std::fopen(Utility::Path::join(_saveDirectory, file).data(), "wb");
if(out == nullptr) { if(out == nullptr) {
_lastError = Utility::format(error_format.data(), file, std::strerror(errno)); _lastError = Utility::format(error_format.data(), file, std::strerror(errno));
LOG_ERROR(_lastError);
return false; return false;
} }
@ -324,6 +339,7 @@ auto ProfileManager::restoreBackup(std::size_t index) -> bool {
zip_file_t* zf = zip_fopen(zip, file.data(), ZIP_FL_ENC_GUESS); zip_file_t* zf = zip_fopen(zip, file.data(), ZIP_FL_ENC_GUESS);
if(zf == nullptr) { if(zf == nullptr) {
_lastError = Utility::format(error_format.data(), file, zip_strerror(zip)); _lastError = Utility::format(error_format.data(), file, zip_strerror(zip));
LOG_ERROR(_lastError);
return false; return false;
} }
@ -335,12 +351,14 @@ auto ProfileManager::restoreBackup(std::size_t index) -> bool {
while((bytes_read = zip_fread(zf, buf.data(), buf.size())) > 0) { while((bytes_read = zip_fread(zf, buf.data(), buf.size())) > 0) {
if(std::fwrite(buf.data(), sizeof(char), bytes_read, out) < static_cast<std::size_t>(bytes_read)) { if(std::fwrite(buf.data(), sizeof(char), bytes_read, out) < static_cast<std::size_t>(bytes_read)) {
_lastError = Utility::format(error_format.data(), file, "not enough bytes written."); _lastError = Utility::format(error_format.data(), file, "not enough bytes written.");
LOG_ERROR(_lastError);
return false; return false;
} }
} }
if(bytes_read == -1) { if(bytes_read == -1) {
_lastError = Utility::format(error_format.data(), file, "couldn't read bytes from archive."); _lastError = Utility::format(error_format.data(), file, "couldn't read bytes from archive.");
LOG_ERROR(_lastError);
return false; return false;
} }
} }