xref: /titanic_51/usr/src/lib/libuutil/common/uu_dprintf.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include "libuutil_common.h"
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate #include <errno.h>
32*7c478bd9Sstevel@tonic-gate #include <libintl.h>
33*7c478bd9Sstevel@tonic-gate #include <stdarg.h>
34*7c478bd9Sstevel@tonic-gate #include <stdio.h>
35*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
36*7c478bd9Sstevel@tonic-gate #include <strings.h>
37*7c478bd9Sstevel@tonic-gate 
38*7c478bd9Sstevel@tonic-gate #define	FACILITY_FMT	"%s (%s): "
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
41*7c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
42*7c478bd9Sstevel@tonic-gate #endif
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate static const char *
45*7c478bd9Sstevel@tonic-gate strseverity(uu_dprintf_severity_t severity)
46*7c478bd9Sstevel@tonic-gate {
47*7c478bd9Sstevel@tonic-gate 	switch (severity) {
48*7c478bd9Sstevel@tonic-gate 	case UU_DPRINTF_SILENT:
49*7c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "silent"));
50*7c478bd9Sstevel@tonic-gate 	case UU_DPRINTF_FATAL:
51*7c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "FATAL"));
52*7c478bd9Sstevel@tonic-gate 	case UU_DPRINTF_WARNING:
53*7c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "WARNING"));
54*7c478bd9Sstevel@tonic-gate 	case UU_DPRINTF_NOTICE:
55*7c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "note"));
56*7c478bd9Sstevel@tonic-gate 	case UU_DPRINTF_INFO:
57*7c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "info"));
58*7c478bd9Sstevel@tonic-gate 	case UU_DPRINTF_DEBUG:
59*7c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "debug"));
60*7c478bd9Sstevel@tonic-gate 	default:
61*7c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "unspecified"));
62*7c478bd9Sstevel@tonic-gate 	}
63*7c478bd9Sstevel@tonic-gate }
64*7c478bd9Sstevel@tonic-gate 
65*7c478bd9Sstevel@tonic-gate uu_dprintf_t *
66*7c478bd9Sstevel@tonic-gate uu_dprintf_create(const char *name, uu_dprintf_severity_t severity,
67*7c478bd9Sstevel@tonic-gate     uint_t flags)
68*7c478bd9Sstevel@tonic-gate {
69*7c478bd9Sstevel@tonic-gate 	uu_dprintf_t *D;
70*7c478bd9Sstevel@tonic-gate 
71*7c478bd9Sstevel@tonic-gate 	if (uu_check_name(name, UU_NAME_DOMAIN) == -1) {
72*7c478bd9Sstevel@tonic-gate 		uu_set_error(UU_ERROR_INVALID_ARGUMENT);
73*7c478bd9Sstevel@tonic-gate 		return (NULL);
74*7c478bd9Sstevel@tonic-gate 	}
75*7c478bd9Sstevel@tonic-gate 
76*7c478bd9Sstevel@tonic-gate 	if ((D = uu_zalloc(sizeof (uu_dprintf_t))) == NULL)
77*7c478bd9Sstevel@tonic-gate 		return (NULL);
78*7c478bd9Sstevel@tonic-gate 
79*7c478bd9Sstevel@tonic-gate 	if (name != NULL) {
80*7c478bd9Sstevel@tonic-gate 		D->uud_name = strdup(name);
81*7c478bd9Sstevel@tonic-gate 		if (D->uud_name == NULL) {
82*7c478bd9Sstevel@tonic-gate 			uu_free(D);
83*7c478bd9Sstevel@tonic-gate 			return (NULL);
84*7c478bd9Sstevel@tonic-gate 		}
85*7c478bd9Sstevel@tonic-gate 	} else {
86*7c478bd9Sstevel@tonic-gate 		D->uud_name = NULL;
87*7c478bd9Sstevel@tonic-gate 	}
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate 	D->uud_severity = severity;
90*7c478bd9Sstevel@tonic-gate 	D->uud_flags = flags;
91*7c478bd9Sstevel@tonic-gate 
92*7c478bd9Sstevel@tonic-gate 	return (D);
93*7c478bd9Sstevel@tonic-gate }
94*7c478bd9Sstevel@tonic-gate 
95*7c478bd9Sstevel@tonic-gate /*PRINTFLIKE3*/
96*7c478bd9Sstevel@tonic-gate void
97*7c478bd9Sstevel@tonic-gate uu_dprintf(uu_dprintf_t *D, uu_dprintf_severity_t severity,
98*7c478bd9Sstevel@tonic-gate     const char *format, ...)
99*7c478bd9Sstevel@tonic-gate {
100*7c478bd9Sstevel@tonic-gate 	va_list alist;
101*7c478bd9Sstevel@tonic-gate 
102*7c478bd9Sstevel@tonic-gate 	/* XXX Assert that severity is not UU_DPRINTF_SILENT. */
103*7c478bd9Sstevel@tonic-gate 
104*7c478bd9Sstevel@tonic-gate 	if (severity > D->uud_severity)
105*7c478bd9Sstevel@tonic-gate 		return;
106*7c478bd9Sstevel@tonic-gate 
107*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, FACILITY_FMT, D->uud_name,
108*7c478bd9Sstevel@tonic-gate 	    strseverity(severity));
109*7c478bd9Sstevel@tonic-gate 
110*7c478bd9Sstevel@tonic-gate 	va_start(alist, format);
111*7c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, format, alist);
112*7c478bd9Sstevel@tonic-gate 	va_end(alist);
113*7c478bd9Sstevel@tonic-gate }
114*7c478bd9Sstevel@tonic-gate 
115*7c478bd9Sstevel@tonic-gate void
116*7c478bd9Sstevel@tonic-gate uu_dprintf_destroy(uu_dprintf_t *D)
117*7c478bd9Sstevel@tonic-gate {
118*7c478bd9Sstevel@tonic-gate 	if (D->uud_name)
119*7c478bd9Sstevel@tonic-gate 		free(D->uud_name);
120*7c478bd9Sstevel@tonic-gate 
121*7c478bd9Sstevel@tonic-gate 	uu_free(D);
122*7c478bd9Sstevel@tonic-gate }
123*7c478bd9Sstevel@tonic-gate 
124*7c478bd9Sstevel@tonic-gate const char *
125*7c478bd9Sstevel@tonic-gate uu_dprintf_getname(uu_dprintf_t *D)
126*7c478bd9Sstevel@tonic-gate {
127*7c478bd9Sstevel@tonic-gate 	return (D->uud_name);
128*7c478bd9Sstevel@tonic-gate }
129