Home | History | Annotate | Line # | Download | only in ldpd
ldp_errors.c revision 1.2
      1 /* $NetBSD: ldp_errors.c,v 1.2 2012/03/15 02:02:24 joerg Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2010 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Mihai Chelaru <kefren (at) NetBSD.org>
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/stat.h>
     33 #include <fcntl.h>
     34 #include <stdio.h>
     35 #include <stdarg.h>
     36 #include <syslog.h>
     37 #include <unistd.h>
     38 
     39 #include "ldp.h"
     40 #include "ldp_errors.h"
     41 
     42 int	debug_f = 0, warn_f = 0, syslog_f = 0;
     43 
     44 static void do_syslog(int, const char*, va_list) __printflike(2, 0);
     45 
     46 void
     47 debugp(const char *fmt, ...)
     48 {
     49 	va_list va;
     50 
     51 	if (!debug_f)
     52 		return;
     53 
     54 	va_start(va, fmt);
     55 	if (syslog_f)
     56 		do_syslog(LOG_DEBUG, fmt, va);
     57 	else
     58 		vprintf(fmt, va);
     59 	va_end(va);
     60 }
     61 
     62 void
     63 warnp(const char *fmt, ...)
     64 {
     65 	va_list va;
     66 
     67 	if (!debug_f && !warn_f)
     68 		return;
     69 
     70 	va_start(va, fmt);
     71 	if (syslog_f)
     72 		do_syslog(LOG_WARNING, fmt, va);
     73 	else
     74 		vprintf(fmt, va);
     75 	va_end(va);
     76 }
     77 
     78 void
     79 fatalp(const char *fmt, ...)
     80 {
     81 	va_list va;
     82 
     83 	va_start(va, fmt);
     84 	if (syslog_f)
     85 		do_syslog(LOG_ERR, fmt, va);
     86 	else
     87 		vprintf(fmt, va);
     88 	va_end(va);
     89 }
     90 
     91 static void
     92 do_syslog(int prio, const char *fmt, va_list va)
     93 {
     94 	vsyslog(prio, fmt, va);
     95 }
     96 
     97 void
     98 printtime()
     99 {
    100 	time_t          t;
    101 	char            buf[26];
    102 	int             i;
    103 
    104 	time(&t);
    105 	ctime_r(&t, buf);
    106 	for (i = 0; (i < 26 && buf[i] != 0); i++)
    107 		if (buf[i] == '\n')
    108 			buf[i] = 0;
    109 	printf("%s ", buf);
    110 }
    111