//------------------------------------------------------------------------------
// <copyright file="LqrSMACSysLogger.cpp" company="LiquidRobotics">
//
// Copyright (c) 2014 Liquid Robotics, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
// This software is associated with a release.  Liquid Robotics makes no
// representation that the design, interfaces or component distribution will
// remain unchanged in future releases.
//
// </copyright>
//
// <summary>
// </summary>
//
// <owner></owner>
//------------------------------------------------------------------------------

#include "LqrSMACSysLogger.h"
#include "LqrSMACException.h"
#include "LqrSMACPrintfBuffer.h"

using namespace std;
using namespace boost;

namespace LiquidRobotics {
namespace SMACFramework {

SysLogger::SysLogger(const char* ident_, LogType level_, int option_, int facility_)
: open(false)
, level(0)
{
	level = ToInt(level_);

	openlog(ident_ ? ident_ : "", option_, facility_);
	open = true;

	setlogmask(level);
}

SysLogger::~SysLogger()
{
	if (open)
		closelog();
}

void SysLogger::Write(const LogType logType, const char* data)
{
	if (!open) throw NotInitialized();

	reportSignal(*this, logType, data);

	int level = ToInt(logType);

	if (logPrefix.length())
		syslog(level, "%s: %s", logPrefix.c_str(), data);
	else
		syslog(level, "%s", data);
}

void SysLogger::Format(const LogType logType, const char* format, ...)
{
	if (!open) throw NotInitialized();

	va_list argptr;

	va_start(argptr, format);
	FormatV(logType, format, argptr);
	va_end(argptr);
}

void SysLogger::FormatV(const LogType logType, const char* format, va_list argptr)
{
	if (!open) throw NotInitialized();

	PrintfBuffer buffer(format, argptr);

	if (*buffer.AsPtr())
		Write(logType, buffer.AsPtr());
}

bool SysLogger::LogEnabled(const LogType logType) const
{
	return open;
}

int SysLogger::ToInt(const LogType level)
{
	int returnValue;

	switch(level)
	{
	case LT_EMERGENCY:	returnValue = LOG_EMERG;		break;
	case LT_ALERT:		returnValue = LOG_ALERT;		break;
	case LT_CRITICAL:	returnValue = LOG_CRIT;			break;
	case LT_ERROR:		returnValue = LOG_ERR;			break;
	case LT_WARNING:	returnValue = LOG_WARNING;		break;
	case LT_NOTICE:		returnValue = LOG_NOTICE;		break;
	case LT_INFO:		returnValue = LOG_INFO;			break;
	case LT_DEBUG:		returnValue = LOG_DEBUG;		break;
	default:			throw InvalidArgument("level");
	}

	return returnValue;
}

}
}
