Forgot to add GNU headers, here they are.
[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 684 2003-12-02 16:50:17Z twisti $
31
32 */
33
34
35 #include <stdio.h>
36 #include <sys/time.h>
37 #include <sys/resource.h>
38
39 #include "global.h"
40 #include "loging.h"
41
42
43 /***************************************************************************
44                         LOG FILE HANDLING 
45 ***************************************************************************/
46
47 char logtext[MAXLOGTEXT];   /* Needs to be filled with desired text before */
48                             /* call to dolog() */
49
50
51 FILE *logfile = NULL;
52
53
54
55 void log_init(char *fname)
56 {
57         if (fname) {
58                 if (fname[0]) {
59                         logfile = fopen(fname, "w");
60                 }
61         }
62 }
63
64
65 /*********************** Function: dolog ************************************
66
67 Writes the contents of logtext to both the protocol file (if opened) and to
68 stdout.
69
70 **************************************************************************/
71
72 void dolog()
73 {
74         if (logfile) {
75                 fprintf(logfile, "%s\n",logtext);
76                 fflush(logfile);
77
78         } else {
79                 fprintf(stdout,"LOG: %s\n",logtext);
80                 fflush(stdout);
81         }
82 }
83
84
85 /********************* Function: log_text ********************************/
86
87 void log_text(char *text)
88 {
89         sprintf(logtext, "%s",text);
90         dolog();
91 }
92
93
94 /********************* Function: log_cputime ****************************/
95
96 void log_cputime()
97 {
98    long int t;
99    int sec, usec;
100
101    t = getcputime();
102    sec = t / 1000000;
103    usec = t % 1000000;
104
105    sprintf(logtext, "Total CPU usage: %d seconds and %d milliseconds",
106                    sec, usec / 1000);
107    dolog();
108 }
109
110
111 /************************** Function: error *******************************
112
113 Like dolog(), but terminates the program immediately.
114
115 **************************************************************************/
116
117 void error()
118 {
119         if (logfile) {
120                 fprintf(logfile, "ERROR: %s\n", logtext);
121         }
122
123         printf("ERROR: %s\n",logtext);
124         exit(10);
125 }
126
127
128 /************************ Function: panic (txt) ****************************
129
130   Like error(), takes the text to output as an argument
131
132 ***************************************************************************/
133
134 void panic(char *txt)
135 {
136         sprintf(logtext, "%s", txt);
137         error();
138 }
139
140
141 /********************** Function: getcputime ********************************
142
143         Returns the used CPU time in microseconds
144         
145 ****************************************************************************/
146
147 long int getcputime()
148 {
149         struct rusage ru;
150         int sec, usec;
151
152         getrusage(RUSAGE_SELF, &ru);
153         sec = ru.ru_utime.tv_sec + ru.ru_stime.tv_sec;
154         usec = ru.ru_utime.tv_usec + ru.ru_stime.tv_usec;
155         return sec * 1000000 + usec;
156 }
157
158
159 /*
160  * These are local overrides for various environment variables in Emacs.
161  * Please do not remove this and leave it at the end of the file, where
162  * Emacs will automagically detect them.
163  * ---------------------------------------------------------------------
164  * Local variables:
165  * mode: c
166  * indent-tabs-mode: t
167  * c-basic-offset: 4
168  * tab-width: 4
169  * End:
170  */