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