1 /* Internals of libgccjit: logging
2 Copyright (C) 2014-2023 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/>. */
24 #include "jit-common.h"
30 /* A gcc::jit::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
);
39 void incref (const char *reason
);
40 void decref (const char *reason
);
42 void log (const char *fmt
, ...)
44 void log_va (const char *fmt
, va_list ap
)
47 void enter_scope (const char *scope_name
);
48 void exit_scope (const char *scope_name
);
54 bool m_log_refcount_changes
;
57 /* The class gcc::jit::log_scope is an RAII-style class intended to make
58 it easy to notify a logger about entering and exiting the body of a
64 log_scope (logger
*logger
, const char *name
);
72 /* The constructor for gcc::jit::log_scope.
74 The normal case is that the logger is NULL, in which case this should
77 If we do have a logger, notify it that we're entering the given scope.
78 We also need to hold a reference on it, to avoid a use-after-free
79 when logging the cleanup of the owner of the logger. */
82 log_scope::log_scope (logger
*logger
, const char *name
) :
88 m_logger
->incref ("log_scope ctor");
89 m_logger
->enter_scope (m_name
);
93 /* The destructor for gcc::jit::log_scope; essentially the opposite of
97 log_scope::~log_scope ()
101 m_logger
->exit_scope (m_name
);
102 m_logger
->decref ("log_scope dtor");
106 /* A gcc::jit::log_user is something that potentially uses a
107 gcc::jit::logger (which could be NULL).
109 It is the base class for each of:
111 - class gcc::jit::recording::context
113 - class gcc::jit::playback::context
115 - class gcc::jit::tempdir
117 - class gcc::jit::result
119 The log_user class keeps the reference-count of a logger up-to-date. */
124 log_user (logger
*logger
);
127 logger
* get_logger () const { return m_logger
; }
128 void set_logger (logger
* logger
);
130 void log (const char *fmt
, ...) const
133 void enter_scope (const char *scope_name
);
134 void exit_scope (const char *scope_name
);
140 /* A shortcut for calling log from a context/result, handling the common
141 case where the underlying logger is NULL via a no-op. */
144 log_user::log (const char *fmt
, ...) const
150 m_logger
->log_va (fmt
, ap
);
155 /* A shortcut for recording entry into a scope from a context/result,
156 handling the common case where the underlying logger is NULL via
160 log_user::enter_scope (const char *scope_name
)
163 m_logger
->enter_scope (scope_name
);
166 /* A shortcut for recording exit from a scope from a context/result,
167 handling the common case where the underlying logger is NULL via
171 log_user::exit_scope (const char *scope_name
)
174 m_logger
->exit_scope (scope_name
);
177 } // namespace gcc::jit
181 /* If the given logger is non-NULL, log entry/exit of this scope to
182 it, identifying it using __PRETTY_FUNCTION__. */
184 #define JIT_LOG_SCOPE(LOGGER) \
185 gcc::jit::log_scope s (LOGGER, __PRETTY_FUNCTION__)
187 /* If the given logger is non-NULL, log entry/exit of this scope to
188 it, identifying it using __func__. */
190 #define JIT_LOG_FUNC(LOGGER) \
191 gcc::jit::log_scope s (LOGGER, __func__)
193 #endif /* JIT_LOGGING_H */