new array classes cleanup + made tests (except fp*) work
[cacao.git] / toolbox / loging.c
1 /************************* toolbox/loging.c ************************************
2
3         Copyright (c) 1997 A. Krall, R. Grafl, M. Gschwind, M. Probst
4
5         See file COPYRIGHT for information on usage and disclaimer of warranties
6
7         Not documented, see loging.h.
8
9         Authors: Reinhard Grafl      EMAIL: cacao@complang.tuwien.ac.at
10
11         Last Change: 1996/10/03
12
13 *******************************************************************************/
14
15 #include <stdio.h>
16 #include <sys/time.h>
17 #include <sys/resource.h>
18
19 #include "global.h"
20 #include "loging.h"
21
22 /***************************************************************************
23                         LOG FILE HANDLING 
24 ***************************************************************************/
25
26 char logtext[MAXLOGTEXT];   /* Needs to be filled with desired text before */
27                             /* call to dolog() */
28
29
30 FILE *logfile = NULL;
31
32
33
34
35 void log_init(char *fname)
36 {
37         if (fname) {
38                 if (fname[0]) {
39                         logfile = fopen(fname, "w");
40                         }
41                 }
42 }
43
44
45
46 /*********************** Function: dolog ************************************
47
48 Writes the contents of logtext to both the protocol file (if opened) and to
49 stdout.
50
51 **************************************************************************/
52
53 void dolog()
54 {
55         if (logfile) {
56                 fprintf (logfile, "%s\n",logtext);
57                 fflush (logfile);
58                 }
59         else {
60                 fprintf (stderr,"LOG: %s\n",logtext);
61                 fflush (stderr);
62                 }
63 }
64
65 /********************* Function: log_text ********************************/
66
67 void log_text (char *text)
68 {
69         sprintf (logtext, "%s",text);
70         dolog();
71 }
72
73
74 /********************* Function: log_cputime ****************************/
75
76 void log_cputime ()
77 {
78    long int t;
79    int sec,usec;
80
81    t = getcputime();
82    sec = t/1000000;
83    usec = t%1000000;
84
85    sprintf (logtext, "Total CPU usage: %d seconds and %d milliseconds",
86             sec,usec/1000);
87    dolog();
88 }
89
90
91
92 /************************** Function: error *******************************
93
94 Like dolog(), but terminates the program immediately.
95
96 **************************************************************************/
97
98 void error()
99 {
100         if (logfile) {
101                 fprintf (logfile, "ERROR: %s\n", logtext);
102                 }   
103         printf ("ERROR: %s\n",logtext);
104         exit(10);
105 }
106
107
108 /************************ Function: panic (txt) ****************************
109
110   Like error(), takes the text to output as an argument
111
112 ***************************************************************************/
113
114 void panic(char *txt)
115 {
116         sprintf (logtext, "%s", txt);
117         error();
118 }
119
120
121 /********************** Function: getcputime ********************************
122
123         Returns the used CPU time in microseconds
124         
125 ****************************************************************************/
126
127 long int getcputime()
128 {
129    struct rusage ru;
130    int sec,usec;
131
132    getrusage (RUSAGE_SELF, &ru);
133    sec = ru.ru_utime.tv_sec + ru.ru_stime.tv_sec;
134    usec = ru.ru_utime.tv_usec + ru.ru_stime.tv_usec;
135    return sec*1000000 + usec;
136 }
137