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