From 096e680f525d096cc0575face8251179e9629761 Mon Sep 17 00:00:00 2001 From: Christian Herdtweck Date: Mon, 6 Aug 2018 12:43:33 +0200 Subject: [PATCH] Wrap Scopetracker constructor/destructor in big try-catch --- src/tracefunc.cpp | 109 +++++++++++++++++++++++++++++++--------------------- src/tracefunc.hpp | 7 ++- 2 files changed, 70 insertions(+), 46 deletions(-) diff --git a/src/tracefunc.cpp b/src/tracefunc.cpp index 10351f0..bad392d 100644 --- a/src/tracefunc.cpp +++ b/src/tracefunc.cpp @@ -90,6 +90,7 @@ static PerThreadContainer *TLS_get_container() throw () * @param level the desired indent level. */ static void ensure_indent_level(PerThreadContainer *per_thread, unsigned int level) + throw(std::length_error, std::bad_alloc) { // First initialization? if (per_thread->indents.empty()) @@ -137,9 +138,12 @@ static void ensure_indent_level(PerThreadContainer *per_thread, unsigned int lev * @param loc the source location. * * the message is indented according to the current depth of nested ScopeTracker instances. + * + * All exceptions happening in here (from vector::reserve, vector::push_back, acquiring lock for logger) + * are caught. Will try to log only that problem occurred */ ScopeTracker::ScopeTracker( - const SourceLocation& loc) + const SourceLocation& loc) throw () : Location(loc) , Depth(0) , FuncDepth(0) @@ -150,29 +154,38 @@ ScopeTracker::ScopeTracker( try_logging_error("Failed to get thread local storage"); return; } - if (!PerThread->scope_tracker_list.empty()) - { - ScopeTracker* last_tracker= PerThread->scope_tracker_list.back(); - if (Location) - { - if (last_tracker->Location - && last_tracker->Location.File == Location.File - && last_tracker->Location.FunctionName == Location.FunctionName) - { - FuncDepth= last_tracker->FuncDepth+1; - } - } - Depth= last_tracker->Depth + 1; - } - ensure_indent_level(PerThread, Depth); - PerThread->scope_tracker_list.push_back(this); - // spit a message - if (Logger::has_log_level(Logger::LogLevel::Debug)) - { - GlobalLogger.debug() << PerThread->indents[Depth] << "ENTER " << get_tag(); - } -} // eo ScopeTrcaker::ScopeTracker(const SourceLocation&) + try + { + if (!PerThread->scope_tracker_list.empty()) + { + ScopeTracker* last_tracker= PerThread->scope_tracker_list.back(); + if (Location) + { + if (last_tracker->Location + && last_tracker->Location.File == Location.File + && last_tracker->Location.FunctionName == Location.FunctionName) + { + FuncDepth= last_tracker->FuncDepth+1; + } + } + Depth= last_tracker->Depth + 1; + } + + ensure_indent_level(PerThread, Depth); + PerThread->scope_tracker_list.push_back(this); + + // spit a message + if (Logger::has_log_level(Logger::LogLevel::Debug)) + { + GlobalLogger.debug() << PerThread->indents[Depth] << "ENTER " << get_tag(); + } + } + catch (...) + { + try_logging_error("Caught exception in constructor"); + } +} // eo ScopeTracker::ScopeTracker(const SourceLocation&) /** @@ -208,34 +221,42 @@ std::string& ScopeTracker::get_tag() /** * @brief destructor. emits a LEAVE message. * - * + * All exceptions are caught, will try to log only that some problem occurred */ -ScopeTracker::~ScopeTracker() +ScopeTracker::~ScopeTracker() throw () { if (PerThread == NULL) { try_logging_error("Failed to get thread local storage"); return; } - // spit a message - if (Logger::has_log_level(Logger::LogLevel::Debug)) - { - GlobalLogger.debug() << PerThread->indents[Depth] << "LEAVE " << get_tag(); - } - if (PerThread->scope_tracker_list.empty()) - { - return; - } - if (PerThread->scope_tracker_list.back() == this) - { - PerThread->scope_tracker_list.pop_back(); - } - else - { - // oh hell, this should never be the case! - //TODO - GlobalLogger.error() << "inconsistency detected in scope tracker"; - } + + try + { + // spit a message + if (Logger::has_log_level(Logger::LogLevel::Debug)) + { + GlobalLogger.debug() << PerThread->indents[Depth] << "LEAVE " << get_tag(); + } + if (PerThread->scope_tracker_list.empty()) + { + return; + } + if (PerThread->scope_tracker_list.back() == this) + { + PerThread->scope_tracker_list.pop_back(); + } + else + { + // oh hell, this should never be the case! + //TODO + GlobalLogger.error() << "inconsistency detected in scope tracker"; + } + } + catch (...) + { + try_logging_error("Caught exception in destructor"); + } } // eo ScopeTracker::~ScopeTracker() diff --git a/src/tracefunc.hpp b/src/tracefunc.hpp index a575bfe..36a6227 100644 --- a/src/tracefunc.hpp +++ b/src/tracefunc.hpp @@ -44,12 +44,15 @@ class PerThreadContainer; * * basically: emits a ENTER message on construction and a LEAVE message on destruction. * And indent these messages acoording to the nesting depth. + * + * If something goes wrong nothing is thrown from constructor/destructor. Will try to + * log only that some problem occurred. */ class ScopeTracker { public: - ScopeTracker(const SourceLocation& loc); - ~ScopeTracker(); + ScopeTracker(const SourceLocation& loc) throw (); + ~ScopeTracker() throw (); private: ScopeTracker(const ScopeTracker& rhs); -- 1.7.1