Branch data Line data Source code
1 : : /* Hierarchical log messages for the analyzer.
2 : : Copyright (C) 2014-2026 Free Software Foundation, Inc.
3 : : Contributed by David Malcolm <dmalcolm@redhat.com>.
4 : :
5 : : This file is part of GCC.
6 : :
7 : : GCC is free software; you can redistribute it and/or modify it
8 : : under the terms of the GNU General Public License as published by
9 : : the Free Software Foundation; either version 3, or (at your option)
10 : : any later version.
11 : :
12 : : GCC is distributed in the hope that it will be useful, but
13 : : WITHOUT ANY WARRANTY; without even the implied warranty of
14 : : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 : : General Public License for more details.
16 : :
17 : : You should have received a copy of the GNU General Public License
18 : : along with GCC; see the file COPYING3. If not see
19 : : <http://www.gnu.org/licenses/>. */
20 : :
21 : : /* Adapted from jit-logging.h. */
22 : :
23 : : #ifndef ANALYZER_LOGGING_H
24 : : #define ANALYZER_LOGGING_H
25 : :
26 : : #include "diagnostic-core.h"
27 : :
28 : : namespace ana {
29 : :
30 : : /* A logger encapsulates a logging stream: a way to send
31 : : lines of pertinent information to a FILE *. */
32 : :
33 : : class logger
34 : : {
35 : : public:
36 : : logger (FILE *f_out, int flags, int verbosity, const pretty_printer &reference_pp);
37 : : ~logger ();
38 : :
39 : : void incref (const char *reason);
40 : : void decref (const char *reason);
41 : :
42 : : void log (const char *fmt, ...)
43 : : ATTRIBUTE_GCC_DIAG(2, 3);
44 : : void log_va (const char *fmt, va_list *ap)
45 : : ATTRIBUTE_GCC_DIAG(2, 0);
46 : : void start_log_line ();
47 : : void log_partial (const char *fmt, ...)
48 : : ATTRIBUTE_GCC_DIAG(2, 3);
49 : : void log_va_partial (const char *fmt, va_list *ap)
50 : : ATTRIBUTE_GCC_DIAG(2, 0);
51 : : void end_log_line ();
52 : :
53 : : void enter_scope (const char *scope_name);
54 : : void enter_scope (const char *scope_name, const char *fmt, va_list *ap)
55 : : ATTRIBUTE_GCC_DIAG(3, 0);
56 : : void exit_scope (const char *scope_name);
57 : 2182 : void inc_indent () { m_indent_level++; }
58 : 2182 : void dec_indent () { m_indent_level--; }
59 : :
60 : 2845 : pretty_printer *get_printer () const { return m_pp.get (); }
61 : 2 : FILE *get_file () const { return m_f_out; }
62 : :
63 : : private:
64 : : DISABLE_COPY_AND_ASSIGN (logger);
65 : :
66 : : int m_refcount;
67 : : FILE *m_f_out;
68 : : int m_indent_level;
69 : : bool m_log_refcount_changes;
70 : : std::unique_ptr<pretty_printer> m_pp;
71 : : };
72 : :
73 : : /* The class log_scope is an RAII-style class intended to make
74 : : it easy to notify a logger about entering and exiting the body of a
75 : : given function. */
76 : :
77 : : class log_scope
78 : : {
79 : : public:
80 : : log_scope (logger *logger, const char *name);
81 : : log_scope (logger *logger, const char *name, const char *fmt, ...)
82 : : ATTRIBUTE_GCC_DIAG(4, 5);
83 : : ~log_scope ();
84 : :
85 : : private:
86 : : DISABLE_COPY_AND_ASSIGN (log_scope);
87 : :
88 : : logger *m_logger;
89 : : const char *m_name;
90 : : };
91 : :
92 : : /* The constructor for log_scope.
93 : :
94 : : The normal case is that the logger is nullptr, in which case this should
95 : : be largely a no-op.
96 : :
97 : : If we do have a logger, notify it that we're entering the given scope.
98 : : We also need to hold a reference on it, to avoid a use-after-free
99 : : when logging the cleanup of the owner of the logger. */
100 : :
101 : : inline
102 : 4216667 : log_scope::log_scope (logger *logger, const char *name) :
103 : 4216667 : m_logger (logger),
104 : 4216667 : m_name (name)
105 : : {
106 : 4216667 : if (m_logger)
107 : : {
108 : 1375 : m_logger->incref ("log_scope ctor");
109 : 1375 : m_logger->enter_scope (m_name);
110 : : }
111 : 4216667 : }
112 : :
113 : : inline
114 : 1486867 : log_scope::log_scope (logger *logger, const char *name, const char *fmt, ...):
115 : 1486867 : m_logger (logger),
116 : 1486867 : m_name (name)
117 : : {
118 : 1486867 : if (m_logger)
119 : : {
120 : 318 : m_logger->incref ("log_scope ctor");
121 : 318 : va_list ap;
122 : 318 : va_start (ap, fmt);
123 : 318 : m_logger->enter_scope (m_name, fmt, &ap);
124 : 318 : va_end (ap);
125 : : }
126 : 1486867 : }
127 : :
128 : :
129 : : /* The destructor for log_scope; essentially the opposite of
130 : : the constructor. */
131 : :
132 : : inline
133 : 5703528 : log_scope::~log_scope ()
134 : : {
135 : 5703528 : if (m_logger)
136 : : {
137 : 1693 : m_logger->exit_scope (m_name);
138 : 1693 : m_logger->decref ("log_scope dtor");
139 : : }
140 : 5703528 : }
141 : :
142 : : class log_nesting_level
143 : : {
144 : : public:
145 : : log_nesting_level (logger *logger, const char *fmt, ...)
146 : : ATTRIBUTE_GCC_DIAG(3, 4);
147 : : ~log_nesting_level ();
148 : :
149 : : private:
150 : : logger *m_logger;
151 : : };
152 : :
153 : : inline
154 : 482203 : log_nesting_level::log_nesting_level (logger *logger, const char *fmt, ...)
155 : 482203 : : m_logger (logger)
156 : : {
157 : 482203 : if (logger)
158 : : {
159 : 101 : va_list ap;
160 : 101 : va_start (ap, fmt);
161 : :
162 : 101 : logger->start_log_line ();
163 : 101 : logger->log_va_partial (fmt, &ap);
164 : 101 : logger->end_log_line ();
165 : :
166 : 101 : logger->inc_indent ();
167 : :
168 : 101 : va_end (ap);
169 : : }
170 : 482203 : }
171 : :
172 : :
173 : : /* The destructor for log_nesting_level; essentially the opposite of
174 : : the constructor. */
175 : :
176 : : inline
177 : 482203 : log_nesting_level::~log_nesting_level ()
178 : : {
179 : 482203 : if (m_logger)
180 : 101 : m_logger->dec_indent ();
181 : 410146 : }
182 : :
183 : : /* A log_user is something that potentially uses a logger (which could be
184 : : nullptr).
185 : :
186 : : The log_user class keeps the reference-count of a logger up-to-date. */
187 : :
188 : : class log_user
189 : : {
190 : : public:
191 : : log_user (logger *logger);
192 : : ~log_user ();
193 : :
194 : 7983611 : logger * get_logger () const { return m_logger; }
195 : : void set_logger (logger * logger);
196 : :
197 : : void log (const char *fmt, ...) const
198 : : ATTRIBUTE_GCC_DIAG(2, 3);
199 : :
200 : : void start_log_line () const;
201 : : void end_log_line () const;
202 : :
203 : : void enter_scope (const char *scope_name);
204 : : void exit_scope (const char *scope_name);
205 : :
206 : : pretty_printer *get_logger_pp () const
207 : : {
208 : : gcc_assert (m_logger);
209 : : return m_logger->get_printer ();
210 : : }
211 : :
212 : 3299 : FILE *get_logger_file () const
213 : : {
214 : 3297 : if (m_logger == nullptr)
215 : : return nullptr;
216 : 2 : return m_logger->get_file ();
217 : : }
218 : :
219 : : private:
220 : : DISABLE_COPY_AND_ASSIGN (log_user);
221 : :
222 : : logger *m_logger;
223 : : };
224 : :
225 : : /* A shortcut for calling log from a log_user, handling the common
226 : : case where the underlying logger is nullptr via a no-op. */
227 : :
228 : : inline void
229 : 51239 : log_user::log (const char *fmt, ...) const
230 : : {
231 : 51239 : if (m_logger)
232 : : {
233 : 159 : va_list ap;
234 : 159 : va_start (ap, fmt);
235 : 159 : m_logger->log_va (fmt, &ap);
236 : 159 : va_end (ap);
237 : : }
238 : 51239 : }
239 : :
240 : : /* A shortcut for starting a log line from a log_user,
241 : : handling the common case where the underlying logger is nullptr via
242 : : a no-op. */
243 : :
244 : : inline void
245 : : log_user::start_log_line () const
246 : : {
247 : : if (m_logger)
248 : : m_logger->start_log_line ();
249 : : }
250 : :
251 : : /* A shortcut for ending a log line from a log_user,
252 : : handling the common case where the underlying logger is nullptr via
253 : : a no-op. */
254 : :
255 : : inline void
256 : : log_user::end_log_line () const
257 : : {
258 : : if (m_logger)
259 : : m_logger->end_log_line ();
260 : : }
261 : :
262 : : /* A shortcut for recording entry into a scope from a log_user,
263 : : handling the common case where the underlying logger is nullptr via
264 : : a no-op. */
265 : :
266 : : inline void
267 : : log_user::enter_scope (const char *scope_name)
268 : : {
269 : : if (m_logger)
270 : : m_logger->enter_scope (scope_name);
271 : : }
272 : :
273 : : /* A shortcut for recording exit from a scope from a log_user,
274 : : handling the common case where the underlying logger is nullptr via
275 : : a no-op. */
276 : :
277 : : inline void
278 : : log_user::exit_scope (const char *scope_name)
279 : : {
280 : : if (m_logger)
281 : : m_logger->exit_scope (scope_name);
282 : : }
283 : :
284 : : /* If the given logger is non-NULL, log entry/exit of this scope to
285 : : it, identifying it using __PRETTY_FUNCTION__. */
286 : :
287 : : #define LOG_SCOPE(LOGGER) \
288 : : log_scope s (LOGGER, __PRETTY_FUNCTION__)
289 : :
290 : : /* If the given logger is non-NULL, log entry/exit of this scope to
291 : : it, identifying it using __func__. */
292 : :
293 : : #define LOG_FUNC(LOGGER) \
294 : : log_scope s (LOGGER, __func__)
295 : :
296 : : #define LOG_FUNC_1(LOGGER, FMT, A0) \
297 : : log_scope s (LOGGER, __func__, FMT, A0)
298 : :
299 : : #define LOG_FUNC_2(LOGGER, FMT, A0, A1) \
300 : : log_scope s (LOGGER, __func__, FMT, A0, A1)
301 : :
302 : : #define LOG_FUNC_3(LOGGER, FMT, A0, A1, A2) \
303 : : log_scope s (LOGGER, __func__, FMT, A0, A1, A2)
304 : :
305 : : #define LOG_FUNC_4(LOGGER, FMT, A0, A1, A2, A3) \
306 : : log_scope s (LOGGER, __func__, FMT, A0, A1, A2, A3)
307 : :
308 : : } // namespace ana
309 : :
310 : : #endif /* ANALYZER_LOGGING_H */
|