CACAO
logging.hpp
Go to the documentation of this file.
1 /* src/toolbox/logging.hpp - contains logging functions
2 
3  Copyright (C) 1996-2013
4  CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5 
6  This file is part of CACAO.
7 
8  This program is free software; you can redistribute it and/or
9  modify it under the terms of the GNU General Public License as
10  published by the Free Software Foundation; either version 2, or (at
11  your option) any later version.
12 
13  This program is distributed in the hope that it will be useful, but
14  WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with this program; if not, write to the Free Software
20  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  02110-1301, USA.
22 
23 */
24 
25 
26 #ifndef LOGGING_HPP_
27 #define LOGGING_HPP_ 1
28 
29 #include "config.h"
30 
31 #include "vm/os.hpp"
32 
33 #include <stdio.h>
34 #include <stdarg.h>
35 
36 #include "vm/utf8.hpp"
37 
38 #include "toolbox/Debug.hpp"
39 #include "toolbox/OStream.hpp"
40 
41 struct classinfo;
42 struct methodinfo;
43 
44 namespace cacao {
45 
46 /// The default destination for logging messages
47 OStream& dbg();
48 
49 #ifdef ENABLE_LOGGING
50 
51 /**
52  * Log EXPR to OStream cacao::dbg if debugging is enabled for the given
53  * subsystem.
54  */
55 #define LOG_WITH_NAME_N(DBG_NAME, VERBOSE, EXPR) \
56  do { \
57  if (DEBUG_COND_WITH_NAME_N( (DBG_NAME) , (VERBOSE) )) { \
58  cacao::OStream stream = cacao::dbg(); \
59  \
60  if (cacao::Debug::thread_enabled) { \
61  stream << "LOG: " << cacao::threadid << " "; \
62  } \
63  \
64  if (cacao::Debug::prefix_enabled) { \
65  stream << setprefix(DBG_NAME, cacao::log_color()); \
66  } \
67  \
68  { stream << EXPR ; } \
69  } \
70  } while (0)
71 
72 
73 /// Set the file dbg() writes to
74 void set_log_file(FILE *file);
75 
76 /// Set the color for line prefixes of debug messages
77 void set_log_color(Color color);
78 
79 /// Get the color for line prefixes of debug messages
80 Color log_color();
81 
82 #else // defined(ENABLE_LOGGING)
83 
84 #define LOG_WITH_NAME_N(DBG_NAME, VERBOSE, STMT) do { } while(0)
85 
86 #endif // defined(ENABLE_LOGGING)
87 
88 #define LOG_WITH_NAME(DBG_NAME, STMT) LOG_WITH_NAME_N(DBG_NAME, 0, STMT)
89 #define LOG_N(VERBOSE, STMT) LOG_WITH_NAME_N(DEBUG_NAME, VERBOSE, STMT)
90 /// Analogous to DEBUG
91 #define LOG(STMT) LOG_N(0, STMT)
92 #define LOG1(STMT) LOG_N(1, STMT)
93 #define LOG2(STMT) LOG_N(2, STMT)
94 #define LOG3(STMT) LOG_N(3, STMT)
95 
96 #define WARNING_MSG(EXPR_SHORT, EXPR_LONG) \
97  do { \
98  cacao::OStream stream = cacao::err(); \
99  \
100  { stream << cacao::BoldWhite << __FILE__ << ":" << __LINE__ << ": ";} \
101  { stream << cacao::BoldMagenta << "warning: " ; } \
102  { stream << cacao::BoldWhite << EXPR_SHORT << cacao::reset_color ; } \
103  { stream << cacao::nl << EXPR_LONG << cacao::nl; } \
104  } while (0)
105 
106 #ifndef NDEBUG
107 #define assert_msg(COND, EXPR) \
108  do { \
109  if ( ! (COND) ) { \
110  cacao::OStream stream = cacao::err(); \
111  \
112  { stream << cacao::BoldRed << "assertion failed: " ; } \
113  { stream << cacao::BoldWhite<<"`"#COND"'"<< cacao::reset_color; } \
114  { stream << cacao::nl << EXPR << cacao::nl ; } \
115  assert( COND ); \
116  } \
117  } while (0)
118 #else
119 
120 #define assert_msg(COND, EXPR) /* nothing */
121 
122 #endif
123 
124 #define ERROR_MSG(EXPR_SHORT, EXPR_LONG) \
125  do { \
126  cacao::OStream stream = cacao::err(); \
127  \
128  { stream << cacao::BoldRed << "error: " ; } \
129  { stream << cacao::BoldWhite << EXPR_SHORT << cacao::reset_color ; } \
130  { stream << cacao::nl << EXPR_LONG << cacao::nl; } \
131  } while (0)
132 
133 #define ABORT_MSG(EXPR_SHORT, EXPR_LONG) \
134  do { \
135  ERROR_MSG(EXPR_SHORT, EXPR_LONG); \
136  os::abort(); \
137  } while (0)
138 
139 #define SHOULDNOTREACH_MSG(EXPR_LONG) \
140  do { \
141  ERROR_MSG("should not reach", EXPR_LONG); \
142  os::abort(); \
143  } while (0)
144 
145 #define UNIMPLEMENTED_MSG(EXPR_LONG) \
146  do { \
147  ERROR_MSG("not implemented yet", EXPR_LONG); \
148  os::abort(); \
149  } while (0)
150 
151 } // end namespace cacao
152 
153 /* function prototypes ********************************************************/
154 
155 // TODO: remove, this is just a temporary hack
156 // that allows cycle-stats to be printed to the regular log file
157 // so we can run make check with cycle-stats enabled.
158 FILE* log_get_logfile();
159 
160 void log_init(const char *fname);
161 
162 void log_start(void);
163 
164 void log_vprint(const char *text, va_list ap);
165 void log_print(const char *text, ...);
166 void log_println(const char *text, ...);
167 
168 void log_finish(void);
169 
170 #define log_text(s) log_println("%s", (s))
171 #define dolog log_println
172 
173 /* log message functions */
174 void log_message_utf(const char *msg, Utf8String u);
175 void log_message_class(const char *msg, classinfo *c);
176 void log_message_class_message_class(const char *msg1, classinfo *c1,
177  const char *msg2, classinfo *c2);
178 void log_message_method(const char *msg, methodinfo *m);
179 
180 #endif // LOGGING_HPP_
181 
182 /*
183  * These are local overrides for various environment variables in Emacs.
184  * Please do not remove this and leave it at the end of the file, where
185  * Emacs will automagically detect them.
186  * ---------------------------------------------------------------------
187  * Local variables:
188  * mode: c++
189  * indent-tabs-mode: t
190  * c-basic-offset: 4
191  * tab-width: 4
192  * End:
193  * vim:noexpandtab:sw=4:ts=4:
194  */
void log_message_method(const char *msg, methodinfo *m)
Definition: logging.cpp:275
void log_message_class_message_class(const char *msg1, classinfo *c1, const char *msg2, classinfo *c2)
Definition: logging.cpp:251
FILE * log_get_logfile()
Definition: logging.cpp:90
void log_finish(void)
Definition: logging.cpp:117
void log_println(const char *text,...)
Definition: logging.cpp:193
void log_print(const char *text,...)
Definition: logging.cpp:149
void log_vprint(const char *text, va_list ap)
Definition: logging.cpp:135
void log_start(void)
Definition: logging.cpp:106
void log_init(const char *fname)
Definition: logging.cpp:75
void log_message_utf(const char *msg, Utf8String u)
Definition: logging.cpp:214
OStream & dbg()
The default destination for logging messages.
void log_message_class(const char *msg, classinfo *c)
Definition: logging.cpp:237