1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*
28*7c478bd9Sstevel@tonic-gate  *  errlog -- error logging facility for application programs
29*7c478bd9Sstevel@tonic-gate  */
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate #include <stdio.h>
32*7c478bd9Sstevel@tonic-gate #include <stdarg.h>
33*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
34*7c478bd9Sstevel@tonic-gate #include "errlog.h"
35*7c478bd9Sstevel@tonic-gate 
36*7c478bd9Sstevel@tonic-gate /*  Statics (this object is not threadable). */
37*7c478bd9Sstevel@tonic-gate static int Severity;
38*7c478bd9Sstevel@tonic-gate static struct location {
39*7c478bd9Sstevel@tonic-gate 	char *l_file;
40*7c478bd9Sstevel@tonic-gate 	int l_lineno;
41*7c478bd9Sstevel@tonic-gate 	char *l_tag;
42*7c478bd9Sstevel@tonic-gate 	char *l_line;
43*7c478bd9Sstevel@tonic-gate } Location;
44*7c478bd9Sstevel@tonic-gate 
45*7c478bd9Sstevel@tonic-gate /* Indentation */
46*7c478bd9Sstevel@tonic-gate static int  Trace_indent;
47*7c478bd9Sstevel@tonic-gate /* XXX YES, its 80 spaces !@#$%^ */
48*7c478bd9Sstevel@tonic-gate static char Trace_padding[] =
49*7c478bd9Sstevel@tonic-gate 	"                                              "
50*7c478bd9Sstevel@tonic-gate 	"                                  ";
51*7c478bd9Sstevel@tonic-gate 
52*7c478bd9Sstevel@tonic-gate #define	INDENT &Trace_padding[80-(Trace_indent*4)]
53*7c478bd9Sstevel@tonic-gate #define	PRINTHDR	INPUT
54*7c478bd9Sstevel@tonic-gate 
55*7c478bd9Sstevel@tonic-gate /*
56*7c478bd9Sstevel@tonic-gate  * errlog -- simulate the syslog printf-like interface, but
57*7c478bd9Sstevel@tonic-gate  *	with a first argument oriented toward reporting
58*7c478bd9Sstevel@tonic-gate  *	application errors.
59*7c478bd9Sstevel@tonic-gate  */
60*7c478bd9Sstevel@tonic-gate /*VARARGS2*/
61*7c478bd9Sstevel@tonic-gate void
errlog(const int descriptor,const char * format,...)62*7c478bd9Sstevel@tonic-gate errlog(const int descriptor, const char *format, ...)
63*7c478bd9Sstevel@tonic-gate {
64*7c478bd9Sstevel@tonic-gate 	va_list ap;
65*7c478bd9Sstevel@tonic-gate 
66*7c478bd9Sstevel@tonic-gate 	va_start(ap, format);
67*7c478bd9Sstevel@tonic-gate 	if ((Severity < (descriptor & FATAL)) &&
68*7c478bd9Sstevel@tonic-gate 	    ((descriptor & FATAL) != FATAL)) {
69*7c478bd9Sstevel@tonic-gate 		/* We don't need to say/do anything. */
70*7c478bd9Sstevel@tonic-gate 		return;
71*7c478bd9Sstevel@tonic-gate 	}
72*7c478bd9Sstevel@tonic-gate 
73*7c478bd9Sstevel@tonic-gate 	/* Produce the message. */
74*7c478bd9Sstevel@tonic-gate 	(void) fflush(stdout); /* Synchronize streams. */
75*7c478bd9Sstevel@tonic-gate 	if ((descriptor & PRINTHDR) != 0) {
76*7c478bd9Sstevel@tonic-gate 		if (Location.l_file == NULL) {
77*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "programmer error, logerr "
78*7c478bd9Sstevel@tonic-gate 			    "told to print file, line number, "
79*7c478bd9Sstevel@tonic-gate 			    "but file was not set.\n");
80*7c478bd9Sstevel@tonic-gate 		} else {
81*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "\"%s\", line %d: ",
82*7c478bd9Sstevel@tonic-gate 			    Location.l_file, Location.l_lineno);
83*7c478bd9Sstevel@tonic-gate 		}
84*7c478bd9Sstevel@tonic-gate 	}
85*7c478bd9Sstevel@tonic-gate 
86*7c478bd9Sstevel@tonic-gate 	/* Indent/outdent. */
87*7c478bd9Sstevel@tonic-gate 	if (descriptor & INDENTED) {
88*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s", INDENT);
89*7c478bd9Sstevel@tonic-gate 		Trace_indent = (Trace_indent < 20)? Trace_indent + 1: 20;
90*7c478bd9Sstevel@tonic-gate 	} else if (descriptor & OUTDENTED) {
91*7c478bd9Sstevel@tonic-gate 		Trace_indent = (Trace_indent > 0)? Trace_indent - 1: 0;
92*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s", INDENT);
93*7c478bd9Sstevel@tonic-gate 	} else {
94*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s", INDENT);
95*7c478bd9Sstevel@tonic-gate 	}
96*7c478bd9Sstevel@tonic-gate 
97*7c478bd9Sstevel@tonic-gate 	/* Print the stuff we did all this for. */
98*7c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, format, ap);
99*7c478bd9Sstevel@tonic-gate 
100*7c478bd9Sstevel@tonic-gate 	if ((descriptor & INPUT) && Location.l_line != NULL) {
101*7c478bd9Sstevel@tonic-gate 		/* Emit trailers. Formatting TBD. */
102*7c478bd9Sstevel@tonic-gate 		(void) putc('\n', stderr);
103*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "\tLine was: %s %s",
104*7c478bd9Sstevel@tonic-gate 		    Location.l_tag, Location.l_line);
105*7c478bd9Sstevel@tonic-gate 	}
106*7c478bd9Sstevel@tonic-gate 	(void) putc('\n', stderr);
107*7c478bd9Sstevel@tonic-gate 	(void) fflush(stderr); /* No-op on Solaris. */
108*7c478bd9Sstevel@tonic-gate 
109*7c478bd9Sstevel@tonic-gate 	if ((descriptor & FATAL) == FATAL) {
110*7c478bd9Sstevel@tonic-gate 		/* Say goodbye! */
111*7c478bd9Sstevel@tonic-gate 		exit(1);
112*7c478bd9Sstevel@tonic-gate 	}
113*7c478bd9Sstevel@tonic-gate 	va_end(ap);
114*7c478bd9Sstevel@tonic-gate }
115*7c478bd9Sstevel@tonic-gate 
116*7c478bd9Sstevel@tonic-gate /*
117*7c478bd9Sstevel@tonic-gate  * seterrline -- tell errlog what the context of the error is.
118*7c478bd9Sstevel@tonic-gate  */
119*7c478bd9Sstevel@tonic-gate void
seterrline(const int lineno,const char * file,const char * tag,const char * line)120*7c478bd9Sstevel@tonic-gate seterrline(const int lineno, const char *file,
121*7c478bd9Sstevel@tonic-gate     const char *tag, const char *line)
122*7c478bd9Sstevel@tonic-gate {
123*7c478bd9Sstevel@tonic-gate 	Location.l_lineno = lineno;
124*7c478bd9Sstevel@tonic-gate 	Location.l_file = (char *)file;
125*7c478bd9Sstevel@tonic-gate 	Location.l_tag = (char *)tag;
126*7c478bd9Sstevel@tonic-gate 	Location.l_line = (char *)line;
127*7c478bd9Sstevel@tonic-gate }
128*7c478bd9Sstevel@tonic-gate 
129*7c478bd9Sstevel@tonic-gate /*
130*7c478bd9Sstevel@tonic-gate  * seterrseverity -- set the severity/loudness variable.
131*7c478bd9Sstevel@tonic-gate  *	This is traditionally the ``Verbosity'' level.
132*7c478bd9Sstevel@tonic-gate  */
133*7c478bd9Sstevel@tonic-gate void
seterrseverity(const int loudness)134*7c478bd9Sstevel@tonic-gate seterrseverity(const int loudness)
135*7c478bd9Sstevel@tonic-gate {
136*7c478bd9Sstevel@tonic-gate 	Severity = (loudness & FATAL);
137*7c478bd9Sstevel@tonic-gate }
138