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 by Intra2net AG
25 * @note This module is not thread safe!
26 * @todo make this module thread safe (only useful when we can use threads...)
29 #include "tracefunc.hpp"
41 using Logger::GlobalLogger;
47 typedef std::list< ScopeTracker* > ScopeTrackerList;
49 ScopeTrackerList scope_tracker_list;
52 std::vector< std::string > indents;
56 * @brief ensures indent level string to exist up to the desired level.
57 * @param level the desired indent level.
59 void ensure_indent_level(int level)
61 while (indents.size() <= level)
63 indents.push_back( indents.back() + " " );
65 } // eo ensure_indent_level(int)
69 * @brief module initializer.
76 // pre initialize indent strings:
79 indents.push_back("");
82 indents.push_back( indents.back() + " " );
88 } // eo namespace <anonymous>
92 ** implementation of ScopeTracker
96 * @brief constructor. initializes object with a source location and emits a ENTER message.
97 * @param loc the source location.
99 * the message is indented according to the current depth of nested ScopeTracker instances.
101 ScopeTracker::ScopeTracker(
102 const SourceLocation& loc)
107 if (!scope_tracker_list.empty())
109 ScopeTracker* last_tracker= scope_tracker_list.back();
112 if (last_tracker->Location
113 && last_tracker->Location.File == Location.File
114 && last_tracker->Location.FunctionName == Location.FunctionName)
116 FuncDepth= last_tracker->FuncDepth+1;
119 Depth= last_tracker->Depth + 1;
121 ensure_indent_level(Depth);
122 scope_tracker_list.push_back(this);
124 std::ostringstream ostr;
125 if (Location.FunctionName.empty())
127 ostr << "<unknown> (global scope?)";
131 ostr << Location.FunctionName;
134 ostr << "#" << FuncDepth+1;
140 if (Logger::has_log_level(Logger::LogLevel::Debug))
142 GlobalLogger.debug() << indents[Depth] << "ENTER " << Tag;
144 } // eo ScopeTrcaker::ScopeTracker(const SourceLocation&)
148 * @brief destructor. emits a LEAVE message.
152 ScopeTracker::~ScopeTracker()
155 if (Logger::has_log_level(Logger::LogLevel::Debug))
157 GlobalLogger.debug() << indents[Depth] << "LEAVE " << Tag;
159 if (scope_tracker_list.empty())
163 if (scope_tracker_list.back() == this)
165 scope_tracker_list.pop_back();
169 // oh hell, this should never be the case!
171 GlobalLogger.error() << "inconsistency detected in scope tracker";
173 } // eo ScopeTracker::~ScopeTracker()
178 } // eo namespace Tracer
179 } // eo namespace I2n