File: log.c

package info (click to toggle)
radvd 1:0.7.3-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 608 kB
  • ctags: 280
  • sloc: sh: 5,816; ansic: 2,801; yacc: 415; lex: 121; makefile: 46
file content (175 lines) | stat: -rw-r--r-- 3,402 bytes parent folder | download
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
 *	$Id: log.c,v 1.4 2004/02/05 18:44:00 lutchann Exp $
 *
 *	Authors:
 *	 Lars Fenneberg		<lf@elemental.net>	 
 *
 *	This software is Copyright 1996,1997 by the above mentioned author(s), 
 *	All Rights Reserved.
 *
 *	The license which is distributed with this software in the file
 *	COPYRIGHT applies to this software. If your distribution is missing 
 *	this file, you may request it from <lutchann@litech.org>.
 *
 */

#include <config.h>
#include <includes.h>
#include <radvd.h>

static int	log_method = L_NONE;
static char *log_ident;
static char *log_file;
static FILE *log_file_fd;
static int log_facility;
static int debug_level = 0;

int
log_open(int method, char *ident, char *log, int facility)
{
	log_method = method;
	log_ident = ident;
	
	switch (log_method) {
		case L_NONE:
		case L_STDERR:
			break;
		case L_SYSLOG:
			if (facility == -1)
				log_facility = LOG_DAEMON;
			else 
				log_facility = facility;
				
			openlog(log_ident, LOG_PID, log_facility);
			break;
		case L_LOGFILE:
			if (!log)
			{
				fprintf(stderr, "%s: no logfile specified\n", log_ident);
				return (-1);				
			}
			log_file = log;
			if ((log_file_fd = fopen(log_file, "a")) == NULL)
			{
				fprintf(stderr, "%s: can't open %s: %s\n", log_ident, log_file, strerror(errno));
				return (-1);				
			}
			break;
		default:
			fprintf(stderr, "%s: unknown logging method: %d\n", log_ident, log_method);
			log_method = L_NONE;
			return (-1);				
	}
	return 0;
}

static int
vlog(int prio, char *format, va_list ap)
{
	char tstamp[64], buff[1024];
	struct tm *tm;
	time_t current;
                  
	switch (log_method) {
		case L_NONE:
			break;
		case L_SYSLOG:
	    		vsnprintf(buff, sizeof(buff), format, ap);
			syslog(prio, "%s", buff);
			break;
		case L_STDERR:
			current = time(NULL);
			tm = localtime(&current);
			(void) strftime(tstamp, sizeof(tstamp), LOG_TIME_FORMAT, tm);

			fprintf(stderr, "[%s] %s: ", tstamp, log_ident);
	    		vfprintf(stderr, format, ap);
    			fputs("\n", stderr);
    			fflush(stderr);
			break;
		case L_LOGFILE:
			current = time(NULL);
			tm = localtime(&current);
			(void) strftime(tstamp, sizeof(tstamp), LOG_TIME_FORMAT, tm);

			fprintf(log_file_fd, "[%s] %s: ", tstamp, log_ident);
    			vfprintf(log_file_fd, format, ap);
    			fputs("\n", log_file_fd);
    			fflush(log_file_fd);
			break;
		default:
			fprintf(stderr, "%s: unknown logging method: %d\n", log_ident, log_method);
			log_method = L_NONE;
			return (-1);				
	}
	return 0;
}

int
dlog(int prio, int level, char *format, ...)
{
	va_list ap;
	int res;

	if (debug_level < level)
		return 0;
	
	va_start(ap, format);
	res = vlog(prio, format, ap);
	va_end(ap);		
	
	return res;
}

int
flog(int prio, char *format, ...)
{
	va_list ap;
	int res;

	va_start(ap, format);
	res = vlog(prio, format, ap);
	va_end(ap);		
	
	return res;
}

int
log_close(void)
{
	switch (log_method) {
		case L_NONE:
		case L_STDERR:
			break;
		case L_SYSLOG:
			closelog();
			break;
		case L_LOGFILE:
			fclose(log_file_fd);
			break;
		default:
			fprintf(stderr, "%s: unknown logging method: %d\n", log_ident, log_method);
			log_method = L_NONE;
			return (-1);				
	}
	return 0;
}

int
log_reopen(void)
{
	log_close();
	return log_open(log_method, log_ident, log_file, log_facility);
}

void
set_debuglevel(int level) 
{
	debug_level = level;
}

int
get_debuglevel(void) 
{
	return debug_level;
}