xref: /onnv-gate/usr/src/cmd/fps/fpsd/fpsd_log.c (revision 7346:8a16b5b71337)
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 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*
28  * Logging support for the FPS Daemon
29  */
30 
31 #include <stdio.h>
32 #include <stdarg.h>
33 #include <string.h>
34 #include <errno.h>
35 #include <syslog.h>
36 #include <stdlib.h>
37 #include <pthread.h>
38 #include <synch.h>
39 #include <unistd.h>
40 
41 #include <fpsapi.h>
42 
43 #include "fpsd.h"
44 #include "messages.h"
45 
46 #define	FPSD_MAX_MSG_HDR_LEN	256
47 #define	FPSD_MAX_TIME_LEN	32
48 
49 pthread_mutex_t log_mutex;
50 
51 static char *log_prio_str[] = {
52 	"ERROR: ",	/* LOG_ERR */
53 	"WARNING: ",	/* LOG_WARNING */
54 	"INFO: ",	/* LOG_INFO */
55 	"DEBUG: "	/* LOG_DEBUG */
56 };
57 
58 /*
59  * Generate a timestamp string in the provided buffer.
60  * If any errors are encountered, the function returns
61  * with the buffer containing an empty string.
62  */
63 static void
fpsd_timestamp(char * buf,size_t buflen)64 fpsd_timestamp(char *buf, size_t buflen)
65 {
66 	struct	tm	ltime;
67 	struct	timeval	now;
68 
69 	if ((buf == NULL) || (buflen == 0))
70 		return;
71 
72 	buf[0] = '\0';
73 
74 	if (gettimeofday(&now, NULL) != 0) {
75 		(void) fprintf(stderr, GET_TIME_FAILED,
76 		    strerror(errno));
77 		return;
78 	}
79 
80 	if (localtime_r(&now.tv_sec, &ltime) == NULL) {
81 		(void) fprintf(stderr, LOCAL_TIME_FAILED,
82 		    strerror(errno));
83 		return;
84 	}
85 
86 	if (strftime(buf, buflen, "%b %e %T ", &ltime) == 0) {
87 		(void) fprintf(stderr, STRFTIME_FAILED, buflen);
88 	/*
89 	 * On failure, the contents of the buffer
90 	 * are indeterminate. Restore it to a known
91 	 * state before returning.
92 	 */
93 		buf[0] = '\0';
94 	}
95 }
96 
97 static void
fpsd_log_msg(int prio,const char * fmt,va_list vap)98 fpsd_log_msg(int prio, const char *fmt, va_list vap)
99 {
100 	char msgbuf[FPSD_MAX_MSG_HDR_LEN];
101 	char timebuf[FPSD_MAX_TIME_LEN] = "";
102 
103 	if ((prio > debug_level) || (prio < 0))
104 		return;
105 	if ((fpsd.d_fg) || (!fpsd.d_daemon)) {
106 		/* generate a timestamp for output */
107 		fpsd_timestamp(timebuf, sizeof (timebuf));
108 		(void) snprintf(msgbuf, sizeof (msgbuf), "%s  %s %s ",
109 		    timebuf, FPS_DAEMON_NAME, log_prio_str[prio]);
110 	}
111 
112 	/* In debug mode, messages will be sent to the controlling terminal */
113 
114 	if (fpsd.d_fg || !fpsd.d_daemon) {
115 		(void) fprintf(stderr, PRINT_BUFFER, msgbuf);
116 		(void) vfprintf(stderr, fmt, vap);
117 		return;
118 	}
119 
120 	switch (prio) {
121 
122 		case FPS_ERROR: /* Log into syslog */
123 			vsyslog(LOG_ERR, fmt, vap);
124 			break;
125 
126 		case FPS_WARNING:
127 			vsyslog(LOG_WARNING, fmt, vap);
128 			break;
129 
130 		case FPS_INFO:
131 			vsyslog(LOG_INFO, fmt, vap);
132 			break;
133 
134 		case FPS_DEBUG:
135 			vsyslog(LOG_DEBUG, fmt, vap);
136 			break;
137 	}
138 
139 }
140 
141 void
fpsd_message(int return_code,int msg_type,char * fmt,...)142 fpsd_message(int return_code, int msg_type, char *fmt,  ...)
143 {
144 	va_list vap;
145 	(void) pthread_mutex_lock(&log_mutex);
146 	va_start(vap, fmt);
147 	fpsd_log_msg(msg_type, fmt, vap);
148 	va_end(vap);
149 	(void) pthread_mutex_unlock(&log_mutex);
150 
151 	if (return_code > 0)  {
152 		terminate_process();
153 		_exit(return_code);
154 	}
155 }
156