GCC Middle and Back End API Reference
analyzer-logging.h
Go to the documentation of this file.
1/* Hierarchical log messages for the analyzer.
2 Copyright (C) 2014-2025 Free Software Foundation, Inc.
3 Contributed by David Malcolm <dmalcolm@redhat.com>.
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify it
8under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 3, or (at your option)
10any later version.
11
12GCC is distributed in the hope that it will be useful, but
13WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along 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
28namespace ana {
29
30/* A logger encapsulates a logging stream: a way to send
31 lines of pertinent information to a FILE *. */
32
33class logger
34{
35 public:
36 logger (FILE *f_out, int flags, int verbosity, const pretty_printer &reference_pp);
38
39 void incref (const char *reason);
40 void decref (const char *reason);
41
42 void log (const char *fmt, ...)
44 void log_va (const char *fmt, va_list *ap)
47 void log_partial (const char *fmt, ...)
49 void log_va_partial (const char *fmt, va_list *ap)
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)
56 void exit_scope (const char *scope_name);
59
60 pretty_printer *get_printer () const { return m_pp.get (); }
61 FILE *get_file () const { return m_f_out; }
62
63private:
65
67 FILE *m_f_out;
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
78{
79public:
80 log_scope (logger *logger, const char *name);
81 log_scope (logger *logger, const char *name, const char *fmt, ...)
83 ~log_scope ();
84
85 private:
87
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
101inline
102log_scope::log_scope (logger *logger, const char *name) :
104 m_name (name)
105{
106 if (m_logger)
107 {
108 m_logger->incref ("log_scope ctor");
109 m_logger->enter_scope (m_name);
110 }
111}
112
113inline
114log_scope::log_scope (logger *logger, const char *name, const char *fmt, ...):
116 m_name (name)
117{
118 if (m_logger)
119 {
120 m_logger->incref ("log_scope ctor");
121 va_list ap;
122 va_start (ap, fmt);
123 m_logger->enter_scope (m_name, fmt, &ap);
124 va_end (ap);
125 }
126}
127
128
129/* The destructor for log_scope; essentially the opposite of
130 the constructor. */
131
132inline
134{
135 if (m_logger)
136 {
137 m_logger->exit_scope (m_name);
138 m_logger->decref ("log_scope dtor");
139 }
140}
141
142/* A log_user is something that potentially uses a logger (which could be
143 nullptr).
144
145 The log_user class keeps the reference-count of a logger up-to-date. */
146
148{
149 public:
152
153 logger * get_logger () const { return m_logger; }
155
156 void log (const char *fmt, ...) const
157 ATTRIBUTE_GCC_DIAG(2, 3);
158
159 void start_log_line () const;
160 void end_log_line () const;
161
162 void enter_scope (const char *scope_name);
163 void exit_scope (const char *scope_name);
164
166 {
168 return m_logger->get_printer ();
169 }
170
171 FILE *get_logger_file () const
172 {
173 if (m_logger == nullptr)
174 return nullptr;
175 return m_logger->get_file ();
176 }
177
178 private:
180
182};
183
184/* A shortcut for calling log from a log_user, handling the common
185 case where the underlying logger is nullptr via a no-op. */
186
187inline void
188log_user::log (const char *fmt, ...) const
189{
190 if (m_logger)
191 {
192 va_list ap;
193 va_start (ap, fmt);
194 m_logger->log_va (fmt, &ap);
195 va_end (ap);
196 }
197}
198
199/* A shortcut for starting a log line from a log_user,
200 handling the common case where the underlying logger is nullptr via
201 a no-op. */
202
203inline void
205{
206 if (m_logger)
207 m_logger->start_log_line ();
208}
209
210/* A shortcut for ending a log line from a log_user,
211 handling the common case where the underlying logger is nullptr via
212 a no-op. */
213
214inline void
216{
217 if (m_logger)
218 m_logger->end_log_line ();
219}
220
221/* A shortcut for recording entry into a scope from a log_user,
222 handling the common case where the underlying logger is nullptr via
223 a no-op. */
224
225inline void
226log_user::enter_scope (const char *scope_name)
227{
228 if (m_logger)
229 m_logger->enter_scope (scope_name);
230}
231
232/* A shortcut for recording exit from a scope from a log_user,
233 handling the common case where the underlying logger is nullptr via
234 a no-op. */
235
236inline void
237log_user::exit_scope (const char *scope_name)
238{
239 if (m_logger)
240 m_logger->exit_scope (scope_name);
241}
242
243/* If the given logger is non-NULL, log entry/exit of this scope to
244 it, identifying it using __PRETTY_FUNCTION__. */
245
246#define LOG_SCOPE(LOGGER) \
247 log_scope s (LOGGER, __PRETTY_FUNCTION__)
248
249/* If the given logger is non-NULL, log entry/exit of this scope to
250 it, identifying it using __func__. */
251
252#define LOG_FUNC(LOGGER) \
253 log_scope s (LOGGER, __func__)
254
255#define LOG_FUNC_1(LOGGER, FMT, A0) \
256 log_scope s (LOGGER, __func__, FMT, A0)
257
258#define LOG_FUNC_2(LOGGER, FMT, A0, A1) \
259 log_scope s (LOGGER, __func__, FMT, A0, A1)
260
261#define LOG_FUNC_3(LOGGER, FMT, A0, A1, A2) \
262 log_scope s (LOGGER, __func__, FMT, A0, A1, A2)
263
264#define LOG_FUNC_4(LOGGER, FMT, A0, A1, A2, A3) \
265 log_scope s (LOGGER, __func__, FMT, A0, A1, A2, A3)
266
267} // namespace ana
268
269#endif /* ANALYZER_LOGGING_H */
const char * m_name
Definition analyzer-logging.h:89
logger * m_logger
Definition analyzer-logging.h:88
~log_scope()
Definition analyzer-logging.h:133
DISABLE_COPY_AND_ASSIGN(log_scope)
log_scope(logger *logger, const char *name)
Definition analyzer-logging.h:102
DISABLE_COPY_AND_ASSIGN(log_user)
logger * get_logger() const
Definition analyzer-logging.h:153
logger * m_logger
Definition analyzer-logging.h:181
void log(const char *fmt,...) const ATTRIBUTE_GCC_DIAG(2
Definition analyzer-logging.h:188
void enter_scope(const char *scope_name)
Definition analyzer-logging.h:226
void void start_log_line() const
Definition analyzer-logging.h:204
log_user(logger *logger)
void set_logger(logger *logger)
void exit_scope(const char *scope_name)
Definition analyzer-logging.h:237
FILE * get_logger_file() const
Definition analyzer-logging.h:171
void end_log_line() const
Definition analyzer-logging.h:215
pretty_printer * get_logger_pp() const
Definition analyzer-logging.h:165
Definition analyzer-logging.h:34
void dec_indent()
Definition analyzer-logging.h:58
int m_indent_level
Definition analyzer-logging.h:68
pretty_printer * get_printer() const
Definition analyzer-logging.h:60
void void log_va_partial(const char *fmt, va_list *ap) ATTRIBUTE_GCC_DIAG(2
void log_partial(const char *fmt,...) ATTRIBUTE_GCC_DIAG(2
void log(const char *fmt,...) ATTRIBUTE_GCC_DIAG(2
DISABLE_COPY_AND_ASSIGN(logger)
FILE * get_file() const
Definition analyzer-logging.h:61
void enter_scope(const char *scope_name)
bool m_log_refcount_changes
Definition analyzer-logging.h:69
FILE * m_f_out
Definition analyzer-logging.h:67
int m_refcount
Definition analyzer-logging.h:66
void void void start_log_line()
logger(FILE *f_out, int flags, int verbosity, const pretty_printer &reference_pp)
void incref(const char *reason)
void void log_va(const char *fmt, va_list *ap) ATTRIBUTE_GCC_DIAG(2
void inc_indent()
Definition analyzer-logging.h:57
void decref(const char *reason)
std::unique_ptr< pretty_printer > m_pp
Definition analyzer-logging.h:70
void void void end_log_line()
void void exit_scope(const char *scope_name)
Definition pretty-print.h:241
#define ATTRIBUTE_GCC_DIAG(m, n)
Definition diagnostic-core.h:71
Definition access-diagram.h:30
static void const char va_list ap
Definition read-md.cc:205
#define gcc_assert(EXPR)
Definition system.h:814