#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include "errlog.h"
static int Severity;
static struct location {
char *l_file;
int l_lineno;
char *l_tag;
char *l_line;
} Location;
static int Trace_indent;
static char Trace_padding[] =
" "
" ";
#define INDENT &Trace_padding[80-(Trace_indent*4)]
#define PRINTHDR INPUT
void
errlog(const int descriptor, const char *format, ...)
{
va_list ap;
va_start(ap, format);
if ((Severity < (descriptor & FATAL)) &&
((descriptor & FATAL) != FATAL)) {
return;
}
(void) fflush(stdout);
if ((descriptor & PRINTHDR) != 0) {
if (Location.l_file == NULL) {
(void) fprintf(stderr, "programmer error, logerr "
"told to print file, line number, "
"but file was not set.\n");
} else {
(void) fprintf(stderr, "\"%s\", line %d: ",
Location.l_file, Location.l_lineno);
}
}
if (descriptor & INDENTED) {
(void) fprintf(stderr, "%s", INDENT);
Trace_indent = (Trace_indent < 20)? Trace_indent + 1: 20;
} else if (descriptor & OUTDENTED) {
Trace_indent = (Trace_indent > 0)? Trace_indent - 1: 0;
(void) fprintf(stderr, "%s", INDENT);
} else {
(void) fprintf(stderr, "%s", INDENT);
}
(void) vfprintf(stderr, format, ap);
if ((descriptor & INPUT) && Location.l_line != NULL) {
(void) putc('\n', stderr);
(void) fprintf(stderr, "\tLine was: %s %s",
Location.l_tag, Location.l_line);
}
(void) putc('\n', stderr);
(void) fflush(stderr);
if ((descriptor & FATAL) == FATAL) {
exit(1);
}
va_end(ap);
}
void
seterrline(const int lineno, const char *file,
const char *tag, const char *line)
{
Location.l_lineno = lineno;
Location.l_file = (char *)file;
Location.l_tag = (char *)tag;
Location.l_line = (char *)line;
}
void
seterrseverity(const int loudness)
{
Severity = (loudness & FATAL);
}