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