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, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22 /*
23 * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 */
26
27 #pragma ident "%Z%%M% %I% %E% SMI"
28
29 #include <errno.h>
30 #include <libintl.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <time.h>
35
36 #include "volume_output.h"
37 #include "volume_error.h"
38
39 static int max_verbosity = OUTPUT_QUIET;
40 static FILE *output = NULL;
41
42 /*
43 * Set the maximum level of verbosity to be reported to the user.
44 * Strings sent to oprintf() with a higher verbosity level than this
45 * maximum level will not be reported to the user.
46 *
47 * @param verbosity
48 * One of the predefined constants:
49 * OUTPUT_QUIET
50 * OUTPUT_TERSE
51 * OUTPUT_VERBOSE
52 * OUTPUT_DEBUG
53 *
54 * @param stream
55 * The stream to print all qualifying output to.
56 *
57 * @return 0 on success, non-zero otherwise.
58 */
59 int
set_max_verbosity(int verbosity,FILE * stream)60 set_max_verbosity(
61 int verbosity,
62 FILE *stream)
63 {
64 int error = 0;
65
66 switch (verbosity) {
67 case OUTPUT_QUIET:
68 case OUTPUT_TERSE:
69 case OUTPUT_VERBOSE:
70 case OUTPUT_DEBUG:
71 max_verbosity = verbosity;
72 output = stream;
73 break;
74
75 default:
76 volume_set_error(
77 gettext("%d: invalid verbosity level"), verbosity);
78 error = -1;
79 }
80
81 return (error);
82 }
83
84 /*
85 * Get the maximum level of verbosity to be reported to the user.
86 *
87 * @return OUTPUT_QUIET
88 *
89 * @return OUTPUT_TERSE
90 *
91 * @return OUTPUT_VERBOSE
92 *
93 * @return OUTPUT_DEBUG
94 */
95 int
get_max_verbosity()96 get_max_verbosity()
97 {
98 return (max_verbosity);
99 }
100
101 /*
102 * Prints the given formatted string arguments to a predefined stream,
103 * if the given verbosity is less than or equal to the set maximum
104 * verbosity.
105 *
106 * @param verbosity
107 * Same as for set_max_verbosity()
108 *
109 * @param fmt, ...
110 * printf-style arguments
111 *
112 * @return the number of characters output
113 * if successful
114 *
115 * @return negative value
116 * if unsuccessful
117 */
118 int
oprintf(int verbosity,char * fmt,...)119 oprintf(
120 int verbosity,
121 char *fmt,
122 ...)
123 {
124 int ret;
125 va_list ap;
126
127 va_start(ap, fmt);
128 ret = oprintf_va(verbosity, fmt, ap);
129 va_end(ap);
130
131 return (ret);
132 }
133
134 /*
135 * Identical to oprintf but with a va_list instead of variable length
136 * argument list. This function is provided for external printf-style
137 * wrappers.
138 *
139 * @param verbosity
140 * Same as for set_max_verbosity()
141 *
142 * @param fmt
143 * printf format string
144 *
145 * @param ap
146 * a va_list containing remaining printf-style arguments
147 *
148 * @return the number of characters output
149 * if successful
150 *
151 * @return negative value
152 * if unsuccessful
153 */
154 /*PRINTFLIKE2*/
155 int
oprintf_va(int verbosity,char * fmt,va_list ap)156 oprintf_va(
157 int verbosity,
158 char *fmt,
159 va_list ap)
160 {
161 int ret = 0;
162
163 /* Is this verbosity high enough to print? */
164 if (output != NULL && verbosity <= max_verbosity) {
165 #ifdef DEBUG
166 if (getenv(METASSIST_DEBUG_ENV) != NULL) {
167 time_t now = time(NULL);
168 struct tm *time = localtime(&now);
169 fprintf(output, "%.2d:%.2d:%.2d: ",
170 time->tm_hour, time->tm_min, time->tm_sec);
171 }
172 #endif
173 ret = vfprintf(output, fmt, ap);
174 }
175
176 return (ret);
177 }
178