Moved utf log functions from native.c.
[cacao.git] / src / toolbox / logging.c
1 /* 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    $Id: logging.c 1925 2005-02-10 10:46:33Z twisti $
30
31 */
32
33
34 #include <stdio.h>
35 #include <stdarg.h>
36 #include <stdlib.h>
37 #include <string.h>
38
39 #include "mm/memory.h"
40 #include "toolbox/logging.h"
41 #include "vm/global.h"
42 #include "vm/tables.h"
43 #include "vm/statistics.h"
44
45
46 /***************************************************************************
47                         LOG FILE HANDLING 
48 ***************************************************************************/
49
50 FILE *logfile = NULL;
51
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 /*********************** Function: dolog ************************************
65
66 Writes logtext to the protocol file (if opened) or to stdout.
67
68 **************************************************************************/
69
70 void dolog(const char *txt, ...)
71 {
72         char logtext[MAXLOGTEXT];
73         va_list ap;
74
75         va_start(ap, txt);
76         vsprintf(logtext, txt, ap);
77         va_end(ap);
78
79         if (logfile) {
80                 fprintf(logfile, "%s\n",logtext);
81                 fflush(logfile);
82
83         } else {
84                 fprintf(stdout,"LOG: %s\n",logtext);
85                 fflush(stdout);
86         }
87 }
88
89
90 /******************** Function: dolog_plain *******************************
91
92 Writes logtext to the protocol file (if opened) or to stdout.
93
94 **************************************************************************/
95
96 void dolog_plain(const char *txt, ...)
97 {
98         char logtext[MAXLOGTEXT];
99         va_list ap;
100
101         va_start(ap, txt);
102         vsprintf(logtext, txt, ap);
103         va_end(ap);
104
105         if (logfile) {
106                 fprintf(logfile, "%s", logtext);
107                 fflush(logfile);
108
109         } else {
110                 fprintf(stdout,"%s", logtext);
111                 fflush(stdout);
112         }
113 }
114
115
116 /********************* Function: log_text ********************************/
117
118 void log_text(const char *text)
119 {
120         dolog("%s", text);
121 }
122
123
124 /******************** Function: log_plain *******************************/
125
126 void log_plain(const char *text)
127 {
128         dolog_plain("%s", text);
129 }
130
131
132 /****************** Function: get_logfile *******************************/
133
134 FILE *get_logfile(void)
135 {
136         return (logfile) ? logfile : stdout;
137 }
138
139
140 /****************** Function: log_flush *********************************/
141
142 void log_flush(void)
143 {
144         fflush(get_logfile());
145 }
146
147
148 /********************* Function: log_nl *********************************/
149
150 void log_nl(void)
151 {
152         log_plain("\n");
153         fflush(get_logfile());
154 }
155
156
157 /********************* Function: log_cputime ****************************/
158
159 void log_cputime(void)
160 {
161         s8 t;
162         int sec, usec;
163         char logtext[MAXLOGTEXT];
164
165         t = getcputime();
166         sec = t / 1000000;
167         usec = t % 1000000;
168
169         sprintf(logtext, "Total CPU usage: %d seconds and %d milliseconds",
170                         sec, usec / 1000);
171         log_text(logtext);
172 }
173
174
175 /* log_message_method **********************************************************
176
177    outputs log text like this:
178
179    LOG: Loading class: java.lang.Object
180
181 *******************************************************************************/
182
183 void log_message_class(const char *msg, classinfo *c)
184 {
185         char *buf;
186         s4    len;
187
188         len = strlen(msg) + utf_strlen(c->name) + 1;
189
190         buf = MNEW(char, len);
191
192         sprintf(buf, msg);
193         utf_sprint_classname(buf + strlen(buf), c->name);
194
195         log_text(buf);
196
197         MFREE(buf, char, len);
198 }
199
200
201 /* log_message_method **********************************************************
202
203    outputs log text like this:
204
205    LOG: Compiling: java.lang.Object.clone()Ljava.lang.Object;
206
207 *******************************************************************************/
208
209 void log_message_method(const char *msg, methodinfo *m)
210 {
211         char *buf;
212         s4    len;
213
214         len = strlen(msg) + utf_strlen(m->class->name) + 1 +
215                 utf_strlen(m->name) + utf_strlen(m->descriptor) + 1;
216
217         buf = MNEW(char, len);
218
219         sprintf(buf, msg);
220         utf_sprint_classname(buf + strlen(buf), m->class->name);
221         strcpy(buf + strlen(buf), ".");
222         utf_sprint(buf + strlen(buf), m->name);
223         utf_sprint_classname(buf + strlen(buf), m->descriptor);
224
225         log_text(buf);
226
227         MFREE(buf, char, len);
228 }
229
230
231 /* error ***********************************************************************
232
233    Like dolog(), but terminates the program immediately.
234
235 *******************************************************************************/
236
237 void error(const char *txt, ...)
238 {
239         char logtext[MAXLOGTEXT];
240         va_list ap;
241
242         va_start(ap, txt);
243         vsprintf(logtext, txt, ap);
244         va_end(ap);
245
246         if (logfile) {
247                 fprintf(logfile, "ERROR: %s\n", logtext);
248         }
249
250         fprintf(stderr, "ERROR: %s\n", logtext);
251
252         exit(1);
253 }
254
255
256 /* panic ***********************************************************************
257
258    Like error(), takes the text to output as an argument.
259
260 *******************************************************************************/
261
262 void panic(const char *txt)
263 {
264         error("%s", txt);
265 }
266
267
268 /* log_utf *********************************************************************
269
270    Log utf symbol.
271
272 *******************************************************************************/
273
274 void log_utf(utf *u)
275 {
276         char buf[MAXLOGTEXT];
277         utf_sprint(buf, u);
278         dolog("%s", buf);
279 }
280
281
282 /* log_plain_utf ***************************************************************
283
284    Log utf symbol (without printing "LOG: " and newline).
285
286 *******************************************************************************/
287
288 void log_plain_utf(utf *u)
289 {
290         char buf[MAXLOGTEXT];
291         utf_sprint(buf, u);
292         dolog_plain("%s", buf);
293 }
294
295
296 /*
297  * These are local overrides for various environment variables in Emacs.
298  * Please do not remove this and leave it at the end of the file, where
299  * Emacs will automagically detect them.
300  * ---------------------------------------------------------------------
301  * Local variables:
302  * mode: c
303  * indent-tabs-mode: t
304  * c-basic-offset: 4
305  * tab-width: 4
306  * End:
307  */