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
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.
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]
23 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
26 * out.c -- some basic output routines
30 #pragma ident "%Z%%M% %I% %E% SMI"
33 #include <fm/fmd_api.h>
45 /* stats we keep for "out" module */
46 static struct stats
*Outcount
;
47 static struct stats
*Errcount
;
48 static struct stats
*Warncount
;
52 static const char *Myname
;
55 /* buffer used to format all prints */
57 static char Outbuf
[MAXOUT
];
58 static int Outidx
; /* next unused char in Outbuf[] */
61 * out_init -- initialize this module
64 out_init(const char *myname
)
66 Outcount
= stats_new_counter("output.calls", "total calls", 1);
67 Errcount
= stats_new_counter("output.errors", "total errors", 0);
68 Warncount
= stats_new_counter("output.warnings", "total warnings", 0);
73 if ((Myname
= strrchr(myname
, '/')) == NULL
&&
74 (Myname
= strrchr(myname
, '\\')) == NULL
)
83 stats_delete(Outcount
);
85 stats_delete(Errcount
);
87 stats_delete(Warncount
);
92 * out_altfp -- store an alternate fp for O_ALTFP
101 * voutbufprintf -- like vprintf, but appends to Outbuf
104 voutbufprintf(const char *fmt
, va_list ap
)
106 int len
= vsnprintf(&Outbuf
[Outidx
], MAXOUT
- Outidx
, fmt
, ap
);
109 if (Outidx
>= MAXOUT
)
114 * outbufprintf -- like printf, but appends to Outbuf
118 outbufprintf(const char *fmt
, ...)
122 voutbufprintf(fmt
, ap
);
127 * vout -- va_list version of out()
129 * all the output processing work is done here.
132 vout(int flags
, const char *fmt
, va_list ap
)
134 int safe_errno
= errno
;
136 stats_counter_bump(Outcount
);
139 * just return if called with a disabled output type. this
140 * prevents debug prints when Debug is off, verbose prints
141 * when Verbose is off, and language warnings when warnings
142 * are quenched (which they are when we're loading a .eft file).
145 if ((flags
& O_DEBUG
) && Debug
== 0)
148 if ((flags
& O_VERB
) && Verbose
== 0)
151 if ((flags
& O_VERB2
) && Verbose
< 2)
154 if ((flags
& O_VERB3
) && Verbose
< 3)
157 if ((flags
& O_WARN
) && Warn
== 0)
160 if ((flags
& O_ALTFP
) && Altfp
== NULL
)
163 /* some things only happen at the beginning of a print */
165 if (flags
& O_USAGE
) {
167 outbufprintf("usage: %s ", Myname
);
169 if (Myname
&& flags
& (O_DIE
|O_ERR
|O_WARN
|O_PROG
))
170 outbufprintf("%s: ", Myname
);
174 outbufprintf("fatal error: ");
175 } else if (flags
& O_ERR
) {
177 stats_counter_bump(Errcount
);
178 outbufprintf("error: ");
179 } else if (flags
& O_WARN
) {
180 stats_counter_bump(Warncount
);
181 outbufprintf("warning: ");
186 /* fmt can be NULL if the caller just wanted flags processed */
188 voutbufprintf(fmt
, ap
);
190 /* O_SYS means convert errno to a string and append it */
192 const char *msg
= strerror(safe_errno
);
198 outbufprintf("%s", msg
);
200 outbufprintf("(error %d)", safe_errno
);
203 /* O_STAMP means convert add a timestamp */
204 if (flags
& O_STAMP
) {
209 tmsg
= ctime(&clock
);
211 tmsg
[strlen(tmsg
) - 1] = '\0';
216 outbufprintf("%s", tmsg
);
221 return; /* not done filling Outbuf */
223 /* done filling Outbuf, platform calls will add newline */
225 (void) fprintf(Altfp
, "%s\n", Outbuf
);
226 else if (flags
& O_ABORT
)
228 else if (flags
& O_DIE
)
230 else if (flags
& O_ERR
)
235 /* reset output buffer */
241 * out -- spew a line of output, with various options
245 out(int flags
, const char *fmt
, ...)
249 vout(flags
, fmt
, ap
);
254 * outfl -- spew a filename:linenumber message
258 outfl(int flags
, const char *fname
, int line
, const char *fmt
, ...)
264 out(flags
|O_NONL
, "%s:%d: ", fname
, line
);
266 vout(flags
, fmt
, ap
);
272 * out_exit -- exit the program
277 io_exit(Exitcode
+ code
);
281 * out_errcount -- return the number of O_ERR messages issued so far
286 return (stats_counter_value(Errcount
));
290 * out_warncount -- return the number of O_WARN messages issued so far
295 return (stats_counter_value(Warncount
));