2 * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
3 * Copyright (c) 2021 Matthias Schmidt <xhr@giessen.ccc.de>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #include <sys/types.h>
36 #define MAXLOGLINE 1024
43 static const char *log_procname;
46 log_init(int n_debug, int facility)
48 extern char *__progname;
52 log_procinit(__progname);
55 openlog(__progname, LOG_PID | LOG_NDELAY, facility);
61 log_procinit(const char *procname)
64 log_procname = procname;
80 logit(int pri, const char *fmt, ...)
90 vlog(int pri, const char *fmt, va_list ap)
93 int saved_errno = errno;
96 /* best effort in out of mem situations */
97 if (asprintf(&nfmt, "%s\n", fmt) == -1) {
98 vfprintf(stderr, fmt, ap);
99 fprintf(stderr, "\n");
101 vfprintf(stderr, nfmt, ap);
106 vsyslog(pri, fmt, ap);
112 log_warn(const char *emsg, ...)
116 int saved_errno = errno;
118 /* best effort to even work in out of memory situations */
120 logit(LOG_ERR, "%s", strerror(saved_errno));
124 if (asprintf(&nfmt, "%s: %s", emsg,
125 strerror(saved_errno)) == -1) {
127 vlog(LOG_ERR, emsg, ap);
128 logit(LOG_ERR, "%s", strerror(saved_errno));
130 vlog(LOG_ERR, nfmt, ap);
140 log_warnx(const char *emsg, ...)
145 vlog(LOG_ERR, emsg, ap);
150 log_info(const char *emsg, ...)
155 vlog(LOG_INFO, emsg, ap);
160 log_debug(const char *emsg, ...)
166 vlog(LOG_DEBUG, emsg, ap);
172 vfatalc(int code, const char *emsg, va_list ap)
174 static char s[BUFSIZ];
178 (void)vsnprintf(s, sizeof(s), emsg, ap);
185 logit(LOG_CRIT, "fatal in %s: %s%s%s",
186 log_procname, s, sep, strerror(code));
188 logit(LOG_CRIT, "fatal in %s%s%s", log_procname, sep, s);
192 fatal(const char *emsg, ...)
197 vfatalc(errno, emsg, ap);
203 fatalx(const char *emsg, ...)
208 vfatalc(0, emsg, ap);
214 open_twind_logs(void)
216 if ((access_fd = open(_PATH_TWIND_ACCESS_LOG, O_WRONLY|O_APPEND|O_CREAT, 0644))
218 fatalx("Cannot open access log: %s", _PATH_TWIND_ACCESS_LOG);
220 if ((error_fd = open(_PATH_TWIND_ERROR_LOG, O_WRONLY|O_APPEND|O_CREAT, 0644))
222 fatalx("Cannot open error log: %s", _PATH_TWIND_ACCESS_LOG);
224 log_debug("Log files open");
228 close_twind_logs(void)
230 log_debug("Closing log file");
236 log_access(const struct client_connection *cc, const char *fmt, ...)
245 if (strftime(tmstr, sizeof(tmstr), "%d/%b/%Y:%H:%M:%S %Z", &tm) == 0) {
246 log_warn("strftime couldn't save time string");
250 user_log(0, "%s - - [%s] %s", cc->client_addr, tmstr, fmt);
254 log_error(const struct client_connection *cc, const char *fmt, ...)
262 if (strftime(tmstr, sizeof(tmstr), "%d/%b/%Y:%H:%M:%S %Z", &tm) == 0) {
263 log_warn("strftime couldn't save time string");
267 user_log(1, "[%s] [error] [client %s] %s", tmstr,
268 cc->client_addr, fmt);
272 user_log(int target, const char *fmt, ...)
280 else if (target == 1)
283 log_warn("Non-existent user log target");
287 vdprintf(fd, fmt, ap);