Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

CEGUILogger.cpp

Go to the documentation of this file.
00001 /************************************************************************
00002     filename:   CEGUILogger.cpp
00003     created:    21/2/2004
00004     author:     Paul D Turner
00005     
00006     purpose:    Implementation of the Logger class
00007 *************************************************************************/
00008 /*************************************************************************
00009     Crazy Eddie's GUI System (http://www.cegui.org.uk)
00010     Copyright (C)2004 - 2005 Paul D Turner (paul@cegui.org.uk)
00011  
00012     This library is free software; you can redistribute it and/or
00013     modify it under the terms of the GNU Lesser General Public
00014     License as published by the Free Software Foundation; either
00015     version 2.1 of the License, or (at your option) any later version.
00016  
00017     This library is distributed in the hope that it will be useful,
00018     but WITHOUT ANY WARRANTY; without even the implied warranty of
00019     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00020     Lesser General Public License for more details.
00021  
00022     You should have received a copy of the GNU Lesser General Public
00023     License along with this library; if not, write to the Free Software
00024     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00025 *************************************************************************/
00026 #include "CEGUILogger.h"
00027 #include <ctime>
00028 #include <iomanip>
00029 
00030 // Start of CEGUI namespace section
00031 namespace CEGUI
00032 {
00033     /*************************************************************************
00034         Static Data Definitions
00035     *************************************************************************/
00036     // singleton instance pointer
00037     template<> Logger* Singleton<Logger>::ms_Singleton  = NULL;
00038 
00039 
00040     /*************************************************************************
00041         Constructor
00042     *************************************************************************/
00043     Logger::Logger(void) :
00044             d_level(Standard),
00045             d_caching(true)
00046     {
00047         // create log header
00048         logEvent("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+");
00049         logEvent("+                     Crazy Eddie's GUI System - Event log                    +");
00050         logEvent("+                          (http://www.cegui.org.uk/)                         +");
00051         logEvent("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n");
00052         logEvent("CEGUI::Logger singleton created.");
00053     }
00054 
00055     /*************************************************************************
00056         Destructor
00057     *************************************************************************/
00058     Logger::~Logger(void)
00059     {
00060         if (d_ostream.is_open())
00061         {
00062             logEvent((utf8*)"CEGUI::Logger singleton destroyed.");
00063             d_ostream.close();
00064         }
00065 
00066     }
00067 
00068     /*************************************************************************
00069         Logs an event
00070     *************************************************************************/
00071     void Logger::logEvent(const String& message, LoggingLevel level /* = Standard */)
00072     {
00073         using namespace std;
00074 
00075         time_t  et;
00076         time(&et);
00077         tm* etm = localtime(&et);
00078 
00079         if (etm != NULL)
00080         {
00081             // clear sting stream
00082             d_workstream.str("");
00083 
00084             // write date
00085             d_workstream << setfill('0') << setw(2) << etm->tm_mday << '/' <<
00086             setfill('0') << setw(2) << 1 + etm->tm_mon << '/' <<
00087             setw(4) << (1900 + etm->tm_year) << ' ';
00088 
00089             // wite time
00090             d_workstream << setfill('0') << setw(2) << etm->tm_hour << ':' <<
00091             setfill('0') << setw(2) << etm->tm_min << ':' <<
00092             setfill('0') << setw(2) << etm->tm_sec << ' ';
00093 
00094             // write event type code
00095             switch(level)
00096             {
00097             case Errors:
00098                 d_workstream << "(Error)\t";
00099                 break;
00100 
00101             case Standard:
00102                 d_workstream << "(InfL1)\t";
00103                 break;
00104 
00105             case Informative:
00106                 d_workstream << "(InfL2)\t";
00107                 break;
00108 
00109             case Insane:
00110                 d_workstream << "(InfL3)\t";
00111                 break;
00112 
00113             default:
00114                 d_workstream << "(Unkwn)\t";
00115                 break;
00116             }
00117 
00118             d_workstream << message << endl;
00119 
00120             if (d_caching)
00121             {
00122                 d_cache.push_back(std::make_pair(d_workstream.str(), level));
00123             }
00124             else if (d_level >= level)
00125             {
00126                 // write message
00127                 d_ostream << d_workstream.str();
00128                 // ensure new event is written to the file, rather than just being buffered.
00129                 d_ostream.flush();
00130             }
00131         }
00132     }
00133 
00134     void Logger::setLogFilename(const String& filename, bool append)
00135     {
00136         // close current log file (if any)
00137         if (d_ostream.is_open())
00138         {
00139             d_ostream.close();
00140         }
00141 
00142         d_ostream.open(filename.c_str(), std::ios_base::out | (append ? std::ios_base::app : std::ios_base::trunc));
00143 
00144         if (!d_ostream)
00145         {
00146             throw "Logger::setLogFilename - Failed to open file.";
00147         }
00148         
00149         // initialise width for date & time alignment.
00150         d_ostream.width(2);
00151 
00152         // write out cahced log strings.
00153         if (d_caching)
00154         {
00155             d_caching = false;
00156 
00157             std::vector<std::pair<String, LoggingLevel> >::iterator iter = d_cache.begin();
00158 
00159             while (iter != d_cache.end())
00160             {
00161                 if (d_level >= (*iter).second)
00162                 {
00163                     // write message
00164                     d_ostream << (*iter).first;
00165                     // ensure new event is written to the file, rather than just being buffered.
00166                     d_ostream.flush();
00167                 }
00168 
00169                 ++iter;
00170             }
00171 
00172             d_cache.clear();
00173         }
00174     }
00175 
00176 
00177     Logger& Logger::getSingleton(void)
00178     {
00179         return Singleton<Logger>::getSingleton();
00180     }
00181 
00182 } // End of  CEGUI namespace section

Generated on Wed Sep 7 09:56:32 2005 for Crazy Eddies GUI System by  doxygen 1.4.3