From cbc2639aec361e3832cc1adfc28079b49a89f0ad Mon Sep 17 00:00:00 2001 From: Chris Wilson Date: Mon, 23 Jan 2012 01:32:08 +0000 Subject: Allow overriding Logging::Guard to dump stack backtraces as well. --- lib/common/Box.h | 20 ++++++++++++++++++-- lib/common/Logging.cpp | 3 +++ lib/common/Logging.h | 16 ++++++++++++++++ 3 files changed, 37 insertions(+), 2 deletions(-) (limited to 'lib/common') diff --git a/lib/common/Box.h b/lib/common/Box.h index 4a30891a..6ef797be 100644 --- a/lib/common/Box.h +++ b/lib/common/Box.h @@ -17,6 +17,8 @@ #include "BoxPlatform.h" +#include + // uncomment this line to enable full memory leak finding on all // malloc-ed blocks (at least, ones used by the STL) //#define MEMLEAKFINDER_FULL_MALLOC_MONITORING @@ -104,8 +106,15 @@ #define THROW_EXCEPTION(type, subtype) \ { \ if(!HideExceptionMessageGuard::ExceptionsHidden() \ - || Logging::IsEnabled(Log::EVERYTHING)) \ + || Logging::Guard::IsGuardingFrom(Log::EVERYTHING)) \ { \ + std::auto_ptr guard; \ + \ + if(Logging::Guard::IsGuardingFrom(Log::EVERYTHING)) \ + { \ + guard.reset(new Logging::Guard(Log::EVERYTHING)); \ + } \ + \ OPTIONAL_DO_BACKTRACE \ BOX_WARNING("Exception thrown: " \ #type "(" #subtype ") " \ @@ -119,8 +128,15 @@ std::ostringstream _box_throw_line; \ _box_throw_line << message; \ if(!HideExceptionMessageGuard::ExceptionsHidden() \ - || Logging::IsEnabled(Log::EVERYTHING)) \ + || Logging::Guard::IsGuardingFrom(Log::EVERYTHING)) \ { \ + std::auto_ptr guard; \ + \ + if(Logging::Guard::IsGuardingFrom(Log::EVERYTHING)) \ + { \ + guard.reset(new Logging::Guard(Log::EVERYTHING)); \ + } \ + \ OPTIONAL_DO_BACKTRACE \ BOX_WARNING("Exception thrown: " \ #type "(" #subtype ") (" << message << \ diff --git a/lib/common/Logging.cpp b/lib/common/Logging.cpp index f7f72713..f0cf74b4 100644 --- a/lib/common/Logging.cpp +++ b/lib/common/Logging.cpp @@ -46,6 +46,9 @@ Log::Level Logging::sGlobalLevel = Log::EVERYTHING; Logging Logging::sGlobalLogging; //automatic initialisation std::string Logging::sProgramName; +int Logging::Guard::sGuardCount = 0; +Log::Level Logging::Guard::sOriginalLevel = Log::INVALID; + Logging::Logging() { ASSERT(!spConsole); diff --git a/lib/common/Logging.h b/lib/common/Logging.h index 4ddf760d..1fcbe59e 100644 --- a/lib/common/Logging.h +++ b/lib/common/Logging.h @@ -303,17 +303,33 @@ class Logging { private: Log::Level mOldLevel; + static int sGuardCount; + static Log::Level sOriginalLevel; public: Guard(Log::Level newLevel) { mOldLevel = Logging::GetGlobalLevel(); + if(sGuardCount == 0) + { + sOriginalLevel = mOldLevel; + } + sGuardCount++; Logging::SetGlobalLevel(newLevel); } ~Guard() { + sGuardCount--; Logging::SetGlobalLevel(mOldLevel); } + + static bool IsActive() { return (sGuardCount > 0); } + static Log::Level GetOriginalLevel() { return sOriginalLevel; } + static bool IsGuardingFrom(Log::Level originalLevel) + { + return IsActive() && + (int)sOriginalLevel >= (int)originalLevel; + } }; class Tagger -- cgit v1.2.3