/*
 * $Id: 1d00893e4d74b129bcf7dd9011fbdb7707895330 $
 *
 * This file is part of nsmtp.
 *
 * Copyright (c) 2007, 2008 Niklas Olmes <niklas@noxa.de>
 *                          http://www.nevm.de/nsmtp/
 *                          NEVM-RIPE
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License Version 2
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <time.h>
#include <syslog.h>
#include <unistd.h>

#include "log.h"
#include "misc.h"
#include "ntl.h"
#include "config.h"

#define DEBUG_STDOUT

static const char rcsid[] = "$Id: 1d00893e4d74b129bcf7dd9011fbdb7707895330 $";

void
xinfo(const char *fmt, ...)
{
	char buf[2048];
	va_list ap;

	va_start(ap, fmt);
	(void)vsnprintf(buf, sizeof(buf), fmt, ap);
	va_end(ap);

	syslog(LOG_INFO, "%s\n", buf);
#ifdef DEBUG_STDOUT
	(void)printf("[%u] I @ %lu: %s\n", getpid(), (unsigned long)time(NULL), buf);
#endif
}

void
xwarn(const char *fmt, ...)
{
	char buf[2048];
	va_list ap;

	va_start(ap, fmt);
	(void)vsnprintf(buf, sizeof(buf), fmt, ap);
	va_end(ap);

	syslog(LOG_WARNING, "%s\n", buf);
#ifdef DEBUG_STDOUT
	(void)printf("[%u] W @ %lu: %s\n", getpid(), (unsigned long)time(NULL), buf);
#endif
}

void
xfatal(const char *fmt, ...)
{
	va_list ap;
	char buf[2048];

	va_start(ap, fmt);
	(void)vsnprintf(buf, sizeof(buf), fmt, ap);
	va_end(ap);

	syslog(LOG_WARNING, "%s\n", buf);
#ifdef DEBUG_STDOUT
	(void)printf("[%u] F @ %lu: %s\n", getpid(), (unsigned long)time(NULL), buf);
#endif
}

void
xdie(const char *fmt, ...)
{
	va_list ap;
	char buf[2048];

	va_start(ap, fmt);
	(void)vsnprintf(buf, sizeof(buf), fmt, ap);
	va_end(ap);

	syslog(LOG_WARNING, "%s; terminating this process\n", buf);
#ifdef DEBUG_STDOUT
	(void)printf("[%u] X @ %lu: %s\n", getpid(), (unsigned long)time(NULL), buf);
#endif

	if (nsmc_ldap_cfgs != NULL)
		free_ldap_cfg();

	nsm_config_free();

	exit(EXIT_FAILURE);
}
