Skip to content

Commit

Permalink
Improved logging
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidLazarescu committed Feb 12, 2023
1 parent 65de1f1 commit de110b6
Show file tree
Hide file tree
Showing 12 changed files with 111 additions and 72 deletions.
4 changes: 2 additions & 2 deletions src/adapters/controllers/settings_controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ bool SettingsController::keyIsValid(int key)
int settingKeysEnd = static_cast<int>(SettingKeys::SettingKeys_END);
if(key >= settingKeysEnd || key < 0)
{
qWarning() << "Invalid setting-key: " << key;
qWarning() << QString("Invalid setting-key: %1").arg(key);
return false;
}

Expand All @@ -64,7 +64,7 @@ bool SettingsController::groupIsValid(int group)
int settingGroupsEnd = static_cast<int>(SettingGroups::SettingGroups_END);
if(group >= settingGroupsEnd || group < 0)
{
qWarning() << "Invalid setting-group: " << group;
qWarning() << QString("Invalid setting-group: %1").arg(group);
return false;
}

Expand Down
5 changes: 5 additions & 0 deletions src/application/services/authentication_service.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "authentication_service.hpp"
#include <QDebug>
#include "i_authentication_gateway.hpp"


Expand All @@ -24,6 +25,8 @@ void AuthenticationService::loginUser(const LoginModel& loginModel)
{
if(!loginModel.isValid())
{
qWarning() << "Login failed due to invalid login model";

emit loginFinished(false);
return;
}
Expand All @@ -43,6 +46,7 @@ void AuthenticationService::registerUser(const RegisterModel& registerModel)
if(status != RegisterModel::RegistrationResult::Valid)
{
QString failureReason = registerModel.generateErrorMessage(status);
qWarning() << QString("Failed registration: %1").arg(failureReason);
emit registrationFinished(false, failureReason);
return;
}
Expand All @@ -54,6 +58,7 @@ void AuthenticationService::processAuthenticationResult(const QString& token)
{
if(token.isEmpty())
{
qWarning() << "Authentication token is empty";
emit loginFinished(false);
return;
}
Expand Down
64 changes: 36 additions & 28 deletions src/application/services/book_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ BookOperationStatus BookService::addBook(const QString& filePath)
auto bookMetaData = m_bookMetadataHelper->getBookMetaData(filePath);
if(!bookMetaData)
{
qWarning() << "Could not open book at path: " << filePath;
qWarning() << QString("Could not open book at path: %1 ").arg(filePath);
return BookOperationStatus::OpeningBookFailed;
}

Expand Down Expand Up @@ -72,8 +72,9 @@ BookOperationStatus BookService::deleteBook(const QUuid& uuid)
{
if(!getBook(uuid))
{
qWarning() << "Could not delete book with uuid: " << uuid
<< ". The book was not found.";
qWarning() << QString("Could not delete book with uuid: %1. "
"No book with this uuid exists.")
.arg(uuid.toString());
return BookOperationStatus::BookDoesNotExist;
}

Expand All @@ -100,8 +101,9 @@ BookOperationStatus BookService::uninstallBook(const QUuid& uuid)
auto book = getBook(uuid);
if(!book)
{
qWarning() << "Could not uninstall book with uuid: " << uuid
<< ". No book with this uuid exists.";
qWarning() << QString("Could not uninstall book with uuid: %1. "
"No book with this uuid exists.")
.arg(uuid.toString());
return BookOperationStatus::BookDoesNotExist;
}

Expand All @@ -119,8 +121,9 @@ BookOperationStatus BookService::updateBook(const Book& newBook)
auto book = getBook(newBook.getUuid());
if(!book)
{
qWarning() << "Could not update book with uuid: " << newBook.getUuid()
<< ". No book with this uuid exists.";
qWarning() << QString("Failed updating book with uuid: %1."
"No book with this uuid exists.")
.arg(newBook.getUuid().toString());
return BookOperationStatus::BookDoesNotExist;
}

Expand Down Expand Up @@ -148,16 +151,17 @@ BookOperationStatus BookService::addTag(const QUuid& uuid,
auto book = getBook(uuid);
if(!book)
{
qWarning() << "Adding tag to book with uuid: " << uuid << " failed."
<< " No book with this uuid exists.";
qWarning() << QString("Adding tag to book with uuid: %1 failed. No "
"book with this uuid exists.")
.arg(uuid.toString());
return BookOperationStatus::BookDoesNotExist;
}

if(!book->addTag(tag))
{
qWarning() << "Adding tag called: " << tag.getName()
<< " to book with uuid: " << uuid << " failed."
<< " A tag with this name already exists.";
qWarning() << QString("Adding tag called: %1 to book with uuid: %2 "
"failed. A tag with this name already exists.")
.arg(tag.getName(), uuid.toString());
return BookOperationStatus::TagAlreadyExists;
}

Expand All @@ -176,16 +180,17 @@ BookOperationStatus BookService::removeTag(const QUuid& bookUuid,
auto book = getBook(bookUuid);
if(!book)
{
qWarning() << "Removing tag from book with uuid: " << bookUuid
<< " failed. No book with this uuid exists.";
qWarning() << QString("Removing tag from book with uuid: %1 failed. No "
"book with this uuid exists.")
.arg(bookUuid.toString());
return BookOperationStatus::BookDoesNotExist;
}

if(!book->removeTag(tagUuid))
{
qWarning() << "Removing tag with uuid: " << tagUuid
<< " from book with uuid: " << bookUuid << " failed."
<< " No tag with this uuid exists.";
qWarning() << QString("Removing tag with uuid: %1 from book with "
"uuid: %2 failed. No tag with this uuid exists.")
.arg(tagUuid.toString(), bookUuid.toString());
return BookOperationStatus::TagDoesNotExist;
}

Expand All @@ -205,17 +210,18 @@ BookOperationStatus BookService::renameTag(const QUuid& bookUuid,
auto book = getBook(bookUuid);
if(!book)
{
qWarning() << "Renaming tag from book with uuid: " << bookUuid
<< " failed. No book with this uuid exists.";
qWarning() << QString("Renaming tag from book with uuid: %1 failed."
"No book with this uuid exists.")
.arg(bookUuid.toString());
return BookOperationStatus::BookDoesNotExist;
}

if(!book->renameTag(tagUuid, newName))
{
qWarning() << "Renaming tag with uuid: " << tagUuid
<< " from book with uuid: " << bookUuid << " failed."
<< " No tag with this uuid exists or a tag with this name"
" already exists.";
qWarning() << QString("Renaming tag with uuid: %1 from book with "
"uuid: %2 failed. No tag with this uuid exists "
"or a tag with this name already exists.")
.arg(tagUuid.toString(), bookUuid.toString());
return BookOperationStatus::TagDoesNotExist;
}

Expand Down Expand Up @@ -279,8 +285,9 @@ BookOperationStatus BookService::saveBookToFile(const QUuid& uuid,
auto book = getBook(uuid);
if(!book)
{
qWarning() << "Saving book with uuid: " << uuid << " to file failed."
<< " No book with this uuid exists.";
qWarning() << QString("Saving book with uuid: %1 to folder %2 failed."
" No book with this uuid exists.")
.arg(uuid.toString(), pathToFolder.path());
return BookOperationStatus::BookDoesNotExist;
}

Expand All @@ -290,9 +297,10 @@ BookOperationStatus BookService::saveBookToFile(const QUuid& uuid,
auto result = QFile::copy(currentBookPath.path(), destinaton.path());
if(!result)
{
qWarning() << "Saving book with uuid: " << uuid
<< " to folder: " << pathToFolder.toLocalFile() << " failed."
<< " No book with this uuid exists.";
qWarning() << QString("Saving book with uuid: %1 to folder: %2 failed. "
"No book with this uuid exists.")
.arg(uuid.toString(), pathToFolder.path());

return BookOperationStatus::OperationFailed;
}

Expand Down
36 changes: 22 additions & 14 deletions src/application/services/settings_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ void SettingsService::resetSettingGroup(SettingGroups group)
}
else
{
qWarning() << "Failed converting setting-key from default settings "
"file with value: "
<< defaultSettingKey << " to an enum.";
qWarning() << QString("Failed converting setting-key from default "
"settings file with value: %1 to an enum.")
.arg(defaultSettingKey);
continue;
}
}
Expand Down Expand Up @@ -140,12 +140,8 @@ void SettingsService::loadDefaultSettings(SettingGroups group,

for(const auto& defaultSettingKey : defaultSettings.keys())
{
// Only add default setting, if it does not already exist
auto groupName = getNameForEnumValue(group);
m_settings->beginGroup(groupName);
bool alreadyContainsKey = m_settings->contains(defaultSettingKey);
m_settings->endGroup();
if(alreadyContainsKey)
// Only load default settings which dont yet exist
if(defaultSettingAlreadyExists(defaultSettingKey, group))
continue;

auto defaultSettingValue =
Expand All @@ -158,20 +154,32 @@ void SettingsService::loadDefaultSettings(SettingGroups group,
}
else
{
qWarning() << "Failed converting setting-key from default settings "
"file with value: "
<< defaultSettingKey << " to an enum.";
continue;
qWarning() << QString("Failed converting setting-key from default "
"settings file with value: %1 to an enum.")
.arg(defaultSettingKey);
}
}
}

bool SettingsService::defaultSettingAlreadyExists(const QString& key,
SettingGroups group)
{
auto groupName = getNameForEnumValue(group);

m_settings->beginGroup(groupName);
bool exists = m_settings->contains(key);
m_settings->endGroup();

return exists;
}

QJsonObject SettingsService::getDefaultSettings(const QString& path)
{
QFile defaultSettingsFile(path);
if(!defaultSettingsFile.open(QIODevice::ReadOnly | QIODevice::Text))
{
qWarning() << "Failed to open default settings file: " << path << "!";
qWarning() << QString("Failed opening the default settings file: %1")
.arg(path);
}

QByteArray rawJson = defaultSettingsFile.readAll();
Expand Down
1 change: 1 addition & 0 deletions src/application/services/settings_service.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public slots:
void loadDefaultSettings(SettingGroups group, const QString& filePath);
QJsonObject getDefaultSettings(const QString& path);
bool settingsAreValid();
bool defaultSettingAlreadyExists(const QString& key, SettingGroups group);

template<typename Enum>
QString getNameForEnumValue(Enum value) const;
Expand Down
27 changes: 20 additions & 7 deletions src/application/services/user_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,30 +96,43 @@ QUuid UserService::addTag(const domain::entities::Tag& tag)
{
auto success = m_user.addTag(tag);
if(!success)
{
qWarning() << QString("Failed adding tag with name: %1 to user.")
.arg(tag.getName());
return QUuid();
}

return tag.getUuid();
}

bool UserService::deleteTag(const QUuid& uuid)
{
auto success = m_user.deleteTag(uuid);
if(success)
m_userStorageGateway->deleteTag(m_authenticationToken, uuid);
if(!success)
{
qWarning() << QString("Failed deleting tag with uuid: %1 from user.")
.arg(uuid.toString());
return false;
}

return success;
m_userStorageGateway->deleteTag(m_authenticationToken, uuid);
return true;
}

bool UserService::renameTag(const QUuid& uuid, const QString& newName)
{
auto success = m_user.renameTag(uuid, newName);
if(success)
if(!success)
{
m_userStorageGateway->renameTag(m_authenticationToken, uuid,
m_user.getTagByUuid(uuid)->getName());
qWarning() << QString("Failed renaming tag with uuid: %1 with new "
"name: %2 from user.")
.arg(uuid.toString(), newName);
return false;
}

return success;
m_userStorageGateway->renameTag(m_authenticationToken, uuid,
m_user.getTagByUuid(uuid)->getName());
return true;
}

void UserService::proccessUserInformation(const domain::entities::User& user,
Expand Down
2 changes: 1 addition & 1 deletion src/application/utility/book_metadata_helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ std::optional<BookMetaData> BookMetadataHelper::getBookMetaData(
auto setupSucceeded = setupDocument(filePath);
if(!setupSucceeded)
{
qWarning() << "Failed to open document at path: " << filePath << ".";
qWarning() << QString("Failed opening document at: %1.").arg(filePath);
return std::nullopt;
}

Expand Down
25 changes: 14 additions & 11 deletions src/application/utility/downloaded_books_tracker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ std::vector<Book> DownloadedBooksTracker::getTrackedBooks()
QFile metaFile(libraryDir.filePath(metaFileName));
if(!metaFile.open(QFile::ReadOnly | QIODevice::Text))
{
qWarning() << "Failed opening .libmeta file at: "
<< metaFile.fileName()
<< " for operation 'getTrackedBooks'";
qWarning() << QString("Getting tracked book failed."
"Failed opening .libmeta file at: %1")
.arg(metaFile.fileName());
continue;
}

Expand All @@ -53,8 +53,9 @@ std::optional<Book> DownloadedBooksTracker::getTrackedBook(const QUuid& uuid)
QFile metaFile(libraryDir.path() + "/" + fileName + m_fileExtension);
if(!metaFile.open(QFile::ReadOnly))
{
qWarning() << "Failed opening .libmeta file at: " << metaFile.fileName()
<< " for operation 'getTrackedBook'";
qWarning() << QString("Getting tracked book failed."
"Failed opening .libmeta file at: %1")
.arg(metaFile.fileName());
return std::nullopt;
}

Expand All @@ -75,8 +76,9 @@ bool DownloadedBooksTracker::trackBook(const Book& book)

if(file.exists() || !file.open(QFile::WriteOnly))
{
qWarning() << "Failed opening .libmeta file at: " << file.fileName()
<< " for operation 'trackBook'";
qWarning() << QString("Tracking book failed."
"Failed opening .libmeta file at: %1")
.arg(file.fileName());
return false;
}

Expand All @@ -95,8 +97,9 @@ bool DownloadedBooksTracker::untrackBook(const QUuid& uuid)
auto success = libraryDir.remove(fileToUntrack);
if(!success)
{
qWarning() << "Failed removing .libmeta file called: " << fileToUntrack
<< " for operation 'untrackBook'";
qWarning() << QString("Untracking book failed."
"Failed deleting .libmeta file: %1")
.arg(fileToUntrack);
}

return success;
Expand Down Expand Up @@ -139,8 +142,8 @@ QJsonDocument DownloadedBooksTracker::parseLibMetaFile(QByteArray&& data) const
auto jsonDoc = QJsonDocument::fromJson(data, &parseError);
if(parseError.error != QJsonParseError::NoError)
{
qWarning() << "Error parsing .libmeta file:"
<< parseError.errorString();
qWarning() << QString("Error parsing .libmeta file: %1")
.arg(parseError.errorString());
}

return jsonDoc;
Expand Down
4 changes: 2 additions & 2 deletions src/infrastructure/persistance/authentication_access.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ ServerReplyStatus AuthenticationAccess::validateServerReply(
if(replyHasError || statusCode != expectedStatusCode)
{
QString errorMessage = m_reply->readAll();
qWarning() << "Authentication error: " << m_reply->errorString()
<< "\nServer reply: " << errorMessage;
qWarning() << QString("Authentication error: %1 \nServer replied: %2")
.arg(m_reply->errorString(), errorMessage);

return ServerReplyStatus {
.success = false,
Expand Down
Loading

0 comments on commit de110b6

Please sign in to comment.