Main Page | Class List | File List | Class Members

E:/Projekte/Open Source/Unwritten/Unwritten C++/projects/Library/source/UnwrittenLogfileManager.cpp

00001 /*
00002  *  Unwritten - A distrubuted logging engine
00003  *  Copyright © 2005 by "The Unwritten Team"
00004  *
00005  *  This library is free software; you can redistribute it and/or
00006  *  modify it under the terms of the GNU Lesser General Public
00007  *  License as published by the Free Software Foundation; either
00008  *  version 2.1 of the License, or (at your option) any later version.
00009  *
00010  *  This library is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  *  Lesser General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU Lesser General Public
00016  *  License along with this library; if not, write to the Free Software
00017  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018  **/
00019 
00020 #include <UnwrittenLogfileManager.hpp>
00021 #include <assert.h>
00022 
00023 //
00024 // the one and only UnwrittenLogfileManager object
00025 //
00026 UnwrittenLogfileManager * UnwrittenLogfileManager::m_pInstance = NULL;
00027 
00028 UnwrittenLogfileManager::UnwrittenLogfileManager()
00029 {
00030 }
00031 
00032 UnwrittenLogfileManager::~UnwrittenLogfileManager()
00033 {
00034 }
00035 
00036 UnwrittenLogfileManager * UnwrittenLogfileManager::GetInstance()
00037 {
00038         if( UnwrittenLogfileManager::m_pInstance == NULL )
00039                 UnwrittenLogfileManager::m_pInstance = new UnwrittenLogfileManager();
00040         return( UnwrittenLogfileManager::m_pInstance );
00041 }
00042 
00043 bool UnwrittenLogfileManager::ShouldPrintMessageType()
00044 {
00045         return( this->m_bPrintMessageType );
00046 }
00047 
00048 void UnwrittenLogfileManager::ShouldPrintMessageType( bool bShouldPrint )
00049 {
00050         this->m_bPrintMessageType = bShouldPrint;
00051 }
00052 
00053 void UnwrittenLogfileManager::CloseLogfile( const std::string & szFilename )
00054 {
00055         // if there is no corresponding logfile in the managed list, just return
00056         if( this->m_pLogfiles.find( szFilename ) == this->m_pLogfiles.end() )
00057                 return;
00058 
00059         // get an iterator for the given logging object
00060         std::map< std::string, UnwrittenLogfile * >::const_iterator i = this->m_pLogfiles.find( szFilename );
00061 
00062         // cast the second part of this pair, into an CLogFile class
00063         UnwrittenLogfile * pLogfile = static_cast< UnwrittenLogfile * >( i->second );
00064 
00065         // call the disposing function and destroy the object
00066         pLogfile->Dispose();
00067         delete( pLogfile );
00068 
00069         // remove the object from our managing list
00070         this->m_pLogfiles.erase( szFilename );
00071 }
00072 
00073 void UnwrittenLogfileManager::CloseLogfile( const UnwrittenLogfile * pLogfile )
00074 {
00075         // just call the same function for filenames
00076         this->CloseLogfile( pLogfile->m_szLogfileName );
00077 }
00078 
00079 void UnwrittenLogfileManager::SetDefaultLoggingType( UnwrittenLogfile::UnwrittenLoggingType newDefaultType )
00080 {
00081         // set the new default logging type
00082         this->m_eDefaultLoggingType = newDefaultType;
00083 }
00084 
00085 UnwrittenLogfile::UnwrittenLoggingType UnwrittenLogfileManager::GetDefaultLoggingType()
00086 {
00087         // get the current default logging type
00088         return( this->m_eDefaultLoggingType );
00089 }
00090 
00091 UnwrittenLogfile * UnwrittenLogfileManager::GetLogfile( const std::string & szFilename )
00092 {
00093         // if we have already a logfile which is managed, return it
00094         if( this->m_pLogfiles.find( szFilename ) != this->m_pLogfiles.end() )
00095         {
00096                 // get an iterator for the given logging object
00097                 std::map< std::string, UnwrittenLogfile * >::const_iterator i = this->m_pLogfiles.find( szFilename );
00098 
00099                 // cast the second part of this pair, into an CLogFile class
00100                 UnwrittenLogfile * pLogfile = static_cast< UnwrittenLogfile * >( i->second );
00101 
00102                 // return the object
00103                 return( pLogfile );
00104         }
00105 
00106         // else, we must create a new logfile, add it to our manager, and return it
00107         
00108         // create it and set the logging type to the default value
00109         UnwrittenLogfile * pLogfile = new UnwrittenLogfile( szFilename, this->m_bPrintMessageType );
00110         pLogfile->SetLoggingType( this->m_eDefaultLoggingType );
00111 
00112         // add it
00113         this->m_pLogfiles[ szFilename ] = pLogfile;
00114 
00115         // return it
00116         return( pLogfile );
00117 }
00118 
00119 void UnwrittenLogfileManager::WriteToAllFiles( UnwrittenLogfile::UnwrittenLoggingType typeOfMsg, const std::string & szText, bool bNewLine )
00120 {
00121         // get the first iterator
00122         std::map< std::string, UnwrittenLogfile * >::const_iterator i = this->m_pLogfiles.begin();
00123 
00124         // iterate through all logfiles
00125         while( i != this->m_pLogfiles.end() )
00126         {
00127                 // cast the pointer
00128                 UnwrittenLogfile * pCurrentLogfile = static_cast< UnwrittenLogfile * >( i->second );
00129 
00130                 // if a new line should be set, use the WriteLine function
00131                 if( bNewLine )
00132                         pCurrentLogfile->WriteLine( typeOfMsg, szText.c_str() );
00133 
00134                 // else use the Write function
00135                 else
00136                         pCurrentLogfile->Write( typeOfMsg, szText.c_str() );
00137 
00138                 // next logfile
00139                 ++i;
00140         }
00141 }
00142 
00143 void UnwrittenLogfileManager::FlushAllFiles()
00144 {
00145         // get the first iterator
00146         std::map< std::string, UnwrittenLogfile * >::const_iterator i = this->m_pLogfiles.begin();
00147 
00148         // iterate through all logfiles
00149         while( i != this->m_pLogfiles.end() )
00150         {
00151                 // cast the pointer
00152                 UnwrittenLogfile * pCurrentLogfile = static_cast< UnwrittenLogfile * >( i->second );
00153 
00154 #if defined(_DEBUG)
00155                 // if this is a debug build, write a short sync message
00156                 pCurrentLogfile->WriteLine( UnwrittenLogfile::UnwrittenDebug, "UnwrittenLogfileManager Synchronization Event: Flushing all logfiles!" );
00157 #endif
00158 
00159                 // flush the file
00160                 pCurrentLogfile->Flush();
00161 
00162 
00163                 // next logfile
00164                 ++i;
00165         }
00166 }
00167 
00168 size_t UnwrittenLogfileManager::GetManagedLogfiles()
00169 {
00170         return( this->m_pLogfiles.size() );
00171 }

Generated on Sun Feb 27 19:49:00 2005 for Unwritten - Independet Logging Library by  doxygen 1.4.1