/*
	ZazouMiniWebServer

	Copyright (C) 2003-2011 Xavier Garreau <xavier@xgarreau.org>

	This file is part of ZazouMiniWebServer.

    ZazouMiniWebServer 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.

    ZazouMiniWebServer 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 ZazouMiniWebServer; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
*/

#include "stdafx.h"
#include <iostream>

#include "ZMWSLogger.h"

void ZMWSLoggerThread::handle_request(ZRequest* r)
{
	ZMWSLoggerRequest* lr = static_cast<ZMWSLoggerRequest*>(r);
	std::ofstream ofs;
	do {
		ofs.open(lr->logFile.c_str(), std::ios::app);
	} while (!ofs.is_open());

	ofs << lr->logLine;
	ofs.close();
	delete lr;
}

ZMWSLogger::ZMWSLogger()
{
	ztp = new ZThreadPool<ZMWSLoggerThread>(1);
}

ZMWSLogger::~ZMWSLogger()
{
	delete ztp;
}

void ZMWSLogger::LogIt(const std::string& file, const std::string line)
{
	static ZMWSLogger* logger = NULL;

	if (!logger) {
		logger = new ZMWSLogger();
	}
	logger->ztp->request_add(new ZMWSLoggerRequest(file, line));
}
