 /*
  * Routines to report various classes of problems. Each report is decorated
  * with the current context (file name and line number), if available.
  *
  * tcpd_warn() reports a problem and proceeds.
  *
  * tcpd_jump() reports a problem and jumps.
  *
  * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
  */

#ifndef lint
static char sccsid[] = "@(#) diag.c 1.1 94/12/28 17:42:20";
#endif

/* System libraries */
#include <stdio.h>
#include <setjmp.h>
#include "syslogif.h"

/* Local stuff */

#include "tcpd.h"
#include "mystdarg.h"

struct tcpd_context tcpd_context = { NULL, 0, 0, NULL, 0 };
jmp_buf tcpd_buf;

/* tcpd_diag - centralize error reporter */

static void tcpd_diag(int severity, char *tag, char *format,
                      va_list ap)
{
    char    fmt[BUFSIZ];
    char    msg[BUFSIZ];

    if (tcpd_context.file)
	sprintf(fmt, "%s: %s, line %d: %s",
		tag, tcpd_context.file, tcpd_context.line, format);
    else
	sprintf(fmt, "%s: %s", tag, format);
    vsprintf(msg,  fmt, ap);
    if (tcpd_context.use_syslog)
    {
      xsyslogf_irq(tcpd_context.syslog_file,severity,msg);
    }
    else
    {
      fprintf(stderr,"%s\n",msg);
    }
}

/* tcpd_warn - report problem of some sort and proceed */

void    VARARGS(tcpd_warn, char *, format)
{
    va_list ap;

    VASTART(ap, char *, format);
    tcpd_diag(tcpd_context.syslog_warning_level, "warning", format, ap);
    VAEND(ap);
}

/* tcpd_jump - report serious problem and jump */

void    VARARGS(tcpd_jump, char *, format)
{
    va_list ap;

    VASTART(ap, char *, format);
    tcpd_diag(tcpd_context.syslog_error_level, "error", format, ap);
    VAEND(ap);
    longjmp(tcpd_buf, AC_ERROR);
}
