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