xref: /freebsd/usr.sbin/ypldap/log.c (revision 2a63c3be158216222d89a073dcbd6a72ee4aab5a)
19e7c127fSCraig Rodrigues /*	$OpenBSD: log.c,v 1.1 2008/06/26 15:10:01 pyr Exp $	*/
29e7c127fSCraig Rodrigues 
39e7c127fSCraig Rodrigues /*
49e7c127fSCraig Rodrigues  * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
59e7c127fSCraig Rodrigues  *
69e7c127fSCraig Rodrigues  * Permission to use, copy, modify, and distribute this software for any
79e7c127fSCraig Rodrigues  * purpose with or without fee is hereby granted, provided that the above
89e7c127fSCraig Rodrigues  * copyright notice and this permission notice appear in all copies.
99e7c127fSCraig Rodrigues  *
109e7c127fSCraig Rodrigues  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
119e7c127fSCraig Rodrigues  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
129e7c127fSCraig Rodrigues  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
139e7c127fSCraig Rodrigues  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
149e7c127fSCraig Rodrigues  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
159e7c127fSCraig Rodrigues  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
169e7c127fSCraig Rodrigues  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
179e7c127fSCraig Rodrigues  */
189e7c127fSCraig Rodrigues 
199e7c127fSCraig Rodrigues #include <sys/types.h>
209e7c127fSCraig Rodrigues 
219e7c127fSCraig Rodrigues #include <errno.h>
229e7c127fSCraig Rodrigues #include <stdarg.h>
239e7c127fSCraig Rodrigues #include <stdio.h>
249e7c127fSCraig Rodrigues #include <stdlib.h>
259e7c127fSCraig Rodrigues #include <string.h>
269e7c127fSCraig Rodrigues #include <syslog.h>
27*21f67832SCraig Rodrigues #include <time.h>
289e7c127fSCraig Rodrigues 
299e7c127fSCraig Rodrigues void		 log_init(int);
309e7c127fSCraig Rodrigues void		 log_warn(const char *, ...);
319e7c127fSCraig Rodrigues void		 log_warnx(const char *, ...);
329e7c127fSCraig Rodrigues void		 log_info(const char *, ...);
339e7c127fSCraig Rodrigues void		 log_debug(const char *, ...);
34350ed599SCraig Rodrigues __dead2 void	 fatal(const char *);
35350ed599SCraig Rodrigues __dead2 void	 fatalx(const char *);
369e7c127fSCraig Rodrigues 
379e7c127fSCraig Rodrigues int	 debug;
389e7c127fSCraig Rodrigues 
399e7c127fSCraig Rodrigues void	 vlog(int, const char *, va_list);
409e7c127fSCraig Rodrigues void	 logit(int, const char *, ...);
419e7c127fSCraig Rodrigues 
429e7c127fSCraig Rodrigues void
log_init(int n_debug)439e7c127fSCraig Rodrigues log_init(int n_debug)
449e7c127fSCraig Rodrigues {
459e7c127fSCraig Rodrigues 	extern char	*__progname;
469e7c127fSCraig Rodrigues 
479e7c127fSCraig Rodrigues 	debug = n_debug;
489e7c127fSCraig Rodrigues 
499e7c127fSCraig Rodrigues 	if (!debug)
509e7c127fSCraig Rodrigues 		openlog(__progname, LOG_PID | LOG_NDELAY, LOG_DAEMON);
519e7c127fSCraig Rodrigues 
529e7c127fSCraig Rodrigues 	tzset();
539e7c127fSCraig Rodrigues }
549e7c127fSCraig Rodrigues 
559e7c127fSCraig Rodrigues void
logit(int pri,const char * fmt,...)569e7c127fSCraig Rodrigues logit(int pri, const char *fmt, ...)
579e7c127fSCraig Rodrigues {
589e7c127fSCraig Rodrigues 	va_list	ap;
599e7c127fSCraig Rodrigues 
609e7c127fSCraig Rodrigues 	va_start(ap, fmt);
619e7c127fSCraig Rodrigues 	vlog(pri, fmt, ap);
629e7c127fSCraig Rodrigues 	va_end(ap);
639e7c127fSCraig Rodrigues }
649e7c127fSCraig Rodrigues 
659e7c127fSCraig Rodrigues void
vlog(int pri,const char * fmt,va_list ap)669e7c127fSCraig Rodrigues vlog(int pri, const char *fmt, va_list ap)
679e7c127fSCraig Rodrigues {
689e7c127fSCraig Rodrigues 	char	*nfmt;
699e7c127fSCraig Rodrigues 
709e7c127fSCraig Rodrigues 	if (debug) {
719e7c127fSCraig Rodrigues 		/* best effort in out of mem situations */
729e7c127fSCraig Rodrigues 		if (asprintf(&nfmt, "%s\n", fmt) == -1) {
739e7c127fSCraig Rodrigues 			vfprintf(stderr, fmt, ap);
749e7c127fSCraig Rodrigues 			fprintf(stderr, "\n");
759e7c127fSCraig Rodrigues 		} else {
769e7c127fSCraig Rodrigues 			vfprintf(stderr, nfmt, ap);
779e7c127fSCraig Rodrigues 			free(nfmt);
789e7c127fSCraig Rodrigues 		}
799e7c127fSCraig Rodrigues 		fflush(stderr);
809e7c127fSCraig Rodrigues 	} else
819e7c127fSCraig Rodrigues 		vsyslog(pri, fmt, ap);
829e7c127fSCraig Rodrigues }
839e7c127fSCraig Rodrigues 
849e7c127fSCraig Rodrigues 
859e7c127fSCraig Rodrigues void
log_warn(const char * emsg,...)869e7c127fSCraig Rodrigues log_warn(const char *emsg, ...)
879e7c127fSCraig Rodrigues {
889e7c127fSCraig Rodrigues 	char	*nfmt;
899e7c127fSCraig Rodrigues 	va_list	 ap;
909e7c127fSCraig Rodrigues 
919e7c127fSCraig Rodrigues 	/* best effort to even work in out of memory situations */
929e7c127fSCraig Rodrigues 	if (emsg == NULL)
939e7c127fSCraig Rodrigues 		logit(LOG_CRIT, "%s", strerror(errno));
949e7c127fSCraig Rodrigues 	else {
959e7c127fSCraig Rodrigues 		va_start(ap, emsg);
969e7c127fSCraig Rodrigues 
979e7c127fSCraig Rodrigues 		if (asprintf(&nfmt, "%s: %s", emsg, strerror(errno)) == -1) {
989e7c127fSCraig Rodrigues 			/* we tried it... */
999e7c127fSCraig Rodrigues 			vlog(LOG_CRIT, emsg, ap);
1009e7c127fSCraig Rodrigues 			logit(LOG_CRIT, "%s", strerror(errno));
1019e7c127fSCraig Rodrigues 		} else {
1029e7c127fSCraig Rodrigues 			vlog(LOG_CRIT, nfmt, ap);
1039e7c127fSCraig Rodrigues 			free(nfmt);
1049e7c127fSCraig Rodrigues 		}
1059e7c127fSCraig Rodrigues 		va_end(ap);
1069e7c127fSCraig Rodrigues 	}
1079e7c127fSCraig Rodrigues }
1089e7c127fSCraig Rodrigues 
1099e7c127fSCraig Rodrigues void
log_warnx(const char * emsg,...)1109e7c127fSCraig Rodrigues log_warnx(const char *emsg, ...)
1119e7c127fSCraig Rodrigues {
1129e7c127fSCraig Rodrigues 	va_list	 ap;
1139e7c127fSCraig Rodrigues 
1149e7c127fSCraig Rodrigues 	va_start(ap, emsg);
1159e7c127fSCraig Rodrigues 	vlog(LOG_CRIT, emsg, ap);
1169e7c127fSCraig Rodrigues 	va_end(ap);
1179e7c127fSCraig Rodrigues }
1189e7c127fSCraig Rodrigues 
1199e7c127fSCraig Rodrigues void
log_info(const char * emsg,...)1209e7c127fSCraig Rodrigues log_info(const char *emsg, ...)
1219e7c127fSCraig Rodrigues {
1229e7c127fSCraig Rodrigues 	va_list	 ap;
1239e7c127fSCraig Rodrigues 
1249e7c127fSCraig Rodrigues 	va_start(ap, emsg);
1259e7c127fSCraig Rodrigues 	vlog(LOG_INFO, emsg, ap);
1269e7c127fSCraig Rodrigues 	va_end(ap);
1279e7c127fSCraig Rodrigues }
1289e7c127fSCraig Rodrigues 
1299e7c127fSCraig Rodrigues void
log_debug(const char * emsg,...)1309e7c127fSCraig Rodrigues log_debug(const char *emsg, ...)
1319e7c127fSCraig Rodrigues {
1329e7c127fSCraig Rodrigues 	va_list	 ap;
1339e7c127fSCraig Rodrigues 
1349e7c127fSCraig Rodrigues 	if (debug > 1) {
1359e7c127fSCraig Rodrigues 		va_start(ap, emsg);
1369e7c127fSCraig Rodrigues 		vlog(LOG_DEBUG, emsg, ap);
1379e7c127fSCraig Rodrigues 		va_end(ap);
1389e7c127fSCraig Rodrigues 	}
1399e7c127fSCraig Rodrigues }
1409e7c127fSCraig Rodrigues 
1419e7c127fSCraig Rodrigues void
fatal(const char * emsg)1429e7c127fSCraig Rodrigues fatal(const char *emsg)
1439e7c127fSCraig Rodrigues {
1449e7c127fSCraig Rodrigues 	if (emsg == NULL)
1459e7c127fSCraig Rodrigues 		logit(LOG_CRIT, "fatal: %s", strerror(errno));
1469e7c127fSCraig Rodrigues 	else
1479e7c127fSCraig Rodrigues 		if (errno)
1489e7c127fSCraig Rodrigues 			logit(LOG_CRIT, "fatal: %s: %s",
1499e7c127fSCraig Rodrigues 			    emsg, strerror(errno));
1509e7c127fSCraig Rodrigues 		else
1519e7c127fSCraig Rodrigues 			logit(LOG_CRIT, "fatal: %s", emsg);
1529e7c127fSCraig Rodrigues 
1539e7c127fSCraig Rodrigues 	exit(1);
1549e7c127fSCraig Rodrigues }
1559e7c127fSCraig Rodrigues 
1569e7c127fSCraig Rodrigues void
fatalx(const char * emsg)1579e7c127fSCraig Rodrigues fatalx(const char *emsg)
1589e7c127fSCraig Rodrigues {
1599e7c127fSCraig Rodrigues 	errno = 0;
1609e7c127fSCraig Rodrigues 	fatal(emsg);
1619e7c127fSCraig Rodrigues }
162