OgreLog.h

Go to the documentation of this file.
00001 /*
00002 -----------------------------------------------------------------------------
00003 This source file is part of OGRE
00004     (Object-oriented Graphics Rendering Engine)
00005 For the latest info, see http://www.ogre3d.org/
00006 
00007 Copyright (c) 2000-2006 Torus Knot Software Ltd
00008 Also see acknowledgements in Readme.html
00009 
00010 This program is free software; you can redistribute it and/or modify it under
00011 the terms of the GNU Lesser General Public License as published by the Free Software
00012 Foundation; either version 2 of the License, or (at your option) any later
00013 version.
00014 
00015 This program is distributed in the hope that it will be useful, but WITHOUT
00016 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00017 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
00018 
00019 You should have received a copy of the GNU Lesser General Public License along with
00020 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
00021 Place - Suite 330, Boston, MA 02111-1307, USA, or go to
00022 http://www.gnu.org/copyleft/lesser.txt.
00023 
00024 You may alternatively use this source under the terms of a specific version of
00025 the OGRE Unrestricted License provided you have obtained such a license from
00026 Torus Knot Software Ltd.
00027 -----------------------------------------------------------------------------
00028 */
00029 
00030 #ifndef __Log_H__
00031 #define __Log_H__
00032 
00033 #include "OgrePrerequisites.h"
00034 #include "OgreString.h"
00035 
00036 namespace Ogre {
00037 
00038     // LogMessageLevel + LoggingLevel > OGRE_LOG_THRESHOLD = message logged
00039     #define OGRE_LOG_THRESHOLD 4
00040 
00043     enum LoggingLevel
00044     {
00045         LL_LOW = 1,
00046         LL_NORMAL = 2,
00047         LL_BOREME = 3
00048     };
00049 
00052     enum LogMessageLevel
00053     {
00054         LML_TRIVIAL = 1,
00055         LML_NORMAL = 2,
00056         LML_CRITICAL = 3
00057     };
00058 
00060     class LogListener
00061     {
00062     public:
00063         virtual ~LogListener() {};
00064 
00077         virtual void messageLogged( const String& message, LogMessageLevel lml, bool maskDebug, const String &logName ) = 0;
00078     };
00079 
00080 
00087     class _OgreExport Log : public LogAlloc
00088     {
00089     protected:
00090         std::ofstream   mfpLog;
00091         LoggingLevel    mLogLevel;
00092         bool            mDebugOut;
00093         bool            mSuppressFile;
00094         String          mLogName;
00095 
00096         typedef std::vector<LogListener*> mtLogListener;
00097         mtLogListener mListeners;
00098 
00099     public:
00100 
00101         class Stream;
00102 
00103         OGRE_AUTO_MUTEX // public to allow external locking
00108         Log( const String& name, bool debugOutput = true, bool suppressFileOutput = false);
00109 
00114         ~Log();
00115 
00117         const String& getName() const { return mLogName; }
00119         bool isDebugOutputEnabled() const { return mDebugOut; }
00121         bool isFileOutputSuppressed() const { return mSuppressFile; }
00122 
00126         void logMessage( const String& message, LogMessageLevel lml = LML_NORMAL, bool maskDebug = false );
00127 
00129         Stream stream(LogMessageLevel lml = LML_NORMAL, bool maskDebug = false);
00130 
00135         void setDebugOutputEnabled(bool debugOutput);
00140         void setLogDetail(LoggingLevel ll);
00143         LoggingLevel getLogDetail() const { return mLogLevel; }
00150         void addListener(LogListener* listener);
00151 
00158         void removeListener(LogListener* listener);
00159 
00178         class Stream
00179         {
00180         protected:
00181             Log* mTarget;
00182             LogMessageLevel mLevel;
00183             bool mMaskDebug;
00184             typedef StringUtil::StrStreamType BaseStream;
00185             BaseStream mCache;
00186 
00187         public:
00188 
00190             struct Flush {};
00191 
00192             Stream(Log* target, LogMessageLevel lml, bool maskDebug)
00193                 :mTarget(target), mLevel(lml), mMaskDebug(maskDebug)
00194             {
00195 
00196             }
00197             // copy constructor
00198             Stream(const Stream& rhs) 
00199                 : mTarget(rhs.mTarget), mLevel(rhs.mLevel), mMaskDebug(rhs.mMaskDebug)
00200             {
00201                 // explicit copy of stream required, gcc doesn't like implicit
00202                 mCache.str(rhs.mCache.str());
00203             } 
00204             ~Stream()
00205             {
00206                 // flush on destroy
00207                 if (mCache.tellp() > 0)
00208                 {
00209                     mTarget->logMessage(mCache.str(), mLevel, mMaskDebug);
00210                 }
00211             }
00212 
00213             template <typename T>
00214             Stream& operator<< (const T& v)
00215             {
00216                 mCache << v;
00217                 return *this;
00218             }
00219 
00220             Stream& operator<< (const Flush& v)
00221             {
00222                 mTarget->logMessage(mCache.str(), mLevel, mMaskDebug);
00223                 mCache.str(StringUtil::BLANK);
00224                 return *this;
00225             }
00226 
00227 
00228         };
00229 
00230     };
00231 }
00232 
00233 #endif

Copyright © 2008 Torus Knot Software Ltd
Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 2.5 License.
Last modified Sun Sep 27 22:02:23 2009