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