* log_cputime: Removed (unused).
[cacao.git] / src / toolbox / logging.c
1 /* src/toolbox/logging.c - contains logging functions
2
3    Copyright (C) 1996-2005 R. Grafl, A. Krall, C. Kruegel, C. Oates,
4    R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner,
5    C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger,
6    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., 59 Temple Place - Suite 330, Boston, MA
23    02111-1307, USA.
24
25    Contact: cacao@complang.tuwien.ac.at
26
27    Authors: Reinhard Grafl
28
29    Changes: Christian Thalinger
30
31    $Id: logging.c 3183 2005-09-15 20:07:37Z 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 "mm/memory.h"
43 #include "toolbox/logging.h"
44 #include "toolbox/util.h"
45 #include "vm/global.h"
46 #include "vm/tables.h"
47 #include "vm/statistics.h"
48
49 #if defined(USE_THREADS)
50 # if defined(NATIVE_THREADS)
51 #  include "threads/native/threads.h"
52 # endif
53 #endif
54
55
56 /***************************************************************************
57                         LOG FILE HANDLING 
58 ***************************************************************************/
59
60 FILE *logfile = NULL;
61
62
63 void log_init(const char *fname)
64 {
65         if (fname) {
66                 if (fname[0]) {
67                         logfile = fopen(fname, "w");
68                 }
69         }
70 }
71
72
73 /* dolog ***********************************************************************
74
75    Writes logtext to the protocol file (if opened) or to stdout.
76
77 *******************************************************************************/
78
79 void dolog(const char *text, ...)
80 {
81         va_list ap;
82
83         if (logfile) {
84 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
85                 fprintf(logfile, "[%p] ", (void *) THREADOBJECT);
86 #endif
87
88                 va_start(ap, text);
89                 vfprintf(logfile, text, ap);
90                 va_end(ap);
91
92                 fflush(logfile);
93
94         } else {
95 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
96                 fprintf(stdout, "LOG: [%p] ", (void *) THREADOBJECT);
97 #else
98                 fputs("LOG: ", stdout);
99 #endif
100
101                 va_start(ap, text);
102                 vfprintf(stdout, text, ap);
103                 va_end(ap);
104
105                 fprintf(stdout, "\n");
106
107                 fflush(stdout);
108         }
109 }
110
111
112 /******************** Function: dolog_plain *******************************
113
114 Writes logtext to the protocol file (if opened) or to stdout.
115
116 **************************************************************************/
117
118 void dolog_plain(const char *txt, ...)
119 {
120         char logtext[MAXLOGTEXT];
121         va_list ap;
122
123         va_start(ap, txt);
124         vsprintf(logtext, txt, ap);
125         va_end(ap);
126
127         if (logfile) {
128                 fprintf(logfile, "%s", logtext);
129                 fflush(logfile);
130
131         } else {
132                 fprintf(stdout,"%s", logtext);
133                 fflush(stdout);
134         }
135 }
136
137
138 /********************* Function: log_text ********************************/
139
140 void log_text(const char *text)
141 {
142         dolog("%s", text);
143 }
144
145
146 /******************** Function: log_plain *******************************/
147
148 void log_plain(const char *text)
149 {
150         dolog_plain("%s", text);
151 }
152
153
154 /****************** Function: get_logfile *******************************/
155
156 FILE *get_logfile(void)
157 {
158         return (logfile) ? logfile : stdout;
159 }
160
161
162 /****************** Function: log_flush *********************************/
163
164 void log_flush(void)
165 {
166         fflush(get_logfile());
167 }
168
169
170 /********************* Function: log_nl *********************************/
171
172 void log_nl(void)
173 {
174         log_plain("\n");
175         fflush(get_logfile());
176 }
177
178
179 /* log_message_utf *************************************************************
180
181    Outputs log text like this:
182
183    LOG: Creating class: java/lang/Object
184
185 *******************************************************************************/
186
187 void log_message_utf(const char *msg, utf *u)
188 {
189         char *buf;
190         s4    len;
191
192         len = strlen(msg) + utf_strlen(u) + strlen("0");
193
194         buf = MNEW(char, len);
195
196         strcpy(buf, msg);
197         utf_strcat(buf, u);
198
199         log_text(buf);
200
201         MFREE(buf, char, len);
202 }
203
204
205 /* log_message_class ***********************************************************
206
207    Outputs log text like this:
208
209    LOG: Loading class: java/lang/Object
210
211 *******************************************************************************/
212
213 void log_message_class(const char *msg, classinfo *c)
214 {
215         log_message_utf(msg, c->name);
216 }
217
218
219 /* log_message_class_message_class *********************************************
220
221    Outputs log text like this:
222
223    LOG: Initialize super class java/lang/Object from java/lang/VMThread
224
225 *******************************************************************************/
226
227 void log_message_class_message_class(const char *msg1, classinfo *c1,
228                                                                          const char *msg2, classinfo *c2)
229 {
230         char *buf;
231         s4    len;
232
233         len =
234                 strlen(msg1) + utf_strlen(c1->name) +
235                 strlen(msg2) + utf_strlen(c2->name) + strlen("0");
236
237         buf = MNEW(char, len);
238
239         strcpy(buf, msg1);
240         utf_strcat(buf, c1->name);
241         strcat(buf, msg2);
242         utf_strcat(buf, c2->name);
243
244         log_text(buf);
245
246         MFREE(buf, char, len);
247 }
248
249
250 /* log_message_method **********************************************************
251
252    Outputs log text like this:
253
254    LOG: Compiling: java.lang.Object.clone()Ljava/lang/Object;
255
256 *******************************************************************************/
257
258 void log_message_method(const char *msg, methodinfo *m)
259 {
260         char *buf;
261         s4    len;
262
263         len = strlen(msg) + utf_strlen(m->class->name) + strlen(".") +
264                 utf_strlen(m->name) + utf_strlen(m->descriptor) + strlen("0");
265
266         buf = MNEW(char, len);
267
268         strcpy(buf, msg);
269         utf_strcat_classname(buf, m->class->name);
270         strcat(buf, ".");
271         utf_strcat(buf, m->name);
272         utf_strcat(buf, m->descriptor);
273
274         log_text(buf);
275
276         MFREE(buf, char, len);
277 }
278
279
280 /* log_utf *********************************************************************
281
282    Log utf symbol.
283
284 *******************************************************************************/
285
286 void log_utf(utf *u)
287 {
288         char buf[MAXLOGTEXT];
289         utf_sprint(buf, u);
290         dolog("%s", buf);
291 }
292
293
294 /* log_plain_utf ***************************************************************
295
296    Log utf symbol (without printing "LOG: " and newline).
297
298 *******************************************************************************/
299
300 void log_plain_utf(utf *u)
301 {
302         char buf[MAXLOGTEXT];
303         utf_sprint(buf, u);
304         dolog_plain("%s", buf);
305 }
306
307
308 /*
309  * These are local overrides for various environment variables in Emacs.
310  * Please do not remove this and leave it at the end of the file, where
311  * Emacs will automagically detect them.
312  * ---------------------------------------------------------------------
313  * Local variables:
314  * mode: c
315  * indent-tabs-mode: t
316  * c-basic-offset: 4
317  * tab-width: 4
318  * End:
319  */