syslogger.cpp 948 Bytes
Newer Older
Swann Perarnau's avatar
Swann Perarnau committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
#include "syslogger.hpp"
#include <cassert>
#include "utils.hpp"
#include "trace.hpp"

Syslogger::Syslogger(uid_t ruid, Log_type log_type, bool log_to_stderr):
	ILogger(ruid, log_type)
{
	int options = LOG_CONS | LOG_PID | LOG_ODELAY;
	if(log_to_stderr)
		options |= LOG_PERROR;
	openlog(APP_NAME, options, LOG_LOCAL0);


	TRACE_LOC_ON_MASK(LOGGING_MASK, 
			"Built Syslogger with log_type = %s\n",
			log_type_to_str(log_type).c_str());
}

Syslogger::~Syslogger()
{
	closelog();
}

int Syslogger::log_type_to_priority(Log_type type)
{
	switch(type)
	{
		case LOG_TYPE_INFO: return LOG_INFO;
		case LOG_TYPE_WARNING: return LOG_WARNING;
		case LOG_TYPE_ERROR: return LOG_ERR;
		case LOG_TYPE_DEBUG: return LOG_DEBUG;
		default:
			 assert(false);
	}
}

void Syslogger::log(Log_type type, const string& message, int errnum)
{
	string msg_out;
	if(prepare_log(type, message, msg_out, errnum))
		syslog(log_type_to_priority(type), msg_out.c_str());
}