1*498aec4fSguenther /* $OpenBSD: log.c,v 1.7 2016/08/27 04:21:08 guenther Exp $ */
27aea46c5Sho
37aea46c5Sho /*
47aea46c5Sho * Copyright (c) 2005 H�kan Olsson. All rights reserved.
57aea46c5Sho *
67aea46c5Sho * Redistribution and use in source and binary forms, with or without
77aea46c5Sho * modification, are permitted provided that the following conditions
87aea46c5Sho * are met:
97aea46c5Sho *
107aea46c5Sho * 1. Redistributions of source code must retain the above copyright
117aea46c5Sho * notice, this list of conditions and the following disclaimer.
127aea46c5Sho * 2. Redistributions in binary form must reproduce the above copyright
137aea46c5Sho * notice, this list of conditions and the following disclaimer in the
147aea46c5Sho * documentation and/or other materials provided with the distribution.
157aea46c5Sho *
167aea46c5Sho * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
177aea46c5Sho * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
187aea46c5Sho * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
197aea46c5Sho * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
207aea46c5Sho * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
217aea46c5Sho * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
227aea46c5Sho * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
237aea46c5Sho * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
247aea46c5Sho * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
257aea46c5Sho * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
267aea46c5Sho */
277aea46c5Sho
287aea46c5Sho /*
297aea46c5Sho * This code was written under funding by Multicom Security AB.
307aea46c5Sho */
317aea46c5Sho
327aea46c5Sho #include <sys/types.h>
33*498aec4fSguenther #include <sys/select.h>
347aea46c5Sho #include <stdio.h>
357aea46c5Sho #include <string.h>
367aea46c5Sho #include <stdarg.h>
377aea46c5Sho #include <stdlib.h>
387aea46c5Sho #include <syslog.h>
397aea46c5Sho #include <errno.h>
407aea46c5Sho #include <time.h>
417aea46c5Sho
427aea46c5Sho #include "sasyncd.h"
437aea46c5Sho
447aea46c5Sho static char logbuf[2048];
457aea46c5Sho
467aea46c5Sho void
log_init(char * pname)477aea46c5Sho log_init(char *pname)
487aea46c5Sho {
497aea46c5Sho tzset();
5050b83ccaSmarkus openlog(pname, LOG_CONS | LOG_PID, LOG_DAEMON);
517aea46c5Sho }
527aea46c5Sho
537aea46c5Sho static void
log_output(char * msg)547aea46c5Sho log_output(char *msg)
557aea46c5Sho {
567aea46c5Sho if (cfgstate.debug)
577aea46c5Sho fprintf(stderr, "%s\n", msg);
587aea46c5Sho else
59c08b6224Sho syslog(LOG_CRIT, "%s", msg);
607aea46c5Sho }
617aea46c5Sho
627aea46c5Sho void
log_err(const char * fmt,...)637aea46c5Sho log_err(const char *fmt, ...)
647aea46c5Sho {
6526bbdef8Sho extern char *__progname;
667aea46c5Sho int off = 0;
6726bbdef8Sho va_list ap;
687aea46c5Sho
697aea46c5Sho if (cfgstate.debug) {
707aea46c5Sho snprintf(logbuf, sizeof logbuf, "%s: ", __progname);
717aea46c5Sho off = strlen(logbuf);
727aea46c5Sho }
737aea46c5Sho
747aea46c5Sho va_start(ap, fmt);
757aea46c5Sho (void)vsnprintf(logbuf + off, sizeof logbuf - off, fmt, ap);
767aea46c5Sho va_end(ap);
777aea46c5Sho
787aea46c5Sho strlcat(logbuf, ": ", sizeof logbuf);
797aea46c5Sho strlcat(logbuf, strerror(errno), sizeof logbuf);
807aea46c5Sho
817aea46c5Sho log_output(logbuf);
827aea46c5Sho return;
837aea46c5Sho }
847aea46c5Sho
857aea46c5Sho void
log_msg(int minlevel,const char * fmt,...)867aea46c5Sho log_msg(int minlevel, const char *fmt, ...)
877aea46c5Sho {
887aea46c5Sho va_list ap;
897aea46c5Sho
907aea46c5Sho if (cfgstate.verboselevel < minlevel)
917aea46c5Sho return;
927aea46c5Sho
937aea46c5Sho va_start(ap, fmt);
947aea46c5Sho (void)vsnprintf(logbuf, sizeof logbuf, fmt, ap);
957aea46c5Sho va_end(ap);
967aea46c5Sho
977aea46c5Sho log_output(logbuf);
987aea46c5Sho return;
997aea46c5Sho }
100