640 number_to_scaled_string is duplicated in several commands
[unleashed.git] / usr / src / cmd / svr4pkg / libinst / log.c
blob65c46b6c347949adbef4b351aa2412e1760c1776
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
28 /* unix system includes */
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>
44 * consolidation pkg command library includes
47 #include "pkglib.h"
50 * local pkg command library includes
53 #include "install.h"
54 #include "libinst.h"
55 #include "libadm.h"
56 #include "messages.h"
58 /* Should be defined by cc -D */
59 #if !defined(TEXT_DOMAIN)
60 #define TEXT_DOMAIN "SYS_TEST"
61 #endif
63 /* local static data */
65 static boolean_t verbose = B_FALSE;
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
76 /*PRINTFLIKE2*/
77 void
78 log_msg(LogMsgType a_type, const char *a_format, ...)
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();
88 /* process message based on type */
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;
110 out = stderr;
111 prefix = NULL;
113 /* output debug prefix to match echoDebug() format */
115 (void) fprintf(stderr, "# [%6d %3d", getpid(), getzoneid());
117 if ((p != (char *)NULL) && (*p != '\0')) {
118 fprintf(stderr, " %-11s", p);
121 (void) fprintf(stderr, "] ");
122 break;
125 /* output prefix if specified */
127 if (prefix != NULL) {
128 (void) fprintf(out, "%s: ", prefix);
131 /* determine size of the message in bytes */
133 va_start(ap, a_format);
134 vres = vsnprintf(bfr, 1, a_format, ap);
135 va_end(ap);
137 /* allocate storage to hold the message */
139 rstr = (char *)malloc(vres+2);
141 /* generate the results of the printf conversion */
143 va_start(ap, a_format);
144 vres = vsnprintf(rstr, vres+1, a_format, ap);
145 va_end(ap);
147 /* output formatted message to appropriate destination */
149 if (fprintf(out, "%s\n", rstr) < 0) {
150 if (out != stderr) {
152 * nothing output, try stderr as a
153 * last resort
155 (void) fprintf(stderr, ERR_LOG_FAIL, a_format);
159 /* free temporary message storage */
161 free(rstr);
165 * Name: set_verbose
166 * Description: Turns on verbose output
167 * Scope: public
168 * Arguments: verbose = B_TRUE indicates verbose mode
169 * Returns: none
172 void
173 log_set_verbose(boolean_t setting)
175 verbose = setting;
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
186 boolean_t
187 log_get_verbose()
189 return (verbose);