summaryrefslogtreecommitdiff
path: root/usr/src/cmd/svr4pkg/libinst/log.c
blob: 65c46b6c347949adbef4b351aa2412e1760c1776 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */


/* unix system includes */

#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <locale.h>
#include <errno.h>
#include <sys/param.h>
#include <instzones_api.h>

/*
 * consolidation pkg command library includes
 */

#include "pkglib.h"

/*
 * local pkg command library includes
 */

#include "install.h"
#include "libinst.h"
#include "libadm.h"
#include "messages.h"

/* Should be defined by cc -D */
#if	!defined(TEXT_DOMAIN)
#define	TEXT_DOMAIN "SYS_TEST"
#endif

/* local static data */

static boolean_t	verbose = B_FALSE;

/*
 * Name:	log_msg
 * Description:	Outputs messages to logging facility.
 * Scope:	public
 * Arguments:	a_type - the severity of the message
 *		a_format - the printf format, plus its arguments
 * Returns:	none
 */

/*PRINTFLIKE2*/
void
log_msg(LogMsgType a_type, const char *a_format, ...)
{
	FILE	*out;
	char	*rstr = (char *)NULL;
	char	bfr[1];
	char	*prefix;
	size_t	vres = 0;
	va_list	ap;
	char	*p = get_prog_name();

	/* process message based on type */

	switch (a_type) {
	case LOG_MSG_ERR:
	default:	/* treat unknown type as LOG_MSG_ERR */
		out = stderr;
		prefix = MSG_LOG_ERROR;
		break;
	case LOG_MSG_WRN:	/* warning message */
		out = stderr;
		prefix = MSG_LOG_WARNING;
		break;
	case LOG_MSG_INFO:	/* information message */
		out = stdout;
		prefix = NULL;
		break;
	case LOG_MSG_DEBUG:	/* debugging message */
		if (!log_get_verbose()) {
			/* no debug messages if not verbose mode */
			return;
		}

		out = stderr;
		prefix = NULL;

		/* output debug prefix to match echoDebug() format */

		(void) fprintf(stderr, "# [%6d %3d", getpid(), getzoneid());

		if ((p != (char *)NULL) && (*p != '\0')) {
			fprintf(stderr, " %-11s", p);
		}

		(void) fprintf(stderr, "] ");
		break;
	}

	/* output prefix if specified */

	if (prefix != NULL) {
		(void) fprintf(out, "%s: ", prefix);
	}

	/* determine size of the message in bytes */

	va_start(ap, a_format);
	vres = vsnprintf(bfr, 1, a_format, ap);
	va_end(ap);

	/* allocate storage to hold the message */

	rstr = (char *)malloc(vres+2);

	/* generate the results of the printf conversion */

	va_start(ap, a_format);
	vres = vsnprintf(rstr, vres+1, a_format, ap);
	va_end(ap);

	/* output formatted message to appropriate destination */

	if (fprintf(out, "%s\n", rstr) < 0) {
		if (out != stderr) {
			/*
			 * nothing output, try stderr as a
			 * last resort
			 */
			(void) fprintf(stderr, ERR_LOG_FAIL, a_format);
		}
	}

	/* free temporary message storage */

	free(rstr);
}

/*
 * Name:	set_verbose
 * Description:	Turns on verbose output
 * Scope:	public
 * Arguments:	verbose = B_TRUE indicates verbose mode
 * Returns:	none
 */

void
log_set_verbose(boolean_t setting)
{
	verbose = setting;
}

/*
 * Name:	get_verbose
 * Description:	Returns whether or not to output verbose messages
 * Scope:	public
 * Arguments:	none
 * Returns:	B_TRUE - verbose messages should be output
 */

boolean_t
log_get_verbose()
{
	return (verbose);
}