1158115Sume/*-
2158115Sume * Copyright (c) 2005 Michael Bushkov <bushman@rsu.ru>
3158115Sume * All rights reserved.
4158115Sume *
5158115Sume * Redistribution and use in source and binary forms, with or without
6158115Sume * modification, are permitted provided that the following conditions
7158115Sume * are met:
8158115Sume * 1. Redistributions of source code must retain the above copyright
9158115Sume *    notice, this list of conditions and the following disclaimer.
10158115Sume * 2. Redistributions in binary form must reproduce the above copyright
11158115Sume *    notice, this list of conditions and the following disclaimer in the
12158115Sume *    documentation and/or other materials provided with the distribution.
13158115Sume *
14158115Sume * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15158115Sume * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16158115Sume * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17158115Sume * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18158115Sume * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19158115Sume * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20158115Sume * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21158115Sume * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22158115Sume * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23158115Sume * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24158115Sume * SUCH DAMAGE.
25158115Sume *
26158115Sume */
27158115Sume
28158115Sume#include <sys/cdefs.h>
29158115Sume__FBSDID("$FreeBSD$");
30158115Sume
31158115Sume#include <assert.h>
32158115Sume#include <stdarg.h>
33158115Sume#include <stdio.h>
34158115Sume#include <stdlib.h>
35158115Sume#include <syslog.h>
36194093Sdes
37158115Sume#include "log.h"
38158115Sume
39158115Sumevoid
40158115Sume__log_msg(int level, const char *sender, const char *message, ...)
41158115Sume{
42158115Sume	va_list ap;
43158115Sume	char	*fmessage;
44158115Sume
45158115Sume	fmessage = NULL;
46158115Sume	va_start(ap, message);
47158115Sume	vasprintf(&fmessage, message, ap);
48158115Sume	va_end(ap);
49158115Sume	assert(fmessage != NULL);
50158115Sume
51158115Sume	printf("M%d from %s: %s\n", level, sender, fmessage);
52158115Sume#ifndef NO_SYSLOG
53158115Sume	if (level == 0)
54171795Sbushman		syslog(LOG_INFO, "nscd message (from %s): %s", sender,
55158115Sume		fmessage);
56158115Sume#endif
57158115Sume	free(fmessage);
58158115Sume}
59158115Sume
60158115Sumevoid
61158115Sume__log_err(int level, const char *sender, const char *error, ...)
62158115Sume{
63158115Sume	va_list ap;
64158115Sume	char	*ferror;
65158115Sume
66158115Sume	ferror = NULL;
67158115Sume	va_start(ap, error);
68158115Sume	vasprintf(&ferror, error, ap);
69158115Sume	va_end(ap);
70158115Sume	assert(ferror != NULL);
71158115Sume
72158115Sume	printf("E%d from %s: %s\n", level, sender, ferror);
73158115Sume
74158115Sume#ifndef NO_SYSLOG
75158115Sume	if (level == 0)
76171795Sbushman		syslog(LOG_ERR, "nscd error (from %s): %s", sender, ferror);
77158115Sume#endif
78158115Sume	free(ferror);
79158115Sume}
80