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 text_art { class canvas; }
29 :
30 : namespace ana {
31 :
32 : /* A logger encapsulates a logging stream: a way to send
33 : lines of pertinent information to a FILE *. */
34 :
35 : class logger
36 : {
37 : public:
38 : logger (FILE *f_out, int flags, int verbosity, const pretty_printer &reference_pp);
39 : ~logger ();
40 :
41 : void incref (const char *reason);
42 : void decref (const char *reason);
43 :
44 : void log (const char *fmt, ...)
45 : ATTRIBUTE_GCC_DIAG(2, 3);
46 : void log_va (const char *fmt, va_list *ap)
47 : ATTRIBUTE_GCC_DIAG(2, 0);
48 : void start_log_line ();
49 : void log_partial (const char *fmt, ...)
50 : ATTRIBUTE_GCC_DIAG(2, 3);
51 : void log_va_partial (const char *fmt, va_list *ap)
52 : ATTRIBUTE_GCC_DIAG(2, 0);
53 : void end_log_line ();
54 :
55 : void log_canvas (const text_art::canvas &);
56 :
57 : const text_art::theme *
58 2575 : get_text_art_theme () const
59 : {
60 2575 : return m_text_art_theme.get ();
61 : }
62 :
63 : void enter_scope (const char *scope_name);
64 : void enter_scope (const char *scope_name, const char *fmt, va_list *ap)
65 : ATTRIBUTE_GCC_DIAG(3, 0);
66 : void exit_scope (const char *scope_name);
67 2238 : void inc_indent () { m_indent_level++; }
68 2238 : void dec_indent () { m_indent_level--; }
69 :
70 4663 : pretty_printer *get_printer () const { return m_pp.get (); }
71 5 : FILE *get_file () const { return m_f_out; }
72 :
73 : private:
74 : DISABLE_COPY_AND_ASSIGN (logger);
75 :
76 : int m_refcount;
77 : FILE *m_f_out;
78 : int m_indent_level;
79 : bool m_log_refcount_changes;
80 : std::unique_ptr<pretty_printer> m_pp;
81 : std::unique_ptr<text_art::theme> m_text_art_theme;
82 : };
83 :
84 : /* The class log_scope is an RAII-style class intended to make
85 : it easy to notify a logger about entering and exiting the body of a
86 : given function. */
87 :
88 : class log_scope
89 : {
90 : public:
91 : log_scope (logger *logger, const char *name);
92 : log_scope (logger *logger, const char *name, const char *fmt, ...)
93 : ATTRIBUTE_GCC_DIAG(4, 5);
94 : ~log_scope ();
95 :
96 : private:
97 : DISABLE_COPY_AND_ASSIGN (log_scope);
98 :
99 : logger *m_logger;
100 : const char *m_name;
101 : };
102 :
103 : /* The constructor for log_scope.
104 :
105 : The normal case is that the logger is nullptr, in which case this should
106 : be largely a no-op.
107 :
108 : If we do have a logger, notify it that we're entering the given scope.
109 : We also need to hold a reference on it, to avoid a use-after-free
110 : when logging the cleanup of the owner of the logger. */
111 :
112 : inline
113 4848903 : log_scope::log_scope (logger *logger, const char *name) :
114 4848903 : m_logger (logger),
115 4848903 : m_name (name)
116 : {
117 4848903 : if (m_logger)
118 : {
119 1412 : m_logger->incref ("log_scope ctor");
120 1412 : m_logger->enter_scope (m_name);
121 : }
122 4848903 : }
123 :
124 : inline
125 1523798 : log_scope::log_scope (logger *logger, const char *name, const char *fmt, ...):
126 1523798 : m_logger (logger),
127 1523798 : m_name (name)
128 : {
129 1523798 : if (m_logger)
130 : {
131 320 : m_logger->incref ("log_scope ctor");
132 320 : va_list ap;
133 320 : va_start (ap, fmt);
134 320 : m_logger->enter_scope (m_name, fmt, &ap);
135 320 : va_end (ap);
136 : }
137 1523798 : }
138 :
139 :
140 : /* The destructor for log_scope; essentially the opposite of
141 : the constructor. */
142 :
143 : inline
144 6372701 : log_scope::~log_scope ()
145 : {
146 6372701 : if (m_logger)
147 : {
148 1732 : m_logger->exit_scope (m_name);
149 1732 : m_logger->decref ("log_scope dtor");
150 : }
151 6372701 : }
152 :
153 : class log_nesting_level
154 : {
155 : public:
156 : log_nesting_level (logger *logger, const char *fmt, ...)
157 : ATTRIBUTE_GCC_DIAG(3, 4);
158 : ~log_nesting_level ();
159 :
160 : private:
161 : logger *m_logger;
162 : };
163 :
164 : inline
165 535273 : log_nesting_level::log_nesting_level (logger *logger, const char *fmt, ...)
166 535273 : : m_logger (logger)
167 : {
168 535273 : if (logger)
169 : {
170 246 : va_list ap;
171 246 : va_start (ap, fmt);
172 :
173 246 : logger->start_log_line ();
174 246 : logger->log_va_partial (fmt, &ap);
175 246 : logger->end_log_line ();
176 :
177 246 : logger->inc_indent ();
178 :
179 246 : va_end (ap);
180 : }
181 535273 : }
182 :
183 :
184 : /* The destructor for log_nesting_level; essentially the opposite of
185 : the constructor. */
186 :
187 : inline
188 535273 : log_nesting_level::~log_nesting_level ()
189 : {
190 535273 : if (m_logger)
191 246 : m_logger->dec_indent ();
192 454883 : }
193 :
194 : /* A log_user is something that potentially uses a logger (which could be
195 : nullptr).
196 :
197 : The log_user class keeps the reference-count of a logger up-to-date. */
198 :
199 : class log_user
200 : {
201 : public:
202 : log_user (logger *logger);
203 : ~log_user ();
204 :
205 7889834 : logger * get_logger () const { return m_logger; }
206 : void set_logger (logger * logger);
207 :
208 : void log (const char *fmt, ...) const
209 : ATTRIBUTE_GCC_DIAG(2, 3);
210 :
211 : void start_log_line () const;
212 : void end_log_line () const;
213 :
214 : void enter_scope (const char *scope_name);
215 : void exit_scope (const char *scope_name);
216 :
217 : pretty_printer *get_logger_pp () const
218 : {
219 : gcc_assert (m_logger);
220 : return m_logger->get_printer ();
221 : }
222 :
223 3578 : FILE *get_logger_file () const
224 : {
225 3573 : if (m_logger == nullptr)
226 : return nullptr;
227 5 : return m_logger->get_file ();
228 : }
229 :
230 : private:
231 : DISABLE_COPY_AND_ASSIGN (log_user);
232 :
233 : logger *m_logger;
234 : };
235 :
236 : /* A shortcut for calling log from a log_user, handling the common
237 : case where the underlying logger is nullptr via a no-op. */
238 :
239 : inline void
240 52572 : log_user::log (const char *fmt, ...) const
241 : {
242 52572 : if (m_logger)
243 : {
244 255 : va_list ap;
245 255 : va_start (ap, fmt);
246 255 : m_logger->log_va (fmt, &ap);
247 255 : va_end (ap);
248 : }
249 52572 : }
250 :
251 : /* A shortcut for starting 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::start_log_line () const
257 : {
258 : if (m_logger)
259 : m_logger->start_log_line ();
260 : }
261 :
262 : /* A shortcut for ending a log line 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::end_log_line () const
268 : {
269 : if (m_logger)
270 : m_logger->end_log_line ();
271 : }
272 :
273 : /* A shortcut for recording entry into 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::enter_scope (const char *scope_name)
279 : {
280 : if (m_logger)
281 : m_logger->enter_scope (scope_name);
282 : }
283 :
284 : /* A shortcut for recording exit from a scope from a log_user,
285 : handling the common case where the underlying logger is nullptr via
286 : a no-op. */
287 :
288 : inline void
289 : log_user::exit_scope (const char *scope_name)
290 : {
291 : if (m_logger)
292 : m_logger->exit_scope (scope_name);
293 : }
294 :
295 : /* If the given logger is non-NULL, log entry/exit of this scope to
296 : it, identifying it using __PRETTY_FUNCTION__. */
297 :
298 : #define LOG_SCOPE(LOGGER) \
299 : log_scope s (LOGGER, __PRETTY_FUNCTION__)
300 :
301 : /* If the given logger is non-NULL, log entry/exit of this scope to
302 : it, identifying it using __func__. */
303 :
304 : #define LOG_FUNC(LOGGER) \
305 : log_scope s (LOGGER, __func__)
306 :
307 : #define LOG_FUNC_1(LOGGER, FMT, A0) \
308 : log_scope s (LOGGER, __func__, FMT, A0)
309 :
310 : #define LOG_FUNC_2(LOGGER, FMT, A0, A1) \
311 : log_scope s (LOGGER, __func__, FMT, A0, A1)
312 :
313 : #define LOG_FUNC_3(LOGGER, FMT, A0, A1, A2) \
314 : log_scope s (LOGGER, __func__, FMT, A0, A1, A2)
315 :
316 : #define LOG_FUNC_4(LOGGER, FMT, A0, A1, A2, A3) \
317 : log_scope s (LOGGER, __func__, FMT, A0, A1, A2, A3)
318 :
319 : } // namespace ana
320 :
321 : #endif /* ANALYZER_LOGGING_H */
|