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