1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22/*
23 * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
24 * Use is subject to license terms.
25 */
26
27#include <pthread.h>
28#include <stdarg.h>
29#include <stdio.h>
30#include <stdlib.h>
31#include <syslog.h>
32
33#include "util.h"
34
35/*
36 * logging.c - contains various logging functions.
37 */
38
39boolean_t debug = B_TRUE;
40
41/*
42 * This idea for having this function is so that you can drop a dtrace probe
43 * here and trace complete strings (not just those containing formatting).  Its
44 * important that we actually format the debug strings so we could trace them
45 * even if we choose not to send them to syslog.
46 */
47static void
48log_out(int severity, const char *str)
49{
50	if (severity == LOG_DEBUG && !debug)
51		return;
52
53	syslog(severity, str);
54}
55
56static void
57log_format(int severity, const char *fmt, va_list ap, char *buf, int bufsize)
58{
59	int offset;
60	char vbuf[256];
61
62	if (buf == NULL) {
63		buf = vbuf;
64		bufsize = sizeof (vbuf);
65	}
66
67	offset = snprintf(buf, bufsize, "%d: ", pthread_self());
68	(void) vsnprintf(buf + offset, bufsize - offset, fmt, ap);
69
70	log_out(severity, buf);
71}
72
73/*
74 * This function takes a syslog severity and uses it to determine what to do
75 * with the message (currently send it to syslog).
76 */
77void
78nlog(int severity, const char *fmt, ...)
79{
80	va_list ap;
81
82	va_start(ap, fmt);
83	log_format(severity, fmt, ap, NULL, 0);
84	va_end(ap);
85}
86
87void
88pfail(const char *fmt, ...)
89{
90	char *msg;
91	va_list ap;
92
93	msg = malloc(256);
94
95	va_start(ap, fmt);
96	log_format(LOG_ERR, fmt, ap, msg, 256);
97	va_end(ap);
98
99	if (msg == NULL)
100		msg = "ran out of memory exiting.  see log.";
101
102	(void) puts(msg);
103	exit(EXIT_FAILURE);
104}
105