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