PR c++/64356
[official-gcc.git] / gcc / jit / jit-logging.c
blob61b898b00bd1af379efe2beb6b9a4eca25e9241f
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)
10 any later version.
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 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
25 #include "jit-logging.h"
27 namespace gcc {
29 namespace jit {
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,
37 int, /* flags */
38 int /* verbosity */) :
39 m_refcount (0),
40 m_f_out (f_out),
41 m_indent_level (0),
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). */
50 logger::~logger ()
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. */
59 void
60 logger::incref (const char *reason)
62 m_refcount++;
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. */
71 void
72 logger::decref (const char *reason)
74 gcc_assert (m_refcount > 0);
75 --m_refcount;
76 if (m_log_refcount_changes)
77 log ("%s: reason: %s refcount now %i",
78 __PRETTY_FUNCTION__, reason, m_refcount);
79 if (0 == m_refcount)
80 delete this;
83 /* Write a formatted message to the log, by calling the log_va method. */
85 void
86 logger::log (const char *fmt, ...)
88 va_list ap;
89 va_start (ap, fmt);
90 log_va (fmt, ap);
91 va_end (ap);
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
99 buffer. */
101 void
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");
109 fflush (m_f_out);
112 /* Record the entry within a particular scope, indenting subsequent
113 log lines accordingly. */
115 void
116 logger::enter_scope (const char *scope_name)
118 log ("entering: %s", scope_name);
119 m_indent_level += 1;
122 /* Record the exit from a particular scope, restoring the indent level to
123 before the scope was entered. */
125 void
126 logger::exit_scope (const char *scope_name)
128 if (m_indent_level)
129 m_indent_level -= 1;
130 else
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)
141 if (m_logger)
142 m_logger->incref("log_user ctor");
145 /* The destructor for gcc::jit::log_user. */
147 log_user::~log_user ()
149 if (m_logger)
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). */
156 void
157 log_user::set_logger (logger *logger)
159 if (logger)
160 logger->incref ("log_user::set_logger");
161 if (m_logger)
162 m_logger->decref ("log_user::set_logger");
163 m_logger = logger;
166 } // namespace gcc::jit
168 } // namespace gcc