Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 4 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
38 changes: 25 additions & 13 deletions code/Common/Logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <cppcore/Common/Logger.h>
#include <cppcore/Common/DateTime.h>

#include <cassert>
#include <iomanip>
Expand Down Expand Up @@ -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<void>(create());
Expand All @@ -97,7 +106,7 @@ Logger *Logger::getInstance() {
}

void Logger::kill() {
if (sLogger) {
if (sLogger != nullptr) {
delete sLogger;
sLogger = nullptr;
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down
62 changes: 62 additions & 0 deletions include/cppcore/Common/DateTime.h
Original file line number Diff line number Diff line change
@@ -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 <cppcore/CPPCoreCommon.h>
#include <ctime>

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, &timestamp);
+#else
::localtime_r(&timestamp, &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

6 changes: 4 additions & 2 deletions include/cppcore/Common/Logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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)

44 changes: 44 additions & 0 deletions test/common/DateTimeTest.cpp
Original file line number Diff line number Diff line change
@@ -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 <cppcore/Common/DateTime.h>
#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<uint32_t>(now.tm_year + 1900));
EXPECT_GE(dt.month, 1u);
EXPECT_LE(dt.month, 12u);
EXPECT_GE(dt.day, 1u);
EXPECT_LE(dt.day, 31u);
}
32 changes: 32 additions & 0 deletions test/common/LoggerTest.cpp
Original file line number Diff line number Diff line change
@@ -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 <cppcore/Common/Logger.h>
#include "gtest/gtest.h"

using namespace cppcore;

class LoggerTest : public testing::Test {};

TEST_F(LoggerTest, CreateTest) {
// will come
}