/**********************************************************************
 * LeechCraft - modular cross-platform feature rich internet client.
 * Copyright (C) 2006-2014  Georg Rudoy
 *
 * Boost Software License - Version 1.0 - August 17th, 2003
 *
 * Permission is hereby granted, free of charge, to any person or organization
 * obtaining a copy of the software and accompanying documentation covered by
 * this license (the "Software") to use, reproduce, display, distribute,
 * execute, and transmit the Software, and to prepare derivative works of the
 * Software, and to permit third-parties to whom the Software is furnished to
 * do so, all subject to the following:
 *
 * The copyright notices in the Software and this entire statement, including
 * the above license grant, this restriction and the following disclaimer,
 * must be included in all copies of the Software, in whole or in part, and
 * all derivative works of the Software, unless such copies or derivative
 * works are solely in the form of machine-executable object code generated by
 * a source language processor.
 *
 * 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, TITLE AND NON-INFRINGEMENT. IN NO EVENT
 * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
 * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 **********************************************************************/

#include "logger.h"
#include <QDateTime>
#include <QUrl>
#include <QDir>
#include <QtDebug>
#include <util/sll/serializejson.h>
#include <util/sys/paths.h>

namespace LC
{
namespace Azoth
{
namespace Murm
{
	Logger::LogProxy::LogProxy (Logger& l, IHaveConsole::PacketDirection dir)
	: L_ (l)
	, Dir_ { dir }
	, File_ { new QFile { l.Filename_ } }
	{
		if (L_.FileEnabled_)
		{
			if (!File_->open (QIODevice::WriteOnly | QIODevice::Append))
				qWarning () << Q_FUNC_INFO
						<< "cannot open log file"
						<< File_->fileName ()
						<< File_->errorString ();
			File_->write ("[" + QDateTime::currentDateTime ().toString (Qt::ISODate).toUtf8 () + "] ");
		}
	}

	Logger::LogProxy::~LogProxy ()
	{
		if (!File_)
			return;

		L_.gotConsolePacket (CurrentString_, Dir_, {});
		WriteImpl ("\n");
	}

	void Logger::LogProxy::Write (const char *msg)
	{
		WriteImpl (msg);
	}

	void Logger::LogProxy::Write (const QString& str)
	{
		WriteImpl (str.toUtf8 ());
	}

	void Logger::LogProxy::Write (qint64 num)
	{
		WriteImpl (QByteArray::number (num));
	}

	void Logger::LogProxy::Write (const QUrl& url)
	{
		WriteImpl (url.toEncoded ());
	}

	void Logger::LogProxy::WriteImpl (const QByteArray& ba)
	{
		CurrentString_ += ba;

		if (L_.FileEnabled_)
			File_->write (ba);
	}

	void Logger::LogProxy::Write (const QVariant& json)
	{
		WriteImpl (Util::SerializeJson (json, false));
	}

	Logger::Logger (const QString& id, QObject *parent)
	: QObject { parent }
	, Filename_ { Util::CreateIfNotExists ("azoth/murm").absoluteFilePath (id) + ".log" }
	{
	}

	void Logger::SetFileEnabled (bool enabled)
	{
		FileEnabled_ = enabled;
	}
}
}
}
