8a90374b76d6292bd393a85df39b0b05d0d737ae
[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    $Id: logging.c 7685 2007-04-11 08:21:06Z twisti $
26
27 */
28
29
30 #include "config.h"
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35
36 #include "vm/types.h"
37
38 #include "mm/memory.h"
39 #include "toolbox/logging.h"
40 #include "toolbox/util.h"
41 #include "vm/global.h"
42
43 #if defined(ENABLE_STATISTICS)
44 # include "vmcore/statistics.h"
45 #endif
46
47
48 /***************************************************************************
49                         LOG FILE HANDLING 
50 ***************************************************************************/
51
52 FILE *logfile = NULL;
53
54
55 void log_init(const char *fname)
56 {
57         if (fname) {
58                 if (fname[0]) {
59                         logfile = fopen(fname, "w");
60                 }
61         }
62 }
63
64
65 /* log_start *******************************************************************
66
67    Writes the preleading LOG: text to the protocol file (if opened) or
68    to stdout.
69
70 *******************************************************************************/
71
72 /* ATTENTION: Don't include threads-common.h, because we can't
73    bootstrap this file in that case (missing java_lang_Thread.h).
74    Instead we declare threads_get_current_threadobject differently: */
75
76 /* #if defined(ENABLE_THREADS) */
77 /* # include "threads/threads-common.h" */
78 /* #endif */
79
80 extern ptrint threads_get_current_tid(void);
81
82
83 void log_start(void)
84 {
85 #if defined(ENABLE_THREADS)
86         ptrint tid;
87
88         tid = threads_get_current_tid();
89 #endif
90
91         if (logfile) {
92 #if defined(ENABLE_THREADS)
93 # if SIZEOF_VOID_P == 8
94                 fprintf(logfile, "[0x%016lx] ", tid );
95 # else
96                 fprintf(logfile, "[0x%08x] ", tid);
97 # endif
98 #endif
99         }
100         else {
101 #if defined(ENABLE_THREADS)
102 # if SIZEOF_VOID_P == 8
103                 fprintf(stdout, "LOG: [0x%016lx] ", tid);
104 # else
105                 fprintf(stdout, "LOG: [0x%08x] ", tid);
106 # endif
107 #else
108                 fputs("LOG: ", stdout);
109 #endif
110         }
111 }
112
113
114 /* log_vprint ******************************************************************
115
116    Writes logtext to the protocol file (if opened) or to stdout.
117
118 *******************************************************************************/
119
120 void log_vprint(const char *text, va_list ap)
121 {
122         if (logfile)
123                 vfprintf(logfile, text, ap);
124         else
125                 vfprintf(stdout, text, ap);
126 }
127
128
129 /* log_print *******************************************************************
130
131    Writes logtext to the protocol file (if opened) or to stdout.
132
133 *******************************************************************************/
134
135 void log_print(const char *text, ...)
136 {
137         va_list ap;
138
139         va_start(ap, text);
140         log_vprint(text, ap);
141         va_end(ap);
142 }
143
144
145 /* log_println *****************************************************************
146
147    Writes logtext to the protocol file (if opened) or to stdout with a
148    trailing newline.
149
150 *******************************************************************************/
151
152 void log_println(const char *text, ...)
153 {
154         va_list ap;
155
156         log_start();
157
158         va_start(ap, text);
159         log_vprint(text, ap);
160         va_end(ap);
161
162         log_finish();
163 }
164
165
166 /* log_finish ******************************************************************
167
168    Finishes a logtext line with trailing newline and a fflush.
169
170 *******************************************************************************/
171
172 void log_finish(void)
173 {
174         if (logfile) {
175                 fputs("\n", logfile);
176                 fflush(logfile);
177         }
178         else {
179                 fputs("\n", stdout);
180                 fflush(stdout);
181         }
182 }
183
184
185 /* log_message_utf *************************************************************
186
187    Outputs log text like this:
188
189    LOG: Creating class: java/lang/Object
190
191 *******************************************************************************/
192
193 void log_message_utf(const char *msg, utf *u)
194 {
195         char *buf;
196         s4    len;
197
198         len = strlen(msg) + utf_bytes(u) + strlen("0");
199
200         buf = MNEW(char, len);
201
202         strcpy(buf, msg);
203         utf_cat(buf, u);
204
205         log_text(buf);
206
207         MFREE(buf, char, len);
208 }
209
210
211 /* log_message_class ***********************************************************
212
213    Outputs log text like this:
214
215    LOG: Loading class: java/lang/Object
216
217 *******************************************************************************/
218
219 void log_message_class(const char *msg, classinfo *c)
220 {
221         log_message_utf(msg, c->name);
222 }
223
224
225 /* log_message_class_message_class *********************************************
226
227    Outputs log text like this:
228
229    LOG: Initialize super class java/lang/Object from java/lang/VMThread
230
231 *******************************************************************************/
232
233 void log_message_class_message_class(const char *msg1, classinfo *c1,
234                                                                          const char *msg2, classinfo *c2)
235 {
236         char *buf;
237         s4    len;
238
239         len =
240                 strlen(msg1) + utf_bytes(c1->name) +
241                 strlen(msg2) + utf_bytes(c2->name) + strlen("0");
242
243         buf = MNEW(char, len);
244
245         strcpy(buf, msg1);
246         utf_cat_classname(buf, c1->name);
247         strcat(buf, msg2);
248         utf_cat_classname(buf, c2->name);
249
250         log_text(buf);
251
252         MFREE(buf, char, len);
253 }
254
255
256 /* log_message_method **********************************************************
257
258    Outputs log text like this:
259
260    LOG: Compiling: java.lang.Object.clone()Ljava/lang/Object;
261
262 *******************************************************************************/
263
264 void log_message_method(const char *msg, methodinfo *m)
265 {
266         char *buf;
267         s4    len;
268
269         len = strlen(msg) + utf_bytes(m->class->name) + strlen(".") +
270                 utf_bytes(m->name) + utf_bytes(m->descriptor) + strlen("0");
271
272         buf = MNEW(char, len);
273
274         strcpy(buf, msg);
275         utf_cat_classname(buf, m->class->name);
276         strcat(buf, ".");
277         utf_cat(buf, m->name);
278         utf_cat(buf, m->descriptor);
279
280         log_text(buf);
281
282         MFREE(buf, char, len);
283 }
284
285
286 /*
287  * These are local overrides for various environment variables in Emacs.
288  * Please do not remove this and leave it at the end of the file, where
289  * Emacs will automagically detect them.
290  * ---------------------------------------------------------------------
291  * Local variables:
292  * mode: c
293  * indent-tabs-mode: t
294  * c-basic-offset: 4
295  * tab-width: 4
296  * End:
297  * vim:noexpandtab:sw=4:ts=4:
298  */