KeosLogger.cpp

Go to the documentation of this file.
00001 /*
00002  * This source file is part of KEOS (Free 3D Engine)
00003  * For the latest info, see http://www.keosengine.org/
00004  * E-mails : thierry.vouriot@keosengine.org, yeri@keosengine.org
00005  *
00006  * This program is free software; you can redistribute it and/or modify it under
00007  * the terms of the GNU Lesser General Public License as published by the Free Software
00008  * Foundation; either version 2 of the License, or (at your option) any later
00009  * version.
00010  *
00011  * This program is distributed in the hope that it will be useful, but WITHOUT
00012  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00013  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU Lesser General Public License along with
00016  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
00017  * Place - Suite 330, Boston, MA 02111-1307, USA, or go to
00018  * http://www.gnu.org/copyleft/lesser.txt.
00019  *
00020  */
00021 
00022 #include "KeosLogger.h"
00023 #include "KeosException.h"
00024 
00025 namespace Keos
00026 {
00027 
00028   //=======================================================================
00029   // ILogger implementation
00030   //=======================================================================
00031 
00032   ILogger* ILogger::ms_pInstance = NULL;
00033 
00034   //-----------------------------------------------------------------------
00035   ILogger::~ILogger()
00036   {}
00037 
00038   //-----------------------------------------------------------------------
00039   void ILogger::SetLogger(ILogger* pLogger)
00040   {
00041     Destroy();
00042     ms_pInstance = pLogger;
00043   }
00044 
00045   //-----------------------------------------------------------------------
00046   void ILogger::Destroy()
00047   {
00048     delete ms_pInstance;
00049     ms_pInstance = NULL;
00050   }
00051 
00052   //-----------------------------------------------------------------------
00053   void ILogger::Log(const char* strFormat, ...)
00054   {
00055     Assert(ms_pInstance != NULL);
00056 
00057     // Format message in a string
00058     char sBuffer[512];
00059     va_list Params;
00060     va_start(Params, strFormat);
00061     vsprintf(sBuffer, strFormat, Params);
00062     va_end(Params);
00063 
00064     // Add "\n"
00065     strcat(sBuffer, "\n");
00066 
00067     // Log
00068     ms_pInstance->Write(sBuffer);
00069   }
00070 
00071   //-----------------------------------------------------------------------
00072   String ILogger::CurrentDate() const
00073   {
00074     // Recovery and formatting of date
00075     char sTime[24];
00076     time_t CurrentTime = time(NULL);
00077     strftime(sTime, sizeof(sTime), "%d/%m/%Y", localtime(&CurrentTime));
00078 
00079     return sTime;
00080   }
00081 
00082   //-----------------------------------------------------------------------
00083   String ILogger::CurrentTime() const
00084   {
00085     // Recovery and formatting of time
00086     char sTime[24];
00087     time_t CurrentTime = time(NULL);
00088     strftime(sTime, sizeof(sTime), "%H:%M:%S", localtime(&CurrentTime));
00089 
00090     return sTime;
00091   }
00092 
00093 
00094   //=======================================================================
00095   // CLoggerFile implementation
00096   //=======================================================================
00097 
00098   //-----------------------------------------------------------------------
00099   CLoggerFile::CLoggerFile(const String& strFilename) :
00100       m_File(strFilename.c_str())
00101   {
00102     // The file is open ?
00103     if (!m_File)
00104       LOADINGFAILED_EXCEPT(strFilename, "Impossible to write in file", "CLoggerFile::CLoggerFile");
00105 
00106     // Write header
00107     m_File << "=======================================================" << std::endl;
00108     m_File << " Keos::Engine v" << KEOS_VERSION_MAJOR << "." << KEOS_VERSION_MINOR <<
00109     "." << KEOS_VERSION_PATCH << " - Event log - " << CurrentDate() << " " << CurrentTime() << std::endl;
00110     m_File << "=======================================================" << std::endl << std::endl;
00111   }
00112 
00113   //-----------------------------------------------------------------------
00114   CLoggerFile::~CLoggerFile()
00115   {
00116     // Close file
00117     m_File << std::endl;
00118     m_File << "=============================================" << std::endl;
00119     m_File << " Keos::Engine closed at " << CurrentTime() << " - Goodbye ! " << std::endl;
00120     m_File << "=============================================" << std::endl;
00121   }
00122 
00123   //-----------------------------------------------------------------------
00124   void CLoggerFile::Write(const String& strMessage)
00125   {
00126     Assert(m_File.is_open());
00127 
00128     m_File << CurrentTime() << ": " << strMessage;
00129 
00131     printf("%s", strMessage.c_str());
00132   }
00133 
00134 } // namespace Keos

Generated on Fri Mar 9 14:29:02 2007 for Keos by  doxygen 1.5.1-p1