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