2 The software in this package is distributed under the GNU General
3 Public License version 2 (with a special exception described below).
5 A copy of GNU General Public License (GPL) is included in this distribution,
6 in the file COPYING.GPL.
8 As a special exception, if other files instantiate templates or use macros
9 or inline functions from this file, or you compile this file and link it
10 with other works to produce a work based on this file, this file
11 does not by itself cause the resulting work to be covered
12 by the GNU General Public License.
14 However the source code for this file must still be made available
15 in accordance with section (3) of the GNU General Public License.
17 This exception does not invalidate any other reasons why a work based
18 on this file might be covered by the GNU General Public License.
21 * @brief implementation of tracing functionality.
23 * @copyright © Copyright 2008,2011 by Intra2net AG
25 * Every thread gets its' own ScopeTracker stack via TLS (thread local storage)
29 #include "tracefunc.hpp"
34 #include <boost/thread/tss.hpp>
36 #include <stringfunc.hxx>
43 using Logger::GlobalLogger;
45 class PerThreadContainer
48 typedef std::vector< ScopeTracker* > ScopeTrackerList;
50 /// List of currently active scope trackers
51 ScopeTrackerList scope_tracker_list;
53 /// Precomputed indent levels
54 std::vector< std::string > indents;
60 /// Internal TLS (thread local storage) data container
61 boost::thread_specific_ptr<PerThreadContainer> thread_container;
64 * Helper function to retrieve TLS pointer.
65 * Initializes the pointer if called for the first time.
66 * @return Pointer to the per thread container or NULL if error occurs
68 static PerThreadContainer *TLS_get_container() throw ()
70 // First call? Construct options container
71 if (thread_container.get() == NULL)
75 thread_container.reset(new PerThreadContainer());
83 PerThreadContainer *per_thread = thread_container.get();
89 * @brief ensures indent level string to exist up to the desired level.
90 * @param level the desired indent level.
92 static void ensure_indent_level(PerThreadContainer *per_thread, unsigned int level)
94 // First initialization?
95 if (per_thread->indents.empty())
97 per_thread->indents.reserve(10);
101 per_thread->indents.push_back("");
104 while (per_thread->indents.size() <= level)
106 per_thread->indents.push_back( per_thread->indents.back() + " " );
108 } // eo ensure_indent_level(int)
111 * @brief try logging that some problem occurred
113 * Keep in mind that problem could be with the logger itself, so wrap all this
114 * in a try-catch agin
116 static void try_logging_error(const std::string &message) throw ()
120 GlobalLogger.debug() << "Problem occurred in ScopeTracker: " << message;
123 { // nothing more we can do
128 } // eo namespace <anonymous>
132 ** implementation of ScopeTracker
136 * @brief constructor. initializes object with a source location and emits a ENTER message.
137 * @param loc the source location.
139 * the message is indented according to the current depth of nested ScopeTracker instances.
141 * All exceptions happening in here (from vector::reserve, vector::push_back, acquiring lock for logger)
142 * are caught. Will try to log only that problem occurred
144 ScopeTracker::ScopeTracker(
145 const SourceLocation& loc) throw ()
149 , PerThread(TLS_get_container())
151 if (PerThread == NULL)
153 try_logging_error("Failed to get thread local storage");
159 if (!PerThread->scope_tracker_list.empty())
161 ScopeTracker* last_tracker= PerThread->scope_tracker_list.back();
164 if (last_tracker->Location
165 && last_tracker->Location.File == Location.File
166 && last_tracker->Location.FunctionName == Location.FunctionName)
168 FuncDepth= last_tracker->FuncDepth+1;
171 Depth= last_tracker->Depth + 1;
174 ensure_indent_level(PerThread, Depth);
175 PerThread->scope_tracker_list.push_back(this);
178 if (Logger::has_log_level(Logger::LogLevel::Debug))
180 GlobalLogger.debug() << PerThread->indents[Depth] << "ENTER " << get_tag();
185 try_logging_error("Caught exception in constructor");
187 } // eo ScopeTracker::ScopeTracker(const SourceLocation&)
191 * @brief create Tag if empty; return reference to it
193 * Moved this from constructor into own function to avoid creating Tag if it is
194 * not required (i.e. log level is not DEBUG) but still create it only once and
195 * ensure it is available if log level changes inside tracked function
197 std::string& ScopeTracker::get_tag()
201 if (Location.FunctionName.empty())
203 Tag = "<unknown> (global scope?)";
207 std::ostringstream ostr;
208 ostr << shorten_stl_types(Location.FunctionName);
211 ostr << "#" << FuncDepth+1;
221 * @brief destructor. emits a LEAVE message.
223 * All exceptions are caught, will try to log only that some problem occurred
225 ScopeTracker::~ScopeTracker() throw ()
227 if (PerThread == NULL)
229 try_logging_error("Failed to get thread local storage");
236 if (Logger::has_log_level(Logger::LogLevel::Debug))
238 GlobalLogger.debug() << PerThread->indents[Depth] << "LEAVE " << get_tag();
240 if (PerThread->scope_tracker_list.empty())
244 if (PerThread->scope_tracker_list.back() == this)
246 PerThread->scope_tracker_list.pop_back();
250 // oh hell, this should never be the case!
252 GlobalLogger.error() << "inconsistency detected in scope tracker";
257 try_logging_error("Caught exception in destructor");
259 } // eo ScopeTracker::~ScopeTracker()
264 } // eo namespace Tracer
265 } // eo namespace I2n