* Updated header: Added 2006. Changed address of FSF. Changed email
[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
31    $Id: logging.c 4357 2006-01-22 23:33:38Z twisti $
32
33 */
34
35
36 #include <stdio.h>
37 #include <stdarg.h>
38 #include <stdlib.h>
39 #include <string.h>
40
41 #include "config.h"
42 #include "vm/types.h"
43
44 #include "mm/memory.h"
45 #include "toolbox/logging.h"
46 #include "toolbox/util.h"
47 #include "vm/global.h"
48 #include "vm/statistics.h"
49
50 #if defined(USE_THREADS)
51 # if defined(NATIVE_THREADS)
52 #  include "threads/native/threads.h"
53 # endif
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 /* dolog ***********************************************************************
75
76    Writes logtext to the protocol file (if opened) or to stdout.
77
78 *******************************************************************************/
79
80 void dolog(const char *text, ...)
81 {
82         va_list ap;
83
84         if (logfile) {
85 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
86                 fprintf(logfile, "[%p] ", (void *) THREADOBJECT);
87 #endif
88
89                 va_start(ap, text);
90                 vfprintf(logfile, text, ap);
91                 va_end(ap);
92
93                 fflush(logfile);
94
95         } else {
96 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
97                 fprintf(stdout, "LOG: [%p] ", (void *) THREADOBJECT);
98 #else
99                 fputs("LOG: ", stdout);
100 #endif
101
102                 va_start(ap, text);
103                 vfprintf(stdout, text, ap);
104                 va_end(ap);
105
106                 fprintf(stdout, "\n");
107
108                 fflush(stdout);
109         }
110 }
111
112
113 /******************** Function: dolog_plain *******************************
114
115 Writes logtext to the protocol file (if opened) or to stdout.
116
117 **************************************************************************/
118
119 void dolog_plain(const char *txt, ...)
120 {
121         char logtext[MAXLOGTEXT];
122         va_list ap;
123
124         va_start(ap, txt);
125         vsprintf(logtext, txt, ap);
126         va_end(ap);
127
128         if (logfile) {
129                 fprintf(logfile, "%s", logtext);
130                 fflush(logfile);
131
132         } else {
133                 fprintf(stdout,"%s", logtext);
134                 fflush(stdout);
135         }
136 }
137
138
139 /********************* Function: log_text ********************************/
140
141 void log_text(const char *text)
142 {
143         dolog("%s", text);
144 }
145
146
147 /******************** Function: log_plain *******************************/
148
149 void log_plain(const char *text)
150 {
151         dolog_plain("%s", text);
152 }
153
154
155 /****************** Function: get_logfile *******************************/
156
157 FILE *get_logfile(void)
158 {
159         return (logfile) ? logfile : stdout;
160 }
161
162
163 /****************** Function: log_flush *********************************/
164
165 void log_flush(void)
166 {
167         fflush(get_logfile());
168 }
169
170
171 /********************* Function: log_nl *********************************/
172
173 void log_nl(void)
174 {
175         log_plain("\n");
176         fflush(get_logfile());
177 }
178
179
180 /* log_message_utf *************************************************************
181
182    Outputs log text like this:
183
184    LOG: Creating class: java/lang/Object
185
186 *******************************************************************************/
187
188 void log_message_utf(const char *msg, utf *u)
189 {
190         char *buf;
191         s4    len;
192
193         len = strlen(msg) + utf_strlen(u) + strlen("0");
194
195         buf = MNEW(char, len);
196
197         strcpy(buf, msg);
198         utf_strcat(buf, u);
199
200         log_text(buf);
201
202         MFREE(buf, char, len);
203 }
204
205
206 /* log_message_class ***********************************************************
207
208    Outputs log text like this:
209
210    LOG: Loading class: java/lang/Object
211
212 *******************************************************************************/
213
214 void log_message_class(const char *msg, classinfo *c)
215 {
216         log_message_utf(msg, c->name);
217 }
218
219
220 /* log_message_class_message_class *********************************************
221
222    Outputs log text like this:
223
224    LOG: Initialize super class java/lang/Object from java/lang/VMThread
225
226 *******************************************************************************/
227
228 void log_message_class_message_class(const char *msg1, classinfo *c1,
229                                                                          const char *msg2, classinfo *c2)
230 {
231         char *buf;
232         s4    len;
233
234         len =
235                 strlen(msg1) + utf_strlen(c1->name) +
236                 strlen(msg2) + utf_strlen(c2->name) + strlen("0");
237
238         buf = MNEW(char, len);
239
240         strcpy(buf, msg1);
241         utf_strcat(buf, c1->name);
242         strcat(buf, msg2);
243         utf_strcat(buf, c2->name);
244
245         log_text(buf);
246
247         MFREE(buf, char, len);
248 }
249
250
251 /* log_message_method **********************************************************
252
253    Outputs log text like this:
254
255    LOG: Compiling: java.lang.Object.clone()Ljava/lang/Object;
256
257 *******************************************************************************/
258
259 void log_message_method(const char *msg, methodinfo *m)
260 {
261         char *buf;
262         s4    len;
263
264         len = strlen(msg) + utf_strlen(m->class->name) + strlen(".") +
265                 utf_strlen(m->name) + utf_strlen(m->descriptor) + strlen("0");
266
267         buf = MNEW(char, len);
268
269         strcpy(buf, msg);
270         utf_strcat_classname(buf, m->class->name);
271         strcat(buf, ".");
272         utf_strcat(buf, m->name);
273         utf_strcat(buf, m->descriptor);
274
275         log_text(buf);
276
277         MFREE(buf, char, len);
278 }
279
280
281 /* log_utf *********************************************************************
282
283    Log utf symbol.
284
285 *******************************************************************************/
286
287 void log_utf(utf *u)
288 {
289         char buf[MAXLOGTEXT];
290         utf_sprint(buf, u);
291         dolog("%s", buf);
292 }
293
294
295 /* log_plain_utf ***************************************************************
296
297    Log utf symbol (without printing "LOG: " and newline).
298
299 *******************************************************************************/
300
301 void log_plain_utf(utf *u)
302 {
303         char buf[MAXLOGTEXT];
304         utf_sprint(buf, u);
305         dolog_plain("%s", buf);
306 }
307
308
309 /*
310  * These are local overrides for various environment variables in Emacs.
311  * Please do not remove this and leave it at the end of the file, where
312  * Emacs will automagically detect them.
313  * ---------------------------------------------------------------------
314  * Local variables:
315  * mode: c
316  * indent-tabs-mode: t
317  * c-basic-offset: 4
318  * tab-width: 4
319  * End:
320  */