/*
** SellaNMS
** $Id: syslog.cxx,v 1.2 2005/08/09 05:37:02 sella Exp $
** Copyright (c) 2001-2005 Digital Genesis Software, LLC. All Rights Reserved.
** Released under the GPL Version 2 License.
** http://www.digitalgenesis.com
*/

static char rcsid[] = "$Id: syslog.cxx,v 1.2 2005/08/09 05:37:02 sella Exp $";

#include "syslog.h"
#include "daemon.h"
#include "common/storagedata.h"
#include "storage/storage.h"
#include <stdio.h>
#include <unistd.h>
#include <sched.h>
#include <sys/stat.h>
#include <stl/slist>
#include <stl/string>
#include <stl/vector>

extern ConfigNode* config;
extern bool interrupt;

Syslog::Syslog() {
}

Syslog::~Syslog() {
}

RESULT Syslog::execute() {
	RESULT result = SUCCESS;

	time_t now, next_output = 0;

   TRACE(TRACE_INTERNAL, "Syslog::execute() starting");

	while (!interrupt) {
      now = time(NULL);

		if (now >= next_output) {
			next_output = now + interval;
		}

		usleep(USLEEP_LONGLOOP_TIME);
	}

	TRACE(TRACE_THREAD, "Syslog::execute() exiting");

	return result;
}

RESULT Syslog::readConfig() {
	RESULT result = SUCCESS;

	ConfigNode node;

	if (!config->assignNode("output syslog", node)) {
		LMSG("No 'output syslog' section found within the config file.");

		return FAILURE;
	}

	node.assignValue("interval", interval, OUTPUT_SYSLOG_INTERVAL_DEF, OUTPUT_SYSLOG_INTERVAL_MIN, OUTPUT_SYSLOG_INTERVAL_MAX);

	/* Configure Storage */
	if (storage.setConfig(*config) != SUCCESS) {
		result = FAILURE;
	}

	printConfig();

	return result;
}

void Syslog::printConfig() {
	string str;
	vector<string> v;
	vector<string>::iterator v_it;

	TRACE(TRACE_CONFIG, "interval: %lu", interval);

	if (config->assignValues("policy input", v)) {
		str.erase();
		for (v_it = v.begin(); v_it != v.end(); ++v_it) {
			str.append(*v_it + " ");
		}
		TRACE(TRACE_CONFIG, "policy input: %s", str.c_str());
	} else {
		TRACE(TRACE_CONFIG, "policy input: none");
	}

	if (config->assignValues("policy output", v)) {
		str.erase();
		for (v_it = v.begin(); v_it != v.end(); ++v_it) {
			str.append(*v_it + " ");
		}
		TRACE(TRACE_CONFIG, "policy output: %s", str.c_str());
	} else {
		TRACE(TRACE_CONFIG, "policy output: none");
	}
}

#if 0
RESULT Syslog::xxx() {
	RESULT result = SUCCESS;

	StorageData data;

	return result;
}
#endif

/*
** Local Variables:
** c-basic-offset: 3
** tab-width: 3
** End:
** vim600: fdm=marker
** vim: noet ts=3 sw=3
*/
