xref: /minix/crypto/external/bsd/libsaslc/dist/src/msg.c (revision ebfedea0)
1 /* $NetBSD: msg.c,v 1.2 2011/02/12 23:21:32 christos Exp $ */
2 
3 /* Copyright (c) 2010 The NetBSD Foundation, Inc.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *        This product includes software developed by the NetBSD
17  *        Foundation, Inc. and its contributors.
18  * 4. Neither the name of The NetBSD Foundation nor the names of its
19  *    contributors may be used to endorse or promote products derived
20  *    from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25  * PURPOSE ARE DISCLAIMED.	IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34 #include <sys/cdefs.h>
35 __RCSID("$NetBSD: msg.c,v 1.2 2011/02/12 23:21:32 christos Exp $");
36 
37 #include <stdarg.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <syslog.h>
41 
42 #include "msg.h"
43 
44 /**
45  * XXX: global debug flag.  This is unique as it is set as early as
46  * possible by checking the environment (looking for SASLC_ENV_DEBUG)
47  * and the context, mechanism, and session dictionaries (looking for
48  * SASLC_PROP_DEBUG) as soon as they become available.  Hence, the
49  * lookups are scattered in 4 places.  It's ugly.
50  *
51  * It's also global so it isn't tied to a session, but this makes it
52  * easly to use debugging messages as you don't need a context
53  * pointer.
54  */
55 bool saslc_debug = false;
56 
57 /**
58  * @brief conditionally log a message via syslogd
59  * @param flag log the message or not
60  * @param priority syslogd priority to log with
61  * @param fmt message format string
62  * @param ... format parameters
63  */
64 void
saslc__msg_syslog(bool flag,int priority,const char * fmt,...)65 saslc__msg_syslog(bool flag, int priority, const char *fmt, ...)
66 {
67 	va_list ap;
68 	char *tmp;
69 
70 	if (!flag)
71 		return;
72 
73 	va_start(ap, fmt);
74 	if (asprintf(&tmp, "libsaslc: %s", fmt) != -1) {
75 		vsyslog(priority, tmp, ap);
76 		free(tmp);
77 	}
78 	va_end(ap);
79 }
80