QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
tracing.hpp
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
24#ifndef quantlib_tracing_hpp
25#define quantlib_tracing_hpp
26
27#include <ql/types.hpp>
28#include <ql/errors.hpp>
29#include <ql/patterns/singleton.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
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
QL_INTEGER Integer
integer number
Definition: types.hpp:35
Definition: any.hpp:35