/*
** LOG-SYSLOG: Syslog based message logging module
** Copyright (C) 2002 Michael W. Shaffer <mwshaffer@angrypot.com>
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** 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 (see the file COPYING). If not, write to:
**
** The Free Software Foundation, Inc.
** 59 Temple Place, Suite 330,
** Boston, MA  02111-1307  USA
*/

#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <syslog.h>
#include "conf.h"
#include "log.h"

static int log_level_map[] = {
	LOG_INFO,
	LOG_DEBUG,
	LOG_INFO,
	LOG_WARNING,
	LOG_CRIT,
	LOG_ALERT
};

struct globals {
	int log_level;
};

static struct globals g;

void log_init_syslog (void)
{
	openlog (get_config_string ("log-ident"), LOG_PID, LOG_DAEMON);
	log_set_level_syslog ((int) get_config_long ("log-level"));
	return;
}

void log_free_syslog (void)
{
	g.log_level = all;
	closelog ();
	return;
}

void log_set_level_syslog (int level)
{
	g.log_level = level;
	if (g.log_level < all) g.log_level = all;
	if (g.log_level > fatal) g.log_level = fatal;

	return;
}

void log_msg_syslog (int pri, char *msg)
{
	int pos = 0;
	char mbuf[256];

	if ((!msg) || (pri < g.log_level))
		goto EXIT;

	if (pri < all) pri = all;
	if (pri > fatal) pri = fatal;

	memset (mbuf, 0, sizeof (mbuf));
	strncpy (mbuf, msg, (sizeof (mbuf) - 1));
	pos = strlen (mbuf) - 1;
	while (pos >= 0) {
		if ((mbuf[pos] == '\n') ||
			(mbuf[pos] == '\r') ||
			(mbuf[pos] == '\t')) {
			mbuf[pos] = ' ';
		}
		pos--;
	}

	syslog (log_level_map[pri], "%s", msg);

EXIT:
	return;
}



syntax highlighted by Code2HTML, v. 0.9.1