From e768856c1466cb12286d63b6aa367f7a6bc4dc05 Mon Sep 17 00:00:00 2001 From: Simon Weis Date: Sat, 1 Jun 2024 08:43:38 +0200 Subject: [PATCH] Use std::source_location --- impl/jamtemplate/common/log/console.cpp | 8 ++++- impl/jamtemplate/common/log/console.hpp | 1 + impl/jamtemplate/common/log/log_entry.hpp | 2 ++ .../common/log/log_target_file.cpp | 5 ++- impl/jamtemplate/common/log/logger.cpp | 34 ++++++++++-------- impl/jamtemplate/common/log/logger.hpp | 21 +++++++---- .../common/log/logger_interface.hpp | 29 +++++++++++---- impl/jamtemplate/common/log/logger_null.cpp | 36 ++++++++++++------- impl/jamtemplate/common/log/logger_null.hpp | 21 +++++++---- test/integration/demo/main.cpp | 2 ++ .../logging_box2d_contact_manager_test.cpp | 16 ++++----- test/unit/jt_test/mocks/mock_logger.hpp | 20 +++++++---- 12 files changed, 130 insertions(+), 65 deletions(-) diff --git a/impl/jamtemplate/common/log/console.cpp b/impl/jamtemplate/common/log/console.cpp index 897b7cb6..45b9caa5 100644 --- a/impl/jamtemplate/common/log/console.cpp +++ b/impl/jamtemplate/common/log/console.cpp @@ -43,6 +43,8 @@ void jt::Console::doDraw() const ImGui::Checkbox("Level", &m_drawLevel); ImGui::SameLine(); ImGui::Checkbox("Tags", &m_drawTag); + ImGui::SameLine(); + ImGui::Checkbox("Source", &m_drawSource); // Display contents in a scrolling region ImGui::TextColored(ImVec4(1, 1, 0, 1), ""); @@ -148,7 +150,11 @@ void jt::Console::renderOneLogEntry(jt::LogEntry const& entry) const levelText = ""; } - std::string text = timeText + levelText + tagText + entry.message; + std::string sourceText; + if (m_drawSource) + sourceText = std::string { entry.source.file_name() } + ":" + + std::to_string(entry.source.line()) + " [" + entry.source.function_name() + "]"; + std::string text = timeText + levelText + tagText + entry.message + sourceText; std::string filterString = m_inputBufferFilter.data(); strutil::trim(filterString); diff --git a/impl/jamtemplate/common/log/console.hpp b/impl/jamtemplate/common/log/console.hpp index 61a623ff..aafa015c 100644 --- a/impl/jamtemplate/common/log/console.hpp +++ b/impl/jamtemplate/common/log/console.hpp @@ -30,6 +30,7 @@ class Console : public jt::GameObject { mutable bool m_drawLevel { true }; mutable bool m_drawTag { true }; mutable bool m_drawTime { false }; + mutable bool m_drawSource { false }; void doCreate() override; void doUpdate(float const /*elapsed*/) override; diff --git a/impl/jamtemplate/common/log/log_entry.hpp b/impl/jamtemplate/common/log/log_entry.hpp index 8bcf4820..4ae320c1 100644 --- a/impl/jamtemplate/common/log/log_entry.hpp +++ b/impl/jamtemplate/common/log/log_entry.hpp @@ -2,6 +2,7 @@ #define JAMTEMPLATE_LOG_ENTRY_HPP #include +#include #include #include @@ -13,6 +14,7 @@ struct LogEntry { std::string time { "" }; LogLevel level { LogLevel::Off }; std::vector tags {}; + std::source_location source {}; }; } // namespace jt diff --git a/impl/jamtemplate/common/log/log_target_file.cpp b/impl/jamtemplate/common/log/log_target_file.cpp index e3a59e04..c6ce0ade 100644 --- a/impl/jamtemplate/common/log/log_target_file.cpp +++ b/impl/jamtemplate/common/log/log_target_file.cpp @@ -8,5 +8,8 @@ jt::LogTargetFile::LogTargetFile() void jt::LogTargetFile::doLog(jt::LogEntry const& entry) { - m_file << entry.time << ": " << entry.message << std::endl; + std::string sourceText = std::string { entry.source.file_name() } + ":" + + std::to_string(entry.source.line()) + " [" + entry.source.function_name() + "]"; + + m_file << entry.time << ": " << entry.message << " " << sourceText << std::endl; } diff --git a/impl/jamtemplate/common/log/logger.cpp b/impl/jamtemplate/common/log/logger.cpp index b1f440b3..ceed6822 100644 --- a/impl/jamtemplate/common/log/logger.cpp +++ b/impl/jamtemplate/common/log/logger.cpp @@ -5,53 +5,59 @@ #include #include -void jt::Logger::action(std::string const& string) +void jt::Logger::action(std::string const& string, std::source_location source) { - addLogEntry(LogEntry { string, "", LogLevel::Action, {} }); + addLogEntry(LogEntry { string, "", LogLevel::Action, {}, source }); } -void jt::Logger::fatal(std::string const& string, std::vector const& tags) +void jt::Logger::fatal( + std::string const& string, std::vector const& tags, std::source_location source) { if (m_logLevel >= LogLevel::Fatal) { - addLogEntry(LogEntry { string, "", LogLevel::Fatal, tags }); + addLogEntry(LogEntry { string, "", LogLevel::Fatal, tags, source }); } } -void jt::Logger::error(std::string const& string, std::vector const& tags) +void jt::Logger::error( + std::string const& string, std::vector const& tags, std::source_location source) { if (m_logLevel >= LogLevel::Error) { - addLogEntry(LogEntry { string, "", LogLevel::Error, tags }); + addLogEntry(LogEntry { string, "", LogLevel::Error, tags, source }); } } -void jt::Logger::warning(std::string const& string, std::vector const& tags) +void jt::Logger::warning( + std::string const& string, std::vector const& tags, std::source_location source) { if (m_logLevel >= LogLevel::Warning) { - addLogEntry(LogEntry { string, "", LogLevel::Warning, tags }); + addLogEntry(LogEntry { string, "", LogLevel::Warning, tags, source }); } } -void jt::Logger::info(std::string const& string, std::vector const& tags) +void jt::Logger::info( + std::string const& string, std::vector const& tags, std::source_location source) { if (m_logLevel >= LogLevel::Info) { - addLogEntry(LogEntry { string, "", LogLevel::Info, tags }); + addLogEntry(LogEntry { string, "", LogLevel::Info, tags, source }); } } -void jt::Logger::debug(std::string const& string, std::vector const& tags) +void jt::Logger::debug( + std::string const& string, std::vector const& tags, std::source_location source) { #ifdef JT_ENABLE_DEBUG if (m_logLevel >= LogLevel::Debug) { - addLogEntry(LogEntry { string, "", LogLevel::Debug, tags }); + addLogEntry(LogEntry { string, "", LogLevel::Debug, tags, source }); } #endif } -void jt::Logger::verbose(std::string const& string, std::vector const& tags) +void jt::Logger::verbose( + std::string const& string, std::vector const& tags, std::source_location source) { #ifdef JT_ENABLE_DEBUG if (m_logLevel >= LogLevel::Verbose) { - addLogEntry(LogEntry { string, "", LogLevel::Verbose, tags }); + addLogEntry(LogEntry { string, "", LogLevel::Verbose, tags, source }); } #endif } diff --git a/impl/jamtemplate/common/log/logger.hpp b/impl/jamtemplate/common/log/logger.hpp index b7b9da13..99f41770 100644 --- a/impl/jamtemplate/common/log/logger.hpp +++ b/impl/jamtemplate/common/log/logger.hpp @@ -11,13 +11,20 @@ class LogTargetInterface; class Logger : public jt::LoggerInterface { public: - void action(std::string const& string) override; - void fatal(std::string const& string, std::vector const& tags = {}) override; - void error(std::string const& string, std::vector const& tags = {}) override; - void warning(std::string const& string, std::vector const& tags = {}) override; - void info(std::string const& string, std::vector const& tags = {}) override; - void debug(std::string const& string, std::vector const& tags = {}) override; - void verbose(std::string const& string, std::vector const& tags = {}) override; + void action( + std::string const& string, std::source_location = std::source_location::current()) override; + void fatal(std::string const& string, std::vector const& tags = {}, + std::source_location = std::source_location::current()) override; + void error(std::string const& string, std::vector const& tags = {}, + std::source_location = std::source_location::current()) override; + void warning(std::string const& string, std::vector const& tags = {}, + std::source_location = std::source_location::current()) override; + void info(std::string const& string, std::vector const& tags = {}, + std::source_location = std::source_location::current()) override; + void debug(std::string const& string, std::vector const& tags = {}, + std::source_location = std::source_location::current()) override; + void verbose(std::string const& string, std::vector const& tags = {}, + std::source_location = std::source_location::current()) override; void addLogTarget(std::shared_ptr target) override; void setLogLevel(LogLevel level) override; diff --git a/impl/jamtemplate/common/log/logger_interface.hpp b/impl/jamtemplate/common/log/logger_interface.hpp index 6354b7fd..0ccc5513 100644 --- a/impl/jamtemplate/common/log/logger_interface.hpp +++ b/impl/jamtemplate/common/log/logger_interface.hpp @@ -3,6 +3,7 @@ #include #include +#include #include #include @@ -15,37 +16,51 @@ class LoggerInterface { /// Log a fatal message /// \param string the log message /// \param tags the message tags - virtual void fatal(std::string const& string, std::vector const& tags = {}) = 0; + virtual void fatal(std::string const& string, std::vector const& tags = {}, + std::source_location = std::source_location::current()) + = 0; /// Log a error message /// \param string the log message /// \param tags the message tags - virtual void error(std::string const& string, std::vector const& tags = {}) = 0; + virtual void error(std::string const& string, std::vector const& tags = {}, + std::source_location = std::source_location::current()) + = 0; /// Log a warning message /// \param string the log message /// \param tags the message tags - virtual void warning(std::string const& string, std::vector const& tags = {}) = 0; + virtual void warning(std::string const& string, std::vector const& tags = {}, + std::source_location = std::source_location::current()) + = 0; /// Log a info message /// \param string the log message /// \param tags the message tags - virtual void info(std::string const& string, std::vector const& tags = {}) = 0; + virtual void info(std::string const& string, std::vector const& tags = {}, + std::source_location = std::source_location::current()) + = 0; /// Log a debug message /// \param string the log message /// \param tags the message tags - virtual void debug(std::string const& string, std::vector const& tags = {}) = 0; + virtual void debug(std::string const& string, std::vector const& tags = {}, + std::source_location = std::source_location::current()) + = 0; /// Log a verbose message /// \param string the log message /// \param tags the message tags - virtual void verbose(std::string const& string, std::vector const& tags = {}) = 0; + virtual void verbose(std::string const& string, std::vector const& tags = {}, + std::source_location = std::source_location::current()) + = 0; /// Log a action message (from the console /// \param string the log message /// \param tags the message tags - virtual void action(std::string const& string) = 0; + virtual void action( + std::string const& string, std::source_location = std::source_location::current()) + = 0; /// Add a log target to the logger /// \param target the target to be added diff --git a/impl/jamtemplate/common/log/logger_null.cpp b/impl/jamtemplate/common/log/logger_null.cpp index 5d5d3823..021c1d03 100644 --- a/impl/jamtemplate/common/log/logger_null.cpp +++ b/impl/jamtemplate/common/log/logger_null.cpp @@ -1,31 +1,41 @@ #include "logger_null.hpp" #include -void jt::null_objects::LoggerNull::action(std::string const& /*string*/) { } +void jt::null_objects::LoggerNull::action( + std::string const& /*string*/, std::source_location /*source*/) +{ +} -void jt::null_objects::LoggerNull::fatal( - std::string const& /*string*/, std::vector const& /*tags*/) +void jt::null_objects::LoggerNull::fatal(std::string const& /*string*/, + std::vector const& /*tags*/, std::source_location /*source*/) { } -void jt::null_objects::LoggerNull::error( - std::string const& /*string*/, std::vector const& /*tags*/) + +void jt::null_objects::LoggerNull::error(std::string const& /*string*/, + std::vector const& /*tags*/, std::source_location /*source*/) { } -void jt::null_objects::LoggerNull::warning( - std::string const& /*string*/, std::vector const& /*tags*/) + +void jt::null_objects::LoggerNull::warning(std::string const& /*string*/, + std::vector const& /*tags*/, std::source_location /*source*/) { } -void jt::null_objects::LoggerNull::info( - std::string const& /*string*/, std::vector const& /*tags*/) + +void jt::null_objects::LoggerNull::info(std::string const& /*string*/, + std::vector const& /*tags*/, std::source_location /*source*/) { } -void jt::null_objects::LoggerNull::debug( - std::string const& /*string*/, std::vector const& /*tags*/) + +void jt::null_objects::LoggerNull::debug(std::string const& /*string*/, + std::vector const& /*tags*/, std::source_location /*source*/) { } -void jt::null_objects::LoggerNull::verbose( - std::string const& /*string*/, std::vector const& /*tags*/) + +void jt::null_objects::LoggerNull::verbose(std::string const& /*string*/, + std::vector const& /*tags*/, std::source_location /*source*/) { } + void jt::null_objects::LoggerNull::addLogTarget(std::shared_ptr /*target*/) { } + void jt::null_objects::LoggerNull::setLogLevel(LogLevel /*level*/) { } diff --git a/impl/jamtemplate/common/log/logger_null.hpp b/impl/jamtemplate/common/log/logger_null.hpp index 530504db..6e1a6693 100644 --- a/impl/jamtemplate/common/log/logger_null.hpp +++ b/impl/jamtemplate/common/log/logger_null.hpp @@ -11,13 +11,20 @@ namespace null_objects { class LoggerNull : public jt::LoggerInterface { public: - void action(std::string const& string) override; - void fatal(std::string const& string, std::vector const& tags = {}) override; - void error(std::string const& string, std::vector const& tags = {}) override; - void warning(std::string const& string, std::vector const& tags = {}) override; - void info(std::string const& string, std::vector const& tags = {}) override; - void debug(std::string const& string, std::vector const& tags = {}) override; - void verbose(std::string const& string, std::vector const& tags = {}) override; + void action( + std::string const& string, std::source_location = std::source_location::current()) override; + void fatal(std::string const& string, std::vector const& tags = {}, + std::source_location = std::source_location::current()) override; + void error(std::string const& string, std::vector const& tags = {}, + std::source_location = std::source_location::current()) override; + void warning(std::string const& string, std::vector const& tags = {}, + std::source_location = std::source_location::current()) override; + void info(std::string const& string, std::vector const& tags = {}, + std::source_location = std::source_location::current()) override; + void debug(std::string const& string, std::vector const& tags = {}, + std::source_location = std::source_location::current()) override; + void verbose(std::string const& string, std::vector const& tags = {}, + std::source_location = std::source_location::current()) override; void addLogTarget(std::shared_ptr target) override; void setLogLevel(LogLevel level) override; }; diff --git a/test/integration/demo/main.cpp b/test/integration/demo/main.cpp index 6f4db572..2b1ea3bc 100644 --- a/test/integration/demo/main.cpp +++ b/test/integration/demo/main.cpp @@ -76,6 +76,8 @@ int main(int /*argc*/, char* /*argv*/[]) keyboard->listenForKey(jt::KeyCode::Num9); keyboard->listenForKey(jt::KeyCode::F1); + keyboard->listenForKey(jt::KeyCode::Home); + keyboard->listenForKey(jt::KeyCode::End); keyboard->listenForKey(jt::KeyCode::Escape); keyboard->listenForKey(jt::KeyCode::Space); keyboard->listenForKey(jt::KeyCode::LShift); diff --git a/test/unit/jt_test/common/box2dwrapper/logging_box2d_contact_manager_test.cpp b/test/unit/jt_test/common/box2dwrapper/logging_box2d_contact_manager_test.cpp index d50ec164..5b355870 100644 --- a/test/unit/jt_test/common/box2dwrapper/logging_box2d_contact_manager_test.cpp +++ b/test/unit/jt_test/common/box2dwrapper/logging_box2d_contact_manager_test.cpp @@ -14,7 +14,7 @@ TEST(LoggingBox2DContactManagerTest, ConstructorWithNullptrThrowsException) TEST(LoggingBox2DContactManagerTest, ConstructorWithNullptrLogsFatal) { MockLogger logger; - EXPECT_CALL(logger, fatal(::testing::_, ::testing::_)); + EXPECT_CALL(logger, fatal(::testing::_, ::testing::_, ::testing::_)); auto const func = [&logger]() { jt::LoggingBox2DContactManager { nullptr, logger }; }; ASSERT_THROW(func(), std::invalid_argument); } @@ -34,7 +34,7 @@ TEST(LoggingBox2DContactManagerTest, ConstructorLogsInfo) { MockLogger logger; auto decoratee = std::make_shared(); - EXPECT_CALL(logger, info(::testing::_, ::testing::_)); + EXPECT_CALL(logger, info(::testing::_, ::testing::_, ::testing::_)); jt::LoggingBox2DContactManager { decoratee, logger }; } @@ -44,7 +44,7 @@ TEST(LoggingBox2DContactManagerTest, SizeLogsVerbose) auto decoratee = std::make_shared(); auto contactManager = jt::LoggingBox2DContactManager { decoratee, logger }; EXPECT_CALL(*decoratee, size()); - EXPECT_CALL(logger, verbose(::testing::_, ::testing::_)); + EXPECT_CALL(logger, verbose(::testing::_, ::testing::_, ::testing::_)); contactManager.size(); } @@ -54,7 +54,7 @@ TEST(LoggingBox2DContactManagerTest, RegisterCallbackLogsInfo) auto decoratee = std::make_shared(); auto contactManager = jt::LoggingBox2DContactManager { decoratee, logger }; EXPECT_CALL(*decoratee, registerCallback("abcd", ::testing::_)); - EXPECT_CALL(logger, info(::testing::_, ::testing::_)); + EXPECT_CALL(logger, info(::testing::_, ::testing::_, ::testing::_)); contactManager.registerCallback("abcd", nullptr); } @@ -64,7 +64,7 @@ TEST(LoggingBox2DContactManagerTest, UnregisterCallbackLogsInfo) auto decoratee = std::make_shared(); auto contactManager = jt::LoggingBox2DContactManager { decoratee, logger }; EXPECT_CALL(*decoratee, unregisterCallback(::testing::_)); - EXPECT_CALL(logger, info(::testing::_, ::testing::_)); + EXPECT_CALL(logger, info(::testing::_, ::testing::_, ::testing::_)); contactManager.unregisterCallback("abcd"); } @@ -74,7 +74,7 @@ TEST(LoggingBox2DContactManagerTest, GetAllCallbackIdentifiersLogsVerbose) auto decoratee = std::make_shared(); auto contactManager = jt::LoggingBox2DContactManager { decoratee, logger }; EXPECT_CALL(*decoratee, getAllCallbackIdentifiers()); - EXPECT_CALL(logger, verbose(::testing::_, ::testing::_)); + EXPECT_CALL(logger, verbose(::testing::_, ::testing::_, ::testing::_)); contactManager.getAllCallbackIdentifiers(); } @@ -84,7 +84,7 @@ TEST(LoggingBox2DContactManagerTest, BeginContactLogsDebug) auto decoratee = std::make_shared(); auto contactManager = jt::LoggingBox2DContactManager { decoratee, logger }; EXPECT_CALL(*decoratee, BeginContact(::testing::_)); - EXPECT_CALL(logger, debug(::testing::_, ::testing::_)); + EXPECT_CALL(logger, debug(::testing::_, ::testing::_, ::testing::_)); contactManager.BeginContact(nullptr); } @@ -94,6 +94,6 @@ TEST(LoggingBox2DContactManagerTest, EndContactLogsDebug) auto decoratee = std::make_shared(); auto contactManager = jt::LoggingBox2DContactManager { decoratee, logger }; EXPECT_CALL(*decoratee, EndContact(::testing::_)); - EXPECT_CALL(logger, debug(::testing::_, ::testing::_)); + EXPECT_CALL(logger, debug(::testing::_, ::testing::_, ::testing::_)); contactManager.EndContact(nullptr); } diff --git a/test/unit/jt_test/mocks/mock_logger.hpp b/test/unit/jt_test/mocks/mock_logger.hpp index 8326283a..f6752fcb 100644 --- a/test/unit/jt_test/mocks/mock_logger.hpp +++ b/test/unit/jt_test/mocks/mock_logger.hpp @@ -9,13 +9,19 @@ class MockLogger : public jt::LoggerInterface { public: - MOCK_METHOD(void, fatal, (std::string const&, std::vector const&), (override)); - MOCK_METHOD(void, error, (std::string const&, std::vector const&), (override)); - MOCK_METHOD(void, warning, (std::string const&, std::vector const&), (override)); - MOCK_METHOD(void, info, (std::string const&, std::vector const&), (override)); - MOCK_METHOD(void, debug, (std::string const&, std::vector const&), (override)); - MOCK_METHOD(void, verbose, (std::string const&, std::vector const&), (override)); - MOCK_METHOD(void, action, (std::string const&), (override)); + MOCK_METHOD(void, fatal, + (std::string const&, std::vector const&, std::source_location), (override)); + MOCK_METHOD(void, error, + (std::string const&, std::vector const&, std::source_location), (override)); + MOCK_METHOD(void, warning, + (std::string const&, std::vector const&, std::source_location), (override)); + MOCK_METHOD(void, info, + (std::string const&, std::vector const&, std::source_location), (override)); + MOCK_METHOD(void, debug, + (std::string const&, std::vector const&, std::source_location), (override)); + MOCK_METHOD(void, verbose, + (std::string const&, std::vector const&, std::source_location), (override)); + MOCK_METHOD(void, action, (std::string const&, std::source_location), (override)); MOCK_METHOD(void, addLogTarget, (std::shared_ptr), (override)); MOCK_METHOD(void, setLogLevel, (jt::LogLevel), (override)); };