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