From 7e34f23b41353908a656409347dc99a145cbf02b Mon Sep 17 00:00:00 2001 From: Joshua Landwehr Date: Tue, 14 Dec 2021 23:53:54 -0500 Subject: [PATCH 1/2] Fix bug with __PRETTY_FUNCTION__ being always overwritten. --- src/g3log/g3log.hpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/g3log/g3log.hpp b/src/g3log/g3log.hpp index fb841245..1db5bdd6 100644 --- a/src/g3log/g3log.hpp +++ b/src/g3log/g3log.hpp @@ -30,8 +30,12 @@ #include -#if !(defined(__PRETTY_FUNCTION__)) -#define __PRETTY_FUNCTION__ __FUNCTION__ +#if defined(__GNUC__) // GCC extension compatible +#define G3LOG_PRETTY_FUNCTION __PRETTY_FUNCTION__ +#elif defined(_MSC_VER) // Microsoft +#define G3LOG_PRETTY_FUNCTION __FUNCSIG__ +#else // Fallback to c99 / c++11 +#define G3LOG_PRETTY_FUNCTION __func__ #endif // thread_local doesn't exist before VS2013 @@ -138,10 +142,10 @@ namespace g3 { } // internal } // g3 -#define INTERNAL_LOG_MESSAGE(level) LogCapture(__FILE__, __LINE__, static_cast(__PRETTY_FUNCTION__), level) +#define INTERNAL_LOG_MESSAGE(level) LogCapture(__FILE__, __LINE__, static_cast(G3LOG_PRETTY_FUNCTION), level) #define INTERNAL_CONTRACT_MESSAGE(boolean_expression) \ - LogCapture(__FILE__, __LINE__, __PRETTY_FUNCTION__, g3::internal::CONTRACT, boolean_expression) + LogCapture(__FILE__, __LINE__, G3LOG_PRETTY_FUNCTION, g3::internal::CONTRACT, boolean_expression) // LOG(level) is the API for the stream log From b21938161061dc540bf0c38f9567efb9b6b70e35 Mon Sep 17 00:00:00 2001 From: Joshua Landwehr Date: Wed, 15 Dec 2021 00:23:43 -0500 Subject: [PATCH 2/2] Fix unit test to reflect new pretty function. --- test_unit/test_io.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test_unit/test_io.cpp b/test_unit/test_io.cpp index ac981a58..d5220cd1 100644 --- a/test_unit/test_io.cpp +++ b/test_unit/test_io.cpp @@ -640,7 +640,7 @@ TEST(CustomLogLevels, AddANonFatal) { logger.reset(); std::string file_content = readFileToText(logger.logFile()); std::string expected; - expected += "MY_INFO_LEVEL [test_io.cpp->" + std::string(__FUNCTION__) + ":" + std::to_string(line); + expected += "MY_INFO_LEVEL [test_io.cpp->" + std::string(G3LOG_PRETTY_FUNCTION) + ":" + std::to_string(line); EXPECT_TRUE(verifyContent(file_content, expected)) << file_content << "\n\nExpected: \n" << expected; } @@ -663,7 +663,7 @@ TEST(CustomLogLevels, AddFatal) { std::string file_content = readFileToText(logger.logFile()); std::string expected; - expected += "DEADLY [test_io.cpp->" + std::string(__FUNCTION__) + ":" + std::to_string(line); + expected += "DEADLY [test_io.cpp->" + std::string(G3LOG_PRETTY_FUNCTION) + ":" + std::to_string(line); EXPECT_TRUE(verifyContent(file_content, expected)) << file_content << "\n\nExpected: \n" << expected; g_fatal_counter.store(0); // restore @@ -734,7 +734,7 @@ TEST(CustomLogLevels, AddANonFatal__DidtAddItToEnabledValue) { logger.reset(); std::string file_content = readFileToText(logger.logFile()); std::string expected; - expected += "MY_INFO_LEVEL [test_io.cpp->" + std::string(__FUNCTION__) + ":" + std::to_string(line); + expected += "MY_INFO_LEVEL [test_io.cpp->" + std::string(G3LOG_PRETTY_FUNCTION) + ":" + std::to_string(line); EXPECT_TRUE(verifyContent(file_content, expected)) << file_content << "\n\nExpected: \n" << expected; }