Check for copysign - copysignf, finite - finitef, fmod - fmodf,
[cacao.git] / toolbox / loging.c
1 /* toolbox/loging.c - contains loging 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: loging.c 750 2003-12-13 22:17:44Z 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 "loging.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 /************************** Function: error *******************************
174
175 Like dolog(), but terminates the program immediately.
176
177 **************************************************************************/
178
179 void error(char *txt, ...)
180 {
181         char logtext[MAXLOGTEXT];
182         va_list ap;
183
184         va_start(ap, txt);
185         vsprintf(logtext, txt, ap);
186         va_end(ap);
187
188         if (logfile) {
189                 fprintf(logfile, "ERROR: %s\n", logtext);
190         }
191
192         fprintf(stderr, "ERROR: %s\n", logtext);
193         exit(10);
194 }
195
196
197 /************************ Function: panic (txt) ****************************
198
199   Like error(), takes the text to output as an argument
200
201 ***************************************************************************/
202
203 void panic(char *txt)
204 {
205         error("%s", txt);
206 }
207
208
209 /********************** Function: getcputime ********************************
210
211         Returns the used CPU time in microseconds
212         
213 ****************************************************************************/
214
215 s8 getcputime()
216 {
217         struct rusage ru;
218         int sec, usec;
219
220         getrusage(RUSAGE_SELF, &ru);
221         sec = ru.ru_utime.tv_sec + ru.ru_stime.tv_sec;
222         usec = ru.ru_utime.tv_usec + ru.ru_stime.tv_usec;
223
224         return sec * 1000000 + usec;
225 }
226
227
228 /*
229  * These are local overrides for various environment variables in Emacs.
230  * Please do not remove this and leave it at the end of the file, where
231  * Emacs will automagically detect them.
232  * ---------------------------------------------------------------------
233  * Local variables:
234  * mode: c
235  * indent-tabs-mode: t
236  * c-basic-offset: 4
237  * tab-width: 4
238  * End:
239  */