do BasicToStrings automatically
[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 723 2003-12-08 19:51:32Z edwin $
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 /****************** Function: log_flush *********************************/
138
139 void log_flush()
140 {
141         fflush(get_logfile());
142 }
143
144 /********************* Function: log_nl *********************************/
145
146 void log_nl()
147 {
148         log_plain("\n");
149         fflush(get_logfile());
150 }
151
152 /********************* Function: log_cputime ****************************/
153
154 void log_cputime()
155 {
156    long int t;
157    int sec, usec;
158    char logtext[MAXLOGTEXT];
159
160    t = getcputime();
161    sec = t / 1000000;
162    usec = t % 1000000;
163
164    sprintf(logtext, "Total CPU usage: %d seconds and %d milliseconds",
165                    sec, usec / 1000);
166    dolog(logtext);
167 }
168
169
170 /************************** Function: error *******************************
171
172 Like dolog(), but terminates the program immediately.
173
174 **************************************************************************/
175
176 void error(char *txt, ...)
177 {
178         char logtext[MAXLOGTEXT];
179         va_list ap;
180
181         va_start(ap, txt);
182         vsprintf(logtext, txt, ap);
183         va_end(ap);
184
185         if (logfile) {
186                 fprintf(logfile, "ERROR: %s\n", logtext);
187         }
188
189         fprintf(stderr, "ERROR: %s\n", logtext);
190         exit(10);
191 }
192
193
194 /************************ Function: panic (txt) ****************************
195
196   Like error(), takes the text to output as an argument
197
198 ***************************************************************************/
199
200 void panic(char *txt)
201 {
202         error("%s", txt);
203 }
204
205
206 /********************** Function: getcputime ********************************
207
208         Returns the used CPU time in microseconds
209         
210 ****************************************************************************/
211
212 long int getcputime()
213 {
214         struct rusage ru;
215         int sec, usec;
216
217         getrusage(RUSAGE_SELF, &ru);
218         sec = ru.ru_utime.tv_sec + ru.ru_stime.tv_sec;
219         usec = ru.ru_utime.tv_usec + ru.ru_stime.tv_usec;
220         return sec * 1000000 + usec;
221 }
222
223
224 /*
225  * These are local overrides for various environment variables in Emacs.
226  * Please do not remove this and leave it at the end of the file, where
227  * Emacs will automagically detect them.
228  * ---------------------------------------------------------------------
229  * Local variables:
230  * mode: c
231  * indent-tabs-mode: t
232  * c-basic-offset: 4
233  * tab-width: 4
234  * End:
235  */