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