1e0163549SMatthew Dillon /*
2e0163549SMatthew Dillon * Copyright (c) 2005 The DragonFly Project. All rights reserved.
3e0163549SMatthew Dillon *
4e0163549SMatthew Dillon * This code is derived from software contributed to The DragonFly Project
5e0163549SMatthew Dillon * by Matthew Dillon <dillon@backplane.com>
6e0163549SMatthew Dillon *
7e0163549SMatthew Dillon * Redistribution and use in source and binary forms, with or without
8e0163549SMatthew Dillon * modification, are permitted provided that the following conditions
9e0163549SMatthew Dillon * are met:
10e0163549SMatthew Dillon *
11e0163549SMatthew Dillon * 1. Redistributions of source code must retain the above copyright
12e0163549SMatthew Dillon * notice, this list of conditions and the following disclaimer.
13e0163549SMatthew Dillon * 2. Redistributions in binary form must reproduce the above copyright
14e0163549SMatthew Dillon * notice, this list of conditions and the following disclaimer in
15e0163549SMatthew Dillon * the documentation and/or other materials provided with the
16e0163549SMatthew Dillon * distribution.
17e0163549SMatthew Dillon * 3. Neither the name of The DragonFly Project nor the names of its
18e0163549SMatthew Dillon * contributors may be used to endorse or promote products derived
19e0163549SMatthew Dillon * from this software without specific, prior written permission.
20e0163549SMatthew Dillon *
21e0163549SMatthew Dillon * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22e0163549SMatthew Dillon * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23e0163549SMatthew Dillon * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24e0163549SMatthew Dillon * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25e0163549SMatthew Dillon * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26e0163549SMatthew Dillon * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27e0163549SMatthew Dillon * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28e0163549SMatthew Dillon * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29e0163549SMatthew Dillon * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30e0163549SMatthew Dillon * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31e0163549SMatthew Dillon * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32e0163549SMatthew Dillon * SUCH DAMAGE.
33e0163549SMatthew Dillon *
344709be8cSMatthew Dillon * $DragonFly: src/usr.sbin/dntpd/log.c,v 1.5 2007/06/26 00:40:35 dillon Exp $
35e0163549SMatthew Dillon */
36e0163549SMatthew Dillon
37e0163549SMatthew Dillon #include "defs.h"
38e0163549SMatthew Dillon
39b58f1e66SSascha Wildner static void vlogline(int level, int newline, const char *ctl, va_list va)
40b58f1e66SSascha Wildner __printflike(3, 0);
413d15852bSMatthew Dillon
4297c30a15SMatthew Dillon int log_stderr = 1;
4397c30a15SMatthew Dillon
44e0163549SMatthew Dillon void
logerr(const char * ctl,...)45e0163549SMatthew Dillon logerr(const char *ctl, ...)
46e0163549SMatthew Dillon {
47e0163549SMatthew Dillon int saved_errno;
48e0163549SMatthew Dillon va_list va;
49e0163549SMatthew Dillon
50e0163549SMatthew Dillon saved_errno = errno;
51e0163549SMatthew Dillon va_start(va, ctl);
523d15852bSMatthew Dillon vlogline(0, 0, ctl, va);
5397c30a15SMatthew Dillon logerrstr(": %s", strerror(saved_errno));
54e0163549SMatthew Dillon va_end(va);
55e0163549SMatthew Dillon
56e0163549SMatthew Dillon }
57e0163549SMatthew Dillon
58e0163549SMatthew Dillon void
logerrstr(const char * ctl,...)59e0163549SMatthew Dillon logerrstr(const char *ctl, ...)
60e0163549SMatthew Dillon {
61e0163549SMatthew Dillon va_list va;
62e0163549SMatthew Dillon
63e0163549SMatthew Dillon va_start(va, ctl);
643d15852bSMatthew Dillon vlogline(0, 1, ctl, va);
65e0163549SMatthew Dillon va_end(va);
66e0163549SMatthew Dillon }
67e0163549SMatthew Dillon
683d15852bSMatthew Dillon /*
693d15852bSMatthew Dillon * logdebug() does not add the '\n', allowing multiple calls to generate
703d15852bSMatthew Dillon * a debugging log line. The buffer is accumulated until a newline is
713d15852bSMatthew Dillon * detected, then syslogged.
723d15852bSMatthew Dillon */
733d15852bSMatthew Dillon void
_logdebug(int level,const char * ctl,...)743d15852bSMatthew Dillon _logdebug(int level, const char *ctl, ...)
753d15852bSMatthew Dillon {
763d15852bSMatthew Dillon va_list va;
773d15852bSMatthew Dillon
783d15852bSMatthew Dillon if (level <= debug_level) {
793d15852bSMatthew Dillon va_start(va, ctl);
803d15852bSMatthew Dillon vlogline(level, 0, ctl, va);
813d15852bSMatthew Dillon va_end(va);
823d15852bSMatthew Dillon }
833d15852bSMatthew Dillon }
843d15852bSMatthew Dillon
852408d859SMatthew Dillon void
_logdebuginfo(server_info_t info,int level,const char * ctl,...)862408d859SMatthew Dillon _logdebuginfo(server_info_t info, int level, const char *ctl, ...)
872408d859SMatthew Dillon {
882408d859SMatthew Dillon va_list va;
892408d859SMatthew Dillon char *str = NULL;
904709be8cSMatthew Dillon int ipstrpad;
912408d859SMatthew Dillon
922408d859SMatthew Dillon if (level <= debug_level) {
932408d859SMatthew Dillon va_start(va, ctl);
942408d859SMatthew Dillon
952408d859SMatthew Dillon vasprintf(&str, ctl, va);
962408d859SMatthew Dillon if (str) {
974709be8cSMatthew Dillon if (info->ipstr == NULL || strcmp(info->target, info->ipstr) == 0) {
982408d859SMatthew Dillon _logdebug(level, "%s: %s", info->target, str);
994709be8cSMatthew Dillon } else {
1004709be8cSMatthew Dillon if ((ipstrpad = 15 - strlen(info->ipstr)) < 0)
1014709be8cSMatthew Dillon ipstrpad = 0;
1024709be8cSMatthew Dillon _logdebug(level, "%s %*.*s(%s): %s", info->target,
1034709be8cSMatthew Dillon ipstrpad, ipstrpad, "",
1044709be8cSMatthew Dillon info->ipstr, str);
1054709be8cSMatthew Dillon }
106*f2c24df8SMatthew Dillon free(str);
1072408d859SMatthew Dillon }
1082408d859SMatthew Dillon va_end(va);
1092408d859SMatthew Dillon }
1102408d859SMatthew Dillon }
1112408d859SMatthew Dillon
1123d15852bSMatthew Dillon static void
vlogline(int level,int newline,const char * ctl,va_list va)1133d15852bSMatthew Dillon vlogline(int level, int newline, const char *ctl, va_list va)
1143d15852bSMatthew Dillon {
1153d15852bSMatthew Dillon static char line_build[1024];
1163d15852bSMatthew Dillon static int line_index;
1173d15852bSMatthew Dillon int priority;
118213437aaSMatthew Dillon va_list vacopy;
1193d15852bSMatthew Dillon
1203d15852bSMatthew Dillon /*
1213d15852bSMatthew Dillon * Output to stderr directly but build the log line for syslog.
1223d15852bSMatthew Dillon */
1233d15852bSMatthew Dillon if (level <= debug_level) {
124213437aaSMatthew Dillon va_copy(vacopy, va);
12597c30a15SMatthew Dillon if (log_stderr) {
1263d15852bSMatthew Dillon vfprintf(stderr, ctl, va);
12797c30a15SMatthew Dillon if (newline)
12897c30a15SMatthew Dillon fprintf(stderr, "\n");
1293d15852bSMatthew Dillon fflush(stderr);
13097c30a15SMatthew Dillon }
13197c30a15SMatthew Dillon if (debug_opt == 0) {
1323d15852bSMatthew Dillon vsnprintf(line_build + line_index, sizeof(line_build) - line_index,
133213437aaSMatthew Dillon ctl, vacopy);
1343d15852bSMatthew Dillon line_index += strlen(line_build + line_index);
1353d15852bSMatthew Dillon if (line_index && line_build[line_index-1] == '\n') {
1363d15852bSMatthew Dillon newline = 1;
1373d15852bSMatthew Dillon --line_index;
1383d15852bSMatthew Dillon }
1393d15852bSMatthew Dillon if (newline) {
1403d15852bSMatthew Dillon switch(level) {
1413d15852bSMatthew Dillon case 0:
1423d15852bSMatthew Dillon case 1:
1433d15852bSMatthew Dillon case 2:
1443d15852bSMatthew Dillon priority = LOG_NOTICE;
1453d15852bSMatthew Dillon break;
1463d15852bSMatthew Dillon case 3:
1473d15852bSMatthew Dillon priority = LOG_INFO;
1483d15852bSMatthew Dillon break;
1493d15852bSMatthew Dillon default:
1503d15852bSMatthew Dillon priority = LOG_DEBUG;
1513d15852bSMatthew Dillon break;
1523d15852bSMatthew Dillon }
1533d15852bSMatthew Dillon syslog(priority, "%*.*s", line_index, line_index, line_build);
1543d15852bSMatthew Dillon line_index = 0;
1553d15852bSMatthew Dillon }
1563d15852bSMatthew Dillon }
157213437aaSMatthew Dillon va_end(vacopy);
1583d15852bSMatthew Dillon }
1593d15852bSMatthew Dillon }
1603d15852bSMatthew Dillon
161