1 /* Hierarchical log messages for the analyzer.
2 Copyright (C) 2014-2022 Free Software Foundation, Inc.
3 Contributed by David Malcolm <dmalcolm@redhat.com>.
5 This file is part of GCC.
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)
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.
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/>. */
21 /* Adapted from jit-logging.h. */
23 #ifndef ANALYZER_LOGGING_H
24 #define ANALYZER_LOGGING_H
26 #include "diagnostic-core.h"
30 /* A logger encapsulates a logging stream: a way to send
31 lines of pertinent information to a FILE *. */
36 logger (FILE *f_out
, int flags
, int verbosity
, const pretty_printer
&reference_pp
);
39 void incref (const char *reason
);
40 void decref (const char *reason
);
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);
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 void inc_indent () { m_indent_level
++; }
58 void dec_indent () { m_indent_level
--; }
60 pretty_printer
*get_printer () const { return m_pp
; }
61 FILE *get_file () const { return m_f_out
; }
64 DISABLE_COPY_AND_ASSIGN (logger
);
69 bool m_log_refcount_changes
;
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
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);
86 DISABLE_COPY_AND_ASSIGN (log_scope
);
92 /* The constructor for log_scope.
94 The normal case is that the logger is NULL, in which case this should
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. */
102 log_scope::log_scope (logger
*logger
, const char *name
) :
108 m_logger
->incref ("log_scope ctor");
109 m_logger
->enter_scope (m_name
);
114 log_scope::log_scope (logger
*logger
, const char *name
, const char *fmt
, ...):
120 m_logger
->incref ("log_scope ctor");
123 m_logger
->enter_scope (m_name
, fmt
, &ap
);
129 /* The destructor for log_scope; essentially the opposite of
133 log_scope::~log_scope ()
137 m_logger
->exit_scope (m_name
);
138 m_logger
->decref ("log_scope dtor");
142 /* A log_user is something that potentially uses a logger (which could be NULL).
144 The log_user class keeps the reference-count of a logger up-to-date. */
149 log_user (logger
*logger
);
152 logger
* get_logger () const { return m_logger
; }
153 void set_logger (logger
* logger
);
155 void log (const char *fmt
, ...) const
156 ATTRIBUTE_GCC_DIAG(2, 3);
158 void start_log_line () const;
159 void end_log_line () const;
161 void enter_scope (const char *scope_name
);
162 void exit_scope (const char *scope_name
);
164 pretty_printer
*get_logger_pp () const
166 gcc_assert (m_logger
);
167 return m_logger
->get_printer ();
170 FILE *get_logger_file () const
172 if (m_logger
== NULL
)
174 return m_logger
->get_file ();
178 DISABLE_COPY_AND_ASSIGN (log_user
);
183 /* A shortcut for calling log from a log_user, handling the common
184 case where the underlying logger is NULL via a no-op. */
187 log_user::log (const char *fmt
, ...) const
193 m_logger
->log_va (fmt
, &ap
);
198 /* A shortcut for starting a log line from a log_user,
199 handling the common case where the underlying logger is NULL via
203 log_user::start_log_line () const
206 m_logger
->start_log_line ();
209 /* A shortcut for ending a log line from a log_user,
210 handling the common case where the underlying logger is NULL via
214 log_user::end_log_line () const
217 m_logger
->end_log_line ();
220 /* A shortcut for recording entry into a scope from a log_user,
221 handling the common case where the underlying logger is NULL via
225 log_user::enter_scope (const char *scope_name
)
228 m_logger
->enter_scope (scope_name
);
231 /* A shortcut for recording exit from a scope from a log_user,
232 handling the common case where the underlying logger is NULL via
236 log_user::exit_scope (const char *scope_name
)
239 m_logger
->exit_scope (scope_name
);
242 /* If the given logger is non-NULL, log entry/exit of this scope to
243 it, identifying it using __PRETTY_FUNCTION__. */
245 #define LOG_SCOPE(LOGGER) \
246 log_scope s (LOGGER, __PRETTY_FUNCTION__)
248 /* If the given logger is non-NULL, log entry/exit of this scope to
249 it, identifying it using __func__. */
251 #define LOG_FUNC(LOGGER) \
252 log_scope s (LOGGER, __func__)
254 #define LOG_FUNC_1(LOGGER, FMT, A0) \
255 log_scope s (LOGGER, __func__, FMT, A0)
257 #define LOG_FUNC_2(LOGGER, FMT, A0, A1) \
258 log_scope s (LOGGER, __func__, FMT, A0, A1)
260 #define LOG_FUNC_3(LOGGER, FMT, A0, A1, A2) \
261 log_scope s (LOGGER, __func__, FMT, A0, A1, A2)
263 #define LOG_FUNC_4(LOGGER, FMT, A0, A1, A2, A3) \
264 log_scope s (LOGGER, __func__, FMT, A0, A1, A2, A3)
268 #endif /* ANALYZER_LOGGING_H */