Console.h

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   (c) Jürgen Riegel (juergen.riegel@web.de) 2002                        *   
00003  *                                                                         *
00004  *   This file is part of the FreeCAD CAx development system.              *
00005  *                                                                         *
00006  *   This program is free software; you can redistribute it and/or modify  *
00007  *   it under the terms of the GNU Library General Public License (LGPL)   *
00008  *   as published by the Free Software Foundation; either version 2 of     *
00009  *   the License, or (at your option) any later version.                   *
00010  *   for detail see the LICENCE text file.                                 *
00011  *                                                                         *
00012  *   FreeCAD is distributed in the hope that it will be useful,            *
00013  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        * 
00014  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00015  *   GNU Library General Public License for more details.                  *
00016  *                                                                         *
00017  *   You should have received a copy of the GNU Library General Public     *
00018  *   License along with FreeCAD; if not, write to the Free Software        * 
00019  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  *
00020  *   USA                                                                   *
00021  *                                                                         *
00022  *   Juergen Riegel 2002                                                   *
00023  ***************************************************************************/
00024  
00025 
00026 
00027 
00028 #ifndef BASE_CONSOLE_H
00029 #define BASE_CONSOLE_H
00030 
00031 // Std. configurations
00032 #include <Base/PyExport.h>
00033 #include <Base/Stream.h>
00034 
00035 //#pragma warning(disable: 4786)  // specifier longer then 255 chars
00036 #include <assert.h>
00037 #include <set>
00038 #include <string>
00039 
00040 //**************************************************************************
00041 // Loging levels
00042 
00043 #ifdef FC_DEBUG
00045 #  undef FC_LOGPYOBJECTS 
00047 #  define FC_LOGFEATUREUPDATE 
00049 #  undef FC_LOGUPDATECHAIN 
00050 #endif
00051 
00052  
00053 namespace Base {
00054 class ConsoleSingleton;
00055 }; // namespace Base
00056 
00057 typedef Base::ConsoleSingleton ConsoleMsgType;
00058 typedef unsigned int ConsoleMsgFlags;
00059  
00060 namespace Base {
00061 
00068 class BaseExport ConsoleObserver
00069 {
00070 public:
00071     ConsoleObserver()
00072         :bErr(true),bMsg(true),bLog(true),bWrn(true) {}
00073     virtual ~ConsoleObserver() {}
00075     virtual void Warning(const char *){};
00077     virtual void Message(const char *){};
00079     virtual void Error  (const char *)=0;
00081     virtual void Log    (const char *){};
00082 
00083     virtual const char *Name(void){return 0L;}
00084     bool bErr,bMsg,bLog,bWrn;
00085 };
00086 
00087 
00105 class BaseExport ConsoleSingleton
00106 {
00107 
00108 public:
00109 
00110     // exported functions goes here +++++++++++++++++++++++++++++++++++++++
00112     virtual void Message ( const char * pMsg, ... ) ;
00114     virtual void Warning ( const char * pMsg, ... ) ;
00116     virtual void Error   ( const char * pMsg, ... ) ;
00118     virtual void Log     ( const char * pMsg, ... ) ;
00119 
00121     const char* Time(void);
00123     void AttachObserver(ConsoleObserver *pcObserver);
00125     void DetachObserver(ConsoleObserver *pcObserver);
00127     enum ConsoleMode{
00128         Verbose = 1,    // supress Log messages
00129     };
00130 
00131     enum FreeCAD_ConsoleMsgType { 
00132         MsgType_Txt = 1, 
00133         MsgType_Log = 2, 
00134         MsgType_Wrn = 4, 
00135         MsgType_Err = 8 
00136     } ;
00137 
00139     void SetMode(ConsoleMode m);
00141     void UnsetMode(ConsoleMode m);
00143     ConsoleMsgFlags SetEnabledMsgType(const char* sObs, ConsoleMsgFlags type, bool b);
00144 
00146     static ConsoleSingleton &Instance(void);
00147 
00148     // retrieval of an observer by name
00149     ConsoleObserver *Get(const char *Name);
00150 
00151     static PyMethodDef    Methods[]; 
00152 
00153 protected:
00154     // python exports goes here +++++++++++++++++++++++++++++++++++++++++++     
00155     // static python wrapper of the exported functions
00156     static PyObject *sPyLog      (PyObject *self,PyObject *args,PyObject *kwd);
00157     static PyObject *sPyMessage  (PyObject *self,PyObject *args,PyObject *kwd);
00158     static PyObject *sPyWarning  (PyObject *self,PyObject *args,PyObject *kwd);
00159     static PyObject *sPyError    (PyObject *self,PyObject *args,PyObject *kwd);
00160     static PyObject *sPySetStatus(PyObject *self,PyObject *args,PyObject *kwd);
00161     static PyObject *sPyGetStatus(PyObject *self,PyObject *args,PyObject *kwd);
00162 
00163     bool _bVerbose;
00164 
00165     // Singleton!
00166     ConsoleSingleton(void);
00167     virtual ~ConsoleSingleton();
00168 
00169 private:
00170     // singleton
00171     static void Destruct(void);
00172     static ConsoleSingleton *_pcSingleton;
00173 
00174     // observer processing 
00175     void NotifyMessage(const char *sMsg);
00176     void NotifyWarning(const char *sMsg);
00177     void NotifyError  (const char *sMsg);
00178     void NotifyLog    (const char *sMsg);
00179 
00180     // observer list
00181     std::set<ConsoleObserver * > _aclObservers;
00182 };
00183 
00188 inline ConsoleSingleton &Console(void){
00189     return ConsoleSingleton::Instance();
00190 }
00191 
00192 
00193 //=========================================================================
00194 // some special observers
00195 
00199 class BaseExport ConsoleObserverFile : public ConsoleObserver
00200 {
00201 public:
00202     ConsoleObserverFile(const char *sFileName);
00203     virtual ~ConsoleObserverFile();
00204     virtual void Warning(const char *sWarn);
00205     virtual void Message(const char *sMsg);
00206     virtual void Error  (const char *sErr);
00207     virtual void Log    (const char *sLog);
00208     const char* Name(void){return "File";}
00209 
00210 protected:
00211     Base::ofstream cFileStream;
00212 };
00213 
00217 class BaseExport ConsoleObserverStd: public ConsoleObserver
00218 {
00219 public:
00220     ConsoleObserverStd();
00221     virtual ~ConsoleObserverStd();
00222     virtual void Warning(const char *sWarn);
00223     virtual void Message(const char *sMsg); 
00224     virtual void Error  (const char *sErr); 
00225     virtual void Log    (const char *sErr); 
00226     const char* Name(void){return "Console";}
00227 };
00228 
00229 class BaseExport RedirectStdOutput : public std::streambuf
00230 {
00231 public:
00232     RedirectStdOutput();
00233 
00234 protected:
00235     int overflow(int c = EOF);
00236     int sync();
00237 
00238 private:
00239     std::string buffer;
00240 };
00241 
00242 class BaseExport RedirectStdError : public std::streambuf
00243 {
00244 public:
00245     RedirectStdError();
00246 
00247 protected:
00248     int overflow(int c = EOF);
00249     int sync();
00250 
00251 private:
00252     std::string buffer;
00253 };
00254 
00255 class BaseExport RedirectStdLog : public std::streambuf
00256 {
00257 public:
00258     RedirectStdLog();
00259 
00260 protected:
00261     int overflow(int c = EOF);
00262     int sync();
00263 
00264 private:
00265     std::string buffer;
00266 };
00267 
00268 
00269 } // namespace Base 
00270 
00271 #endif // BASE_CONSOLE_H

Generated on Wed Nov 23 19:00:04 2011 for FreeCAD by  doxygen 1.6.1