Doxygen
Loading...
Searching...
No Matches
trace.h
Go to the documentation of this file.
1/******************************************************************************
2 *
3 * Copyright (C) 1997-2023 by Dimitri van Heesch.
4 *
5 * Permission to use, copy, modify, and distribute this software and its
6 * documentation under the terms of the GNU General Public License is hereby
7 * granted. No representations are made about the suitability of this software
8 * for any purpose. It is provided "as is" without express or implied warranty.
9 * See the GNU General Public License for more details.
10 *
11 * Documents produced by Doxygen are derivative works derived from the
12 * input used in their production; they are not affected by this license.
13 *
14 */
15
16#ifndef TRACE_H
17#define TRACE_H
18
19#ifndef ENABLE_TRACING
20#ifndef NDEBUG
21#define ENABLE_TRACING 1
22#else
23#define ENABLE_TRACING 0
24#endif
25#endif
26
27// Since some modules produce a huge amount of tracing we disable those traces by default.
28// Set of or more of the following to 1 to enable the relevant tracing
29#define ENABLE_SYMBOLRESOLVER_TRACING 0
30#define ENABLE_MARKDOWN_TRACING 0
31#define ENABLE_DOCPARSER_TRACING 0
32
33
34#if ENABLE_TRACING
35#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_TRACE // debug build
36#else
37#define SPELOG_ACTIVE_LEVEL SPDLOG_LEVEL_INFO // release build (hide trace/debug levels)
38#endif
39
40#pragma push_macro("warn")
41#undef warn
42#include "spdlog/spdlog.h"
43#pragma pop_macro("warn")
44
45#include "message.h"
46#include "qcstring.h"
47#include "construct.h"
48
49extern std::shared_ptr<spdlog::logger> g_tracer;
50
51void initTracing(const QCString &logFile, bool timing);
52void exitTracing();
53
54namespace Trace
55{
56 inline QCString trunc(const QCString &s,size_t numChars=15)
57 {
58 QCString result;
59 size_t i=0;
60 for (; i<numChars && i<s.length(); i++)
61 {
62 char c=s.at(i);
63 if (c=='\n') { result+="\\n"; }
64 else if (c=='\t') { result+="\\t"; }
65 else if (c=='\r') { result+="\\r"; }
66 else if (c=='\\') { result+="\\\\"; }
67 else result+=c;
68 }
69 if (i<s.length()) result+="...";
70 return result;
71 }
72}
73
74#if ENABLE_TRACING
75#define TRACE(...) if (g_tracer) SPDLOG_LOGGER_TRACE(g_tracer,__VA_ARGS__)
76#else
77#define TRACE(...) (void)0
78#endif
79
80/** Helper class to trace an entry statement at creation and another one at destruction. */
82{
83 public:
84 explicit AutoTrace(spdlog::source_loc loc) : m_loc(loc)
85 {
86 if (g_tracer)
87 {
88 g_tracer->log(m_loc,spdlog::level::trace,">");
89 }
90 }
91 template<typename... Args>
92 explicit AutoTrace(spdlog::source_loc loc,
93 const std::string &fmt, Args&&...args) : m_loc(loc)
94 {
95 if (g_tracer)
96 {
97 if (fmt.empty())
98 {
99 g_tracer->log(m_loc,spdlog::level::trace,">");
100 }
101 else
102 {
103 g_tracer->log(m_loc,spdlog::level::trace,fmt::runtime("> "+fmt),std::forward<Args>(args)...);
104 }
105 }
106 }
108 {
109 if (g_tracer)
110 {
111 if (m_exitMessage.empty())
112 {
113 g_tracer->log(m_loc,spdlog::level::trace,"<");
114 }
115 else
116 {
117 g_tracer->log(m_loc,spdlog::level::trace,"< "+m_exitMessage);
118 }
119 }
120 }
122
123 template<typename... Args>
124 void add(spdlog::source_loc loc,
125 const std::string &fmt, Args&&...args)
126 {
127 if (g_tracer)
128 {
129 g_tracer->log(loc,spdlog::level::trace,fmt::runtime(": "+fmt),std::forward<Args>(args)...);
130 }
131 }
132 template<typename... Args>
133 void setExit(spdlog::source_loc loc,
134 const std::string &msg,Args&&...args)
135 {
136 m_loc = loc;
137 m_exitMessage = fmt::format(fmt::runtime(msg),std::forward<Args>(args)...);
138 }
139 private:
140 spdlog::source_loc m_loc;
141 std::string m_exitMessage;
142};
143
144#if ENABLE_TRACING
145#define AUTO_TRACE(...) AutoTrace trace_{spdlog::source_loc{__FILE__,__LINE__,SPDLOG_FUNCTION},__VA_ARGS__}
146#define AUTO_TRACE_ADD(...) trace_.add(spdlog::source_loc{__FILE__,__LINE__,SPDLOG_FUNCTION},__VA_ARGS__)
147#define AUTO_TRACE_EXIT(...) trace_.setExit(spdlog::source_loc{__FILE__,__LINE__,SPDLOG_FUNCTION},__VA_ARGS__)
148#else
149#define AUTO_TRACE(...) (void)0
150#define AUTO_TRACE_ADD(...) (void)0
151#define AUTO_TRACE_EXIT(...) (void)0
152#endif
153
154
155#endif // TRACE_H
Helper class to trace an entry statement at creation and another one at destruction.
Definition trace.h:82
AutoTrace(spdlog::source_loc loc, const std::string &fmt, Args &&...args)
Definition trace.h:92
AutoTrace(spdlog::source_loc loc)
Definition trace.h:84
~AutoTrace()
Definition trace.h:107
spdlog::source_loc m_loc
Definition trace.h:140
std::string m_exitMessage
Definition trace.h:141
void add(spdlog::source_loc loc, const std::string &fmt, Args &&...args)
Definition trace.h:124
void setExit(spdlog::source_loc loc, const std::string &msg, Args &&...args)
Definition trace.h:133
This is an alternative implementation of QCString.
Definition qcstring.h:101
size_t length() const
Returns the length of the string, not counting the 0-terminator.
Definition qcstring.h:153
char & at(size_t i)
Returns a reference to the character at index i.
Definition qcstring.h:578
#define NON_COPYABLE(cls)
Macro to help implementing the rule of 5 for a non-copyable & movable class.
Definition construct.h:37
const int numChars
Definition image.cpp:36
#define msg(fmt,...)
Definition message.h:94
Definition trace.h:55
QCString trunc(const QCString &s, size_t numChars=15)
Definition trace.h:56
Definition message.h:144
std::shared_ptr< spdlog::logger > g_tracer
Definition trace.cpp:20
void exitTracing()
Definition trace.cpp:52
std::shared_ptr< spdlog::logger > g_tracer
Definition trace.cpp:20
void initTracing(const QCString &logFile, bool timing)
Definition trace.cpp:22