From 4ee30be7ca7d6ede791fd19de45a83c386ed689e Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Wed, 27 Aug 2025 16:34:58 +0200 Subject: [PATCH 01/10] Add datetime class --- CMakeLists.txt | 5 ++- code/Common/Logger.cpp | 35 ++++++++++++------- include/cppcore/Common/DateTime.h | 58 +++++++++++++++++++++++++++++++ include/cppcore/Common/Logger.h | 6 ++-- test/common/DateTimeTest.cpp | 11 ++++++ test/common/LoggerTest.cpp | 10 ++++++ 6 files changed, 109 insertions(+), 16 deletions(-) create mode 100644 include/cppcore/Common/DateTime.h create mode 100644 test/common/DateTimeTest.cpp create mode 100644 test/common/LoggerTest.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 9ccc8aa..10548a2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -74,9 +74,10 @@ SET(cppcore_src ) SET(cppcore_common_src + code/Common/Logger.cpp + include/cppcore/Common/DateTime.h include/cppcore/Common/Hash.h include/cppcore/Common/Logger.h - code/Common/Logger.cpp include/cppcore/Common/TStringBase.h include/cppcore/Common/TStringView.h include/cppcore/Common/Variant.h @@ -135,7 +136,9 @@ IF( CPPCORE_BUILD_UNITTESTS ) ) SET( cppcore_common_test_src + test/common/DateTimeTest.cpp test/common/HashTest.cpp + test/common/LoggerTest.cpp test/common/VariantTest.cpp test/common/SortTest.cpp test/common/TBitFieldTest.cpp diff --git a/code/Common/Logger.cpp b/code/Common/Logger.cpp index a5927a7..3edd943 100644 --- a/code/Common/Logger.cpp +++ b/code/Common/Logger.cpp @@ -21,6 +21,7 @@ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -----------------------------------------------------------------------------------------------*/ #include +#include #include #include @@ -88,6 +89,11 @@ Logger *Logger::create() { return sLogger; } +void Logger::set(Logger *logger) { + kill(); + sLogger = logger; +} + Logger *Logger::getInstance() { if (nullptr == sLogger) { static_cast(create()); @@ -97,7 +103,7 @@ Logger *Logger::getInstance() { } void Logger::kill() { - if (sLogger) { + if (sLogger != nullptr) { delete sLogger; sLogger = nullptr; } @@ -134,7 +140,10 @@ void Logger::debug(const String &domain, const String &msg) { } void Logger::info(const String &domain, const String &msg) { - if (getVerboseMode() == VerboseMode::Normal || getVerboseMode() == VerboseMode::Verbose || getVerboseMode() == VerboseMode::Debug || getVerboseMode() == VerboseMode::Trace) { + if (getVerboseMode() == VerboseMode::Normal || + getVerboseMode() == VerboseMode::Verbose || + getVerboseMode() == VerboseMode::Debug || + getVerboseMode() == VerboseMode::Trace) { String logMsg; logMsg += String("Info: ", 6); @@ -243,19 +252,19 @@ Logger::~Logger() { } String Logger::getDateTime() { - //static const uint32_t Space = 2; - /* DateTime currentDateTime = DateTime::getCurrentUTCTime(); + static const uint32_t Space = 2; + DateTime currentDateTime; std::stringstream stream; stream.fill('0'); - stream << std::setw(Space) << currentDateTime.getCurrentDay() << "." - << std::setw(Space) << currentDateTime.getCurrentMonth() << "." - << std::setw(Space * 2) << currentDateTime.getCurrentYear() << " " - << std::setw(Space) << currentDateTime.getCurrentHour() << ":" - << std::setw(Space) << currentDateTime.getCurrentMinute() << ":" - << std::setw(Space) << currentDateTime.getCurrentSeconds(); - */ - String todo("none", 4); - return todo; + stream << std::setw(Space) << currentDateTime.year << "." + << std::setw(Space) << currentDateTime.month << "." + << std::setw(Space * 2) << currentDateTime.day << " " + << std::setw(Space) << currentDateTime.hour << ":" + << std::setw(Space) << currentDateTime.min << ":" + << std::setw(Space) << currentDateTime.sec; + const std::string tmp(stream.str()); + String dateTime(tmp.c_str(), tmp.size()); + return dateTime; } void Logger::StdLogStream::write(const String &msg) { diff --git a/include/cppcore/Common/DateTime.h b/include/cppcore/Common/DateTime.h new file mode 100644 index 0000000..278c208 --- /dev/null +++ b/include/cppcore/Common/DateTime.h @@ -0,0 +1,58 @@ +/*----------------------------------------------------------------------------------------------- +The MIT License (MIT) + +Copyright (c) 2014-2025 Kim Kulling + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +-----------------------------------------------------------------------------------------------*/ +#pragma once + +#include +#include + +namespace cppcore { + +//------------------------------------------------------------------------------------------------- +/// @class THash +/// @ingroup CPPCore +/// +/// @brief This class is used to get the current date and time. +//------------------------------------------------------------------------------------------------- +struct DateTime { + uint32_t year{}; ///< The current year + uint32_t month{}; ///< The current month + uint32_t day{}; ///< The current day + uint32_t hour{}; ///< The current hour + uint32_t min{}; ///< The current minute + uint32_t sec{}; ///< The current second + + /// @brief The class constructor. + DateTime() { + time_t timestamp = ::time(nullptr); + tm dt{}; + dt = *::localtime(×tamp); + year = dt.tm_year + 1900; + month = dt.tm_mon; + day = dt.tm_mday; + hour = dt.tm_hour; + min = dt.tm_min; + sec = dt.tm_sec; + } +}; + +} // namespace cppcore diff --git a/include/cppcore/Common/Logger.h b/include/cppcore/Common/Logger.h index 381c2bb..8416077 100644 --- a/include/cppcore/Common/Logger.h +++ b/include/cppcore/Common/Logger.h @@ -101,11 +101,14 @@ class DLL_CPPCORE_EXPORT Logger final { Count ///< Number of enums }; -public: /// @brief Creates the unique logger instance and returns a pointer showing to it. /// @return The singleton pointer of the logger. static Logger *create(); + /// @brief Will set a user-defined logger instance. + /// @param[in] logger The new logger instance. + static void set(Logger *logger); + /// @brief returns the singleton instance pointer of the logger. /// @return The singleton pointer of the logger. static Logger *getInstance(); @@ -250,4 +253,3 @@ void fatalPrint( const String &domain, const String &file, int line, const Strin /// @param message The warning to writhe into the log. //------------------------------------------------------------------------------------------------- #define log_fatal(domain, message) ::cppcore::fatalPrint(domain, __FILE__, __LINE__, message) - diff --git a/test/common/DateTimeTest.cpp b/test/common/DateTimeTest.cpp new file mode 100644 index 0000000..d2cb928 --- /dev/null +++ b/test/common/DateTimeTest.cpp @@ -0,0 +1,11 @@ +#include +#include "gtest/gtest.h" + +using namespace cppcore; + +class DateTimeTest : public testing::Test {}; + +TEST_F(DateTimeTest, CreateTest) { + DateTime dt; + EXPECT_EQ(dt.year, 2025); +} diff --git a/test/common/LoggerTest.cpp b/test/common/LoggerTest.cpp new file mode 100644 index 0000000..ea54c24 --- /dev/null +++ b/test/common/LoggerTest.cpp @@ -0,0 +1,10 @@ +#include +#include "gtest/gtest.h" + +using namespace cppcore; + +class LoggerTest : public testing::Test {}; + +TEST_F(LoggerTest, CreateTest) { + +} From c52b1a7ae118f8c648fd3cdd076ad65482a6fe1e Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Wed, 27 Aug 2025 21:30:40 +0200 Subject: [PATCH 02/10] Add check to prevent setting the same logger --- code/Common/Logger.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/code/Common/Logger.cpp b/code/Common/Logger.cpp index 3edd943..8236122 100644 --- a/code/Common/Logger.cpp +++ b/code/Common/Logger.cpp @@ -90,6 +90,9 @@ Logger *Logger::create() { } void Logger::set(Logger *logger) { + if (logger == sLogger) { + return; + } kill(); sLogger = logger; } From cc6ad675215d30fbb98c8772f7f163410d33556a Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Wed, 27 Aug 2025 21:31:09 +0200 Subject: [PATCH 03/10] Modify DateTime constructor for localtime safety --- include/cppcore/Common/DateTime.h | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/include/cppcore/Common/DateTime.h b/include/cppcore/Common/DateTime.h index 278c208..73e586b 100644 --- a/include/cppcore/Common/DateTime.h +++ b/include/cppcore/Common/DateTime.h @@ -45,8 +45,11 @@ struct DateTime { DateTime() { time_t timestamp = ::time(nullptr); tm dt{}; - dt = *::localtime(×tamp); - year = dt.tm_year + 1900; +#if defined(_WIN32) + ::localtime_s(&dt, ×tamp); ++#else + ::localtime_r(×tamp, &dt); ++#endif year = dt.tm_year + 1900; month = dt.tm_mon; day = dt.tm_mday; hour = dt.tm_hour; From fd5d65bb5f15b0bf022ed504813d0e704698c91b Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Fri, 29 Aug 2025 20:58:48 +0200 Subject: [PATCH 04/10] Update DateTimeTest.cpp --- test/common/DateTimeTest.cpp | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/test/common/DateTimeTest.cpp b/test/common/DateTimeTest.cpp index d2cb928..ccc94b2 100644 --- a/test/common/DateTimeTest.cpp +++ b/test/common/DateTimeTest.cpp @@ -7,5 +7,16 @@ class DateTimeTest : public testing::Test {}; TEST_F(DateTimeTest, CreateTest) { DateTime dt; - EXPECT_EQ(dt.year, 2025); + const time_t ts = ::time(nullptr); + tm now{}; +#if defined(_WIN32) + ::localtime_s(&now, &ts); ++#else + ::localtime_r(&ts, &now); ++#endif + EXPECT_EQ(dt.year, static_cast(now.tm_year + 1900)); + EXPECT_GE(dt.month, 1u); + EXPECT_LE(dt.month, 12u); + EXPECT_GE(dt.day, 1u); + EXPECT_LE(dt.day, 31u); } From 3d39de69c277dc5364937a623a3bc3fc2181d7f9 Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Fri, 29 Aug 2025 20:59:35 +0200 Subject: [PATCH 05/10] Fix class name in DateTime.h documentation Updated class documentation to reflect correct class name. --- include/cppcore/Common/DateTime.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/cppcore/Common/DateTime.h b/include/cppcore/Common/DateTime.h index 73e586b..e8d52ac 100644 --- a/include/cppcore/Common/DateTime.h +++ b/include/cppcore/Common/DateTime.h @@ -28,7 +28,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. namespace cppcore { //------------------------------------------------------------------------------------------------- -/// @class THash +/// @class DateTime /// @ingroup CPPCore /// /// @brief This class is used to get the current date and time. From 166705faa9c5d445b3ae65c2ae4aecb269ad5b30 Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Fri, 29 Aug 2025 21:00:54 +0200 Subject: [PATCH 06/10] Rename min and sec to minute and second --- include/cppcore/Common/DateTime.h | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/include/cppcore/Common/DateTime.h b/include/cppcore/Common/DateTime.h index e8d52ac..3599e90 100644 --- a/include/cppcore/Common/DateTime.h +++ b/include/cppcore/Common/DateTime.h @@ -38,8 +38,8 @@ struct DateTime { uint32_t month{}; ///< The current month uint32_t day{}; ///< The current day uint32_t hour{}; ///< The current hour - uint32_t min{}; ///< The current minute - uint32_t sec{}; ///< The current second + uint32_t minute{}; ///< The current minute + uint32_t second{}; ///< The current second /// @brief The class constructor. DateTime() { @@ -50,12 +50,13 @@ struct DateTime { +#else ::localtime_r(×tamp, &dt); +#endif year = dt.tm_year + 1900; - month = dt.tm_mon; - day = dt.tm_mday; - hour = dt.tm_hour; - min = dt.tm_min; - sec = dt.tm_sec; + month = dt.tm_mon; + day = dt.tm_mday; + hour = dt.tm_hour; + minute = dt.tm_min; + second = dt.tm_sec; } }; } // namespace cppcore + From ffb62847f5940cdb257f58b79f958dd90ee96fac Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Fri, 29 Aug 2025 21:03:53 +0200 Subject: [PATCH 07/10] Rename min and sec to minute and second --- code/Common/Logger.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/code/Common/Logger.cpp b/code/Common/Logger.cpp index 8236122..369cfcc 100644 --- a/code/Common/Logger.cpp +++ b/code/Common/Logger.cpp @@ -263,8 +263,8 @@ String Logger::getDateTime() { << std::setw(Space) << currentDateTime.month << "." << std::setw(Space * 2) << currentDateTime.day << " " << std::setw(Space) << currentDateTime.hour << ":" - << std::setw(Space) << currentDateTime.min << ":" - << std::setw(Space) << currentDateTime.sec; + << std::setw(Space) << currentDateTime.minute << ":" + << std::setw(Space) << currentDateTime.second; const std::string tmp(stream.str()); String dateTime(tmp.c_str(), tmp.size()); return dateTime; From ecbe3ff610e7cb6e04429bb553ce07c3573e9e2c Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Fri, 29 Aug 2025 21:05:50 +0200 Subject: [PATCH 08/10] Add placeholder comment in CreateTest Added a placeholder comment for future implementation. --- test/common/LoggerTest.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/common/LoggerTest.cpp b/test/common/LoggerTest.cpp index ea54c24..f3ec546 100644 --- a/test/common/LoggerTest.cpp +++ b/test/common/LoggerTest.cpp @@ -6,5 +6,5 @@ using namespace cppcore; class LoggerTest : public testing::Test {}; TEST_F(LoggerTest, CreateTest) { - + // will come } From f895ffd223a45cd16a3b723f26cbf71d641348cb Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Fri, 29 Aug 2025 21:06:32 +0200 Subject: [PATCH 09/10] Add MIT License header to DateTimeTest.cpp Added MIT License header to DateTimeTest.cpp --- test/common/DateTimeTest.cpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/test/common/DateTimeTest.cpp b/test/common/DateTimeTest.cpp index ccc94b2..a3a417b 100644 --- a/test/common/DateTimeTest.cpp +++ b/test/common/DateTimeTest.cpp @@ -1,3 +1,25 @@ +/*----------------------------------------------------------------------------------------------- +The MIT License (MIT) + +Copyright (c) 2014-2025 Kim Kulling + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +-----------------------------------------------------------------------------------------------*/ #include #include "gtest/gtest.h" From 7c6fe18d1204711715ca7e6faab215509c42a44d Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Fri, 29 Aug 2025 21:06:47 +0200 Subject: [PATCH 10/10] Add license header to LoggerTest.cpp Added MIT License header to LoggerTest.cpp --- test/common/LoggerTest.cpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/test/common/LoggerTest.cpp b/test/common/LoggerTest.cpp index f3ec546..9df8981 100644 --- a/test/common/LoggerTest.cpp +++ b/test/common/LoggerTest.cpp @@ -1,3 +1,25 @@ +/*----------------------------------------------------------------------------------------------- +The MIT License (MIT) + +Copyright (c) 2014-2025 Kim Kulling + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +-----------------------------------------------------------------------------------------------*/ #include #include "gtest/gtest.h"