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

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(const String& filename, bool append /* = false */) :
00044         d_level(Standard),
00045         d_ostream(filename.c_str(), std::ios_base::out | (append ? std::ios_base::app : std::ios_base::trunc))
00046 {
00047         // initialise width for date & time alignment.
00048         d_ostream.width(2);
00049 
00050         // create log header
00051         logEvent((utf8*)"+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+");
00052         logEvent((utf8*)"+                     Crazy Eddie's GUI System - Event log                    +");
00053         logEvent((utf8*)"+                     (http://www.cegui.org.uk)                    +");
00054         logEvent((utf8*)"+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n");
00055         logEvent((utf8*)"CEGUI::Logger singleton created.");
00056 }
00057 
00058 /*************************************************************************
00059         Destructor
00060 *************************************************************************/
00061 Logger::~Logger(void)
00062 {
00063         if (d_ostream.is_open())
00064         {
00065                 logEvent((utf8*)"CEGUI::Logger singleton destroyed.");
00066                 d_ostream.close();
00067         }
00068 
00069 }
00070 
00071 /*************************************************************************
00072         Logs an event
00073 *************************************************************************/
00074 void Logger::logEvent(const String& message, LoggingLevel level /* = Standard */)
00075 {
00076         // only log the message if the current logging level is >= the level for the message
00077         if (d_level >= level)
00078         {
00079                 using namespace std;
00080 
00081                 time_t  et;
00082                 time(&et);
00083                 tm* etm = gmtime(&et);
00084 
00085                 if (etm != NULL)
00086                 {
00087                         // write date
00088                         d_ostream << setfill('0') << setw(2) << etm->tm_mday << '/' <<
00089                                 setfill('0') << setw(2) << 1 + etm->tm_mon << '/' <<
00090                                 setw(4) << (1900 + etm->tm_year) << ' ';
00091 
00092                         // wite time
00093                         d_ostream << setfill('0') << setw(2) << etm->tm_hour << ':' <<
00094                                 setfill('0') << setw(2) << etm->tm_min << ':' <<
00095                                 setfill('0') << setw(2) << etm->tm_sec << ' ';
00096 
00097                         // write event type code
00098                         switch(level)
00099                         {
00100                         case Errors:
00101                                 d_ostream << "(Error)\t";
00102                                 break;
00103 
00104                         case Standard:
00105                                 d_ostream << "(InfL1)\t";
00106                                 break;
00107 
00108                         case Informative:
00109                                 d_ostream << "(InfL2)\t";
00110                                 break;
00111 
00112                         case Insane:
00113                                 d_ostream << "(InfL3)\t";
00114                                 break;
00115 
00116                         default:
00117                                 d_ostream << "(Unkwn)\t";
00118                                 break;
00119                         }
00120 
00121                         // write message
00122                         d_ostream << message << endl;
00123                 }
00124 
00125                 // ensure new event is written to the file, rather than just being buffered.
00126                 d_ostream.flush();
00127         }
00128 }
00129 
00130 
00131 Logger& Logger::getSingleton(void)
00132 {
00133         return Singleton<Logger>::getSingleton();
00134 }
00135 
00136 } // End of  CEGUI namespace section

Generated on Wed Feb 16 12:41:06 2005 for Crazy Eddies GUI System by  doxygen 1.3.9.1