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..369cfcc 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,14 @@ Logger *Logger::create() { return sLogger; } +void Logger::set(Logger *logger) { + if (logger == sLogger) { + return; + } + kill(); + sLogger = logger; +} + Logger *Logger::getInstance() { if (nullptr == sLogger) { static_cast(create()); @@ -97,7 +106,7 @@ Logger *Logger::getInstance() { } void Logger::kill() { - if (sLogger) { + if (sLogger != nullptr) { delete sLogger; sLogger = nullptr; } @@ -134,7 +143,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 +255,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.minute << ":" + << std::setw(Space) << currentDateTime.second; + 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..3599e90 --- /dev/null +++ b/include/cppcore/Common/DateTime.h @@ -0,0 +1,62 @@ +/*----------------------------------------------------------------------------------------------- +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 DateTime +/// @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 minute{}; ///< The current minute + uint32_t second{}; ///< The current second + + /// @brief The class constructor. + DateTime() { + time_t timestamp = ::time(nullptr); + tm dt{}; +#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; + minute = dt.tm_min; + second = 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..a3a417b --- /dev/null +++ b/test/common/DateTimeTest.cpp @@ -0,0 +1,44 @@ +/*----------------------------------------------------------------------------------------------- +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" + +using namespace cppcore; + +class DateTimeTest : public testing::Test {}; + +TEST_F(DateTimeTest, CreateTest) { + DateTime dt; + 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); +} diff --git a/test/common/LoggerTest.cpp b/test/common/LoggerTest.cpp new file mode 100644 index 0000000..9df8981 --- /dev/null +++ b/test/common/LoggerTest.cpp @@ -0,0 +1,32 @@ +/*----------------------------------------------------------------------------------------------- +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" + +using namespace cppcore; + +class LoggerTest : public testing::Test {}; + +TEST_F(LoggerTest, CreateTest) { + // will come +}