Skip to content

Commit de110b6

Browse files
Improved logging
1 parent 65de1f1 commit de110b6

12 files changed

Lines changed: 111 additions & 72 deletions

src/adapters/controllers/settings_controller.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ bool SettingsController::keyIsValid(int key)
5252
int settingKeysEnd = static_cast<int>(SettingKeys::SettingKeys_END);
5353
if(key >= settingKeysEnd || key < 0)
5454
{
55-
qWarning() << "Invalid setting-key: " << key;
55+
qWarning() << QString("Invalid setting-key: %1").arg(key);
5656
return false;
5757
}
5858

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

src/application/services/authentication_service.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "authentication_service.hpp"
2+
#include <QDebug>
23
#include "i_authentication_gateway.hpp"
34

45

@@ -24,6 +25,8 @@ void AuthenticationService::loginUser(const LoginModel& loginModel)
2425
{
2526
if(!loginModel.isValid())
2627
{
28+
qWarning() << "Login failed due to invalid login model";
29+
2730
emit loginFinished(false);
2831
return;
2932
}
@@ -43,6 +46,7 @@ void AuthenticationService::registerUser(const RegisterModel& registerModel)
4346
if(status != RegisterModel::RegistrationResult::Valid)
4447
{
4548
QString failureReason = registerModel.generateErrorMessage(status);
49+
qWarning() << QString("Failed registration: %1").arg(failureReason);
4650
emit registrationFinished(false, failureReason);
4751
return;
4852
}
@@ -54,6 +58,7 @@ void AuthenticationService::processAuthenticationResult(const QString& token)
5458
{
5559
if(token.isEmpty())
5660
{
61+
qWarning() << "Authentication token is empty";
5762
emit loginFinished(false);
5863
return;
5964
}

src/application/services/book_service.cpp

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ BookOperationStatus BookService::addBook(const QString& filePath)
4040
auto bookMetaData = m_bookMetadataHelper->getBookMetaData(filePath);
4141
if(!bookMetaData)
4242
{
43-
qWarning() << "Could not open book at path: " << filePath;
43+
qWarning() << QString("Could not open book at path: %1 ").arg(filePath);
4444
return BookOperationStatus::OpeningBookFailed;
4545
}
4646

@@ -72,8 +72,9 @@ BookOperationStatus BookService::deleteBook(const QUuid& uuid)
7272
{
7373
if(!getBook(uuid))
7474
{
75-
qWarning() << "Could not delete book with uuid: " << uuid
76-
<< ". The book was not found.";
75+
qWarning() << QString("Could not delete book with uuid: %1. "
76+
"No book with this uuid exists.")
77+
.arg(uuid.toString());
7778
return BookOperationStatus::BookDoesNotExist;
7879
}
7980

@@ -100,8 +101,9 @@ BookOperationStatus BookService::uninstallBook(const QUuid& uuid)
100101
auto book = getBook(uuid);
101102
if(!book)
102103
{
103-
qWarning() << "Could not uninstall book with uuid: " << uuid
104-
<< ". No book with this uuid exists.";
104+
qWarning() << QString("Could not uninstall book with uuid: %1. "
105+
"No book with this uuid exists.")
106+
.arg(uuid.toString());
105107
return BookOperationStatus::BookDoesNotExist;
106108
}
107109

@@ -119,8 +121,9 @@ BookOperationStatus BookService::updateBook(const Book& newBook)
119121
auto book = getBook(newBook.getUuid());
120122
if(!book)
121123
{
122-
qWarning() << "Could not update book with uuid: " << newBook.getUuid()
123-
<< ". No book with this uuid exists.";
124+
qWarning() << QString("Failed updating book with uuid: %1."
125+
"No book with this uuid exists.")
126+
.arg(newBook.getUuid().toString());
124127
return BookOperationStatus::BookDoesNotExist;
125128
}
126129

@@ -148,16 +151,17 @@ BookOperationStatus BookService::addTag(const QUuid& uuid,
148151
auto book = getBook(uuid);
149152
if(!book)
150153
{
151-
qWarning() << "Adding tag to book with uuid: " << uuid << " failed."
152-
<< " No book with this uuid exists.";
154+
qWarning() << QString("Adding tag to book with uuid: %1 failed. No "
155+
"book with this uuid exists.")
156+
.arg(uuid.toString());
153157
return BookOperationStatus::BookDoesNotExist;
154158
}
155159

156160
if(!book->addTag(tag))
157161
{
158-
qWarning() << "Adding tag called: " << tag.getName()
159-
<< " to book with uuid: " << uuid << " failed."
160-
<< " A tag with this name already exists.";
162+
qWarning() << QString("Adding tag called: %1 to book with uuid: %2 "
163+
"failed. A tag with this name already exists.")
164+
.arg(tag.getName(), uuid.toString());
161165
return BookOperationStatus::TagAlreadyExists;
162166
}
163167

@@ -176,16 +180,17 @@ BookOperationStatus BookService::removeTag(const QUuid& bookUuid,
176180
auto book = getBook(bookUuid);
177181
if(!book)
178182
{
179-
qWarning() << "Removing tag from book with uuid: " << bookUuid
180-
<< " failed. No book with this uuid exists.";
183+
qWarning() << QString("Removing tag from book with uuid: %1 failed. No "
184+
"book with this uuid exists.")
185+
.arg(bookUuid.toString());
181186
return BookOperationStatus::BookDoesNotExist;
182187
}
183188

184189
if(!book->removeTag(tagUuid))
185190
{
186-
qWarning() << "Removing tag with uuid: " << tagUuid
187-
<< " from book with uuid: " << bookUuid << " failed."
188-
<< " No tag with this uuid exists.";
191+
qWarning() << QString("Removing tag with uuid: %1 from book with "
192+
"uuid: %2 failed. No tag with this uuid exists.")
193+
.arg(tagUuid.toString(), bookUuid.toString());
189194
return BookOperationStatus::TagDoesNotExist;
190195
}
191196

@@ -205,17 +210,18 @@ BookOperationStatus BookService::renameTag(const QUuid& bookUuid,
205210
auto book = getBook(bookUuid);
206211
if(!book)
207212
{
208-
qWarning() << "Renaming tag from book with uuid: " << bookUuid
209-
<< " failed. No book with this uuid exists.";
213+
qWarning() << QString("Renaming tag from book with uuid: %1 failed."
214+
"No book with this uuid exists.")
215+
.arg(bookUuid.toString());
210216
return BookOperationStatus::BookDoesNotExist;
211217
}
212218

213219
if(!book->renameTag(tagUuid, newName))
214220
{
215-
qWarning() << "Renaming tag with uuid: " << tagUuid
216-
<< " from book with uuid: " << bookUuid << " failed."
217-
<< " No tag with this uuid exists or a tag with this name"
218-
" already exists.";
221+
qWarning() << QString("Renaming tag with uuid: %1 from book with "
222+
"uuid: %2 failed. No tag with this uuid exists "
223+
"or a tag with this name already exists.")
224+
.arg(tagUuid.toString(), bookUuid.toString());
219225
return BookOperationStatus::TagDoesNotExist;
220226
}
221227

@@ -279,8 +285,9 @@ BookOperationStatus BookService::saveBookToFile(const QUuid& uuid,
279285
auto book = getBook(uuid);
280286
if(!book)
281287
{
282-
qWarning() << "Saving book with uuid: " << uuid << " to file failed."
283-
<< " No book with this uuid exists.";
288+
qWarning() << QString("Saving book with uuid: %1 to folder %2 failed."
289+
" No book with this uuid exists.")
290+
.arg(uuid.toString(), pathToFolder.path());
284291
return BookOperationStatus::BookDoesNotExist;
285292
}
286293

@@ -290,9 +297,10 @@ BookOperationStatus BookService::saveBookToFile(const QUuid& uuid,
290297
auto result = QFile::copy(currentBookPath.path(), destinaton.path());
291298
if(!result)
292299
{
293-
qWarning() << "Saving book with uuid: " << uuid
294-
<< " to folder: " << pathToFolder.toLocalFile() << " failed."
295-
<< " No book with this uuid exists.";
300+
qWarning() << QString("Saving book with uuid: %1 to folder: %2 failed. "
301+
"No book with this uuid exists.")
302+
.arg(uuid.toString(), pathToFolder.path());
303+
296304
return BookOperationStatus::OperationFailed;
297305
}
298306

src/application/services/settings_service.cpp

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,9 @@ void SettingsService::resetSettingGroup(SettingGroups group)
7676
}
7777
else
7878
{
79-
qWarning() << "Failed converting setting-key from default settings "
80-
"file with value: "
81-
<< defaultSettingKey << " to an enum.";
79+
qWarning() << QString("Failed converting setting-key from default "
80+
"settings file with value: %1 to an enum.")
81+
.arg(defaultSettingKey);
8282
continue;
8383
}
8484
}
@@ -140,12 +140,8 @@ void SettingsService::loadDefaultSettings(SettingGroups group,
140140

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

151147
auto defaultSettingValue =
@@ -158,20 +154,32 @@ void SettingsService::loadDefaultSettings(SettingGroups group,
158154
}
159155
else
160156
{
161-
qWarning() << "Failed converting setting-key from default settings "
162-
"file with value: "
163-
<< defaultSettingKey << " to an enum.";
164-
continue;
157+
qWarning() << QString("Failed converting setting-key from default "
158+
"settings file with value: %1 to an enum.")
159+
.arg(defaultSettingKey);
165160
}
166161
}
167162
}
168163

164+
bool SettingsService::defaultSettingAlreadyExists(const QString& key,
165+
SettingGroups group)
166+
{
167+
auto groupName = getNameForEnumValue(group);
168+
169+
m_settings->beginGroup(groupName);
170+
bool exists = m_settings->contains(key);
171+
m_settings->endGroup();
172+
173+
return exists;
174+
}
175+
169176
QJsonObject SettingsService::getDefaultSettings(const QString& path)
170177
{
171178
QFile defaultSettingsFile(path);
172179
if(!defaultSettingsFile.open(QIODevice::ReadOnly | QIODevice::Text))
173180
{
174-
qWarning() << "Failed to open default settings file: " << path << "!";
181+
qWarning() << QString("Failed opening the default settings file: %1")
182+
.arg(path);
175183
}
176184

177185
QByteArray rawJson = defaultSettingsFile.readAll();

src/application/services/settings_service.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public slots:
3131
void loadDefaultSettings(SettingGroups group, const QString& filePath);
3232
QJsonObject getDefaultSettings(const QString& path);
3333
bool settingsAreValid();
34+
bool defaultSettingAlreadyExists(const QString& key, SettingGroups group);
3435

3536
template<typename Enum>
3637
QString getNameForEnumValue(Enum value) const;

src/application/services/user_service.cpp

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,30 +96,43 @@ QUuid UserService::addTag(const domain::entities::Tag& tag)
9696
{
9797
auto success = m_user.addTag(tag);
9898
if(!success)
99+
{
100+
qWarning() << QString("Failed adding tag with name: %1 to user.")
101+
.arg(tag.getName());
99102
return QUuid();
103+
}
100104

101105
return tag.getUuid();
102106
}
103107

104108
bool UserService::deleteTag(const QUuid& uuid)
105109
{
106110
auto success = m_user.deleteTag(uuid);
107-
if(success)
108-
m_userStorageGateway->deleteTag(m_authenticationToken, uuid);
111+
if(!success)
112+
{
113+
qWarning() << QString("Failed deleting tag with uuid: %1 from user.")
114+
.arg(uuid.toString());
115+
return false;
116+
}
109117

110-
return success;
118+
m_userStorageGateway->deleteTag(m_authenticationToken, uuid);
119+
return true;
111120
}
112121

113122
bool UserService::renameTag(const QUuid& uuid, const QString& newName)
114123
{
115124
auto success = m_user.renameTag(uuid, newName);
116-
if(success)
125+
if(!success)
117126
{
118-
m_userStorageGateway->renameTag(m_authenticationToken, uuid,
119-
m_user.getTagByUuid(uuid)->getName());
127+
qWarning() << QString("Failed renaming tag with uuid: %1 with new "
128+
"name: %2 from user.")
129+
.arg(uuid.toString(), newName);
130+
return false;
120131
}
121132

122-
return success;
133+
m_userStorageGateway->renameTag(m_authenticationToken, uuid,
134+
m_user.getTagByUuid(uuid)->getName());
135+
return true;
123136
}
124137

125138
void UserService::proccessUserInformation(const domain::entities::User& user,

src/application/utility/book_metadata_helper.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ std::optional<BookMetaData> BookMetadataHelper::getBookMetaData(
2424
auto setupSucceeded = setupDocument(filePath);
2525
if(!setupSucceeded)
2626
{
27-
qWarning() << "Failed to open document at path: " << filePath << ".";
27+
qWarning() << QString("Failed opening document at: %1.").arg(filePath);
2828
return std::nullopt;
2929
}
3030

src/application/utility/downloaded_books_tracker.cpp

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ std::vector<Book> DownloadedBooksTracker::getTrackedBooks()
2525
QFile metaFile(libraryDir.filePath(metaFileName));
2626
if(!metaFile.open(QFile::ReadOnly | QIODevice::Text))
2727
{
28-
qWarning() << "Failed opening .libmeta file at: "
29-
<< metaFile.fileName()
30-
<< " for operation 'getTrackedBooks'";
28+
qWarning() << QString("Getting tracked book failed."
29+
"Failed opening .libmeta file at: %1")
30+
.arg(metaFile.fileName());
3131
continue;
3232
}
3333

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

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

7677
if(file.exists() || !file.open(QFile::WriteOnly))
7778
{
78-
qWarning() << "Failed opening .libmeta file at: " << file.fileName()
79-
<< " for operation 'trackBook'";
79+
qWarning() << QString("Tracking book failed."
80+
"Failed opening .libmeta file at: %1")
81+
.arg(file.fileName());
8082
return false;
8183
}
8284

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

102105
return success;
@@ -139,8 +142,8 @@ QJsonDocument DownloadedBooksTracker::parseLibMetaFile(QByteArray&& data) const
139142
auto jsonDoc = QJsonDocument::fromJson(data, &parseError);
140143
if(parseError.error != QJsonParseError::NoError)
141144
{
142-
qWarning() << "Error parsing .libmeta file:"
143-
<< parseError.errorString();
145+
qWarning() << QString("Error parsing .libmeta file: %1")
146+
.arg(parseError.errorString());
144147
}
145148

146149
return jsonDoc;

src/infrastructure/persistance/authentication_access.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ ServerReplyStatus AuthenticationAccess::validateServerReply(
5555
if(replyHasError || statusCode != expectedStatusCode)
5656
{
5757
QString errorMessage = m_reply->readAll();
58-
qWarning() << "Authentication error: " << m_reply->errorString()
59-
<< "\nServer reply: " << errorMessage;
58+
qWarning() << QString("Authentication error: %1 \nServer replied: %2")
59+
.arg(m_reply->errorString(), errorMessage);
6060

6161
return ServerReplyStatus {
6262
.success = false,

0 commit comments

Comments
 (0)