Initial revision
[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 "loging.h"
20
21 /***************************************************************************
22                         LOGFILE - BEHANDLUNG 
23 ***************************************************************************/
24
25 char logtext[MAXLOGTEXT];   /* Musz mit dem gewuenschten Text vor */
26                             /* Aufruf von dolog() beschrieben werden */
27
28
29 FILE *logfile = NULL;
30
31
32
33
34 void log_init(char *fname)
35 {
36         if (fname) {
37                 if (fname[0]) {
38                         logfile = fopen(fname, "w");
39                         }
40                 }
41 }
42
43
44
45 /*********************** Funktion: dolog ************************************
46
47 Gibt den in logtext stehenden Text auf die Protokollierungsdatei
48 aus (wenn sie offen ist) und auszerdem auf stdout. 
49
50 **************************************************************************/
51
52 void dolog()
53 {
54         if (logfile) {
55                 fprintf (logfile, "%s\n",logtext);
56                 fflush (logfile);
57                 }
58         else {
59                 printf ("LOG: %s\n",logtext);
60                 fflush (stdout);
61                 }
62 }
63
64 /********************* Funktion: log_text ********************************/
65
66 void log_text (char *text)
67 {
68         sprintf (logtext, "%s",text);
69         dolog();
70 }
71
72
73 /********************* Funktion: log_cputime ****************************/
74
75 void log_cputime ()
76 {
77    long int t;
78    int sec,usec;
79
80    t = getcputime();
81    sec = t/1000000;
82    usec = t%1000000;
83
84    sprintf (logtext, "Total CPU usage: %d seconds and %d milliseconds",
85             sec,usec/1000);
86    dolog();
87 }
88
89
90
91 /************************** Funktion: error *******************************
92
93 Wie dolog(), aber das Programm wird auszerdem sofort terminiert.
94
95 **************************************************************************/
96
97 void error()
98 {
99         if (logfile) {
100                 fprintf (logfile, "ERROR: %s\n", logtext);
101                 }   
102         printf ("ERROR: %s\n",logtext);
103         exit(10);
104 }
105
106
107 /************************ Funktion: panic (txt) ****************************
108
109   Wie error(), jedoch wird der auszugebende Text als Argument uebergeben
110
111 ***************************************************************************/
112
113 void panic(char *txt)
114 {
115         sprintf (logtext, "%s", txt);
116         error();
117 }
118
119
120 /********************** Funktion: getcputime ********************************
121
122         liefert die verbrauchte CPU-Zeit im Mikrosekunden
123         
124 ****************************************************************************/
125
126 long int getcputime()
127 {
128    struct rusage ru;
129    int sec,usec;
130
131    getrusage (RUSAGE_SELF, &ru);
132    sec = ru.ru_utime.tv_sec + ru.ru_stime.tv_sec;
133    usec = ru.ru_utime.tv_usec + ru.ru_stime.tv_usec;
134    return sec*1000000 + usec;
135 }
136