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