File: log.c

package info (click to toggle)
jesred 1.2pl1-22
  • links: PTS
  • area: main
  • in suites: buster, stretch
  • size: 580 kB
  • ctags: 286
  • sloc: ansic: 1,359; makefile: 76
file content (120 lines) | stat: -rw-r--r-- 2,848 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
 * $Id: log.c,v 1.1 1998/05/17 15:24:54 elkner Exp $
 *
 * Author:  Jens Elkner  elkner@ivs.cs.uni-magdeburg.de
 * Project: Jesred       http://ivs.cs.uni-magdeburg.de/~elkner/webtools/jesred/
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * http://www.gnu.org/copyleft/gpl.html or ./gpl.html
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 */

#include<stdlib.h>
#include<stdarg.h>
#include<stdio.h>
#include<sys/time.h>

#include "main.h"
#include "util.h"
#include "log.h"

FILE *openFile(char *);

void
openLogs(char **redirect, char **rewrite) 
{
    if ( *rewrite ) {
	fd_rewrite = openFile(*rewrite);
	safe_free(*rewrite);
    }
    else
	fd_rewrite = NULL;
    if ( *redirect ) {
	fd_redirect = openFile(*redirect);
	safe_free(*redirect);
    }
    else
	fd_redirect = NULL;
}


FILE *
openFile(char *file) 
{
    FILE *fd;
    
    if ((fd = fopen(file, "a+")) == NULL) {
	fprintf(stderr,"Warning: Can't open file %s for writing! \n"
		"Skipping all related messages!\n",file);
    }
    return fd;
}

void
mylog(log_code c, char *format, ...) {
    FILE *fd;
    
    char msg[BUFSIZE];
    va_list args;
    struct timeval current_time;
    
    va_start(args, format);
    /* Use a safe printf function*/
    if(vsnprintf(msg, BUFSIZE, format, args) > (BUFSIZE - 1)) {
	/* string is longer than the maximum buffer we specified,
	   so just return */
	return;
    }
    va_end(args);
    
    if(interactive) {
	gettimeofday(&current_time, NULL);
	fprintf(stderr, "%d.%03d %s", (int) current_time.tv_sec,
		(int) current_time.tv_usec / 1000, msg);
	fflush(stderr);
	return;
    }
    if (c == MATCH ) {
	if ( ! fd_rewrite)
	    return;
	fd = fd_rewrite;
    }
    else if ( c == ERROR || c == INFO ) {
	if (! fd_redirect)
	    return;
	fd = fd_redirect;
    }
#ifdef DEBUG
    else if ( c == DEBG ) {
	if (! fd_redirect )
	    return;
	fd = fd_redirect;
    }
#endif
    else
	return;
    gettimeofday(&current_time, NULL);
    fprintf(fd, "%d.%03d %s", (int) current_time.tv_sec,
	    (int) current_time.tv_usec / 1000, msg);
    fflush(fd); /* should we really do that all the time ??? */
}

void closeLogs(void) 
{
    if ( fd_rewrite)
	fclose(fd_rewrite);
    if ( fd_redirect)
	fclose(fd_redirect);
}