1168404Spjd/* 2168404Spjd * CDDL HEADER START 3168404Spjd * 4168404Spjd * The contents of this file are subject to the terms of the 5168404Spjd * Common Development and Distribution License, Version 1.0 only 6168404Spjd * (the "License"). You may not use this file except in compliance 7168404Spjd * with the License. 8168404Spjd * 9168404Spjd * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10168404Spjd * or http://www.opensolaris.org/os/licensing. 11168404Spjd * See the License for the specific language governing permissions 12168404Spjd * and limitations under the License. 13168404Spjd * 14168404Spjd * When distributing Covered Code, include this CDDL HEADER in each 15168404Spjd * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16168404Spjd * If applicable, add the following below this CDDL HEADER, with the 17168404Spjd * fields enclosed by brackets "[]" replaced with your own identifying 18168404Spjd * information: Portions Copyright [yyyy] [name of copyright owner] 19168404Spjd * 20168404Spjd * CDDL HEADER END 21168404Spjd */ 22168404Spjd/* 23168404Spjd * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24168404Spjd * Use is subject to license terms. 25168404Spjd */ 26168404Spjd 27168404Spjd#pragma ident "%Z%%M% %I% %E% SMI" 28168404Spjd 29168404Spjd#include "libuutil_common.h" 30168404Spjd 31168404Spjd#include <errno.h> 32168404Spjd#include <libintl.h> 33168404Spjd#include <stdarg.h> 34168404Spjd#include <stdio.h> 35168404Spjd#include <stdlib.h> 36185029Spjd#include <string.h> 37168404Spjd 38168404Spjd#define FACILITY_FMT "%s (%s): " 39168404Spjd 40168404Spjd#if !defined(TEXT_DOMAIN) 41168404Spjd#define TEXT_DOMAIN "SYS_TEST" 42168404Spjd#endif 43168404Spjd 44168404Spjdstatic const char * 45168404Spjdstrseverity(uu_dprintf_severity_t severity) 46168404Spjd{ 47168404Spjd switch (severity) { 48168404Spjd case UU_DPRINTF_SILENT: 49168404Spjd return (dgettext(TEXT_DOMAIN, "silent")); 50168404Spjd case UU_DPRINTF_FATAL: 51168404Spjd return (dgettext(TEXT_DOMAIN, "FATAL")); 52168404Spjd case UU_DPRINTF_WARNING: 53168404Spjd return (dgettext(TEXT_DOMAIN, "WARNING")); 54168404Spjd case UU_DPRINTF_NOTICE: 55168404Spjd return (dgettext(TEXT_DOMAIN, "note")); 56168404Spjd case UU_DPRINTF_INFO: 57168404Spjd return (dgettext(TEXT_DOMAIN, "info")); 58168404Spjd case UU_DPRINTF_DEBUG: 59168404Spjd return (dgettext(TEXT_DOMAIN, "debug")); 60168404Spjd default: 61168404Spjd return (dgettext(TEXT_DOMAIN, "unspecified")); 62168404Spjd } 63168404Spjd} 64168404Spjd 65168404Spjduu_dprintf_t * 66168404Spjduu_dprintf_create(const char *name, uu_dprintf_severity_t severity, 67168404Spjd uint_t flags) 68168404Spjd{ 69168404Spjd uu_dprintf_t *D; 70168404Spjd 71168404Spjd if (uu_check_name(name, UU_NAME_DOMAIN) == -1) { 72168404Spjd uu_set_error(UU_ERROR_INVALID_ARGUMENT); 73168404Spjd return (NULL); 74168404Spjd } 75168404Spjd 76168404Spjd if ((D = uu_zalloc(sizeof (uu_dprintf_t))) == NULL) 77168404Spjd return (NULL); 78168404Spjd 79168404Spjd if (name != NULL) { 80168404Spjd D->uud_name = strdup(name); 81168404Spjd if (D->uud_name == NULL) { 82168404Spjd uu_free(D); 83168404Spjd return (NULL); 84168404Spjd } 85168404Spjd } else { 86168404Spjd D->uud_name = NULL; 87168404Spjd } 88168404Spjd 89168404Spjd D->uud_severity = severity; 90168404Spjd D->uud_flags = flags; 91168404Spjd 92168404Spjd return (D); 93168404Spjd} 94168404Spjd 95168404Spjd/*PRINTFLIKE3*/ 96168404Spjdvoid 97168404Spjduu_dprintf(uu_dprintf_t *D, uu_dprintf_severity_t severity, 98168404Spjd const char *format, ...) 99168404Spjd{ 100168404Spjd va_list alist; 101168404Spjd 102168404Spjd /* XXX Assert that severity is not UU_DPRINTF_SILENT. */ 103168404Spjd 104168404Spjd if (severity > D->uud_severity) 105168404Spjd return; 106168404Spjd 107168404Spjd (void) fprintf(stderr, FACILITY_FMT, D->uud_name, 108168404Spjd strseverity(severity)); 109168404Spjd 110168404Spjd va_start(alist, format); 111168404Spjd (void) vfprintf(stderr, format, alist); 112168404Spjd va_end(alist); 113168404Spjd} 114168404Spjd 115168404Spjdvoid 116168404Spjduu_dprintf_destroy(uu_dprintf_t *D) 117168404Spjd{ 118168404Spjd if (D->uud_name) 119168404Spjd free(D->uud_name); 120168404Spjd 121168404Spjd uu_free(D); 122168404Spjd} 123168404Spjd 124168404Spjdconst char * 125168404Spjduu_dprintf_getname(uu_dprintf_t *D) 126168404Spjd{ 127168404Spjd return (D->uud_name); 128168404Spjd} 129