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