1 /*
2  * $Id: log.c,v 1.1 1998/05/17 15:24:54 elkner Exp $
3  *
4  * Author:  Jens Elkner  elkner@ivs.cs.uni-magdeburg.de
5  * Project: Jesred       http://ivs.cs.uni-magdeburg.de/~elkner/webtools/jesred/
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * http://www.gnu.org/copyleft/gpl.html or ./gpl.html
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21  *
22  */
23 
24 #include<stdlib.h>
25 #include<stdarg.h>
26 #include<stdio.h>
27 #include<sys/time.h>
28 
29 #include "main.h"
30 #include "util.h"
31 #include "log.h"
32 
33 FILE *openFile(char *);
34 
35 void
openLogs(char ** redirect,char ** rewrite)36 openLogs(char **redirect, char **rewrite)
37 {
38     if ( *rewrite ) {
39 	fd_rewrite = openFile(*rewrite);
40 	safe_free(*rewrite);
41     }
42     else
43 	fd_rewrite = NULL;
44     if ( *redirect ) {
45 	fd_redirect = openFile(*redirect);
46 	safe_free(*redirect);
47     }
48     else
49 	fd_redirect = NULL;
50 }
51 
52 
53 FILE *
openFile(char * file)54 openFile(char *file)
55 {
56     FILE *fd;
57 
58     if ((fd = fopen(file, "a+")) == NULL) {
59 	fprintf(stderr,"Warning: Can't open file %s for writing! \n"
60 		"Skipping all related messages!\n",file);
61     }
62     return fd;
63 }
64 
65 void
log(log_code c,char * format,...)66 log(log_code c, char *format, ...) {
67     FILE *fd;
68 
69     char msg[BUFSIZE];
70     va_list args;
71     struct timeval current_time;
72 
73     va_start(args, format);
74     if(vsprintf(msg, format, args) > (BUFSIZE - 1)) {
75 	/* string is longer than the maximum buffer we specified,
76 	   so just return */
77 	return;
78     }
79     va_end(args);
80 
81     if(interactive) {
82 	gettimeofday(&current_time, NULL);
83 	fprintf(stderr, "%d.%03d %s", (int) current_time.tv_sec,
84 		(int) current_time.tv_usec / 1000, msg);
85 	fflush(stderr);
86 	return;
87     }
88     if (c == MATCH ) {
89 	if ( ! fd_rewrite)
90 	    return;
91 	fd = fd_rewrite;
92     }
93     else if ( c == ERROR || c == INFO ) {
94 	if (! fd_redirect)
95 	    return;
96 	fd = fd_redirect;
97     }
98 #ifdef DEBUG
99     else if ( c == DEBG ) {
100 	if (! fd_redirect )
101 	    return;
102 	fd = fd_redirect;
103     }
104 #endif
105     else
106 	return;
107     gettimeofday(&current_time, NULL);
108     fprintf(fd, "%d.%03d %s", (int) current_time.tv_sec,
109 	    (int) current_time.tv_usec / 1000, msg);
110     fflush(fd); /* should we really do that all the time ??? */
111 }
112 
closeLogs(void)113 void closeLogs(void)
114 {
115     if ( fd_rewrite)
116 	fclose(fd_rewrite);
117     if ( fd_redirect)
118 	fclose(fd_redirect);
119 }
120