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