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