1 |
/* logf.c -- Log file and error reporting routines. */ |
2 |
|
3 |
/* Copyright 1992 Godfrey Paul, University of Kent of Canterbury. */ |
4 |
|
5 |
char ukcprog_logf_sccsid[] = "$Id: logf.c,v 1.1 2002/03/08 14:37:29 tdb Exp $ UKC"; |
6 |
|
7 |
#ifndef MSDOS |
8 |
#include <sys/types.h> |
9 |
#else |
10 |
#include <sys/tk_types.h> |
11 |
#endif |
12 |
|
13 |
#include <fcntl.h> |
14 |
|
15 |
#include <stdlib.h> |
16 |
#include <stdio.h> |
17 |
#include <string.h> |
18 |
#include <time.h> |
19 |
#include <errno.h> |
20 |
#ifdef __STDC__ |
21 |
#ifndef VMS |
22 |
#include <unistd.h> |
23 |
#endif /* VMS */ |
24 |
#include <stdarg.h> |
25 |
#else |
26 |
#include <varargs.h> |
27 |
#endif |
28 |
|
29 |
#ifndef MSDOS |
30 |
#ifndef __FreeBSD__ |
31 |
#ifndef linux |
32 |
extern int sys_nerr, errno; |
33 |
extern char *sys_errlist[]; |
34 |
#endif /* !linux */ |
35 |
#endif /* !__FreeBSD__ */ |
36 |
#endif /* !MSDOS */ |
37 |
|
38 |
#include "ukcprog.h" |
39 |
|
40 |
static void write_logstr PROTO((const char *str, bool err)); |
41 |
static const char *get_errno_str PROTO((void)); |
42 |
|
43 |
static int Log_fd = 2; /* just print to stderr by default */ |
44 |
static int Log_level = LG_INFO; /* by default */ |
45 |
static char *Log_prefix; /* identification tag */ |
46 |
static int Log_prefix_len = 0; |
47 |
|
48 |
/* This is to avoid using %m in logf_set_ofile. We should really make |
49 |
* this public and use it in formf(). |
50 |
*/ |
51 |
static const char * |
52 |
get_errno_str() |
53 |
{ |
54 |
static char buf[40]; |
55 |
|
56 |
if (errno > 0 && errno < sys_nerr && *sys_errlist[errno] != '\0') |
57 |
return sys_errlist[errno]; |
58 |
|
59 |
sprintf(buf, "[errno=%d (unknown value)]", errno); |
60 |
return buf; |
61 |
} |
62 |
|
63 |
int |
64 |
logf_set_ofile(filename, prefix) |
65 |
const char *filename, *prefix; |
66 |
{ |
67 |
static bool first_time = TRUE; |
68 |
int fd; |
69 |
|
70 |
if (filename != NULL) { |
71 |
if ((fd = open(filename, O_RDWR|O_APPEND|O_CREAT, 0666)) < 0) { |
72 |
errf("Can't open logfile %s (%s)", |
73 |
filename, get_errno_str()); |
74 |
return -1; |
75 |
} |
76 |
|
77 |
if (!first_time) |
78 |
(void)close(Log_fd); |
79 |
else |
80 |
first_time = FALSE; |
81 |
|
82 |
Log_fd = fd; |
83 |
} |
84 |
|
85 |
if (prefix != NULL) { |
86 |
if (Log_prefix != NULL) |
87 |
free(Log_prefix); |
88 |
Log_prefix = strsave(prefix); |
89 |
Log_prefix_len = strlen(Log_prefix); |
90 |
} |
91 |
|
92 |
return 0; |
93 |
} |
94 |
|
95 |
|
96 |
static void |
97 |
write_logstr(str, err) |
98 |
const char *str; |
99 |
bool err; |
100 |
{ |
101 |
time_t now; |
102 |
const char *errstr; |
103 |
|
104 |
if (Log_fd < 0) { |
105 |
fprintf(stderr, "%s: %s\n", errf_get_progname(), str); |
106 |
return; |
107 |
} |
108 |
|
109 |
now = time((time_t *)NULL); |
110 |
errstr = err ? " ERROR: " : " "; |
111 |
|
112 |
write(Log_fd, ctime(&now), sizeof("Fri Sep 9 15:10:00 ") - 1); |
113 |
write(Log_fd, Log_prefix, Log_prefix_len); |
114 |
write(Log_fd, errstr, strlen(errstr)); |
115 |
write(Log_fd, str, strlen(str)); |
116 |
write(Log_fd, "\n", 1); |
117 |
} |
118 |
|
119 |
|
120 |
#ifdef __STDC__ |
121 |
void |
122 |
logf(int level, const char *fmt, ...) |
123 |
{ |
124 |
|
125 |
#else /* !__STDC__ */ |
126 |
void |
127 |
logf(va_alist) |
128 |
va_dcl |
129 |
{ |
130 |
int level; |
131 |
char *fmt; |
132 |
#endif /* !__STDC__ */ |
133 |
va_list args; |
134 |
char buffer[100]; |
135 |
char *s; |
136 |
|
137 |
#ifdef __STDC__ |
138 |
va_start(args, fmt); |
139 |
#else |
140 |
va_start(args); |
141 |
level = va_arg(args, int); |
142 |
fmt = va_arg(args, char *); |
143 |
#endif |
144 |
if (level < Log_level) { |
145 |
va_end(args); |
146 |
return; |
147 |
} |
148 |
|
149 |
s = formf(buffer, sizeof(buffer), fmt, args); |
150 |
va_end(args); |
151 |
write_logstr(s, FALSE); |
152 |
|
153 |
if (s != buffer) |
154 |
free(s); /* allocated by formf() */ |
155 |
} |
156 |
|
157 |
int |
158 |
logf_set_level(new_level) |
159 |
int new_level; |
160 |
{ |
161 |
int old; |
162 |
|
163 |
old = Log_level; |
164 |
Log_level = new_level; |
165 |
logf(LG_LOG, "Log level changed from %d to %d", old, new_level); |
166 |
|
167 |
return old; |
168 |
} |
169 |
|
170 |
|
171 |
void |
172 |
logf_errf_ofunc(str) |
173 |
const char *str; |
174 |
{ |
175 |
write_logstr(str, TRUE); |
176 |
} |