/*
 * This source file is part of KEOS (Free 3D Engine)
 * For the latest info, see http://www.keosengine.org/
 * E-mails : thierry.vouriot@keosengine.org, yeri@keosengine.org
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 *
 * This program 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place - Suite 330, Boston, MA 02111-1307, USA, or go to
 * http://www.gnu.org/copyleft/lesser.txt.
 *
 */

#include "KeosLogger.h"
#include "KeosException.h"

namespace Keos
{

    //=======================================================================
    // ILogger implementation
    //=======================================================================

    ILogger* ILogger::ms_pInstance = NULL;

    //-----------------------------------------------------------------------
    ILogger::~ILogger()
    {

    }

    //-----------------------------------------------------------------------
    void ILogger::SetLogger(ILogger* pLogger)
    {
        Destroy();
        ms_pInstance = pLogger;
    }

    //-----------------------------------------------------------------------
    void ILogger::Destroy()
    {
        delete ms_pInstance;
        ms_pInstance = NULL;
    }

    //-----------------------------------------------------------------------
    void ILogger::Log(const char* strFormat, ...)
    {
        Assert(ms_pInstance != NULL);

        // Format message in a string
        char sBuffer[512];
        va_list Params;
        va_start(Params, strFormat);
        vsprintf(sBuffer, strFormat, Params);
        va_end(Params);

        // Add "\n"
        strcat(sBuffer, "\n");

        // Log
        ms_pInstance->Write(sBuffer);
    }

    //-----------------------------------------------------------------------
    String ILogger::CurrentDate() const
    {
        // Recovery and formatting of date
        char sTime[24];
        time_t CurrentTime = time(NULL);
        strftime(sTime, sizeof(sTime), "%d/%m/%Y", localtime(&CurrentTime));

        return sTime;
    }

    //-----------------------------------------------------------------------
    String ILogger::CurrentTime() const
    {
        // Recovery and formatting of time
        char sTime[24];
        time_t CurrentTime = time(NULL);
        strftime(sTime, sizeof(sTime), "%H:%M:%S", localtime(&CurrentTime));

        return sTime;
    }


    //=======================================================================
    // CLoggerFile implementation
    //=======================================================================
    
    //-----------------------------------------------------------------------
    CLoggerFile::CLoggerFile(const String& strFilename) :
    m_File(strFilename.c_str())
    {
        // The file is open ?
        if (!m_File)
            LOADINGFAILED_EXCEPT(strFilename, "Impossible to write in file", "CLoggerFile::CLoggerFile");

        // Write header
        m_File << "=======================================================" << std::endl;
        m_File << " Keos::Engine v" << KEOS_VERSION_MAJOR << "." << KEOS_VERSION_MINOR << 
            "." << KEOS_VERSION_PATCH << " - Event log - " << CurrentDate() << " " << CurrentTime() << std::endl;
        m_File << "=======================================================" << std::endl << std::endl;
    }

    //-----------------------------------------------------------------------
    CLoggerFile::~CLoggerFile()
    {
        // Close file
        m_File << std::endl;
        m_File << "=============================================" << std::endl;
        m_File << " Keos::Engine closed at " << CurrentTime() << " - Goodbye ! " << std::endl;
        m_File << "=============================================" << std::endl;
    }

    //-----------------------------------------------------------------------
    void CLoggerFile::Write(const String& strMessage)
    {
        Assert(m_File.is_open());

        m_File << CurrentTime() << ": " << strMessage;

        //@todo ne pas ecrire dans la fenetre MSDOS à partir du CLoggerFile
        printf("%s",strMessage.c_str());
    }

} // namespace Keos
