From 1e2e505773ae2531092093b99040e38e6fa4e779 Mon Sep 17 00:00:00 2001 From: Luc Guyot Date: Fri, 15 Nov 2024 11:34:12 +0100 Subject: [PATCH 01/15] Enables the logger to handle std::string directly --- src/libcommon/CMakeLists.txt | 2 +- ...{customlogwstream.h => customlogstreams.h} | 75 ++++++++++++- src/libcommon/utility/types.h | 7 +- src/libcommonserver/db/db.cpp | 6 +- src/libcommonserver/log/log.h | 100 +++++++++++++----- .../jobs/network/abstractnetworkjob.cpp | 2 +- .../platforminconsistencycheckerworker.cpp | 6 +- test/libcommonserver/log/testlog.cpp | 25 +++-- 8 files changed, 180 insertions(+), 43 deletions(-) rename src/libcommon/log/{customlogwstream.h => customlogstreams.h} (50%) diff --git a/src/libcommon/CMakeLists.txt b/src/libcommon/CMakeLists.txt index 7225d27b3..16b474be0 100644 --- a/src/libcommon/CMakeLists.txt +++ b/src/libcommon/CMakeLists.txt @@ -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 ) diff --git a/src/libcommon/log/customlogwstream.h b/src/libcommon/log/customlogstreams.h similarity index 50% rename from src/libcommon/log/customlogwstream.h rename to src/libcommon/log/customlogstreams.h index bcc4d045c..aa52a8300 100644 --- a/src/libcommon/log/customlogwstream.h +++ b/src/libcommon/log/customlogstreams.h @@ -17,7 +17,71 @@ */ #pragma once #include -#include "libcommon/utility/types.h" + +#include + +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::str(); } + + // We need to cast to std::stringstream as operators<<(std::stringstream, const wchar_t *str /*and const std::wstring + // &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(*this) << str; + return *this; + } + CustomLogStream &operator<<(const std::string &str) { + static_cast(*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: @@ -40,6 +104,7 @@ class CustomLogWStream : private std::wstringstream { } CustomLogWStream &operator<<(bool b) { + std::wstringstream::operator<<(std::boolalpha); std::wstringstream::operator<<(b); return *this; } @@ -47,6 +112,10 @@ class CustomLogWStream : private std::wstringstream { 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; @@ -71,4 +140,8 @@ class CustomLogWStream : private std::wstringstream { std::wstringstream::operator<<(ptr); return *this; } + CustomLogWStream &operator<<(const std::error_code &code) { + std::wstringstream::operator<<(code.value()); + return *this; + } }; diff --git a/src/libcommon/utility/types.h b/src/libcommon/utility/types.h index 3fea8198f..d9f151991 100644 --- a/src/libcommon/utility/types.h +++ b/src/libcommon/utility/types.h @@ -28,7 +28,7 @@ #include #include #include -#include "libcommon/log/customlogwstream.h" +#include "libcommon/log/customlogstreams.h" namespace KDC { @@ -615,6 +615,11 @@ inline CustomLogWStream &operator<<(CustomLogWStream &os, C e) { } template + inline CustomLogStream &operator<<(CustomLogStream &os, C e) { + return os << toStringWithCode(e); +} + +template inline QDebug &operator<<(QDebug &os, C e) { return os << toStringWithCode(e).c_str(); } diff --git a/src/libcommonserver/db/db.cpp b/src/libcommonserver/db/db.cpp index 7bb749eec..4152ae186 100644 --- a/src/libcommonserver/db/db.cpp +++ b/src/libcommonserver/db/db.cpp @@ -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); if (!prepareQuery(INSERT_VERSION_REQUEST_ID, INSERT_VERSION_REQUEST)) return false; if (!insertVersion(version)) { LOG_WARN(_logger, "Error in Db::insertVersion"); @@ -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"); if (bool retry = false; !create(retry)) { if (retry) { LOG_WARN(_logger, "Error in Db::create - Retry"); @@ -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()); return; } _transaction = true; diff --git a/src/libcommonserver/log/log.h b/src/libcommonserver/log/log.h index ddb704524..e6e1d8fcb 100644 --- a/src/libcommonserver/log/log.h +++ b/src/libcommonserver/log/log.h @@ -25,14 +25,15 @@ #include #include "libcommon/log/sentry/sentryhandler.h" -#include "libcommon/log/customlogwstream.h" +#include "libcommon/log/customlogstreams.h" +#include "libcommon/utility/types.h" namespace KDC { #ifdef NDEBUG #define LOG_DEBUG(logger, logEvent) \ { \ - std::ostringstream stream; \ + CustomLogStream stream; \ stream << logEvent; \ sentry_value_t crumb = sentry_value_new_breadcrumb(nullptr, stream.str().c_str()); \ sentry_value_set_by_key(crumb, "level", sentry_value_new_string("debug")); \ @@ -52,7 +53,7 @@ namespace KDC { #define LOG_INFO(logger, logEvent) \ { \ - std::ostringstream stream; \ + CustomLogStream stream; \ stream << logEvent; \ sentry_value_t crumb = sentry_value_new_breadcrumb(nullptr, stream.str().c_str()); \ sentry_value_set_by_key(crumb, "level", sentry_value_new_string("info")); \ @@ -72,7 +73,7 @@ namespace KDC { #define LOG_WARN(logger, logEvent) \ { \ - std::ostringstream stream; \ + CustomLogStream stream; \ stream << logEvent; \ sentry_value_t crumb = sentry_value_new_breadcrumb(nullptr, stream.str().c_str()); \ sentry_value_set_by_key(crumb, "level", sentry_value_new_string("warning")); \ @@ -92,7 +93,7 @@ namespace KDC { #define LOG_ERROR(logger, logEvent) \ { \ - std::ostringstream stream; \ + CustomLogStream stream; \ stream << logEvent; \ sentry_value_t crumb = sentry_value_new_breadcrumb(nullptr, stream.str().c_str()); \ sentry_value_set_by_key(crumb, "level", sentry_value_new_string("error")); \ @@ -112,7 +113,7 @@ namespace KDC { #define LOG_FATAL(logger, logEvent) \ { \ - std::ostringstream stream; \ + CustomLogStream stream; \ stream << logEvent; \ sentry_value_t crumb = sentry_value_new_breadcrumb(nullptr, stream.str().c_str()); \ sentry_value_set_by_key(crumb, "level", sentry_value_new_string("fatal")); \ @@ -130,25 +131,76 @@ namespace KDC { } \ LOG4CPLUS_FATAL(logger, logEvent) #else -#define LOG_DEBUG(logger, logEvent) LOG4CPLUS_DEBUG(logger, logEvent) - -#define LOGW_DEBUG(logger, logEvent) LOG4CPLUS_DEBUG(logger, logEvent) - -#define LOG_INFO(logger, logEvent) LOG4CPLUS_INFO(logger, logEvent) - -#define LOGW_INFO(logger, logEvent) LOG4CPLUS_INFO(logger, logEvent) - -#define LOG_WARN(logger, logEvent) LOG4CPLUS_WARN(logger, logEvent) - -#define LOGW_WARN(logger, logEvent) LOG4CPLUS_WARN(logger, logEvent) - -#define LOG_ERROR(logger, logEvent) LOG4CPLUS_ERROR(logger, logEvent) - -#define LOGW_ERROR(logger, logEvent) LOG4CPLUS_ERROR(logger, logEvent) - -#define LOG_FATAL(logger, logEvent) LOG4CPLUS_FATAL(logger, logEvent) +#define LOG_DEBUG(logger, logEvent) \ + { \ + CustomLogStream customLogstream_; \ + customLogstream_ << logEvent; \ + LOG4CPLUS_DEBUG(logger, customLogstream_.str().c_str()); \ + } + +#define LOGW_DEBUG(logger, logEvent) \ + { \ + CustomLogWStream customLogWstream_; \ + customLogWstream_ << logEvent; \ + LOG4CPLUS_DEBUG(logger, customLogWstream_.str().c_str()); \ + } + +#define LOG_INFO(logger, logEvent) \ + { \ + CustomLogStream customLogstream_; \ + customLogstream_ << logEvent; \ + LOG4CPLUS_INFO(logger, customLogstream_.str().c_str()); \ + } + +#define LOGW_INFO(logger, logEvent) \ + { \ + CustomLogWStream customLogWstream_; \ + customLogWstream_ << logEvent; \ + LOG4CPLUS_INFO(logger, customLogWstream_.str().c_str()); \ + } + +#define LOG_WARN(logger, logEvent) \ + { \ + CustomLogStream customLogstream_; \ + customLogstream_ << logEvent; \ + LOG4CPLUS_WARN(logger, customLogstream_.str().c_str()); \ + } + +#define LOGW_WARN(logger, logEvent) \ + { \ + CustomLogWStream customLogWstream_; \ + customLogWstream_ << logEvent; \ + LOG4CPLUS_WARN(logger, customLogWstream_.str().c_str()); \ + } + +#define LOG_ERROR(logger, logEvent) \ + { \ + CustomLogStream customLogstream_; \ + customLogstream_ << logEvent; \ + LOG4CPLUS_ERROR(logger, customLogstream_.str().c_str()); \ + } + +#define LOGW_ERROR(logger, logEvent) \ + { \ + CustomLogWStream customLogWstream_; \ + customLogWstream_ << logEvent; \ + LOG4CPLUS_ERROR(logger, customLogWstream_.str().c_str()); \ + } + +#define LOG_FATAL(logger, logEvent) \ + { \ + CustomLogStream customLogstream_; \ + customLogstream_ << logEvent; \ + LOG4CPLUS_FATAL(logger, customLogstream_.str().c_str()); \ + } + +#define LOGW_FATAL(logger, logEvent) \ + { \ + CustomLogWStream customLogWstream_; \ + customLogWstream_ << logEvent; \ + LOG4CPLUS_FATAL(logger, customLogWstream_.str().c_str()); \ + } -#define LOGW_FATAL(logger, logEvent) LOG4CPLUS_FATAL(logger, logEvent) #endif class COMMONSERVER_EXPORT Log { diff --git a/src/libsyncengine/jobs/network/abstractnetworkjob.cpp b/src/libsyncengine/jobs/network/abstractnetworkjob.cpp index da1243d10..bfc5dfd92 100644 --- a/src/libsyncengine/jobs/network/abstractnetworkjob.cpp +++ b/src/libsyncengine/jobs/network/abstractnetworkjob.cpp @@ -76,7 +76,7 @@ AbstractNetworkJob::AbstractNetworkJob() { LOG_INFO(_logger, "Unknown error in Poco::Net::Context constructor: " << errorText(e).c_str() << ", retrying..."); } else { - LOG_ERROR(_logger, "Unknown error in Poco::Net::Context constructor: " << errorText(e).c_str()); + LOG_ERROR(_logger, "Unknown error in Poco::Net::Context constructor: " << errorText(e)); throw std::runtime_error(ABSTRACTNETWORKJOB_NEW_ERROR_MSG); } } diff --git a/src/libsyncengine/reconciliation/platform_inconsistency_checker/platforminconsistencycheckerworker.cpp b/src/libsyncengine/reconciliation/platform_inconsistency_checker/platforminconsistencycheckerworker.cpp index 1784a70c0..c55f8a867 100644 --- a/src/libsyncengine/reconciliation/platform_inconsistency_checker/platforminconsistencycheckerworker.cpp +++ b/src/libsyncengine/reconciliation/platform_inconsistency_checker/platforminconsistencycheckerworker.cpp @@ -291,9 +291,9 @@ void PlatformInconsistencyCheckerWorker::removeLocalNodeFromDb(std::shared_ptr Date: Fri, 15 Nov 2024 14:31:50 +0100 Subject: [PATCH 02/15] Re-use the custom logger stream string once it is computed --- src/libcommonserver/log/log.h | 238 ++++++++++++++++++---------------- 1 file changed, 124 insertions(+), 114 deletions(-) diff --git a/src/libcommonserver/log/log.h b/src/libcommonserver/log/log.h index e6e1d8fcb..ceab122eb 100644 --- a/src/libcommonserver/log/log.h +++ b/src/libcommonserver/log/log.h @@ -31,111 +31,121 @@ namespace KDC { #ifdef NDEBUG -#define LOG_DEBUG(logger, logEvent) \ - { \ - CustomLogStream stream; \ - stream << logEvent; \ - sentry_value_t crumb = sentry_value_new_breadcrumb(nullptr, stream.str().c_str()); \ - sentry_value_set_by_key(crumb, "level", sentry_value_new_string("debug")); \ - sentry_add_breadcrumb(crumb); \ - } \ - LOG4CPLUS_DEBUG(logger, logEvent) - -#define LOGW_DEBUG(logger, logEvent) \ - { \ - CustomLogWStream stream; \ - stream << logEvent; \ - sentry_value_t crumb = sentry_value_new_breadcrumb(nullptr, Utility::ws2s(stream.str()).c_str()); \ - sentry_value_set_by_key(crumb, "level", sentry_value_new_string("debug")); \ - sentry_add_breadcrumb(crumb); \ - } \ - LOG4CPLUS_DEBUG(logger, logEvent) - -#define LOG_INFO(logger, logEvent) \ - { \ - CustomLogStream stream; \ - stream << logEvent; \ - sentry_value_t crumb = sentry_value_new_breadcrumb(nullptr, stream.str().c_str()); \ - sentry_value_set_by_key(crumb, "level", sentry_value_new_string("info")); \ - sentry_add_breadcrumb(crumb); \ - } \ - LOG4CPLUS_INFO(logger, logEvent) - -#define LOGW_INFO(logger, logEvent) \ - { \ - CustomLogWStream stream; \ - stream << logEvent; \ - sentry_value_t crumb = sentry_value_new_breadcrumb(nullptr, Utility::ws2s(stream.str()).c_str()); \ - sentry_value_set_by_key(crumb, "level", sentry_value_new_string("info")); \ - sentry_add_breadcrumb(crumb); \ - } \ - LOG4CPLUS_INFO(logger, logEvent) - -#define LOG_WARN(logger, logEvent) \ - { \ - CustomLogStream stream; \ - stream << logEvent; \ - sentry_value_t crumb = sentry_value_new_breadcrumb(nullptr, stream.str().c_str()); \ - sentry_value_set_by_key(crumb, "level", sentry_value_new_string("warning")); \ - sentry_add_breadcrumb(crumb); \ - } \ - LOG4CPLUS_WARN(logger, logEvent) - -#define LOGW_WARN(logger, logEvent) \ - { \ - CustomLogWStream stream; \ - stream << logEvent; \ - sentry_value_t crumb = sentry_value_new_breadcrumb(nullptr, Utility::ws2s(stream.str()).c_str()); \ - sentry_value_set_by_key(crumb, "level", sentry_value_new_string("warning")); \ - sentry_add_breadcrumb(crumb); \ - } \ - LOG4CPLUS_WARN(logger, logEvent) - -#define LOG_ERROR(logger, logEvent) \ - { \ - CustomLogStream stream; \ - stream << logEvent; \ - sentry_value_t crumb = sentry_value_new_breadcrumb(nullptr, stream.str().c_str()); \ - sentry_value_set_by_key(crumb, "level", sentry_value_new_string("error")); \ - sentry_add_breadcrumb(crumb); \ - } \ - LOG4CPLUS_ERROR(logger, logEvent) - -#define LOGW_ERROR(logger, logEvent) \ - { \ - CustomLogWStream stream; \ - stream << logEvent; \ - sentry_value_t crumb = sentry_value_new_breadcrumb(nullptr, Utility::ws2s(stream.str()).c_str()); \ - sentry_value_set_by_key(crumb, "level", sentry_value_new_string("error")); \ - sentry_add_breadcrumb(crumb); \ - } \ - LOG4CPLUS_ERROR(logger, logEvent) - -#define LOG_FATAL(logger, logEvent) \ - { \ - CustomLogStream stream; \ - stream << logEvent; \ - sentry_value_t crumb = sentry_value_new_breadcrumb(nullptr, stream.str().c_str()); \ - sentry_value_set_by_key(crumb, "level", sentry_value_new_string("fatal")); \ - sentry_add_breadcrumb(crumb); \ - } \ - LOG4CPLUS_FATAL(logger, logEvent) - -#define LOGW_FATAL(logger, logEvent) \ - { \ - CustomLogWStream stream; \ - stream << logEvent; \ - sentry_value_t crumb = sentry_value_new_breadcrumb(nullptr, Utility::ws2s(stream.str()).c_str()); \ - sentry_value_set_by_key(crumb, "level", sentry_value_new_string("fatal")); \ - sentry_add_breadcrumb(crumb); \ - } \ - LOG4CPLUS_FATAL(logger, logEvent) +#define LOG_DEBUG(logger, logEvent) \ + { \ + CustomLogStream customLogStream_; \ + customLogStream_ << logEvent; \ + const auto &customLogStreamStr_ = customLogStream_.str(); \ + sentry_value_t crumb = sentry_value_new_breadcrumb(nullptr, customLogStreamStr_.c_str())); \ + sentry_value_set_by_key(crumb, "level", sentry_value_new_string("debug")); \ + sentry_add_breadcrumb(crumb); \ + } \ + LOG4CPLUS_DEBUG(logger, customLogStreamStr_.c_str()) + +#define LOGW_DEBUG(logger, logEvent) \ + { \ + CustomLogWStream customLogWStream_; \ + customLogWStream_ << logEvent; \ + const auto &customLogWStreamStr_ = customLogWStream_.str(); \ + sentry_value_t crumb = sentry_value_new_breadcrumb(nullptr, Utility::ws2s(customLogWStreamStr_).c_str()); \ + sentry_value_set_by_key(crumb, "level", sentry_value_new_string("debug")); \ + sentry_add_breadcrumb(crumb); \ + } \ + LOG4CPLUS_DEBUG(logger, customLogWStreamStr_.c_str()) + +#define LOG_INFO(logger, logEvent) \ + { \ + CustomLogStream customLogStream_; \ + customLogStream_ << logEvent; \ + const auto customLogStreamStr_ = customLogStream_.str(); \ + sentry_value_t crumb = sentry_value_new_breadcrumb(nullptr, customLogStreamStr_.c_str()); \ + sentry_value_set_by_key(crumb, "level", sentry_value_new_string("info")); \ + sentry_add_breadcrumb(crumb); \ + } \ + LOG4CPLUS_INFO(logger, customLogStreamStr_.c_str()) + +#define LOGW_INFO(logger, logEvent) \ + { \ + CustomLogWStream customLogWStream_; \ + customLogWStream_ << logEvent; \ + const auto &customLogWStreamStr_ = customLogWStream_.str(); \ + sentry_value_t crumb = sentry_value_new_breadcrumb(nullptr, Utility::ws2s(customLogWStreamStr_).c_str()); \ + sentry_value_set_by_key(crumb, "level", sentry_value_new_string("info")); \ + sentry_add_breadcrumb(crumb); \ + } \ + LOG4CPLUS_INFO(logger, customLogWStreamStr_.c_str()) + +#define LOG_WARN(logger, logEvent) \ + { \ + CustomLogStream customLogStream_; \ + customLogStream_ << logEvent; \ + const auto customLogStreamStr_ = customLogStream_.str(); \ + sentry_value_t crumb = sentry_value_new_breadcrumb(nullptr, customLogStreamStr_.c_str()); \ + sentry_value_set_by_key(crumb, "level", sentry_value_new_string("warning")); \ + sentry_add_breadcrumb(crumb); \ + } \ + LOG4CPLUS_WARN(logger, customLogStreamStr_.c_str()) + +#define LOGW_WARN(logger, logEvent) \ + { \ + CustomLogWStream customLogWStream_; \ + customLogWStream_ << logEvent; \ + const auto &customLogWStreamStr_ = customLogWStream_.str(); \ + sentry_value_t crumb = sentry_value_new_breadcrumb(nullptr, Utility::ws2s(customLogWStreamStr_).c_str()); \ + sentry_value_set_by_key(crumb, "level", sentry_value_new_string("warning")); \ + sentry_add_breadcrumb(crumb); \ + } \ + LOG4CPLUS_WARN(logger, customLogWStreamStr_.c_str()) + +#define LOG_ERROR(logger, logEvent) \ + { \ + CustomLogStream customLogStream_; \ + customLogStream_ << logEvent; \ + const auto customLogStreamStr_ = customLogStream_.str(); \ + sentry_value_t crumb = sentry_value_new_breadcrumb(nullptr, customLogStreamStr_.c_str()); \ + sentry_value_set_by_key(crumb, "level", sentry_value_new_string("error")); \ + sentry_add_breadcrumb(crumb); \ + } \ + LOG4CPLUS_ERROR(logger, customLogStreamStr_.c_str()) + +#define LOGW_ERROR(logger, logEvent) \ + { \ + CustomLogWStream customLogWStream_; \ + customLogWStream_ << logEvent; \ + const auto &customLogWStreamStr_ = customLogWStream_.str(); \ + sentry_value_t crumb = sentry_value_new_breadcrumb(nullptr, Utility::ws2s(customLogWStreamStr_).c_str()); \ + sentry_value_set_by_key(crumb, "level", sentry_value_new_string("error")); \ + sentry_add_breadcrumb(crumb); \ + } \ + LOG4CPLUS_ERROR(logger, customLogWStreamStr_.c_str()) + +#define LOG_FATAL(logger, logEvent) \ + { \ + CustomLogStream customLogStream_; \ + customLogStream_ << logEvent; \ + const auto customLogStreamStr_ = customLogStream_.str(); \ + sentry_value_t crumb = sentry_value_new_breadcrumb(nullptr, customLogStreamStr_.c_str()); \ + sentry_value_set_by_key(crumb, "level", sentry_value_new_string("fatal")); \ + sentry_add_breadcrumb(crumb); \ + } \ + LOG4CPLUS_FATAL(logger, customLogStreamStr_.c_str()) + +#define LOGW_FATAL(logger, logEvent) \ + { \ + CustomLogWStream customLogWStream_; \ + customLogWStream_ << logEvent; \ + const auto &customLogWStreamStr_ = customLogWStream_.str(); \ + sentry_value_t crumb = sentry_value_new_breadcrumb(nullptr, Utility::ws2s(customLogWStreamStr_).c_str()); \ + sentry_value_set_by_key(crumb, "level", sentry_value_new_string("fatal")); \ + sentry_add_breadcrumb(crumb); \ + } +LOG4CPLUS_FATAL(logger, customLogWStreamStr_.c_str()) #else #define LOG_DEBUG(logger, logEvent) \ { \ - CustomLogStream customLogstream_; \ - customLogstream_ << logEvent; \ - LOG4CPLUS_DEBUG(logger, customLogstream_.str().c_str()); \ + CustomLogStream customLogStream_; \ + customLogStream_ << logEvent; \ + LOG4CPLUS_DEBUG(logger, customLogStream_.str().c_str()); \ } #define LOGW_DEBUG(logger, logEvent) \ @@ -147,9 +157,9 @@ namespace KDC { #define LOG_INFO(logger, logEvent) \ { \ - CustomLogStream customLogstream_; \ - customLogstream_ << logEvent; \ - LOG4CPLUS_INFO(logger, customLogstream_.str().c_str()); \ + CustomLogStream customLogStream_; \ + customLogStream_ << logEvent; \ + LOG4CPLUS_INFO(logger, customLogStream_.str().c_str()); \ } #define LOGW_INFO(logger, logEvent) \ @@ -161,9 +171,9 @@ namespace KDC { #define LOG_WARN(logger, logEvent) \ { \ - CustomLogStream customLogstream_; \ - customLogstream_ << logEvent; \ - LOG4CPLUS_WARN(logger, customLogstream_.str().c_str()); \ + CustomLogStream customLogStream_; \ + customLogStream_ << logEvent; \ + LOG4CPLUS_WARN(logger, customLogStream_.str().c_str()); \ } #define LOGW_WARN(logger, logEvent) \ @@ -175,9 +185,9 @@ namespace KDC { #define LOG_ERROR(logger, logEvent) \ { \ - CustomLogStream customLogstream_; \ - customLogstream_ << logEvent; \ - LOG4CPLUS_ERROR(logger, customLogstream_.str().c_str()); \ + CustomLogStream customLogStream_; \ + customLogStream_ << logEvent; \ + LOG4CPLUS_ERROR(logger, customLogStream_.str().c_str()); \ } #define LOGW_ERROR(logger, logEvent) \ @@ -189,9 +199,9 @@ namespace KDC { #define LOG_FATAL(logger, logEvent) \ { \ - CustomLogStream customLogstream_; \ - customLogstream_ << logEvent; \ - LOG4CPLUS_FATAL(logger, customLogstream_.str().c_str()); \ + CustomLogStream customLogStream_; \ + customLogStream_ << logEvent; \ + LOG4CPLUS_FATAL(logger, customLogStream_.str().c_str()); \ } #define LOGW_FATAL(logger, logEvent) \ From 3b9779df73e15f06c4ccd9661e84b458bf15d42d Mon Sep 17 00:00:00 2001 From: Luc Guyot Date: Fri, 15 Nov 2024 14:42:31 +0100 Subject: [PATCH 03/15] Fixes some variables scopes in log macros (build failures) --- src/libcommonserver/log/log.h | 39 ++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/src/libcommonserver/log/log.h b/src/libcommonserver/log/log.h index ceab122eb..81a1773c7 100644 --- a/src/libcommonserver/log/log.h +++ b/src/libcommonserver/log/log.h @@ -39,8 +39,8 @@ namespace KDC { sentry_value_t crumb = sentry_value_new_breadcrumb(nullptr, customLogStreamStr_.c_str())); \ sentry_value_set_by_key(crumb, "level", sentry_value_new_string("debug")); \ sentry_add_breadcrumb(crumb); \ - } \ - LOG4CPLUS_DEBUG(logger, customLogStreamStr_.c_str()) + LOG4CPLUS_DEBUG(logger, customLogStreamStr_.c_str()) \ + } #define LOGW_DEBUG(logger, logEvent) \ { \ @@ -50,8 +50,8 @@ namespace KDC { sentry_value_t crumb = sentry_value_new_breadcrumb(nullptr, Utility::ws2s(customLogWStreamStr_).c_str()); \ sentry_value_set_by_key(crumb, "level", sentry_value_new_string("debug")); \ sentry_add_breadcrumb(crumb); \ - } \ - LOG4CPLUS_DEBUG(logger, customLogWStreamStr_.c_str()) + LOG4CPLUS_DEBUG(logger, customLogWStreamStr_.c_str()) \ + } #define LOG_INFO(logger, logEvent) \ { \ @@ -61,8 +61,8 @@ namespace KDC { sentry_value_t crumb = sentry_value_new_breadcrumb(nullptr, customLogStreamStr_.c_str()); \ sentry_value_set_by_key(crumb, "level", sentry_value_new_string("info")); \ sentry_add_breadcrumb(crumb); \ - } \ - LOG4CPLUS_INFO(logger, customLogStreamStr_.c_str()) + LOG4CPLUS_INFO(logger, customLogStreamStr_.c_str()) \ + } #define LOGW_INFO(logger, logEvent) \ { \ @@ -72,8 +72,8 @@ namespace KDC { sentry_value_t crumb = sentry_value_new_breadcrumb(nullptr, Utility::ws2s(customLogWStreamStr_).c_str()); \ sentry_value_set_by_key(crumb, "level", sentry_value_new_string("info")); \ sentry_add_breadcrumb(crumb); \ - } \ - LOG4CPLUS_INFO(logger, customLogWStreamStr_.c_str()) + LOG4CPLUS_INFO(logger, customLogWStreamStr_.c_str()) \ + } #define LOG_WARN(logger, logEvent) \ { \ @@ -83,8 +83,9 @@ namespace KDC { sentry_value_t crumb = sentry_value_new_breadcrumb(nullptr, customLogStreamStr_.c_str()); \ sentry_value_set_by_key(crumb, "level", sentry_value_new_string("warning")); \ sentry_add_breadcrumb(crumb); \ - } \ - LOG4CPLUS_WARN(logger, customLogStreamStr_.c_str()) + LOG4CPLUS_WARN(logger, customLogStreamStr_.c_str()) \ + } + #define LOGW_WARN(logger, logEvent) \ { \ @@ -94,8 +95,8 @@ namespace KDC { sentry_value_t crumb = sentry_value_new_breadcrumb(nullptr, Utility::ws2s(customLogWStreamStr_).c_str()); \ sentry_value_set_by_key(crumb, "level", sentry_value_new_string("warning")); \ sentry_add_breadcrumb(crumb); \ - } \ - LOG4CPLUS_WARN(logger, customLogWStreamStr_.c_str()) + LOG4CPLUS_WARN(logger, customLogWStreamStr_.c_str()) \ + } #define LOG_ERROR(logger, logEvent) \ { \ @@ -105,8 +106,8 @@ namespace KDC { sentry_value_t crumb = sentry_value_new_breadcrumb(nullptr, customLogStreamStr_.c_str()); \ sentry_value_set_by_key(crumb, "level", sentry_value_new_string("error")); \ sentry_add_breadcrumb(crumb); \ - } \ - LOG4CPLUS_ERROR(logger, customLogStreamStr_.c_str()) + LOG4CPLUS_ERROR(logger, customLogStreamStr_.c_str()) \ + } #define LOGW_ERROR(logger, logEvent) \ { \ @@ -116,8 +117,8 @@ namespace KDC { sentry_value_t crumb = sentry_value_new_breadcrumb(nullptr, Utility::ws2s(customLogWStreamStr_).c_str()); \ sentry_value_set_by_key(crumb, "level", sentry_value_new_string("error")); \ sentry_add_breadcrumb(crumb); \ - } \ - LOG4CPLUS_ERROR(logger, customLogWStreamStr_.c_str()) + LOG4CPLUS_ERROR(logger, customLogWStreamStr_.c_str()) \ + } #define LOG_FATAL(logger, logEvent) \ { \ @@ -127,8 +128,8 @@ namespace KDC { sentry_value_t crumb = sentry_value_new_breadcrumb(nullptr, customLogStreamStr_.c_str()); \ sentry_value_set_by_key(crumb, "level", sentry_value_new_string("fatal")); \ sentry_add_breadcrumb(crumb); \ - } \ - LOG4CPLUS_FATAL(logger, customLogStreamStr_.c_str()) + LOG4CPLUS_FATAL(logger, customLogStreamStr_.c_str()) \ + } #define LOGW_FATAL(logger, logEvent) \ { \ @@ -138,8 +139,8 @@ namespace KDC { sentry_value_t crumb = sentry_value_new_breadcrumb(nullptr, Utility::ws2s(customLogWStreamStr_).c_str()); \ sentry_value_set_by_key(crumb, "level", sentry_value_new_string("fatal")); \ sentry_add_breadcrumb(crumb); \ + LOG4CPLUS_FATAL(logger, customLogWStreamStr_.c_str()) \ } -LOG4CPLUS_FATAL(logger, customLogWStreamStr_.c_str()) #else #define LOG_DEBUG(logger, logEvent) \ { \ From edff61d00776388b573914d256c84c72833d3bfd Mon Sep 17 00:00:00 2001 From: Luc Guyot Date: Fri, 15 Nov 2024 14:47:39 +0100 Subject: [PATCH 04/15] Fixes typo in macros causing build errors --- src/libcommonserver/log/log.h | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/libcommonserver/log/log.h b/src/libcommonserver/log/log.h index 81a1773c7..a57dcae84 100644 --- a/src/libcommonserver/log/log.h +++ b/src/libcommonserver/log/log.h @@ -31,15 +31,15 @@ namespace KDC { #ifdef NDEBUG -#define LOG_DEBUG(logger, logEvent) \ - { \ - CustomLogStream customLogStream_; \ - customLogStream_ << logEvent; \ - const auto &customLogStreamStr_ = customLogStream_.str(); \ - sentry_value_t crumb = sentry_value_new_breadcrumb(nullptr, customLogStreamStr_.c_str())); \ - sentry_value_set_by_key(crumb, "level", sentry_value_new_string("debug")); \ - sentry_add_breadcrumb(crumb); \ - LOG4CPLUS_DEBUG(logger, customLogStreamStr_.c_str()) \ +#define LOG_DEBUG(logger, logEvent) \ + { \ + CustomLogStream customLogStream_; \ + customLogStream_ << logEvent; \ + const auto &customLogStreamStr_ = customLogStream_.str(); \ + sentry_value_t crumb = sentry_value_new_breadcrumb(nullptr, customLogStreamStr_.c_str()); \ + sentry_value_set_by_key(crumb, "level", sentry_value_new_string("debug")); \ + sentry_add_breadcrumb(crumb); \ + LOG4CPLUS_DEBUG(logger, customLogStreamStr_.c_str()) \ } #define LOGW_DEBUG(logger, logEvent) \ From 2723a2cd9e46ef70592238beeb5180928581a874 Mon Sep 17 00:00:00 2001 From: Luc Guyot Date: Fri, 15 Nov 2024 15:08:36 +0100 Subject: [PATCH 05/15] Adds semicolon after macro to fix build errors caused by macro expansions --- src/libcommonserver/log/log.h | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/libcommonserver/log/log.h b/src/libcommonserver/log/log.h index a57dcae84..cf85f5aee 100644 --- a/src/libcommonserver/log/log.h +++ b/src/libcommonserver/log/log.h @@ -39,7 +39,7 @@ namespace KDC { sentry_value_t crumb = sentry_value_new_breadcrumb(nullptr, customLogStreamStr_.c_str()); \ sentry_value_set_by_key(crumb, "level", sentry_value_new_string("debug")); \ sentry_add_breadcrumb(crumb); \ - LOG4CPLUS_DEBUG(logger, customLogStreamStr_.c_str()) \ + LOG4CPLUS_DEBUG(logger, customLogStreamStr_.c_str()); \ } #define LOGW_DEBUG(logger, logEvent) \ @@ -50,7 +50,7 @@ namespace KDC { sentry_value_t crumb = sentry_value_new_breadcrumb(nullptr, Utility::ws2s(customLogWStreamStr_).c_str()); \ sentry_value_set_by_key(crumb, "level", sentry_value_new_string("debug")); \ sentry_add_breadcrumb(crumb); \ - LOG4CPLUS_DEBUG(logger, customLogWStreamStr_.c_str()) \ + LOG4CPLUS_DEBUG(logger, customLogWStreamStr_.c_str()); \ } #define LOG_INFO(logger, logEvent) \ @@ -61,7 +61,7 @@ namespace KDC { sentry_value_t crumb = sentry_value_new_breadcrumb(nullptr, customLogStreamStr_.c_str()); \ sentry_value_set_by_key(crumb, "level", sentry_value_new_string("info")); \ sentry_add_breadcrumb(crumb); \ - LOG4CPLUS_INFO(logger, customLogStreamStr_.c_str()) \ + LOG4CPLUS_INFO(logger, customLogStreamStr_.c_str()); \ } #define LOGW_INFO(logger, logEvent) \ @@ -72,7 +72,7 @@ namespace KDC { sentry_value_t crumb = sentry_value_new_breadcrumb(nullptr, Utility::ws2s(customLogWStreamStr_).c_str()); \ sentry_value_set_by_key(crumb, "level", sentry_value_new_string("info")); \ sentry_add_breadcrumb(crumb); \ - LOG4CPLUS_INFO(logger, customLogWStreamStr_.c_str()) \ + LOG4CPLUS_INFO(logger, customLogWStreamStr_.c_str()); \ } #define LOG_WARN(logger, logEvent) \ @@ -83,7 +83,7 @@ namespace KDC { sentry_value_t crumb = sentry_value_new_breadcrumb(nullptr, customLogStreamStr_.c_str()); \ sentry_value_set_by_key(crumb, "level", sentry_value_new_string("warning")); \ sentry_add_breadcrumb(crumb); \ - LOG4CPLUS_WARN(logger, customLogStreamStr_.c_str()) \ + LOG4CPLUS_WARN(logger, customLogStreamStr_.c_str()); \ } @@ -95,7 +95,7 @@ namespace KDC { sentry_value_t crumb = sentry_value_new_breadcrumb(nullptr, Utility::ws2s(customLogWStreamStr_).c_str()); \ sentry_value_set_by_key(crumb, "level", sentry_value_new_string("warning")); \ sentry_add_breadcrumb(crumb); \ - LOG4CPLUS_WARN(logger, customLogWStreamStr_.c_str()) \ + LOG4CPLUS_WARN(logger, customLogWStreamStr_.c_str()); \ } #define LOG_ERROR(logger, logEvent) \ @@ -106,7 +106,7 @@ namespace KDC { sentry_value_t crumb = sentry_value_new_breadcrumb(nullptr, customLogStreamStr_.c_str()); \ sentry_value_set_by_key(crumb, "level", sentry_value_new_string("error")); \ sentry_add_breadcrumb(crumb); \ - LOG4CPLUS_ERROR(logger, customLogStreamStr_.c_str()) \ + LOG4CPLUS_ERROR(logger, customLogStreamStr_.c_str()); \ } #define LOGW_ERROR(logger, logEvent) \ @@ -117,7 +117,7 @@ namespace KDC { sentry_value_t crumb = sentry_value_new_breadcrumb(nullptr, Utility::ws2s(customLogWStreamStr_).c_str()); \ sentry_value_set_by_key(crumb, "level", sentry_value_new_string("error")); \ sentry_add_breadcrumb(crumb); \ - LOG4CPLUS_ERROR(logger, customLogWStreamStr_.c_str()) \ + LOG4CPLUS_ERROR(logger, customLogWStreamStr_.c_str()); \ } #define LOG_FATAL(logger, logEvent) \ @@ -128,7 +128,7 @@ namespace KDC { sentry_value_t crumb = sentry_value_new_breadcrumb(nullptr, customLogStreamStr_.c_str()); \ sentry_value_set_by_key(crumb, "level", sentry_value_new_string("fatal")); \ sentry_add_breadcrumb(crumb); \ - LOG4CPLUS_FATAL(logger, customLogStreamStr_.c_str()) \ + LOG4CPLUS_FATAL(logger, customLogStreamStr_.c_str()); \ } #define LOGW_FATAL(logger, logEvent) \ @@ -139,7 +139,7 @@ namespace KDC { sentry_value_t crumb = sentry_value_new_breadcrumb(nullptr, Utility::ws2s(customLogWStreamStr_).c_str()); \ sentry_value_set_by_key(crumb, "level", sentry_value_new_string("fatal")); \ sentry_add_breadcrumb(crumb); \ - LOG4CPLUS_FATAL(logger, customLogWStreamStr_.c_str()) \ + LOG4CPLUS_FATAL(logger, customLogWStreamStr_.c_str()); \ } #else #define LOG_DEBUG(logger, logEvent) \ From 3a1d8dfc137c39a336d70752039529939f4a8b41 Mon Sep 17 00:00:00 2001 From: Luc Guyot Date: Mon, 18 Nov 2024 08:58:31 +0100 Subject: [PATCH 06/15] Fixes unit tests failure --- .../log/customrollingfileappender.cpp | 4 +-- test/libcommonserver/log/testlog.cpp | 34 ++++++++++++------- test/test_utility/testhelpers.cpp | 25 +++++++++++++- test/test_utility/testhelpers.h | 3 ++ 4 files changed, 51 insertions(+), 15 deletions(-) diff --git a/src/libcommonserver/log/customrollingfileappender.cpp b/src/libcommonserver/log/customrollingfileappender.cpp index 573600b1c..73fcd2804 100644 --- a/src/libcommonserver/log/customrollingfileappender.cpp +++ b/src/libcommonserver/log/customrollingfileappender.cpp @@ -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; diff --git a/test/libcommonserver/log/testlog.cpp b/test/libcommonserver/log/testlog.cpp index aba057cfd..fc65aafe8 100644 --- a/test/libcommonserver/log/testlog.cpp +++ b/test/libcommonserver/log/testlog.cpp @@ -16,16 +16,20 @@ * along with this program. If not, see . */ +#include "test_utility/testhelpers.h" #include "testlog.h" + #include "libcommonserver/log/log.h" #include "libcommonserver/utility/utility.h" #include "libcommonserver/io/iohelper.h" #include "libcommon/utility/utility.h" #include +#include #include + #include -#include +#include using namespace CppUnit; namespace KDC { @@ -41,17 +45,16 @@ void TestLog::testLog() { LOG_WARN(_logger, "Test warn log " << (long unsigned int) 3 << " " << false << " " << std::error_code{}); LOG_ERROR(_logger, "Test error log " << (long long unsigned int) 4 << " " << true); const QIODevice *device = nullptr; - LOG_FATAL(_logger, "Test fatal log" << std::error_code{} << device); - - LOG_DEBUG(_logger, "家屋香袈睷晦"); + LOG_FATAL(_logger, "Test fatal log" << std::error_code{} << " " << device); LOGW_DEBUG(_logger, L"Test debug log " << (int) 1 << L" " << true << L" " << (double) 1.0); LOGW_INFO(_logger, L"Test info log " << (unsigned int) 2 << L" " << false << L" " << (float) 1.0); LOGW_WARN(_logger, L"Test warn log " << (long unsigned int) 3 << L" " << false << L" " << std::error_code{}); LOGW_ERROR(_logger, L"Test error log " << (long long unsigned int) 4 << L" " << true); - LOGW_FATAL(_logger, L"Test fatal log " << std::error_code{} << device << (long unsigned int) 5); + LOGW_FATAL(_logger, L"Test fatal log " << std::error_code{} << L" " << device << L" " << (long unsigned int) 5); - LOGW_DEBUG(_logger, L"家屋香袈睷晦"); + // Wide characters are mandatory in this case as a bare call to LOG4CPLUS_DEBUG fails to print the string below. + LOGW_DEBUG(_logger, L"Test debug log " << L" " << L"家屋香袈睷晦"); CPPUNIT_ASSERT(true); } @@ -66,11 +69,13 @@ void TestLog::testLargeLogRolling(void) { customRollingFileAppender->setMaxFileSize(maxSize); LOG_DEBUG(_logger, "Ensure the log file is created"); + CPPUNIT_ASSERT_GREATER(1, countFilesInDirectory(_logDir)); // Generate a log larger than the max log file size. (log header is 50bytes) const auto testLog = std::string(maxSize, 'a'); LOG_DEBUG(_logger, testLog.c_str()); + CPPUNIT_ASSERT_GREATER(2, countFilesInDirectory(_logDir)); SyncPath rolledFile = _logDir / (Log::instance()->getLogFilePath().filename().string() + ".1.gz"); @@ -90,20 +95,24 @@ void TestLog::testExpiredLogFiles(void) { std::ofstream fakeLogFile(_logDir / APPLICATION_NAME "_fake.log.gz"); fakeLogFile << "Fake old log file" << std::endl; fakeLogFile.close(); - LOG_INFO(_logger, "Test log file expiration"); // Ensure the log file is created + LOG_INFO(_logger, "Test log file expiration"); // Ensure the log file is created. - CPPUNIT_ASSERT_EQUAL(2, countFilesInDirectory(_logDir)); // The current log file and the fake archived log file + CPPUNIT_ASSERT_EQUAL(2, countFilesInDirectory(_logDir)); // The current log file and the fake archived log file. auto *appender = static_cast(_logger.getAppender(Log::rfName).get()); - appender->setExpire(2); // 1 seconds + appender->setExpire(2); // 2 seconds Utility::msleep(1000); appender->checkForExpiredFiles(); - LOG_INFO(_logger, "Test log file expiration"); // Ensure the current log file is not older than 2 seconds - CPPUNIT_ASSERT_EQUAL(2, countFilesInDirectory(_logDir)); // The fake log file should not be deleted (< 2 seconds) + + const auto now = std::chrono::system_clock::now(); + KDC::testhelpers::setModificationDate(Log::instance()->getLogFilePath(), now); + + CPPUNIT_ASSERT_EQUAL(2, countFilesInDirectory(_logDir)); // The fake log file should not be deleted yet (< 2 seconds). Utility::msleep(1000); appender->checkForExpiredFiles(); - CPPUNIT_ASSERT_EQUAL(1, countFilesInDirectory(_logDir)); // The fake log file should be deleted + + CPPUNIT_ASSERT_EQUAL(1, countFilesInDirectory(_logDir)); // The fake log file should be deleted now. appender->setExpire(CommonUtility::logsPurgeRate * 24 * 3600); } @@ -120,6 +129,7 @@ int TestLog::countFilesInDirectory(const SyncPath &directory) const { count++; } CPPUNIT_ASSERT(endOfDirectory); + return count; } diff --git a/test/test_utility/testhelpers.cpp b/test/test_utility/testhelpers.cpp index b47dbc4c8..228ac5ae7 100644 --- a/test/test_utility/testhelpers.cpp +++ b/test/test_utility/testhelpers.cpp @@ -15,11 +15,20 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -#include + #include "testhelpers.h" #include "libcommon/utility/utility.h" +#include +#include +#include + + +#if defined(__APPLE__) || defined(__unix__) +#include +#endif + namespace KDC::testhelpers { SyncName makeNfdSyncName() { @@ -52,4 +61,18 @@ std::string loadEnvVariable(const std::string& key) { return val; } +void setModificationDate(const SyncPath& path, const std::chrono::time_point& timePoint) { + struct stat fileStat; + struct utimbuf new_times; + + const auto fileNameStr = path.string(); + const auto fileName = fileNameStr.c_str(); + + stat(fileName, &fileStat); + + const std::time_t timeInSeconds = std::chrono::system_clock::to_time_t(timePoint); + new_times.modtime = timeInSeconds; + utime(fileName, &new_times); +} + } // namespace KDC::testhelpers diff --git a/test/test_utility/testhelpers.h b/test/test_utility/testhelpers.h index 4ea30f93d..24debe4cf 100644 --- a/test/test_utility/testhelpers.h +++ b/test/test_utility/testhelpers.h @@ -25,6 +25,8 @@ #include +#include + namespace KDC::testhelpers { const SyncPath localTestDirPath(KDC::Utility::s2ws(TEST_DIR) + L"/test_ci"); @@ -54,5 +56,6 @@ struct TestVariables { }; void generateOrEditTestFile(const SyncPath &path); +void setModificationDate(const SyncPath &path, const std::chrono::time_point &timePoint); } // namespace KDC::testhelpers From 76a08dd41147327fd209f4ca0e44fe07d8aa202c Mon Sep 17 00:00:00 2001 From: Luc Guyot Date: Mon, 18 Nov 2024 09:01:33 +0100 Subject: [PATCH 07/15] Fixes doc string as suggested by Sonar Cloud --- src/libcommon/log/customlogstreams.h | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/libcommon/log/customlogstreams.h b/src/libcommon/log/customlogstreams.h index aa52a8300..8a273b6dc 100644 --- a/src/libcommon/log/customlogstreams.h +++ b/src/libcommon/log/customlogstreams.h @@ -16,6 +16,7 @@ * along with this program. If not, see . */ #pragma once +#include #include #include @@ -28,8 +29,8 @@ class CustomLogStream : private std::stringstream { std::string str() const { return std::basic_stringstream::str(); } - // We need to cast to std::stringstream as operators<<(std::stringstream, const wchar_t *str /*and const std::wstring - // &str*/) are defined outside of the class std::stringstream and therefore it is not applicable to the current object + // 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(*this) << str; @@ -91,8 +92,8 @@ class CustomLogWStream : private std::wstringstream { std::wstring str() const { return std::basic_stringstream::str(); } - // 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 + // 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(*this) << str; From 2f3a66465457e487729e5a5d21b55c6972dc27e8 Mon Sep 17 00:00:00 2001 From: Luc Guyot Date: Mon, 18 Nov 2024 10:54:19 +0100 Subject: [PATCH 08/15] Define testhelpers::setModificationDate for Windows --- test/test_utility/testhelpers.cpp | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/test/test_utility/testhelpers.cpp b/test/test_utility/testhelpers.cpp index 228ac5ae7..e47539501 100644 --- a/test/test_utility/testhelpers.cpp +++ b/test/test_utility/testhelpers.cpp @@ -20,12 +20,12 @@ #include "libcommon/utility/utility.h" -#include #include #include - -#if defined(__APPLE__) || defined(__unix__) +#ifdef _WIN32 +#include "libcommonserver/io/iohelper.h" +#else #include #endif @@ -60,10 +60,23 @@ std::string loadEnvVariable(const std::string& key) { } return val; } +#ifdef _WIN32 +void setModificationDate(const SyncPath& path, const std::chrono::time_point& timePoint) { + struct _utimebuf timeBuffer; + const std::time_t timeInSeconds = std::chrono::system_clock::to_time_t(timePoint); + + IoError ioError = IoError::Success; + FileStat fileStat; + IoHelper::getFileStat(path, &fileStat, ioError); + timeBuffer.tma = fileStat.creationTime; + timeBuffer.tmm = timeInSeconds; + _utime(path.c_str(), &timeBuffer); +} +#else void setModificationDate(const SyncPath& path, const std::chrono::time_point& timePoint) { struct stat fileStat; - struct utimbuf new_times; + struct utimbuf newTime; const auto fileNameStr = path.string(); const auto fileName = fileNameStr.c_str(); @@ -71,8 +84,8 @@ void setModificationDate(const SyncPath& path, const std::chrono::time_point Date: Mon, 18 Nov 2024 11:15:23 +0100 Subject: [PATCH 09/15] Fixes header file inclusion causing build error on Windows --- test/server/socketapi/testsocketapi.cpp | 51 +++++++++++++++++++++++++ test/server/socketapi/testsocketapi.h | 39 +++++++++++++++++++ test/test_utility/testhelpers.cpp | 2 +- 3 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 test/server/socketapi/testsocketapi.cpp create mode 100644 test/server/socketapi/testsocketapi.h diff --git a/test/server/socketapi/testsocketapi.cpp b/test/server/socketapi/testsocketapi.cpp new file mode 100644 index 000000000..87385ad46 --- /dev/null +++ b/test/server/socketapi/testsocketapi.cpp @@ -0,0 +1,51 @@ +/* + * 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 . + */ + +#include "socketapi.h" +#include "testsocketapi.h" + +#include +#include + +#ifdef _WIN32 +#include +#endif + +namespace KDC { + + +void TestSocketApi::setUp() {} + +void TestSocketApi::tearDown() {} + +void TestSocketApi::testFileData() { + FileData fileData; + QString localPath = QString("/Users/hyphensmbp/kDrive Labör/Common documents/"); + + QByteArray b1 = localPath.toLatin1(); + CPPUNIT_ASSERT(QString(b1) == localPath); + + // QString parentPath = QFileInfo(localPath).dir().path().toUtf8(); + + // CPPUNIT_ASSERT(QString("/Users/hyphensmbp/kDrive Labör").toStdString() == parentPath.toStdString()); + + // std::filesystem::path localPath_ = QStr2Path(parentPath); + + // CPPUNIT_ASSERT(localPath_ == SyncPath(QString("/Users/hyphensmbp/kDrive Labör").toStdString())); +} +} // namespace KDC diff --git a/test/server/socketapi/testsocketapi.h b/test/server/socketapi/testsocketapi.h new file mode 100644 index 000000000..5021a4915 --- /dev/null +++ b/test/server/socketapi/testsocketapi.h @@ -0,0 +1,39 @@ +/* + * 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 . + */ + +#include "testincludes.h" + + +namespace KDC { + +class TestSocketApi : public CppUnit::TestFixture { + CPPUNIT_TEST_SUITE(TestSocketApi); + CPPUNIT_TEST(testFileData); + CPPUNIT_TEST_SUITE_END(); + + public: + void setUp(void) final; + void tearDown() override; + void testCreatePlaceholder(); + void testConvertToPlaceholder(); + + protected: + void testFileData(); +}; + +} // namespace KDC diff --git a/test/test_utility/testhelpers.cpp b/test/test_utility/testhelpers.cpp index e47539501..ce95210ef 100644 --- a/test/test_utility/testhelpers.cpp +++ b/test/test_utility/testhelpers.cpp @@ -20,13 +20,13 @@ #include "libcommon/utility/utility.h" -#include #include #ifdef _WIN32 #include "libcommonserver/io/iohelper.h" #else #include +#include #endif namespace KDC::testhelpers { From 2a4344ddb8508d1ceb2d085405c428c3398bda00 Mon Sep 17 00:00:00 2001 From: Luc Guyot Date: Mon, 18 Nov 2024 13:51:01 +0100 Subject: [PATCH 10/15] Adds missing include statements for Windows --- test/test_utility/testhelpers.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/test_utility/testhelpers.cpp b/test/test_utility/testhelpers.cpp index ce95210ef..f2dd5d433 100644 --- a/test/test_utility/testhelpers.cpp +++ b/test/test_utility/testhelpers.cpp @@ -24,6 +24,8 @@ #ifdef _WIN32 #include "libcommonserver/io/iohelper.h" +#include +#include #else #include #include From 00cdcabbc5b59c2fd7365c145c7895f85b559256 Mon Sep 17 00:00:00 2001 From: Luc Guyot Date: Mon, 18 Nov 2024 16:35:41 +0100 Subject: [PATCH 11/15] Fixes typo in Windows struct name --- test/test_utility/testhelpers.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/test/test_utility/testhelpers.cpp b/test/test_utility/testhelpers.cpp index f2dd5d433..e49264437 100644 --- a/test/test_utility/testhelpers.cpp +++ b/test/test_utility/testhelpers.cpp @@ -22,6 +22,7 @@ #include + #ifdef _WIN32 #include "libcommonserver/io/iohelper.h" #include @@ -64,12 +65,12 @@ std::string loadEnvVariable(const std::string& key) { } #ifdef _WIN32 void setModificationDate(const SyncPath& path, const std::chrono::time_point& timePoint) { - struct _utimebuf timeBuffer; + struct _utimbuf timeBuffer; const std::time_t timeInSeconds = std::chrono::system_clock::to_time_t(timePoint); IoError ioError = IoError::Success; FileStat fileStat; - IoHelper::getFileStat(path, &fileStat, ioError); + ::KDC::IoHelper::getFileStat(path, &fileStat, ioError); timeBuffer.tma = fileStat.creationTime; timeBuffer.tmm = timeInSeconds; From 9f5de8a3e849fb3d366fe9c7265b6b07dcde68c0 Mon Sep 17 00:00:00 2001 From: Luc Guyot Date: Mon, 18 Nov 2024 16:56:21 +0100 Subject: [PATCH 12/15] Include missing header and use correct data members of _utimbuf on Windows --- src/libcommonserver/io/iohelper.cpp | 1 - test/test_utility/testhelpers.cpp | 7 ++++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/libcommonserver/io/iohelper.cpp b/src/libcommonserver/io/iohelper.cpp index 821d8b8e8..761d98098 100644 --- a/src/libcommonserver/io/iohelper.cpp +++ b/src/libcommonserver/io/iohelper.cpp @@ -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 diff --git a/test/test_utility/testhelpers.cpp b/test/test_utility/testhelpers.cpp index e49264437..e7541bc5c 100644 --- a/test/test_utility/testhelpers.cpp +++ b/test/test_utility/testhelpers.cpp @@ -24,6 +24,7 @@ #ifdef _WIN32 +#include "libcommonserver/io/filestat.h" #include "libcommonserver/io/iohelper.h" #include #include @@ -72,9 +73,9 @@ void setModificationDate(const SyncPath& path, const std::chrono::time_point& timePoint) { From 8f62c04fdf1949e621851221155030aa956a74c0 Mon Sep 17 00:00:00 2001 From: Luc Guyot Date: Thu, 21 Nov 2024 16:46:46 +0100 Subject: [PATCH 13/15] Fixes error introduced while rebasing wrt develop --- src/libcommon/utility/types.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/libcommon/utility/types.h b/src/libcommon/utility/types.h index d9f151991..4541a3dd7 100644 --- a/src/libcommon/utility/types.h +++ b/src/libcommon/utility/types.h @@ -615,11 +615,11 @@ inline CustomLogWStream &operator<<(CustomLogWStream &os, C e) { } template - inline CustomLogStream &operator<<(CustomLogStream &os, C e) { - return os << toStringWithCode(e); +inline CustomLogStream &operator<<(CustomLogStream &os, C e) { + return os << toStringWithCode(e); } -template +template inline QDebug &operator<<(QDebug &os, C e) { return os << toStringWithCode(e).c_str(); } From 07de4e8f76937c5b5a574e537931d9c3664e8619 Mon Sep 17 00:00:00 2001 From: Luc Guyot Date: Fri, 22 Nov 2024 09:57:56 +0100 Subject: [PATCH 14/15] Addresses one of the comment of Sonar Cloud: uses the Hidden Friend Pattern --- src/libcommon/log/customlogstreams.h | 13 +++++++++++++ src/libcommon/utility/types.h | 11 ----------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/libcommon/log/customlogstreams.h b/src/libcommon/log/customlogstreams.h index 8a273b6dc..37b635f0f 100644 --- a/src/libcommon/log/customlogstreams.h +++ b/src/libcommon/log/customlogstreams.h @@ -21,6 +21,8 @@ #include +#include "libcommon/utility/types.h" + class CustomLogStream : private std::stringstream { public: CustomLogStream() = default; @@ -29,6 +31,12 @@ class CustomLogStream : private std::stringstream { std::string str() const { return std::basic_stringstream::str(); } + template + 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 @@ -92,6 +100,11 @@ class CustomLogWStream : private std::wstringstream { std::wstring str() const { return std::basic_stringstream::str(); } + template + 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 diff --git a/src/libcommon/utility/types.h b/src/libcommon/utility/types.h index 4541a3dd7..fa44c321d 100644 --- a/src/libcommon/utility/types.h +++ b/src/libcommon/utility/types.h @@ -28,7 +28,6 @@ #include #include #include -#include "libcommon/log/customlogstreams.h" namespace KDC { @@ -609,16 +608,6 @@ inline std::ostream &operator<<(std::ostream &os, C e) { return os << toStringWithCode(e); } -template -inline CustomLogWStream &operator<<(CustomLogWStream &os, C e) { - return os << typesUtility::stringToWideString(toStringWithCode(e)); -} - -template -inline CustomLogStream &operator<<(CustomLogStream &os, C e) { - return os << toStringWithCode(e); -} - template inline QDebug &operator<<(QDebug &os, C e) { return os << toStringWithCode(e).c_str(); From 5e6c5aec09eb0987b3d6ddda4a53a41572d6da0c Mon Sep 17 00:00:00 2001 From: Luc Guyot Date: Wed, 27 Nov 2024 08:51:24 +0100 Subject: [PATCH 15/15] Removes irrelevent unit test files comitted accidentally (Clement CR comment) --- test/server/socketapi/testsocketapi.cpp | 51 ------------------------- test/server/socketapi/testsocketapi.h | 39 ------------------- 2 files changed, 90 deletions(-) delete mode 100644 test/server/socketapi/testsocketapi.cpp delete mode 100644 test/server/socketapi/testsocketapi.h diff --git a/test/server/socketapi/testsocketapi.cpp b/test/server/socketapi/testsocketapi.cpp deleted file mode 100644 index 87385ad46..000000000 --- a/test/server/socketapi/testsocketapi.cpp +++ /dev/null @@ -1,51 +0,0 @@ -/* - * 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 . - */ - -#include "socketapi.h" -#include "testsocketapi.h" - -#include -#include - -#ifdef _WIN32 -#include -#endif - -namespace KDC { - - -void TestSocketApi::setUp() {} - -void TestSocketApi::tearDown() {} - -void TestSocketApi::testFileData() { - FileData fileData; - QString localPath = QString("/Users/hyphensmbp/kDrive Labör/Common documents/"); - - QByteArray b1 = localPath.toLatin1(); - CPPUNIT_ASSERT(QString(b1) == localPath); - - // QString parentPath = QFileInfo(localPath).dir().path().toUtf8(); - - // CPPUNIT_ASSERT(QString("/Users/hyphensmbp/kDrive Labör").toStdString() == parentPath.toStdString()); - - // std::filesystem::path localPath_ = QStr2Path(parentPath); - - // CPPUNIT_ASSERT(localPath_ == SyncPath(QString("/Users/hyphensmbp/kDrive Labör").toStdString())); -} -} // namespace KDC diff --git a/test/server/socketapi/testsocketapi.h b/test/server/socketapi/testsocketapi.h deleted file mode 100644 index 5021a4915..000000000 --- a/test/server/socketapi/testsocketapi.h +++ /dev/null @@ -1,39 +0,0 @@ -/* - * 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 . - */ - -#include "testincludes.h" - - -namespace KDC { - -class TestSocketApi : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE(TestSocketApi); - CPPUNIT_TEST(testFileData); - CPPUNIT_TEST_SUITE_END(); - - public: - void setUp(void) final; - void tearDown() override; - void testCreatePlaceholder(); - void testConvertToPlaceholder(); - - protected: - void testFileData(); -}; - -} // namespace KDC