/*********************************************************************
 * This file is part of the cpplibs suite.
 *
 * Copyright (C) 2001 Topi Mäenpää
 * All rights reserved.
 *
 * This program is free software. You can redistribute and/or modify
 * it under the terms of the free software licence found in the
 * accompanying file "COPYING". The licence terms must always be
 * redistributed with this source file. The above copyright notice
 * must be reproduced in all modified and unmodified copies of this
 * source file.
 *
 * $Revision: 1.6 $
 *********************************************************************/

#include "LogFile.h"
#include "IO.h"
#include "../Util.h"

#include <iostream>

using namespace std;

namespace util { namespace io {
	LogFile::LogFile(char* fileName)
	{
		logFile.open(fileName,ios::app);
		if (!logFile)
			throw IOException("Init: cannot open file for appending");
	}

	LogFile::~LogFile()
	{
		logFile.close();
	}

	void LogFile::log(const string& msg)
	{
		char *buf = Util::getData(msg);
		log(buf);
		delete[] buf;
	}
	
	void LogFile::log(const char* msg)
	{
		logMutex.lock();
		char bfr[26];
		time_t clock;
		time(&clock);
#if sun && __GNUC__ < 3
		ctime_r(&clock,bfr,26);
#else
		ctime_r(&clock,bfr);
#endif
		bfr[24] = 0;
		logFile << "[" << bfr << "] " << msg << "\n";
		logFile.flush();
		logMutex.unlock();
	}	
}}
