remove xvm/ipagent
[unleashed.git] / usr / src / cmd / drd / drd_log.c
blob3713c383322a77864312348af007ffe254b5555f
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 2006 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
30 * Logging support for the DR Daemon
33 #include <stdio.h>
34 #include <stdarg.h>
35 #include <string.h>
36 #include <errno.h>
37 #include <syslog.h>
39 #include "drd.h"
41 #define DRD_MAX_MSG_LEN 512
42 #define DRD_MAX_TIME_LEN 32
44 static char *log_prio_str[] = {
45 "EMERG: ", /* LOG_EMERG */
46 "ALERT: ", /* LOG_ALERT */
47 "CRIT: ", /* LOG_CRIT */
48 "ERROR: ", /* LOG_ERR */
49 "WARNING: ", /* LOG_WARNING */
50 "NOTICE: ", /* LOG_NOTICE */
51 "INFO: ", /* LOG_INFO */
52 "" /* LOG_DEBUG */
56 * Generate a timestamp string in the provided buffer.
57 * If any errors are encountered, the function returns
58 * with the buffer containing an empty string.
60 static void
61 drd_timestamp(char *buf, size_t buflen)
63 struct tm ltime;
64 struct timeval now;
66 if ((buf == NULL) || (buflen == 0))
67 return;
69 buf[0] = '\0';
71 if (gettimeofday(&now, NULL) != 0) {
72 (void) fprintf(stderr, "gettimeofday failed: %s\n",
73 strerror(errno));
74 return;
77 if (localtime_r(&now.tv_sec, &ltime) == NULL) {
78 (void) fprintf(stderr, "localtime_r failed: %s\n",
79 strerror(errno));
80 return;
83 if (strftime(buf, buflen, "%b %e %T ", &ltime) == 0) {
84 (void) fprintf(stderr, "strftime failed: buffer[%d] too "
85 "small\n", buflen);
87 * On failure, the contents of the buffer
88 * are indeterminate. Restore it to a known
89 * state before returning.
91 buf[0] = '\0';
95 static void
96 drd_log_msg(int prio, char *fmt, va_list vap)
98 char msgbuf[DRD_MAX_MSG_LEN];
99 char timebuf[DRD_MAX_TIME_LEN] = "";
101 /* generate a timestamp for the SMF log */
102 drd_timestamp(timebuf, sizeof (timebuf));
104 (void) vsnprintf(msgbuf, DRD_MAX_MSG_LEN, fmt, vap);
107 * Print the message to stderr. In daemon mode, it
108 * will be sent to the SMF log. In standalone mode,
109 * it will be sent to the controlling terminal.
111 (void) fprintf(stderr, "%s%s%s\n", timebuf, log_prio_str[prio], msgbuf);
113 if (drd_daemonized)
114 syslog(prio, msgbuf);
117 void
118 drd_err(char *fmt, ...)
120 va_list vap;
122 va_start(vap, fmt);
123 drd_log_msg(LOG_ERR, fmt, vap);
124 va_end(vap);
127 void
128 drd_info(char *fmt, ...)
130 va_list vap;
132 va_start(vap, fmt);
133 drd_log_msg(LOG_INFO, fmt, vap);
134 va_end(vap);
137 void
138 drd_dbg(char *fmt, ...)
140 va_list vap;
142 if (!drd_debug) {
143 /* not debugging */
144 return;
147 va_start(vap, fmt);
148 drd_log_msg(LOG_DEBUG, fmt, vap);
149 va_end(vap);