Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enables the logger to handle std::string directly #393

Merged
merged 15 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/libcommon/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ set(libcommon_SRCS
info/proxyconfiginfo.h info/proxyconfiginfo.cpp
log/sentry/sentryhandler.h log/sentry/sentryhandler.cpp
log/sentry/sentryuser.h
log/customlogwstream.h
log/customlogstreams.h
theme/theme.h theme/theme.cpp
)

Expand Down
161 changes: 161 additions & 0 deletions src/libcommon/log/customlogstreams.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
/*
* Infomaniak kDrive - Desktop
* Copyright (C) 2023-2024 Infomaniak Network SA
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <iostream>
#include <sstream>

#include <QIODevice>

#include "libcommon/utility/types.h"

class CustomLogStream : private std::stringstream {
public:
CustomLogStream() = default;
inline CustomLogStream(const CustomLogStream &str) = delete;
CustomLogStream &operator=(const CustomLogStream &) = delete;

std::string str() const { return std::basic_stringstream<char>::str(); }

template<KDC::LogableType C>
friend inline CustomLogStream &operator<<(CustomLogStream &os, C e) {
return os << KDC::toStringWithCode(e);
}


// We need to cast to std::stringstream as operators<<(std::stringstream, const wchar_t *str) and const std::string
// &str) are defined outside of the class std::stringstream and therefore it is not applicable to the current object
// because of the private inheritance
CustomLogStream &operator<<(const char *str) {
static_cast<std::stringstream &>(*this) << str;
return *this;
}
CustomLogStream &operator<<(const std::string &str) {
static_cast<std::stringstream &>(*this) << str;
return *this;
}

CustomLogStream &operator<<(bool b) {
std::stringstream::operator<<(std::boolalpha);
std::stringstream::operator<<(b);
return *this;
}
CustomLogStream &operator<<(int i) {
std::stringstream::operator<<(i);
return *this;
}
CustomLogStream &operator<<(long i64) {
std::stringstream::operator<<(i64);
return *this;
}
CustomLogStream &operator<<(unsigned int ui) {
std::stringstream::operator<<(ui);
return *this;
}
CustomLogStream &operator<<(long long i64) {
std::stringstream::operator<<(i64);
return *this;
}
CustomLogStream &operator<<(unsigned long ul) {
std::stringstream::operator<<(ul);
return *this;
}
CustomLogStream &operator<<(unsigned long long ui64) {
std::stringstream::operator<<(ui64);
return *this;
}
CustomLogStream &operator<<(double d) {
std::stringstream::operator<<(d);
return *this;
}
CustomLogStream &operator<<(const QIODevice *ptr) {
std::stringstream::operator<<(ptr);
return *this;
}
CustomLogStream &operator<<(const std::error_code &code) {
std::stringstream::operator<<(code.value());
return *this;
}
};

class CustomLogWStream : private std::wstringstream {
public:
CustomLogWStream() = default;
inline CustomLogWStream(const CustomLogWStream &wstr) = delete;
CustomLogWStream &operator=(const CustomLogWStream &) = delete;

std::wstring str() const { return std::basic_stringstream<wchar_t>::str(); }

template<KDC::LogableType C>
friend inline CustomLogWStream &operator<<(CustomLogWStream &os, C e) {
return os << KDC::typesUtility::stringToWideString(toStringWithCode(e));
}

// We need to cast to std::wstringstream as operators<<(std::wstringstream, const wchar_t *str and const std::wstring
// &str) are defined outside of the class std::wstringstream and therefore it is not applicable to the current object
// because of the private inheritance
CustomLogWStream &operator<<(const wchar_t *str) {
static_cast<std::wstringstream &>(*this) << str;
return *this;
}
CustomLogWStream &operator<<(const std::wstring &str) {
static_cast<std::wstringstream &>(*this) << str;
return *this;
}

CustomLogWStream &operator<<(bool b) {
std::wstringstream::operator<<(std::boolalpha);
std::wstringstream::operator<<(b);
return *this;
}
CustomLogWStream &operator<<(int i) {
std::wstringstream::operator<<(i);
return *this;
}
CustomLogWStream &operator<<(unsigned int ui) {
std::wstringstream::operator<<(ui);
return *this;
}
CustomLogWStream &operator<<(long i64) {
std::wstringstream::operator<<(i64);
return *this;
}
CustomLogWStream &operator<<(long long i64) {
std::wstringstream::operator<<(i64);
return *this;
}
CustomLogWStream &operator<<(unsigned long ul) {
std::wstringstream::operator<<(ul);
return *this;
}
CustomLogWStream &operator<<(unsigned long long ui64) {
std::wstringstream::operator<<(ui64);
return *this;
}
CustomLogWStream &operator<<(double d) {
std::wstringstream::operator<<(d);
return *this;
}
CustomLogWStream &operator<<(const QIODevice *ptr) {
std::wstringstream::operator<<(ptr);
return *this;
}
CustomLogWStream &operator<<(const std::error_code &code) {
std::wstringstream::operator<<(code.value());
return *this;
}
};
74 changes: 0 additions & 74 deletions src/libcommon/log/customlogwstream.h

This file was deleted.

6 changes: 0 additions & 6 deletions src/libcommon/utility/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
#include <variant>
#include <qdebug.h>
#include <signal.h>
#include "libcommon/log/customlogwstream.h"

namespace KDC {

Expand Down Expand Up @@ -609,11 +608,6 @@ inline std::ostream &operator<<(std::ostream &os, C e) {
return os << toStringWithCode(e);
}

template<LogableType C>
inline CustomLogWStream &operator<<(CustomLogWStream &os, C e) {
return os << typesUtility::stringToWideString(toStringWithCode(e));
}

template<LogableType C>
inline QDebug &operator<<(QDebug &os, C e) {
return os << toStringWithCode(e).c_str();
Expand Down
6 changes: 3 additions & 3 deletions src/libcommonserver/db/db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ bool Db::init(const std::string &version) {
queryFree(CREATE_VERSION_TABLE_ID);

// Insert version
LOG_DEBUG(_logger, "Insert version " << version.c_str());
LOG_DEBUG(_logger, "Insert version " << version);
ClementKunz marked this conversation as resolved.
Show resolved Hide resolved
if (!prepareQuery(INSERT_VERSION_REQUEST_ID, INSERT_VERSION_REQUEST)) return false;
if (!insertVersion(version)) {
LOG_WARN(_logger, "Error in Db::insertVersion");
Expand All @@ -386,7 +386,7 @@ bool Db::init(const std::string &version) {
queryFree(INSERT_VERSION_REQUEST_ID);

// Create DB
LOG_INFO(_logger, "Create " << dbType().c_str() << " DB");
LOG_INFO(_logger, "Create " << dbType() << " DB");
ClementKunz marked this conversation as resolved.
Show resolved Hide resolved
if (bool retry = false; !create(retry)) {
if (retry) {
LOG_WARN(_logger, "Error in Db::create - Retry");
Expand All @@ -413,7 +413,7 @@ bool Db::init(const std::string &version) {
void Db::startTransaction() {
if (!_transaction) {
if (!_sqliteDb->startTransaction()) {
LOG_WARN(_logger, "ERROR starting transaction: " << _sqliteDb->error().c_str());
LOG_WARN(_logger, "ERROR starting transaction: " << _sqliteDb->error());
ClementKunz marked this conversation as resolved.
Show resolved Hide resolved
return;
}
_transaction = true;
Expand Down
1 change: 0 additions & 1 deletion src/libcommonserver/io/iohelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include "libcommonserver/io/filestat.h"
#include "libcommonserver/io/iohelper.h"
#include "libcommonserver/utility/utility.h" // Path2WStr
#include "libcommon/utility/utility.h"

#include "config.h" // APPLICATION

Expand Down
4 changes: 2 additions & 2 deletions src/libcommonserver/log/customrollingfileappender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -303,8 +303,8 @@ void CustomRollingFileAppender::checkForExpiredFiles() {
// Delete expired files
if (_expire > 0 && entry.path().string().find(APPLICATION_NAME) != std::string::npos) {
const auto now = std::chrono::system_clock::now();
auto lastModified = std::chrono::system_clock::from_time_t(fileStat.modtime);
auto expireDateTime = lastModified + std::chrono::seconds(_expire);
const auto lastModified = std::chrono::system_clock::from_time_t(fileStat.modtime); // Only 1s precision.
const auto expireDateTime = lastModified + std::chrono::seconds(_expire);
if (expireDateTime < now) {
log4cplus::file_remove(Utility::s2ws(entry.path().string()));
continue;
Expand Down
Loading