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 #include "libuutil_common.h"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <errno.h>
30*7c478bd9Sstevel@tonic-gate #include <libintl.h>
31*7c478bd9Sstevel@tonic-gate #include <stdarg.h>
32*7c478bd9Sstevel@tonic-gate #include <stdio.h>
33*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
34*7c478bd9Sstevel@tonic-gate #include <strings.h>
35*7c478bd9Sstevel@tonic-gate 
36*7c478bd9Sstevel@tonic-gate #define	FACILITY_FMT	"%s (%s): "
37*7c478bd9Sstevel@tonic-gate 
38*7c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
39*7c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
40*7c478bd9Sstevel@tonic-gate #endif
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate static const char *
strseverity(uu_dprintf_severity_t severity)43*7c478bd9Sstevel@tonic-gate strseverity(uu_dprintf_severity_t severity)
44*7c478bd9Sstevel@tonic-gate {
45*7c478bd9Sstevel@tonic-gate 	switch (severity) {
46*7c478bd9Sstevel@tonic-gate 	case UU_DPRINTF_SILENT:
47*7c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "silent"));
48*7c478bd9Sstevel@tonic-gate 	case UU_DPRINTF_FATAL:
49*7c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "FATAL"));
50*7c478bd9Sstevel@tonic-gate 	case UU_DPRINTF_WARNING:
51*7c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "WARNING"));
52*7c478bd9Sstevel@tonic-gate 	case UU_DPRINTF_NOTICE:
53*7c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "note"));
54*7c478bd9Sstevel@tonic-gate 	case UU_DPRINTF_INFO:
55*7c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "info"));
56*7c478bd9Sstevel@tonic-gate 	case UU_DPRINTF_DEBUG:
57*7c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "debug"));
58*7c478bd9Sstevel@tonic-gate 	default:
59*7c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "unspecified"));
60*7c478bd9Sstevel@tonic-gate 	}
61*7c478bd9Sstevel@tonic-gate }
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate uu_dprintf_t *
uu_dprintf_create(const char * name,uu_dprintf_severity_t severity,uint_t flags)64*7c478bd9Sstevel@tonic-gate uu_dprintf_create(const char *name, uu_dprintf_severity_t severity,
65*7c478bd9Sstevel@tonic-gate     uint_t flags)
66*7c478bd9Sstevel@tonic-gate {
67*7c478bd9Sstevel@tonic-gate 	uu_dprintf_t *D;
68*7c478bd9Sstevel@tonic-gate 
69*7c478bd9Sstevel@tonic-gate 	if (uu_check_name(name, UU_NAME_DOMAIN) == -1) {
70*7c478bd9Sstevel@tonic-gate 		uu_set_error(UU_ERROR_INVALID_ARGUMENT);
71*7c478bd9Sstevel@tonic-gate 		return (NULL);
72*7c478bd9Sstevel@tonic-gate 	}
73*7c478bd9Sstevel@tonic-gate 
74*7c478bd9Sstevel@tonic-gate 	if ((D = uu_zalloc(sizeof (uu_dprintf_t))) == NULL)
75*7c478bd9Sstevel@tonic-gate 		return (NULL);
76*7c478bd9Sstevel@tonic-gate 
77*7c478bd9Sstevel@tonic-gate 	if (name != NULL) {
78*7c478bd9Sstevel@tonic-gate 		D->uud_name = strdup(name);
79*7c478bd9Sstevel@tonic-gate 		if (D->uud_name == NULL) {
80*7c478bd9Sstevel@tonic-gate 			uu_free(D);
81*7c478bd9Sstevel@tonic-gate 			return (NULL);
82*7c478bd9Sstevel@tonic-gate 		}
83*7c478bd9Sstevel@tonic-gate 	} else {
84*7c478bd9Sstevel@tonic-gate 		D->uud_name = NULL;
85*7c478bd9Sstevel@tonic-gate 	}
86*7c478bd9Sstevel@tonic-gate 
87*7c478bd9Sstevel@tonic-gate 	D->uud_severity = severity;
88*7c478bd9Sstevel@tonic-gate 	D->uud_flags = flags;
89*7c478bd9Sstevel@tonic-gate 
90*7c478bd9Sstevel@tonic-gate 	return (D);
91*7c478bd9Sstevel@tonic-gate }
92*7c478bd9Sstevel@tonic-gate 
93*7c478bd9Sstevel@tonic-gate /*PRINTFLIKE3*/
94*7c478bd9Sstevel@tonic-gate void
uu_dprintf(uu_dprintf_t * D,uu_dprintf_severity_t severity,const char * format,...)95*7c478bd9Sstevel@tonic-gate uu_dprintf(uu_dprintf_t *D, uu_dprintf_severity_t severity,
96*7c478bd9Sstevel@tonic-gate     const char *format, ...)
97*7c478bd9Sstevel@tonic-gate {
98*7c478bd9Sstevel@tonic-gate 	va_list alist;
99*7c478bd9Sstevel@tonic-gate 
100*7c478bd9Sstevel@tonic-gate 	/* XXX Assert that severity is not UU_DPRINTF_SILENT. */
101*7c478bd9Sstevel@tonic-gate 
102*7c478bd9Sstevel@tonic-gate 	if (severity > D->uud_severity)
103*7c478bd9Sstevel@tonic-gate 		return;
104*7c478bd9Sstevel@tonic-gate 
105*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, FACILITY_FMT, D->uud_name,
106*7c478bd9Sstevel@tonic-gate 	    strseverity(severity));
107*7c478bd9Sstevel@tonic-gate 
108*7c478bd9Sstevel@tonic-gate 	va_start(alist, format);
109*7c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, format, alist);
110*7c478bd9Sstevel@tonic-gate 	va_end(alist);
111*7c478bd9Sstevel@tonic-gate }
112*7c478bd9Sstevel@tonic-gate 
113*7c478bd9Sstevel@tonic-gate void
uu_dprintf_destroy(uu_dprintf_t * D)114*7c478bd9Sstevel@tonic-gate uu_dprintf_destroy(uu_dprintf_t *D)
115*7c478bd9Sstevel@tonic-gate {
116*7c478bd9Sstevel@tonic-gate 	if (D->uud_name)
117*7c478bd9Sstevel@tonic-gate 		free(D->uud_name);
118*7c478bd9Sstevel@tonic-gate 
119*7c478bd9Sstevel@tonic-gate 	uu_free(D);
120*7c478bd9Sstevel@tonic-gate }
121*7c478bd9Sstevel@tonic-gate 
122*7c478bd9Sstevel@tonic-gate const char *
uu_dprintf_getname(uu_dprintf_t * D)123*7c478bd9Sstevel@tonic-gate uu_dprintf_getname(uu_dprintf_t *D)
124*7c478bd9Sstevel@tonic-gate {
125*7c478bd9Sstevel@tonic-gate 	return (D->uud_name);
126*7c478bd9Sstevel@tonic-gate }
127