/***************************************************************************
                          SIMPLY POWERFUL TOOLKIT (SPTK)
                          CFileLog.cpp  -  description
                             -------------------
    begin                : Tue Jan 31 2006
    copyright            : (C) 2001-2006 by Alexey Parshin

    This module creation was sponsored by Total Knowledge (http://www.total-knowledge.com).
    Author thanks the developers of CPPSERV project (http://www.total-knowledge.com/progs/cppserv)
    for defining the requirements for this class.
 ***************************************************************************/

/***************************************************************************
   This library is free software; you can redistribute it and/or modify it 
   under the terms of the GNU Library General Public License as published by
   the Free Software Foundation; either version 2 of the License, or (at 
   your option) any later version.

   This library 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 Library
   General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; if not, write to the Free Software Foundation, 
   Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.

   Please report all bugs and problems to "alexeyp@gmail.com"
 ***************************************************************************/

#include <sptk3/CFileLog.h>

using namespace std;
using namespace sptk;

void CFileLog::saveMessage(CDateTime date,const char *message,unsigned sz,const CLogPriority *priority) throw(CException) {
   if (!m_fileStream.is_open()) {
      m_fileStream.open(m_fileName.c_str(),ofstream::out|ofstream::app);
      if (!m_fileStream.is_open()) 
         throw CException("Can't append or create log file '"+m_fileName+"'",__FILE__,__LINE__);
   }
   m_fileStream << date.dateString() << " ";
   m_fileStream << date.timeString(true);
   m_fileStream << " [" << priority->name() << "] ";
   m_fileStream << message << endl;
   if (m_fileStream.bad())
      throw CException("Can't write to log file '"+m_fileName+"'",__FILE__,__LINE__);
}

CFileLog::~CFileLog() {
   m_buffer.flush();
   if (m_fileStream.is_open())
      m_fileStream.close();
}

void CFileLog::reset() throw(CException) {
   if (m_fileStream.is_open())
      m_fileStream.close();
   m_fileStream.open(m_fileName.c_str(),ofstream::out|ofstream::trunc);
   if (!m_fileStream.is_open()) 
      throw CException("Can't open log file '"+m_fileName+"'",__FILE__,__LINE__);
}
