QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
tracing.hpp
Go to the documentation of this file.
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2005 StatPro Italia srl
5
6 This file is part of QuantLib, a free-software/open-source library
7 for financial quantitative analysts and developers - http://quantlib.org/
8
9 QuantLib is free software: you can redistribute it and/or modify it
10 under the terms of the QuantLib license. You should have received a
11 copy of the license along with this program; if not, please email
12 <quantlib-dev@lists.sf.net>. The license is also available online at
13 <http://quantlib.org/license.shtml>.
14
15 This program is distributed in the hope that it will be useful, but WITHOUT
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 FOR A PARTICULAR PURPOSE. See the license for more details.
18*/
19
20/*! \file tracing.hpp
21 \brief tracing facilities
22*/
23
24#ifndef quantlib_tracing_hpp
25#define quantlib_tracing_hpp
26
27#include <ql/types.hpp>
28#include <ql/errors.hpp>
30#include <boost/current_function.hpp>
31#include <iosfwd>
32
33namespace QuantLib {
34
35 namespace detail {
36
37 class Tracing : public Singleton<Tracing> {
38 friend class QuantLib::Singleton<Tracing>;
39 private:
40 Tracing(); // NOLINT(modernize-use-equals-delete)
41 public:
42 void enable() {
43 #if defined(QL_ENABLE_TRACING)
44 enabled_ = true;
45 #else
46 QL_FAIL("tracing support not available");
47 #endif
48 }
49 void disable() { enabled_ = false; }
50 void setStream(std::ostream& stream) { out_ = &stream; }
51 bool enabled() const { return enabled_; }
52 std::ostream& stream() { return *out_; }
53 Integer depth() const { return depth_; }
54 void down() { depth_++; }
55 void up() { depth_--; }
56 private:
57 std::ostream* out_;
58 bool enabled_ = false;
60 };
61
62 }
63
64}
65
66/*! \addtogroup macros
67 @{
68*/
69
70/*! \defgroup debugMacros Debugging macros
71
72 For debugging purposes, macros can be used to output information
73 about the code being executed. Instrumenting code as in:
74 \code
75 namespace Foo {
76
77 int bar(int i) {
78 QL_TRACE_ENTER_FUNCTION;
79 QL_TRACE_VARIABLE(i);
80
81 if (i == 42) {
82 QL_TRACE_LOCATION;
83 QL_TRACE("Right answer, but no question");
84 } else {
85 QL_TRACE_LOCATION;
86 QL_TRACE("Wrong answer");
87 i *= 2;
88 }
89
90 QL_TRACE_VARIABLE(i);
91 QL_TRACE_EXIT_FUNCTION;
92 return i;
93 }
94
95 }
96 \endcode
97 will output a trace like the following when the code is run:
98 \code
99 trace[3]: Entering int Foo::bar(int)
100 trace[3]: i = 21
101 trace[3]: At line 16 in tracing_example.cpp
102 trace[3]: Wrong answer
103 trace[3]: i = 42
104 trace[3]: Exiting int Foo::bar(int)
105 \endcode
106 (the actual output will depend on the compiler and the file
107 names). A word of warning must be added: adding so much tracing
108 to your code might degrade its readability.
109
110 @{
111*/
112
113/*! \def QL_TRACE_ENABLE
114 \brief enable tracing
115
116 The statement
117 \code
118 QL_TRACE_ENABLE;
119 \endcode
120 can be used to enable tracing. Such statement might be
121 ignored; refer to QL_TRACE for details.
122*/
123
124/*! \def QL_TRACE_DISABLE
125 \brief disable tracing
126
127 The statement
128 \code
129 QL_TRACE_DISABLE;
130 \endcode
131 can be used to disable tracing. Such statement might be
132 ignored; refer to QL_TRACE for details.
133*/
134
135/*! \def QL_TRACE_ON
136 \brief set tracing stream
137
138 The statement
139 \code
140 QL_TRACE_ON(stream);
141 \endcode
142 can be used to set the stream where tracing messages are
143 output. Such statement might be ignored; refer to QL_TRACE for
144 details.
145*/
146
147/*! \def QL_TRACE
148 \brief output tracing information
149
150 The statement
151 \code
152 QL_TRACE(message);
153 \endcode
154 can be used to output a trace of the code being executed. If
155 tracing was disabled during configuration, such statements are
156 removed by the preprocessor for maximum performance; if it was
157 enabled, whether and where the message is output depends on the
158 current settings.
159*/
160
161/*! \def QL_TRACE_ENTER_FUNCTION
162 \brief output tracing information
163
164 The statement
165 \code
166 QL_TRACE_ENTER_FUNCTION;
167 \endcode
168 can be used at the beginning of a function to trace the fact that
169 the program execution is entering such function. It should be
170 paired with a corresponding QL_TRACE_EXIT_FUNCTION macro. Such
171 statement might be ignored; refer to QL_TRACE for details. Also,
172 function information might not be available depending on the
173 compiler.
174*/
175
176/*! \def QL_TRACE_EXIT_FUNCTION
177 \brief output tracing information
178
179 The statement
180 \code
181 QL_TRACE_EXIT_FUNCTION;
182 \endcode
183 can be used before returning from a function to trace the fact
184 that the program execution is exiting such function. It should be
185 paired with a corresponding QL_TRACE_ENTER_FUNCTION macro. Such
186 statement might be ignored; refer to QL_TRACE for details. Also,
187 function information might not be available depending on the
188 compiler.
189*/
190
191/*! \def QL_TRACE_LOCATION
192 \brief output tracing information
193
194 The statement
195 \code
196 QL_TRACE_LOCATION;
197 \endcode
198 can be used to trace the current file and line. Such statement
199 might be ignored; refer to QL_TRACE for details.
200*/
201
202/*! \def QL_TRACE_VARIABLE
203 \brief output tracing information
204
205 The statement
206 \code
207 QL_TRACE_VARIABLE(variable);
208 \endcode
209 can be used to trace the current value of a variable. Such
210 statement might be ignored; refer to QL_TRACE for details. Also,
211 the variable type must allow sending it to an output stream.
212*/
213
214/*! @} */
215
216/*! @} */
217
218#if defined(QL_ENABLE_TRACING)
219
220#define QL_DEFAULT_TRACER QuantLib::detail::Tracing::instance()
221
222#define QL_TRACE_ENABLE \
223QL_DEFAULT_TRACER.enable()
224
225#define QL_TRACE_DISABLE \
226QL_DEFAULT_TRACER.disable()
227
228#define QL_TRACE_ON(out) \
229QL_DEFAULT_TRACER.setStream(out)
230
231#define QL_TRACE(message) \
232if (QL_DEFAULT_TRACER.enabled()) \
233 try { \
234 QL_DEFAULT_TRACER.stream() << "trace[" << QL_DEFAULT_TRACER.depth() \
235 << "]: " << message << std::endl; \
236 } catch (...) {} \
237else
238
239#define QL_TRACE_ENTER_FUNCTION \
240if (QL_DEFAULT_TRACER.enabled()) \
241 try { \
242 QL_DEFAULT_TRACER.down(); \
243 QL_DEFAULT_TRACER.stream() << "trace[" << QL_DEFAULT_TRACER.depth() \
244 << "]: " \
245 << "Entering " << BOOST_CURRENT_FUNCTION \
246 << std::endl; \
247 } catch (...) {} \
248else
249
250#define QL_TRACE_EXIT_FUNCTION \
251if (QL_DEFAULT_TRACER.enabled()) \
252 try { \
253 QL_DEFAULT_TRACER.stream() << "trace[" << QL_DEFAULT_TRACER.depth() \
254 << "]: " \
255 << "Exiting " << BOOST_CURRENT_FUNCTION \
256 << std::endl; \
257 QL_DEFAULT_TRACER.up(); \
258 } catch (...) { QL_DEFAULT_TRACER.up(); } \
259else
260
261#define QL_TRACE_LOCATION \
262QL_TRACE("At line " << __LINE__ << " in " << __FILE__)
263
264#define QL_TRACE_VARIABLE(variable) \
265QL_TRACE(#variable << " = " << variable)
266
267#else
268
269#define QL_DEFAULT_TRACER
270#define QL_TRACE_ENABLE
271#define QL_TRACE_DISABLE
272#define QL_TRACE_ON(out)
273#define QL_TRACE(message)
274#define QL_TRACE_ENTER_FUNCTION
275#define QL_TRACE_EXIT_FUNCTION
276#define QL_TRACE_LOCATION
277#define QL_TRACE_VARIABLE(variable)
278
279#endif
280
281#endif
Basic support for the singleton pattern.
Definition: singleton.hpp:58
std::ostream * out_
Definition: tracing.hpp:57
std::ostream & stream()
Definition: tracing.hpp:52
void setStream(std::ostream &stream)
Definition: tracing.hpp:50
Integer depth() const
Definition: tracing.hpp:53
bool enabled() const
Definition: tracing.hpp:51
Classes and functions for error handling.
#define QL_FAIL(message)
throw an error (possibly with file and line information)
Definition: errors.hpp:92
QL_INTEGER Integer
integer number
Definition: types.hpp:35
Definition: any.hpp:35
basic support for the singleton pattern
Custom types.