6da47dae90b18f5e1b04d6ca192f799fab123a3a
[cacao.git] / src / toolbox / logging.c
1 /* toolbox/logging.c - contains logging functions
2
3    Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
4    Institut f. Computersprachen, TU Wien
5    R. Grafl, A. Krall, C. Kruegel, C. Oates, R. Obermaisser, M. Probst,
6    S. Ring, E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich,
7    J. Wenninger
8
9    This file is part of CACAO.
10
11    This program is free software; you can redistribute it and/or
12    modify it under the terms of the GNU General Public License as
13    published by the Free Software Foundation; either version 2, or (at
14    your option) any later version.
15
16    This program is distributed in the hope that it will be useful, but
17    WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19    General Public License for more details.
20
21    You should have received a copy of the GNU General Public License
22    along with this program; if not, write to the Free Software
23    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
24    02111-1307, USA.
25
26    Contact: cacao@complang.tuwien.ac.at
27
28    Authors: Reinhard Grafl
29
30    $Id: logging.c 1621 2004-11-30 13:06:55Z twisti $
31
32 */
33
34
35 #include <stdio.h>
36 #include <stdarg.h>
37 #include <stdlib.h>
38 #include <string.h>
39
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(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(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(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(char *text)
119 {
120         dolog("%s", text);
121 }
122
123
124 /******************** Function: log_plain *******************************/
125
126 void log_plain(char *text)
127 {
128         dolog_plain("%s", text);
129 }
130
131
132 /****************** Function: get_logfile *******************************/
133
134 FILE *get_logfile()
135 {
136         return (logfile) ? logfile : stdout;
137 }
138
139
140 /****************** Function: log_flush *********************************/
141
142 void log_flush()
143 {
144         fflush(get_logfile());
145 }
146
147
148 /********************* Function: log_nl *********************************/
149
150 void log_nl()
151 {
152         log_plain("\n");
153         fflush(get_logfile());
154 }
155
156
157 /********************* Function: log_cputime ****************************/
158
159 void log_cputime()
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(char *msg, classinfo *c)
184 {
185         char logtext[MAXLOGTEXT];
186
187         sprintf(logtext, msg);
188         utf_sprint_classname(logtext + strlen(logtext), c->name);
189
190         log_text(logtext);
191 }
192
193
194 /* log_message_method **********************************************************
195
196    outputs log text like this:
197
198    LOG: Compiling: java.lang.Object.clone()Ljava.lang.Object;
199
200 *******************************************************************************/
201
202 void log_message_method(char *msg, methodinfo *m)
203 {
204         char logtext[MAXLOGTEXT];
205
206         sprintf(logtext, msg);
207         utf_sprint_classname(logtext + strlen(logtext), m->class->name);
208         strcpy(logtext + strlen(logtext), ".");
209         utf_sprint(logtext + strlen(logtext), m->name);
210         utf_sprint_classname(logtext + strlen(logtext), m->descriptor);
211
212         log_text(logtext);
213 }
214
215
216 /************************** Function: error *******************************
217
218 Like dolog(), but terminates the program immediately.
219
220 **************************************************************************/
221
222 void error(char *txt, ...)
223 {
224         char logtext[MAXLOGTEXT];
225         va_list ap;
226
227         va_start(ap, txt);
228         vsprintf(logtext, txt, ap);
229         va_end(ap);
230
231         if (logfile) {
232                 fprintf(logfile, "ERROR: %s\n", logtext);
233         }
234
235         fprintf(stderr, "ERROR: %s\n", logtext);
236
237         exit(1);
238 }
239
240
241 /************************ Function: panic (txt) ****************************
242
243   Like error(), takes the text to output as an argument
244
245 ***************************************************************************/
246
247 void panic(char *txt)
248 {
249         error("%s", txt);
250 }
251
252
253 /*
254  * These are local overrides for various environment variables in Emacs.
255  * Please do not remove this and leave it at the end of the file, where
256  * Emacs will automagically detect them.
257  * ---------------------------------------------------------------------
258  * Local variables:
259  * mode: c
260  * indent-tabs-mode: t
261  * c-basic-offset: 4
262  * tab-width: 4
263  * End:
264  */