Made loging thread-safe
[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 689 2003-12-05 18:03:47Z stefan $
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 the contents of logtext to both the protocol file (if opened) and to
65 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: log_text ********************************/
90
91 void log_text(char *text)
92 {
93         dolog("%s", text);
94 }
95
96
97 /********************* Function: log_cputime ****************************/
98
99 void log_cputime()
100 {
101    long int t;
102    int sec, usec;
103    char logtext[MAXLOGTEXT];
104
105    t = getcputime();
106    sec = t / 1000000;
107    usec = t % 1000000;
108
109    sprintf(logtext, "Total CPU usage: %d seconds and %d milliseconds",
110                    sec, usec / 1000);
111    dolog(logtext);
112 }
113
114
115 /************************** Function: error *******************************
116
117 Like dolog(), but terminates the program immediately.
118
119 **************************************************************************/
120
121 void error(char *txt, ...)
122 {
123         char logtext[MAXLOGTEXT];
124         va_list ap;
125
126         va_start(ap, txt);
127         vsprintf(logtext, txt, ap);
128         va_end(ap);
129
130         if (logfile) {
131                 fprintf(logfile, "ERROR: %s\n", logtext);
132         }
133
134         fprintf(stderr, "ERROR: %s\n", logtext);
135         exit(10);
136 }
137
138
139 /************************ Function: panic (txt) ****************************
140
141   Like error(), takes the text to output as an argument
142
143 ***************************************************************************/
144
145 void panic(char *txt)
146 {
147         error("%s", txt);
148 }
149
150
151 /********************** Function: getcputime ********************************
152
153         Returns the used CPU time in microseconds
154         
155 ****************************************************************************/
156
157 long int getcputime()
158 {
159         struct rusage ru;
160         int sec, usec;
161
162         getrusage(RUSAGE_SELF, &ru);
163         sec = ru.ru_utime.tv_sec + ru.ru_stime.tv_sec;
164         usec = ru.ru_utime.tv_usec + ru.ru_stime.tv_usec;
165         return sec * 1000000 + usec;
166 }
167
168
169 /*
170  * These are local overrides for various environment variables in Emacs.
171  * Please do not remove this and leave it at the end of the file, where
172  * Emacs will automagically detect them.
173  * ---------------------------------------------------------------------
174  * Local variables:
175  * mode: c
176  * indent-tabs-mode: t
177  * c-basic-offset: 4
178  * tab-width: 4
179  * End:
180  */