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"
25 #include "jit-logging.h"
31 /* Implementation of class gcc::jit::logger. */
33 /* The constructor for gcc::jit::logger, used by
34 gcc_jit_context_set_logfile. */
36 logger::logger (FILE *f_out
,
38 int /* verbosity */) :
42 m_log_refcount_changes (false)
46 /* The destructor for gcc::jit::logger, invoked via
47 the decref method when the refcount hits zero.
48 Note that we do not close the underlying FILE * (m_f_out). */
52 /* This should be the last message emitted. */
53 log ("%s", __PRETTY_FUNCTION__
);
54 gcc_assert (m_refcount
== 0);
57 /* Increment the reference count of the gcc::jit::logger. */
60 logger::incref (const char *reason
)
63 if (m_log_refcount_changes
)
64 log ("%s: reason: %s refcount now %i ",
65 __PRETTY_FUNCTION__
, reason
, m_refcount
);
68 /* Decrement the reference count of the gcc::jit::logger,
69 deleting it if nothing is referring to it. */
72 logger::decref (const char *reason
)
74 gcc_assert (m_refcount
> 0);
76 if (m_log_refcount_changes
)
77 log ("%s: reason: %s refcount now %i",
78 __PRETTY_FUNCTION__
, reason
, m_refcount
);
83 /* Write a formatted message to the log, by calling the log_va method. */
86 logger::log (const char *fmt
, ...)
94 /* Write an indented line to the log file.
96 We explicitly flush after each line: if something crashes the process,
97 we want the logfile/stream to contain the most up-to-date hint about the
98 last thing that was happening, without it being hidden in an in-process
102 logger::log_va (const char *fmt
, va_list ap
)
104 fprintf (m_f_out
, "JIT: ");
105 for (int i
= 0; i
< m_indent_level
; i
++)
106 fputc (' ', m_f_out
);
107 vfprintf (m_f_out
, fmt
, ap
);
108 fprintf (m_f_out
, "\n");
112 /* Record the entry within a particular scope, indenting subsequent
113 log lines accordingly. */
116 logger::enter_scope (const char *scope_name
)
118 log ("entering: %s", scope_name
);
122 /* Record the exit from a particular scope, restoring the indent level to
123 before the scope was entered. */
126 logger::exit_scope (const char *scope_name
)
131 log ("(mismatching indentation)");
132 log ("exiting: %s", scope_name
);
135 /* Implementation of class gcc::jit::log_user. */
137 /* The constructor for gcc::jit::log_user. */
139 log_user::log_user (logger
*logger
) : m_logger (logger
)
142 m_logger
->incref("log_user ctor");
145 /* The destructor for gcc::jit::log_user. */
147 log_user::~log_user ()
150 m_logger
->decref("log_user dtor");
153 /* Set the logger for a gcc::jit::log_user, managing the reference counts
154 of the old and new logger (either of which might be NULL). */
157 log_user::set_logger (logger
*logger
)
160 logger
->incref ("log_user::set_logger");
162 m_logger
->decref ("log_user::set_logger");
166 } // namespace gcc::jit