8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / cmd-inet / lib / nwamd / logging.c
blobebf97bb7106604d1e5c6e56535fb44224a3521c7
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
23 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #include <pthread.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <syslog.h>
33 #include "util.h"
36 * logging.c - contains various logging functions.
39 boolean_t debug = B_TRUE;
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.
47 static void
48 log_out(int severity, const char *str)
50 if (severity == LOG_DEBUG && !debug)
51 return;
53 syslog(severity, str);
56 static void
57 log_format(int severity, const char *fmt, va_list ap, char *buf, int bufsize)
59 int offset;
60 char vbuf[256];
62 if (buf == NULL) {
63 buf = vbuf;
64 bufsize = sizeof (vbuf);
67 offset = snprintf(buf, bufsize, "%d: ", pthread_self());
68 (void) vsnprintf(buf + offset, bufsize - offset, fmt, ap);
70 log_out(severity, buf);
74 * This function takes a syslog severity and uses it to determine what to do
75 * with the message (currently send it to syslog).
77 void
78 nlog(int severity, const char *fmt, ...)
80 va_list ap;
82 va_start(ap, fmt);
83 log_format(severity, fmt, ap, NULL, 0);
84 va_end(ap);
87 void
88 pfail(const char *fmt, ...)
90 char *msg;
91 va_list ap;
93 msg = malloc(256);
95 va_start(ap, fmt);
96 log_format(LOG_ERR, fmt, ap, msg, 256);
97 va_end(ap);
99 if (msg == NULL)
100 msg = "ran out of memory exiting. see log.";
102 (void) puts(msg);
103 exit(EXIT_FAILURE);