diff --git a/ZEngine/ZEngine/Logging/Logger.cpp b/ZEngine/ZEngine/Logging/Logger.cpp index 733182933..c41ec1f5b 100644 --- a/ZEngine/ZEngine/Logging/Logger.cpp +++ b/ZEngine/ZEngine/Logging/Logger.cpp @@ -160,6 +160,11 @@ namespace ZEngine::Logging void Logger::Log(LogChannel channel, LogLevel level, std::string_view msg) { + if (!IsInitialized()) + { + return; + } + const int min = k_min_level[static_cast(channel)]; if (static_cast(level) < min) { diff --git a/ZEngine/tests/Logging/Logger_test.cpp b/ZEngine/tests/Logging/Logger_test.cpp index f1a025b84..a85ac1bbb 100644 --- a/ZEngine/tests/Logging/Logger_test.cpp +++ b/ZEngine/tests/Logging/Logger_test.cpp @@ -51,6 +51,18 @@ class LoggerTest : public ::testing::Test std::string LoggerTest::s_log_dir; std::string LoggerTest::s_crash_dir; +TEST(LoggerUninitialized, LogBeforeInitializeDoesNotCrash) +{ + if (Logger::IsInitialized()) + { + Logger::Dispose(); + } + + EXPECT_FALSE(Logger::IsInitialized()); + Logger::Log(LogChannel::ENGINE, LogLevel::WARN, "before initialize"); + ZENGINE_CORE_WARN("before initialize via macro"); +} + TEST_F(LoggerTest, IsInitializedAfterInit) { EXPECT_TRUE(Logger::IsInitialized()); @@ -319,3 +331,11 @@ TEST_F(LoggerTest, LevelToStringAllValues) EXPECT_STREQ(Logger::LevelToString(LogLevel::ERR), "error"); EXPECT_STREQ(Logger::LevelToString(LogLevel::CRITICAL), "critical"); } + +TEST_F(LoggerTest, LogAfterDisposeDoesNotCrash) +{ + Logger::Dispose(); + EXPECT_FALSE(Logger::IsInitialized()); + Logger::Log(LogChannel::ENGINE, LogLevel::ERR, "after dispose"); + ZENGINE_CORE_ERROR("after dispose via macro"); +}