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 2009 Sun Microsystems, Inc.  All rights reserved.
24 * Use is subject to license terms.
25 */
26
27
28/* unix system includes */
29
30#include <stdio.h>
31#include <stdarg.h>
32#include <stdlib.h>
33#include <string.h>
34#include <fcntl.h>
35#include <sys/types.h>
36#include <sys/stat.h>
37#include <unistd.h>
38#include <locale.h>
39#include <errno.h>
40#include <sys/param.h>
41#include <instzones_api.h>
42
43/*
44 * consolidation pkg command library includes
45 */
46
47#include "pkglib.h"
48
49/*
50 * local pkg command library includes
51 */
52
53#include "install.h"
54#include "libinst.h"
55#include "libadm.h"
56#include "messages.h"
57
58/* Should be defined by cc -D */
59#if	!defined(TEXT_DOMAIN)
60#define	TEXT_DOMAIN "SYS_TEST"
61#endif
62
63/* local static data */
64
65static boolean_t	verbose = B_FALSE;
66
67/*
68 * Name:	log_msg
69 * Description:	Outputs messages to logging facility.
70 * Scope:	public
71 * Arguments:	a_type - the severity of the message
72 *		a_format - the printf format, plus its arguments
73 * Returns:	none
74 */
75
76/*PRINTFLIKE2*/
77void
78log_msg(LogMsgType a_type, const char *a_format, ...)
79{
80	FILE	*out;
81	char	*rstr = (char *)NULL;
82	char	bfr[1];
83	char	*prefix;
84	size_t	vres = 0;
85	va_list	ap;
86	char	*p = get_prog_name();
87
88	/* process message based on type */
89
90	switch (a_type) {
91	case LOG_MSG_ERR:
92	default:	/* treat unknown type as LOG_MSG_ERR */
93		out = stderr;
94		prefix = MSG_LOG_ERROR;
95		break;
96	case LOG_MSG_WRN:	/* warning message */
97		out = stderr;
98		prefix = MSG_LOG_WARNING;
99		break;
100	case LOG_MSG_INFO:	/* information message */
101		out = stdout;
102		prefix = NULL;
103		break;
104	case LOG_MSG_DEBUG:	/* debugging message */
105		if (!log_get_verbose()) {
106			/* no debug messages if not verbose mode */
107			return;
108		}
109
110		out = stderr;
111		prefix = NULL;
112
113		/* output debug prefix to match echoDebug() format */
114
115		(void) fprintf(stderr, "# [%6d %3d", getpid(), getzoneid());
116
117		if ((p != (char *)NULL) && (*p != '\0')) {
118			fprintf(stderr, " %-11s", p);
119		}
120
121		(void) fprintf(stderr, "] ");
122		break;
123	}
124
125	/* output prefix if specified */
126
127	if (prefix != NULL) {
128		(void) fprintf(out, "%s: ", prefix);
129	}
130
131	/* determine size of the message in bytes */
132
133	va_start(ap, a_format);
134	vres = vsnprintf(bfr, 1, a_format, ap);
135	va_end(ap);
136
137	/* allocate storage to hold the message */
138
139	rstr = (char *)malloc(vres+2);
140
141	/* generate the results of the printf conversion */
142
143	va_start(ap, a_format);
144	vres = vsnprintf(rstr, vres+1, a_format, ap);
145	va_end(ap);
146
147	/* output formatted message to appropriate destination */
148
149	if (fprintf(out, "%s\n", rstr) < 0) {
150		if (out != stderr) {
151			/*
152			 * nothing output, try stderr as a
153			 * last resort
154			 */
155			(void) fprintf(stderr, ERR_LOG_FAIL, a_format);
156		}
157	}
158
159	/* free temporary message storage */
160
161	free(rstr);
162}
163
164/*
165 * Name:	set_verbose
166 * Description:	Turns on verbose output
167 * Scope:	public
168 * Arguments:	verbose = B_TRUE indicates verbose mode
169 * Returns:	none
170 */
171
172void
173log_set_verbose(boolean_t setting)
174{
175	verbose = setting;
176}
177
178/*
179 * Name:	get_verbose
180 * Description:	Returns whether or not to output verbose messages
181 * Scope:	public
182 * Arguments:	none
183 * Returns:	B_TRUE - verbose messages should be output
184 */
185
186boolean_t
187log_get_verbose()
188{
189	return (verbose);
190}
191