* Removed all Id tags.
[cacao.git] / src / toolbox / logging.c
1 /* src/toolbox/logging.c - contains logging functions
2
3    Copyright (C) 1996-2005, 2006, 2007 R. Grafl, A. Krall, C. Kruegel,
4    C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5    E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6    J. Wenninger, Institut f. Computersprachen - TU Wien
7
8    This file is part of CACAO.
9
10    This program is free software; you can redistribute it and/or
11    modify it under the terms of the GNU General Public License as
12    published by the Free Software Foundation; either version 2, or (at
13    your option) any later version.
14
15    This program is distributed in the hope that it will be useful, but
16    WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23    02110-1301, USA.
24
25 */
26
27
28 #include "config.h"
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33
34 #include "vm/types.h"
35
36 #include "mm/memory.h"
37 #include "toolbox/logging.h"
38 #include "toolbox/util.h"
39 #include "vm/global.h"
40
41 #if defined(ENABLE_STATISTICS)
42 # include "vmcore/statistics.h"
43 #endif
44
45
46 /***************************************************************************
47                         LOG FILE HANDLING 
48 ***************************************************************************/
49
50 FILE *logfile = NULL;
51
52
53 void log_init(const char *fname)
54 {
55         if (fname) {
56                 if (fname[0]) {
57                         logfile = fopen(fname, "w");
58                 }
59         }
60 }
61
62
63 /* log_start *******************************************************************
64
65    Writes the preleading LOG: text to the protocol file (if opened) or
66    to stdout.
67
68 *******************************************************************************/
69
70 /* ATTENTION: Don't include threads-common.h, because we can't
71    bootstrap this file in that case (missing java_lang_Thread.h).
72    Instead we declare threads_get_current_threadobject differently: */
73
74 /* #if defined(ENABLE_THREADS) */
75 /* # include "threads/threads-common.h" */
76 /* #endif */
77
78 extern ptrint threads_get_current_tid(void);
79
80
81 void log_start(void)
82 {
83 #if defined(ENABLE_THREADS)
84         ptrint tid;
85
86         tid = threads_get_current_tid();
87 #endif
88
89         if (logfile) {
90 #if defined(ENABLE_THREADS)
91 # if SIZEOF_VOID_P == 8
92                 fprintf(logfile, "[0x%016lx] ", tid );
93 # else
94                 fprintf(logfile, "[0x%08x] ", tid);
95 # endif
96 #endif
97         }
98         else {
99 #if defined(ENABLE_THREADS)
100 # if SIZEOF_VOID_P == 8
101                 fprintf(stdout, "LOG: [0x%016lx] ", tid);
102 # else
103                 fprintf(stdout, "LOG: [0x%08x] ", tid);
104 # endif
105 #else
106                 fputs("LOG: ", stdout);
107 #endif
108         }
109 }
110
111
112 /* log_vprint ******************************************************************
113
114    Writes logtext to the protocol file (if opened) or to stdout.
115
116 *******************************************************************************/
117
118 void log_vprint(const char *text, va_list ap)
119 {
120         if (logfile)
121                 vfprintf(logfile, text, ap);
122         else
123                 vfprintf(stdout, text, ap);
124 }
125
126
127 /* log_print *******************************************************************
128
129    Writes logtext to the protocol file (if opened) or to stdout.
130
131 *******************************************************************************/
132
133 void log_print(const char *text, ...)
134 {
135         va_list ap;
136
137         va_start(ap, text);
138         log_vprint(text, ap);
139         va_end(ap);
140 }
141
142
143 /* log_println *****************************************************************
144
145    Writes logtext to the protocol file (if opened) or to stdout with a
146    trailing newline.
147
148 *******************************************************************************/
149
150 void log_println(const char *text, ...)
151 {
152         va_list ap;
153
154         log_start();
155
156         va_start(ap, text);
157         log_vprint(text, ap);
158         va_end(ap);
159
160         log_finish();
161 }
162
163
164 /* log_finish ******************************************************************
165
166    Finishes a logtext line with trailing newline and a fflush.
167
168 *******************************************************************************/
169
170 void log_finish(void)
171 {
172         if (logfile) {
173                 fputs("\n", logfile);
174                 fflush(logfile);
175         }
176         else {
177                 fputs("\n", stdout);
178                 fflush(stdout);
179         }
180 }
181
182
183 /* log_message_utf *************************************************************
184
185    Outputs log text like this:
186
187    LOG: Creating class: java/lang/Object
188
189 *******************************************************************************/
190
191 void log_message_utf(const char *msg, utf *u)
192 {
193         char *buf;
194         s4    len;
195
196         len = strlen(msg) + utf_bytes(u) + strlen("0");
197
198         buf = MNEW(char, len);
199
200         strcpy(buf, msg);
201         utf_cat(buf, u);
202
203         log_text(buf);
204
205         MFREE(buf, char, len);
206 }
207
208
209 /* log_message_class ***********************************************************
210
211    Outputs log text like this:
212
213    LOG: Loading class: java/lang/Object
214
215 *******************************************************************************/
216
217 void log_message_class(const char *msg, classinfo *c)
218 {
219         log_message_utf(msg, c->name);
220 }
221
222
223 /* log_message_class_message_class *********************************************
224
225    Outputs log text like this:
226
227    LOG: Initialize super class java/lang/Object from java/lang/VMThread
228
229 *******************************************************************************/
230
231 void log_message_class_message_class(const char *msg1, classinfo *c1,
232                                                                          const char *msg2, classinfo *c2)
233 {
234         char *buf;
235         s4    len;
236
237         len =
238                 strlen(msg1) + utf_bytes(c1->name) +
239                 strlen(msg2) + utf_bytes(c2->name) + strlen("0");
240
241         buf = MNEW(char, len);
242
243         strcpy(buf, msg1);
244         utf_cat_classname(buf, c1->name);
245         strcat(buf, msg2);
246         utf_cat_classname(buf, c2->name);
247
248         log_text(buf);
249
250         MFREE(buf, char, len);
251 }
252
253
254 /* log_message_method **********************************************************
255
256    Outputs log text like this:
257
258    LOG: Compiling: java.lang.Object.clone()Ljava/lang/Object;
259
260 *******************************************************************************/
261
262 void log_message_method(const char *msg, methodinfo *m)
263 {
264         char *buf;
265         s4    len;
266
267         len = strlen(msg) + utf_bytes(m->class->name) + strlen(".") +
268                 utf_bytes(m->name) + utf_bytes(m->descriptor) + strlen("0");
269
270         buf = MNEW(char, len);
271
272         strcpy(buf, msg);
273         utf_cat_classname(buf, m->class->name);
274         strcat(buf, ".");
275         utf_cat(buf, m->name);
276         utf_cat(buf, m->descriptor);
277
278         log_text(buf);
279
280         MFREE(buf, char, len);
281 }
282
283
284 /*
285  * These are local overrides for various environment variables in Emacs.
286  * Please do not remove this and leave it at the end of the file, where
287  * Emacs will automagically detect them.
288  * ---------------------------------------------------------------------
289  * Local variables:
290  * mode: c
291  * indent-tabs-mode: t
292  * c-basic-offset: 4
293  * tab-width: 4
294  * End:
295  * vim:noexpandtab:sw=4:ts=4:
296  */