1 /* Internals of libgccjit: logging
2 Copyright (C) 2014-2015 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/>. */
23 #include "coretypes.h"
24 #include "toplev.h" /* for print_version */
26 #include "jit-logging.h"
32 /* Implementation of class gcc::jit::logger. */
34 /* The constructor for gcc::jit::logger, used by
35 gcc_jit_context_set_logfile. */
37 logger::logger (FILE *f_out
,
39 int /* verbosity */) :
43 m_log_refcount_changes (false)
45 /* Begin the log by writing the GCC version. */
46 print_version (f_out
, "JIT:", false);
49 /* The destructor for gcc::jit::logger, invoked via
50 the decref method when the refcount hits zero.
51 Note that we do not close the underlying FILE * (m_f_out). */
55 /* This should be the last message emitted. */
56 log ("%s", __PRETTY_FUNCTION__
);
57 gcc_assert (m_refcount
== 0);
60 /* Increment the reference count of the gcc::jit::logger. */
63 logger::incref (const char *reason
)
66 if (m_log_refcount_changes
)
67 log ("%s: reason: %s refcount now %i ",
68 __PRETTY_FUNCTION__
, reason
, m_refcount
);
71 /* Decrement the reference count of the gcc::jit::logger,
72 deleting it if nothing is referring to it. */
75 logger::decref (const char *reason
)
77 gcc_assert (m_refcount
> 0);
79 if (m_log_refcount_changes
)
80 log ("%s: reason: %s refcount now %i",
81 __PRETTY_FUNCTION__
, reason
, m_refcount
);
86 /* Write a formatted message to the log, by calling the log_va method. */
89 logger::log (const char *fmt
, ...)
97 /* Write an indented line to the log file.
99 We explicitly flush after each line: if something crashes the process,
100 we want the logfile/stream to contain the most up-to-date hint about the
101 last thing that was happening, without it being hidden in an in-process
105 logger::log_va (const char *fmt
, va_list ap
)
107 fprintf (m_f_out
, "JIT: ");
108 for (int i
= 0; i
< m_indent_level
; i
++)
109 fputc (' ', m_f_out
);
110 vfprintf (m_f_out
, fmt
, ap
);
111 fprintf (m_f_out
, "\n");
115 /* Record the entry within a particular scope, indenting subsequent
116 log lines accordingly. */
119 logger::enter_scope (const char *scope_name
)
121 log ("entering: %s", scope_name
);
125 /* Record the exit from a particular scope, restoring the indent level to
126 before the scope was entered. */
129 logger::exit_scope (const char *scope_name
)
134 log ("(mismatching indentation)");
135 log ("exiting: %s", scope_name
);
138 /* Implementation of class gcc::jit::log_user. */
140 /* The constructor for gcc::jit::log_user. */
142 log_user::log_user (logger
*logger
) : m_logger (logger
)
145 m_logger
->incref("log_user ctor");
148 /* The destructor for gcc::jit::log_user. */
150 log_user::~log_user ()
153 m_logger
->decref("log_user dtor");
156 /* Set the logger for a gcc::jit::log_user, managing the reference counts
157 of the old and new logger (either of which might be NULL). */
160 log_user::set_logger (logger
*logger
)
163 logger
->incref ("log_user::set_logger");
165 m_logger
->decref ("log_user::set_logger");
169 } // namespace gcc::jit