xref: /onnv-gate/usr/src/cmd/svr4pkg/libinst/log.c (revision 9781:ccf49524d5dc)
1*9781SMoriah.Waterland@Sun.COM /*
2*9781SMoriah.Waterland@Sun.COM  * CDDL HEADER START
3*9781SMoriah.Waterland@Sun.COM  *
4*9781SMoriah.Waterland@Sun.COM  * The contents of this file are subject to the terms of the
5*9781SMoriah.Waterland@Sun.COM  * Common Development and Distribution License (the "License").
6*9781SMoriah.Waterland@Sun.COM  * You may not use this file except in compliance with the License.
7*9781SMoriah.Waterland@Sun.COM  *
8*9781SMoriah.Waterland@Sun.COM  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*9781SMoriah.Waterland@Sun.COM  * or http://www.opensolaris.org/os/licensing.
10*9781SMoriah.Waterland@Sun.COM  * See the License for the specific language governing permissions
11*9781SMoriah.Waterland@Sun.COM  * and limitations under the License.
12*9781SMoriah.Waterland@Sun.COM  *
13*9781SMoriah.Waterland@Sun.COM  * When distributing Covered Code, include this CDDL HEADER in each
14*9781SMoriah.Waterland@Sun.COM  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*9781SMoriah.Waterland@Sun.COM  * If applicable, add the following below this CDDL HEADER, with the
16*9781SMoriah.Waterland@Sun.COM  * fields enclosed by brackets "[]" replaced with your own identifying
17*9781SMoriah.Waterland@Sun.COM  * information: Portions Copyright [yyyy] [name of copyright owner]
18*9781SMoriah.Waterland@Sun.COM  *
19*9781SMoriah.Waterland@Sun.COM  * CDDL HEADER END
20*9781SMoriah.Waterland@Sun.COM  */
21*9781SMoriah.Waterland@Sun.COM 
22*9781SMoriah.Waterland@Sun.COM /*
23*9781SMoriah.Waterland@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24*9781SMoriah.Waterland@Sun.COM  * Use is subject to license terms.
25*9781SMoriah.Waterland@Sun.COM  */
26*9781SMoriah.Waterland@Sun.COM 
27*9781SMoriah.Waterland@Sun.COM 
28*9781SMoriah.Waterland@Sun.COM /* unix system includes */
29*9781SMoriah.Waterland@Sun.COM 
30*9781SMoriah.Waterland@Sun.COM #include <stdio.h>
31*9781SMoriah.Waterland@Sun.COM #include <stdarg.h>
32*9781SMoriah.Waterland@Sun.COM #include <stdlib.h>
33*9781SMoriah.Waterland@Sun.COM #include <string.h>
34*9781SMoriah.Waterland@Sun.COM #include <fcntl.h>
35*9781SMoriah.Waterland@Sun.COM #include <sys/types.h>
36*9781SMoriah.Waterland@Sun.COM #include <sys/stat.h>
37*9781SMoriah.Waterland@Sun.COM #include <unistd.h>
38*9781SMoriah.Waterland@Sun.COM #include <locale.h>
39*9781SMoriah.Waterland@Sun.COM #include <errno.h>
40*9781SMoriah.Waterland@Sun.COM #include <sys/param.h>
41*9781SMoriah.Waterland@Sun.COM #include <instzones_api.h>
42*9781SMoriah.Waterland@Sun.COM 
43*9781SMoriah.Waterland@Sun.COM /*
44*9781SMoriah.Waterland@Sun.COM  * consolidation pkg command library includes
45*9781SMoriah.Waterland@Sun.COM  */
46*9781SMoriah.Waterland@Sun.COM 
47*9781SMoriah.Waterland@Sun.COM #include "pkglib.h"
48*9781SMoriah.Waterland@Sun.COM 
49*9781SMoriah.Waterland@Sun.COM /*
50*9781SMoriah.Waterland@Sun.COM  * local pkg command library includes
51*9781SMoriah.Waterland@Sun.COM  */
52*9781SMoriah.Waterland@Sun.COM 
53*9781SMoriah.Waterland@Sun.COM #include "install.h"
54*9781SMoriah.Waterland@Sun.COM #include "libinst.h"
55*9781SMoriah.Waterland@Sun.COM #include "libadm.h"
56*9781SMoriah.Waterland@Sun.COM #include "messages.h"
57*9781SMoriah.Waterland@Sun.COM 
58*9781SMoriah.Waterland@Sun.COM /* Should be defined by cc -D */
59*9781SMoriah.Waterland@Sun.COM #if	!defined(TEXT_DOMAIN)
60*9781SMoriah.Waterland@Sun.COM #define	TEXT_DOMAIN "SYS_TEST"
61*9781SMoriah.Waterland@Sun.COM #endif
62*9781SMoriah.Waterland@Sun.COM 
63*9781SMoriah.Waterland@Sun.COM /* local static data */
64*9781SMoriah.Waterland@Sun.COM 
65*9781SMoriah.Waterland@Sun.COM static boolean_t	verbose = B_FALSE;
66*9781SMoriah.Waterland@Sun.COM 
67*9781SMoriah.Waterland@Sun.COM /*
68*9781SMoriah.Waterland@Sun.COM  * Name:	log_msg
69*9781SMoriah.Waterland@Sun.COM  * Description:	Outputs messages to logging facility.
70*9781SMoriah.Waterland@Sun.COM  * Scope:	public
71*9781SMoriah.Waterland@Sun.COM  * Arguments:	a_type - the severity of the message
72*9781SMoriah.Waterland@Sun.COM  *		a_format - the printf format, plus its arguments
73*9781SMoriah.Waterland@Sun.COM  * Returns:	none
74*9781SMoriah.Waterland@Sun.COM  */
75*9781SMoriah.Waterland@Sun.COM 
76*9781SMoriah.Waterland@Sun.COM /*PRINTFLIKE2*/
77*9781SMoriah.Waterland@Sun.COM void
log_msg(LogMsgType a_type,const char * a_format,...)78*9781SMoriah.Waterland@Sun.COM log_msg(LogMsgType a_type, const char *a_format, ...)
79*9781SMoriah.Waterland@Sun.COM {
80*9781SMoriah.Waterland@Sun.COM 	FILE	*out;
81*9781SMoriah.Waterland@Sun.COM 	char	*rstr = (char *)NULL;
82*9781SMoriah.Waterland@Sun.COM 	char	bfr[1];
83*9781SMoriah.Waterland@Sun.COM 	char	*prefix;
84*9781SMoriah.Waterland@Sun.COM 	size_t	vres = 0;
85*9781SMoriah.Waterland@Sun.COM 	va_list	ap;
86*9781SMoriah.Waterland@Sun.COM 	char	*p = get_prog_name();
87*9781SMoriah.Waterland@Sun.COM 
88*9781SMoriah.Waterland@Sun.COM 	/* process message based on type */
89*9781SMoriah.Waterland@Sun.COM 
90*9781SMoriah.Waterland@Sun.COM 	switch (a_type) {
91*9781SMoriah.Waterland@Sun.COM 	case LOG_MSG_ERR:
92*9781SMoriah.Waterland@Sun.COM 	default:	/* treat unknown type as LOG_MSG_ERR */
93*9781SMoriah.Waterland@Sun.COM 		out = stderr;
94*9781SMoriah.Waterland@Sun.COM 		prefix = MSG_LOG_ERROR;
95*9781SMoriah.Waterland@Sun.COM 		break;
96*9781SMoriah.Waterland@Sun.COM 	case LOG_MSG_WRN:	/* warning message */
97*9781SMoriah.Waterland@Sun.COM 		out = stderr;
98*9781SMoriah.Waterland@Sun.COM 		prefix = MSG_LOG_WARNING;
99*9781SMoriah.Waterland@Sun.COM 		break;
100*9781SMoriah.Waterland@Sun.COM 	case LOG_MSG_INFO:	/* information message */
101*9781SMoriah.Waterland@Sun.COM 		out = stdout;
102*9781SMoriah.Waterland@Sun.COM 		prefix = NULL;
103*9781SMoriah.Waterland@Sun.COM 		break;
104*9781SMoriah.Waterland@Sun.COM 	case LOG_MSG_DEBUG:	/* debugging message */
105*9781SMoriah.Waterland@Sun.COM 		if (!log_get_verbose()) {
106*9781SMoriah.Waterland@Sun.COM 			/* no debug messages if not verbose mode */
107*9781SMoriah.Waterland@Sun.COM 			return;
108*9781SMoriah.Waterland@Sun.COM 		}
109*9781SMoriah.Waterland@Sun.COM 
110*9781SMoriah.Waterland@Sun.COM 		out = stderr;
111*9781SMoriah.Waterland@Sun.COM 		prefix = NULL;
112*9781SMoriah.Waterland@Sun.COM 
113*9781SMoriah.Waterland@Sun.COM 		/* output debug prefix to match echoDebug() format */
114*9781SMoriah.Waterland@Sun.COM 
115*9781SMoriah.Waterland@Sun.COM 		(void) fprintf(stderr, "# [%6d %3d", getpid(), getzoneid());
116*9781SMoriah.Waterland@Sun.COM 
117*9781SMoriah.Waterland@Sun.COM 		if ((p != (char *)NULL) && (*p != '\0')) {
118*9781SMoriah.Waterland@Sun.COM 			fprintf(stderr, " %-11s", p);
119*9781SMoriah.Waterland@Sun.COM 		}
120*9781SMoriah.Waterland@Sun.COM 
121*9781SMoriah.Waterland@Sun.COM 		(void) fprintf(stderr, "] ");
122*9781SMoriah.Waterland@Sun.COM 		break;
123*9781SMoriah.Waterland@Sun.COM 	}
124*9781SMoriah.Waterland@Sun.COM 
125*9781SMoriah.Waterland@Sun.COM 	/* output prefix if specified */
126*9781SMoriah.Waterland@Sun.COM 
127*9781SMoriah.Waterland@Sun.COM 	if (prefix != NULL) {
128*9781SMoriah.Waterland@Sun.COM 		(void) fprintf(out, "%s: ", prefix);
129*9781SMoriah.Waterland@Sun.COM 	}
130*9781SMoriah.Waterland@Sun.COM 
131*9781SMoriah.Waterland@Sun.COM 	/* determine size of the message in bytes */
132*9781SMoriah.Waterland@Sun.COM 
133*9781SMoriah.Waterland@Sun.COM 	va_start(ap, a_format);
134*9781SMoriah.Waterland@Sun.COM 	vres = vsnprintf(bfr, 1, a_format, ap);
135*9781SMoriah.Waterland@Sun.COM 	va_end(ap);
136*9781SMoriah.Waterland@Sun.COM 
137*9781SMoriah.Waterland@Sun.COM 	/* allocate storage to hold the message */
138*9781SMoriah.Waterland@Sun.COM 
139*9781SMoriah.Waterland@Sun.COM 	rstr = (char *)malloc(vres+2);
140*9781SMoriah.Waterland@Sun.COM 
141*9781SMoriah.Waterland@Sun.COM 	/* generate the results of the printf conversion */
142*9781SMoriah.Waterland@Sun.COM 
143*9781SMoriah.Waterland@Sun.COM 	va_start(ap, a_format);
144*9781SMoriah.Waterland@Sun.COM 	vres = vsnprintf(rstr, vres+1, a_format, ap);
145*9781SMoriah.Waterland@Sun.COM 	va_end(ap);
146*9781SMoriah.Waterland@Sun.COM 
147*9781SMoriah.Waterland@Sun.COM 	/* output formatted message to appropriate destination */
148*9781SMoriah.Waterland@Sun.COM 
149*9781SMoriah.Waterland@Sun.COM 	if (fprintf(out, "%s\n", rstr) < 0) {
150*9781SMoriah.Waterland@Sun.COM 		if (out != stderr) {
151*9781SMoriah.Waterland@Sun.COM 			/*
152*9781SMoriah.Waterland@Sun.COM 			 * nothing output, try stderr as a
153*9781SMoriah.Waterland@Sun.COM 			 * last resort
154*9781SMoriah.Waterland@Sun.COM 			 */
155*9781SMoriah.Waterland@Sun.COM 			(void) fprintf(stderr, ERR_LOG_FAIL, a_format);
156*9781SMoriah.Waterland@Sun.COM 		}
157*9781SMoriah.Waterland@Sun.COM 	}
158*9781SMoriah.Waterland@Sun.COM 
159*9781SMoriah.Waterland@Sun.COM 	/* free temporary message storage */
160*9781SMoriah.Waterland@Sun.COM 
161*9781SMoriah.Waterland@Sun.COM 	free(rstr);
162*9781SMoriah.Waterland@Sun.COM }
163*9781SMoriah.Waterland@Sun.COM 
164*9781SMoriah.Waterland@Sun.COM /*
165*9781SMoriah.Waterland@Sun.COM  * Name:	set_verbose
166*9781SMoriah.Waterland@Sun.COM  * Description:	Turns on verbose output
167*9781SMoriah.Waterland@Sun.COM  * Scope:	public
168*9781SMoriah.Waterland@Sun.COM  * Arguments:	verbose = B_TRUE indicates verbose mode
169*9781SMoriah.Waterland@Sun.COM  * Returns:	none
170*9781SMoriah.Waterland@Sun.COM  */
171*9781SMoriah.Waterland@Sun.COM 
172*9781SMoriah.Waterland@Sun.COM void
log_set_verbose(boolean_t setting)173*9781SMoriah.Waterland@Sun.COM log_set_verbose(boolean_t setting)
174*9781SMoriah.Waterland@Sun.COM {
175*9781SMoriah.Waterland@Sun.COM 	verbose = setting;
176*9781SMoriah.Waterland@Sun.COM }
177*9781SMoriah.Waterland@Sun.COM 
178*9781SMoriah.Waterland@Sun.COM /*
179*9781SMoriah.Waterland@Sun.COM  * Name:	get_verbose
180*9781SMoriah.Waterland@Sun.COM  * Description:	Returns whether or not to output verbose messages
181*9781SMoriah.Waterland@Sun.COM  * Scope:	public
182*9781SMoriah.Waterland@Sun.COM  * Arguments:	none
183*9781SMoriah.Waterland@Sun.COM  * Returns:	B_TRUE - verbose messages should be output
184*9781SMoriah.Waterland@Sun.COM  */
185*9781SMoriah.Waterland@Sun.COM 
186*9781SMoriah.Waterland@Sun.COM boolean_t
log_get_verbose()187*9781SMoriah.Waterland@Sun.COM log_get_verbose()
188*9781SMoriah.Waterland@Sun.COM {
189*9781SMoriah.Waterland@Sun.COM 	return (verbose);
190*9781SMoriah.Waterland@Sun.COM }
191