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