GCC Middle and Back End API Reference
logging.h
Go to the documentation of this file.
1/* Debugging code for logging what the diagnostics subsystem is doing.
2 Copyright (C) 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 under
8the terms of the GNU General Public License as published by the Free
9Software Foundation; either version 3, or (at your option) any later
10version.
11
12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for 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#ifndef GCC_DIAGNOSTICS_LOGGING_H
22#define GCC_DIAGNOSTICS_LOGGING_H
23
26#include "diagnostics/kinds.h"
27
28namespace diagnostics {
29
30namespace logging {
31
32/* A class for emitting a temporal log of what the diagnostics subsystem
33 is doing, for debugging.
34 We can't use pretty_printer here as we could potentially be debugging
35 pretty-printing itself. */
36
37class logger
38{
39public:
40 logger (output_file outfile);
41
42 /* High-level functions that emit a line of text. */
43 void log_printf (const char *fmt, ...)
44 __attribute__ ((__format__ (printf, 2, 3)));
45 void log_bool_return (const char *function_name, bool retval);
46
47 /* Lower-level functions for building up a line of text. */
48 void emit_indent () const;
49 void emit_newline () const;
50
51 FILE *get_stream () const
52 {
53 return m_outfile.get_open_file ();
54 }
55
56 int get_indent () const { return m_log_depth * 2; }
57
58 void inc_depth () { m_log_depth++; }
59 void dec_depth () { m_log_depth--; }
60
61private:
64};
65
66/* RAII class for pushing/popping depth within a logger. */
67
69{
70public:
72 : m_logger (log)
73 {
74 if (m_logger)
75 m_logger->inc_depth ();
76 }
78 {
79 if (m_logger)
80 m_logger->dec_depth ();
81 }
82
83private:
85};
86
87/* Class for debugging function call parameters. */
88
90{
91public:
92 log_function_params (logger *logger_, const char *name)
93 : m_logger (logger_),
95 {
96 if (m_logger)
97 {
98 m_logger->emit_indent ();
99 fprintf (m_logger->get_stream (), "%s (", name);
100 }
101 }
103 {
104 if (m_logger)
105 {
106 fprintf (m_logger->get_stream (), ")");
107 m_logger->emit_newline ();
108 }
109 }
110
112 log_param_string (const char *name, const char *value)
113 {
114 if (m_logger)
115 {
116 add_any_comma ();
117 fprintf (m_logger->get_stream (), "%s: \"%s\"", name, value);
118 }
119 return *this;
120 }
121
123 log_param_location_t (const char *name, location_t value)
124 {
125 if (m_logger)
126 {
127 add_any_comma ();
128 fprintf (m_logger->get_stream (),
130 name, (size_t)value);
131 }
132 return *this;
133 }
134
136 log_param_rich_location (const char *name, const rich_location *richloc)
137 {
138 if (m_logger)
139 {
140 add_any_comma ();
141 fprintf (m_logger->get_stream (),
142 "%s: %p",
143 name, const_cast<void *> ((const void *)richloc));
144 }
145 return *this;
146 }
147
150 {
151 if (m_logger)
152 {
153 add_any_comma ();
154 fprintf (m_logger->get_stream (), "%s: %i", name, value.m_idx);
155 }
156 return *this;
157 }
158
160 log_param_kind (const char *name, enum diagnostics::kind value)
161 {
162 if (m_logger)
163 {
164 add_any_comma ();
165 fprintf (m_logger->get_stream (), "%s: %s",
166 name, get_debug_string_for_kind (value));
167 }
168 return *this;
169 }
170
172 log_param_uhwi (const char *name, unsigned HOST_WIDE_INT value)
173 {
174 if (m_logger)
175 {
176 add_any_comma ();
177 fprintf (m_logger->get_stream (),
179 name, value);
180 }
181 return *this;
182 }
183
185 log_params_n_gmsgids (unsigned HOST_WIDE_INT n,
186 const char *singular_gmsgid,
187 const char *plural_gmsgid)
188 {
189 return log_param_uhwi ("n", n)
190 .log_param_string ("singular_gmsgid", singular_gmsgid)
191 .log_param_string ("plural_gmsgid", plural_gmsgid);
192 }
193
194private:
195 void
197 {
199 if (m_first_param)
200 m_first_param = false;
201 else
202 fprintf (m_logger->get_stream (), ", ");
203 }
204
207};
208
209} // namespace logging
210} // namespace diagnostics
211
212/* Various macros for logging a formatted line, and indenting
213 further log messages within a scope. */
214
215#define DIAGNOSTICS_LOG_SCOPE_PRINTF0(LOGGER, FMT) \
216 if (LOGGER) \
217 (LOGGER)->log_printf ((FMT)); \
218 diagnostics::logging::auto_inc_depth depth_sentinel (LOGGER);
219
220#define DIAGNOSTICS_LOG_SCOPE_PRINTF1(LOGGER, FMT, ARG0) \
221 if (LOGGER) \
222 (LOGGER)->log_printf ((FMT), (ARG0)); \
223 diagnostics::logging::auto_inc_depth depth_sentinel (LOGGER);
224
225#define DIAGNOSTICS_LOG_SCOPE_PRINTF2(LOGGER, FMT, ARG0, ARG1) \
226 if (LOGGER) \
227 (LOGGER)->log_printf ((FMT), (ARG0), (ARG1)); \
228 diagnostics::logging::auto_inc_depth depth_sentinel (LOGGER);
229
230#endif /* ! GCC_DIAGNOSTICS_LOGGING_H */
~auto_inc_depth()
Definition logging.h:77
auto_inc_depth(logger *log)
Definition logging.h:71
logger * m_logger
Definition logging.h:84
bool m_first_param
Definition logging.h:206
log_function_params & log_param_rich_location(const char *name, const rich_location *richloc)
Definition logging.h:136
void add_any_comma()
Definition logging.h:196
log_function_params & log_params_n_gmsgids(unsigned HOST_WIDE_INT n, const char *singular_gmsgid, const char *plural_gmsgid)
Definition logging.h:185
log_function_params & log_param_uhwi(const char *name, unsigned HOST_WIDE_INT value)
Definition logging.h:172
log_function_params & log_param_location_t(const char *name, location_t value)
Definition logging.h:123
log_function_params & log_param_string(const char *name, const char *value)
Definition logging.h:112
log_function_params & log_param_option_id(const char *name, diagnostics::option_id value)
Definition logging.h:149
logger * m_logger
Definition logging.h:205
~log_function_params()
Definition logging.h:102
log_function_params(logger *logger_, const char *name)
Definition logging.h:92
log_function_params & log_param_kind(const char *name, enum diagnostics::kind value)
Definition logging.h:160
Definition logging.h:38
int m_log_depth
Definition logging.h:63
logger(output_file outfile)
Definition logging.cc:30
output_file m_outfile
Definition logging.h:62
void emit_indent() const
Definition logging.cc:60
FILE * get_stream() const
Definition logging.h:51
void dec_depth()
Definition logging.h:59
void log_printf(const char *fmt,...) __attribute__((__format__(printf
Definition logging.cc:37
void void log_bool_return(const char *function_name, bool retval)
Definition logging.cc:50
int get_indent() const
Definition logging.h:56
void emit_newline() const
Definition logging.cc:66
void inc_depth()
Definition logging.h:58
Definition output-file.h:32
double log(double)
const char * function_name(const function *fn)
Definition function.cc:6438
#define HOST_SIZE_T_PRINT_HEX
Definition hwint.h:136
#define HOST_WIDE_INT_PRINT_DEC
Definition hwint.h:110
Definition logging.cc:28
Definition coretypes.h:167
kind
Definition kinds.h:27
const char * get_debug_string_for_kind(enum kind kind)
Definition diagnostics/context.cc:716
Definition option-id.h:32
#define gcc_assert(EXPR)
Definition system.h:814
#define true
Definition system.h:887