Main Page | Class List | File List | Class Members

E:/Projekte/Open Source/Unwritten/Unwritten C++/projects/Library/source/UnwrittenLogfile.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 <UnwrittenLogfile.hpp>
00021 #include <assert.h>
00022 #include <stdarg.h>
00023 
00024 UnwrittenLogfile::UnwrittenLogfile( const std::string & szFilename, bool bPrintMessageType )
00025 {
00026         // set the needed internal variables
00027         this->m_bDisposed = false;
00028         this->m_bWaitForNewLine = false;
00029         this->m_bPrintMessageType = bPrintMessageType;
00030         this->m_szLogfileName = szFilename;
00031         this->m_eLoggingType = UnwrittenDebug;
00032 
00033         // try to open the given file
00034         if( NULL == ( this->m_pFileHandle = fopen( szFilename.c_str(), "w+" ) ) )
00035                 throw new std::exception( std::string( "Cannot create the requested logfile '" + szFilename + "'. Maybe you have no rights to write to the given location!" ).c_str() );
00036 }
00037 
00038 UnwrittenLogfile::~UnwrittenLogfile()
00039 {
00040         // the dispose function should be called before calling
00041         // the destructor. If this is a debug bild, assert if 
00042         // if Dispose function was already called
00043         assert( true == this->m_bDisposed );
00044 
00045         // if the object is not dispoed at this time, call
00046         // the dispose event by myself ^^
00047         if( !this->m_bDisposed )
00048                 this->Dispose();
00049 }
00050 
00051 void UnwrittenLogfile::Dispose()
00052 {
00053         // if the logfile is opened, flush the buffer and close the file
00054         if( this->m_pFileHandle != NULL )
00055         {
00056                 fflush( this->m_pFileHandle );
00057                 fclose( this->m_pFileHandle );
00058                 this->m_pFileHandle = NULL;
00059         }
00060 
00061         // this object was disposed
00062         this->m_bDisposed = true;
00063 }
00064 
00065 UnwrittenLogfile::UnwrittenLoggingType UnwrittenLogfile::GetLoggingType()
00066 {
00067         return( this->m_eLoggingType );
00068 }
00069 
00070 void UnwrittenLogfile::SetLoggingType( UnwrittenLoggingType newLoggingType )
00071 {
00072         this->m_eLoggingType = newLoggingType;
00073 }
00074 
00075 void UnwrittenLogfile::PrintMessageType( UnwrittenLoggingType typeOfMessage )
00076 {
00077         switch( typeOfMessage )
00078         {
00079                 case UnwrittenDebug:
00080                         fprintf( this->m_pFileHandle, "[DEBUG]       " );
00081                         break;
00082                 case UnwrittenInformation:
00083                         fprintf( this->m_pFileHandle, "[INFORMATION] " );
00084                         break;
00085                 case UnwrittenWarning:
00086                         fprintf( this->m_pFileHandle, "[WARNING]     " );
00087                         break;
00088                 case UnwrittenError:
00089                         fprintf( this->m_pFileHandle, "[ERROR]       " );
00090                         break;
00091         }
00092 }
00093 
00094 void UnwrittenLogfile::Write( UnwrittenLoggingType typeOfMsg, const char * szText, ... )
00095 {
00096         // the needed va_list
00097         va_list ap;
00098 
00099         // check if the handle is not NULL
00100         assert( this->m_pFileHandle != NULL );
00101 
00102         // print the given text
00103         if( typeOfMsg >= this->m_eLoggingType )
00104         {
00105                 // print the message type (if the flag is set)
00106                 if( this->m_bPrintMessageType && !this->m_bWaitForNewLine )
00107                         this->PrintMessageType( typeOfMsg );
00108 
00109                 // print the message
00110                 va_start( ap, szText );
00111                 vfprintf( this->m_pFileHandle, szText, ap );
00112                 va_end( ap );
00113 
00114                 // say that we wait for a new line
00115                 this->m_bWaitForNewLine = true;
00116         }
00117 }
00118 
00119 void UnwrittenLogfile::WriteLine( UnwrittenLoggingType typeOfMsg, const char * szText, ... )
00120 {
00121         // the needed va_list
00122         va_list ap;
00123 
00124         // check if the handle is not NULL
00125         assert( this->m_pFileHandle != NULL );
00126 
00127         // print the given text with an tailing \n
00128         if( typeOfMsg >= this->m_eLoggingType )
00129         {
00130                 // print the message type (if the flag is set)
00131                 if( this->m_bPrintMessageType && !this->m_bWaitForNewLine )
00132                         this->PrintMessageType( typeOfMsg );
00133 
00134                 // print the message
00135                 va_start( ap, szText );
00136                 vfprintf( this->m_pFileHandle, szText, ap );
00137                 fprintf( this->m_pFileHandle, "\n" );
00138                 va_end( ap );
00139         }
00140 
00141         // a new line was written
00142         this->m_bWaitForNewLine = false;
00143 }
00144 
00145 void UnwrittenLogfile::Flush()
00146 {
00147         // check if the handle is not NULL
00148         assert( this->m_pFileHandle != NULL );
00149 
00150         // flush the buffer
00151         fflush( this->m_pFileHandle );
00152 }

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