Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

OSGLog.h

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002  *                                OpenSG                                     *
00003  *                                                                           *
00004  *                                                                           *
00005  *             Copyright (C) 2000-2002 by the OpenSG Forum                   *
00006  *                                                                           *
00007  *                            www.opensg.org                                 *
00008  *                                                                           *
00009  *         contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de    *
00010  *                                                                           *
00011 \*---------------------------------------------------------------------------*/
00012 /*---------------------------------------------------------------------------*\
00013  *                                License                                    *
00014  *                                                                           *
00015  * This library is free software; you can redistribute it and/or modify it   *
00016  * under the terms of the GNU Library General Public License as published    *
00017  * by the Free Software Foundation, version 2.                               *
00018  *                                                                           *
00019  * This library is distributed in the hope that it will be useful, but       *
00020  * WITHOUT ANY WARRANTY; without even the implied warranty of                *
00021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         *
00022  * Library General Public License for more details.                          *
00023  *                                                                           *
00024  * You should have received a copy of the GNU Library General Public         *
00025  * License along with this library; if not, write to the Free Software       *
00026  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                 *
00027  *                                                                           *
00028 \*---------------------------------------------------------------------------*/
00029 /*---------------------------------------------------------------------------*\
00030  *                                Changes                                    *
00031  *                                                                           *
00032  *                                                                           *
00033  *                                                                           *
00034  *                                                                           *
00035  *                                                                           *
00036  *                                                                           *
00037 \*---------------------------------------------------------------------------*/
00038 
00039 #ifndef _OSGLOG_H_
00040 #define _OSGLOG_H_
00041 #ifdef __sgi
00042 #pragma once
00043 #endif
00044 
00045 #include <OSGBaseTypes.h>
00046 #include <OSGTime.h>
00047 
00048 #include <fstream>
00049 #include <list>
00050 
00055 OSG_BEGIN_NAMESPACE
00056 
00060 enum LogType
00061 {
00062     LOG_NONE   = 0,
00063     LOG_STDOUT = 1,
00064     LOG_STDERR = 2,
00065     LOG_FILE   = 3,
00066     LOG_BUFFER = 4
00067 };
00068 
00072 enum LogLevel
00073 {
00074     LOG_LOG     = 0,
00075     LOG_FATAL   = 1,
00076     LOG_WARNING = 2,
00077     LOG_NOTICE  = 3,
00078     LOG_INFO    = 4,
00079     LOG_DEBUG   = 5
00080 };
00081 
00085 enum LogHeaderElem
00086 {
00087     LOG_NONE_HEADER          = 0,
00088     LOG_BEGIN_NEWLINE_HEADER = 1,
00089     LOG_TYPE_HEADER          = 2,
00090     LOG_TIMESTAMP_HEADER     = 4,
00091     LOG_MODULE_HEADER        = 8,
00092     LOG_FILE_HEADER          = 16,
00093     LOG_LINE_HEADER          = 32,
00094     LOG_END_NEWLINE_HEADER   = 64,
00095 
00096         LOG_COLOR_HEADER         = 8192,
00097         LOG_TAB_HEADER           = 16384,
00098     LOG_ALL_HEADER           = 32767
00099 };
00100 
00105 enum LogModuleHandling
00106 {
00107     LOG_MODULE_NONE           = 0,
00108     LOG_MODULE_KNOWN          = 1,
00109     LOG_MODULE_UNKNOWN        = 2,
00110     LOG_MODULE_UNDEFINED      = 4,
00111     LOG_MODULE_ALL            = 7
00112 };
00113 
00120 struct OSG_BASE_DLLMAPPING LogOStream : public std::ostream
00121 {
00122   public:
00123 
00124     LogOStream(std::streambuf *buf);
00125     
00126     virtual ~LogOStream(void);
00127     
00128     void setrdbuf(std::streambuf *buf);
00129 };
00130 
00137 class OSG_BASE_DLLMAPPING LogBuf : public std::streambuf
00138 {
00139     /*==========================  PUBLIC  =================================*/
00140 
00141   public:
00142 
00143   // only temporary until the functors work with all compiler
00144 
00145     typedef void (*Callback)(const Char8 *data, 
00146                                    Int32  size,
00147                                    void  *clientData);
00148  
00150     struct Chunk 
00151     {
00152         Char8 *data;
00153         Int32  size;
00154         
00155         Chunk(void);
00156         ~Chunk(void);
00157     };
00158 
00159     /*---------------------------------------------------------------------*/
00163     LogBuf(      UInt32  bufferSize = 1024);
00164     LogBuf(const LogBuf &                 );
00165     
00167     /*---------------------------------------------------------------------*/
00171     virtual ~LogBuf();
00172     
00174     /*---------------------------------------------------------------------*/
00178     bool getEnabled   (void             );    
00179     void setEnabled   (bool value = true);
00180 
00181     void clearChunkBag(void             );
00182     
00184     /*---------------------------------------------------------------------*/
00187     void setCallback   (Callback  cb, 
00188                         void     *clientData = 0,
00189                         bool      flushData  = false);
00190     void removeCallback(void                        );
00191     
00192 
00194     /*==========================  PRIVATE  ================================*/
00195 
00196   private:
00197     
00198     bool               _enabled;
00199     
00200     std::list<Chunk*>  _chunkBag;
00201     
00202     Callback           _callback;
00203     void              *_clientData; 
00204     
00205     const LogBuf &operator=(const LogBuf          &);
00206           void    write    (const Char8           *buffer, 
00207                                   std::streamsize  size  );
00208     
00209     /*---------------------------------------------------------------------*/
00213     virtual Int32           overflow(      Int32            c    );
00214     virtual Int32           sync    (      void                  );
00215     virtual std::streamsize xsputn  (const Char8           *buffer, 
00216                                            std::streamsize  size);
00217     
00218 };
00219 
00224 class OSG_BASE_DLLMAPPING Log : public std::ostream
00225 {
00226     /*==========================  PUBLIC  =================================*/
00227 
00228   public:
00229 
00230     /*---------------------------------------------------------------------*/
00234     Log(      LogType   logType  = LOG_STDERR,
00235               LogLevel  logLevel = LOG_NOTICE);
00236 
00237     Log(const Char8    *fileName, 
00238               LogLevel  logLevel = LOG_NOTICE);
00239 
00241     /*---------------------------------------------------------------------*/
00245     virtual ~Log(void);
00246 
00248     /*---------------------------------------------------------------------*/
00252     void lock  (void) {;} // TODO: implement
00253     void unlock(void) {;} // TODO: implement
00254 
00256     /*---------------------------------------------------------------------*/
00260     virtual void setHeaderElem    (      UInt32             elemMask, 
00261                                          bool               force    = false);
00262     virtual void addHeaderElem    (      LogHeaderElem      elem, 
00263                                          bool               force    = false);
00264     virtual void delHeaderElem    (      LogHeaderElem      elem,     
00265                                          bool               force    = false);
00266     virtual bool hasHeaderElem    (      LogHeaderElem      elem);
00267 
00268     virtual void addModuleHandling(      LogModuleHandling  handling, 
00269                                          bool               force    = false);
00270     virtual void delModuleHandling(      LogModuleHandling  handling, 
00271                                          bool               force    = false);
00272 
00273     virtual void addModuleName    (const Char8             *module, 
00274                                          bool               isStatic = false);
00275     virtual void delModuleName    (const Char8             *module          );
00276 
00277             bool hasModule        (const Char8             *module          );
00278             bool checkModule      (const Char8             *module          );
00279 
00281     /*---------------------------------------------------------------------*/
00285     LogType  getLogType  (      void                                  );
00286     void     setLogType  (      LogType   logType,  bool force = false);
00287 
00288     LogLevel getLogLevel (      void);
00289     void     setLogLevel (      LogLevel  logLevel, bool force = false);
00290     bool     checkLevel  (      LogLevel  logLevel                    );
00291 
00292     void     setLogFile  (const Char8    *fileName, bool force = false);
00293 
00294     Time     getRefTime  (      void                                  );
00295     void     setRefTime  (      Time  refTime                         );
00296     void     resetRefTime(      void                                  );
00297 
00299     /*---------------------------------------------------------------------*/
00303     LogBuf       &getLogBuf(void          );
00304 
00305     std::ostream &stream   (LogLevel level);
00306     std::ostream &nilstream(void          );
00307 
00309     /*---------------------------------------------------------------------*/
00313     std::ostream &doHeader(      LogLevel  level,
00314                            const Char8    *module,
00315                            const Char8    *file,
00316                                  UInt32    line       );
00317 
00318     void          doLog   (const Char8    *format, ...);
00319 
00321     /*=========================  PROTECTED  ===============================*/
00322 
00323   protected:
00324 
00325     /*---------------------------------------------------------------------*/
00329     void connect(void );
00330 
00332     /*===========================  PRIVATE  ===============================*/
00333 
00334   private:
00335 
00336     typedef std::ostream Inherited;
00337 
00338     friend OSG_BASE_DLLMAPPING void doInitLog(void);
00339  
00340     /*---------------------------------------------------------------------*/
00345     struct OSG_BASE_DLLMAPPING nilbuf : public std::streambuf 
00346     {
00347     };
00348 
00350     struct Module
00351     {
00352         const Char8 *name;
00353               bool   isStatic;
00354 
00355         Module(void);
00356     };
00357 
00359     /*---------------------------------------------------------------------*/
00363     static       nilbuf       *_nilbufP;
00364     static       std::ostream *_nilstreamP;
00365 
00366     static const Char8        *_levelName[];
00367     static const Char8        *_levelColor[];
00368 
00370     /*---------------------------------------------------------------------*/
00374     LogType            _logType;
00375     LogLevel           _logLevel;
00376 
00377     std::fstream       _fileStream;
00378 
00379     LogBuf             _logBuf;
00380     LogOStream        *_streamVec[6];
00381 
00382     UInt32             _headerElem;
00383     UInt32             _moduleHandling;
00384 
00385 
00386     std::list<Module>  _moduleList;
00387 
00388     Time               _refTime;
00389     
00391     /*---------------------------------------------------------------------*/
00395     Log(const Log &source);
00396     
00398     /*---------------------------------------------------------------------*/
00402     void operator =(const Log &source);
00403     
00405 };
00406 
00407 typedef Log *LogP;
00408 
00409 #ifndef OSG_LOG_MODULE
00410 #define OSG_LOG_MODULE "OpenSG"
00411 #endif
00412 
00413 extern OSG_BASE_DLLMAPPING LogP osgLogP;
00414 
00415 
00416 OSG_BASE_DLLMAPPING void          doInitLog  (      void                   );
00417 inline              void          initLog    (      void                   );
00418 inline              Log          &osgLog     (      void                   );
00419 inline              std::ostream &osgStartLog(      bool          logHeader,
00420                                                     LogLevel      level,
00421                                               const Char8        *module,
00422                                               const Char8        *file,
00423                                                     UInt32        line     );
00424 
00425 inline              std::ostream &endLog      (     std::ostream &strm     );
00426 
00427 inline              void          indentLog   (     UInt32        indent,
00428                                                     std::ostream &stream   );
00429 
00430 
00435 #define SLOG     \
00436     OSG::osgStartLog(true, OSG::LOG_LOG,     OSG_LOG_MODULE, __FILE__, __LINE__)
00437 
00442 #define SFATAL   \
00443     OSG::osgStartLog(true, OSG::LOG_FATAL,   OSG_LOG_MODULE, __FILE__, __LINE__)
00444 
00449 #define SWARNING \
00450     OSG::osgStartLog(true, OSG::LOG_WARNING, OSG_LOG_MODULE, __FILE__, __LINE__)
00451 
00456 #define SNOTICE  \
00457     OSG::osgStartLog(true, OSG::LOG_NOTICE,  OSG_LOG_MODULE, __FILE__, __LINE__)
00458 
00463 #define SINFO    \
00464     OSG::osgStartLog(true, OSG::LOG_INFO,    OSG_LOG_MODULE, __FILE__, __LINE__)
00465 
00466 
00471 #define PLOG     \
00472     OSG::osgStartLog(false, OSG::LOG_LOG,     OSG_LOG_MODULE, __FILE__, __LINE__)
00473 
00478 #define PFATAL   \
00479     OSG::osgStartLog(false, OSG::LOG_FATAL,   OSG_LOG_MODULE, __FILE__, __LINE__)
00480 
00485 #define PWARNING \
00486     OSG::osgStartLog(false, OSG::LOG_WARNING, OSG_LOG_MODULE, __FILE__, __LINE__)
00487 
00492 #define PNOTICE  \
00493     OSG::osgStartLog(false, OSG::LOG_NOTICE,  OSG_LOG_MODULE, __FILE__, __LINE__)
00494 
00499 #define PINFO    \
00500     OSG::osgStartLog(false, OSG::LOG_INFO,    OSG_LOG_MODULE, __FILE__, __LINE__)
00501 
00502 
00503 // C interface, because it can be compiled away
00504 // don't use varargs macros, because they are not supported everywhere
00505 // use the (( )) convention instead
00506 
00511 #define FLOG(par)                                               \
00512 {                                                               \
00513    OSG::initLog();                                              \
00514    OSG::osgStartLog(true,                                       \
00515                     OSG::LOG_LOG,                               \
00516                     OSG_LOG_MODULE,                             \
00517                     __FILE__,                                   \
00518                     __LINE__);                                  \
00519    OSG::osgLogP->doLog par;                                     \
00520    OSG::osgLogP->unlock();                                      \
00521 }
00522 
00527 #define FFATAL(par)                                             \
00528 {                                                               \
00529     OSG::initLog();                                             \
00530     if(OSG::osgLogP->checkLevel(OSG::LOG_FATAL))                \
00531     {                                                           \
00532         OSG::osgStartLog(true,                                  \
00533                          OSG::LOG_FATAL,                        \
00534                          OSG_LOG_MODULE,                        \
00535                          __FILE__,                              \
00536                          __LINE__);                             \
00537         OSG::osgLogP->doLog par;                                \
00538         OSG::osgLogP->unlock();                                 \
00539     }                                                           \
00540 }
00541 
00546 #define FWARNING(par)                                           \
00547 {                                                               \
00548     OSG::initLog();                                             \
00549     if(OSG::osgLogP->checkLevel(OSG::LOG_WARNING))              \
00550     {                                                           \
00551         OSG::osgStartLog(true,                                  \
00552                          OSG::LOG_WARNING,                      \
00553                          OSG_LOG_MODULE,                        \
00554                          __FILE__,                              \
00555                          __LINE__);                             \
00556         OSG::osgLogP->doLog par;                                \
00557         OSG::osgLogP->unlock();                                 \
00558     }                                                           \
00559 }
00560 
00565 #define FNOTICE(par)                                            \
00566 {                                                               \
00567     OSG::initLog();                                             \
00568     if(OSG::osgLogP->checkLevel(OSG::LOG_NOTICE))               \
00569     {                                                           \
00570         OSG::osgStartLog(true,                                  \
00571                          OSG::LOG_NOTICE,                       \
00572                          OSG_LOG_MODULE,                        \
00573                          __FILE__,                              \
00574                          __LINE__);                             \
00575         OSG::osgLogP->doLog par;                                \
00576         OSG::osgLogP->unlock();                                 \
00577     }                                                           \
00578 }
00579 
00584 #define FINFO(par)                                              \
00585 {                                                               \
00586     OSG::initLog();                                             \
00587     if(OSG::osgLogP->checkLevel(OSG::LOG_INFO))                 \
00588     {                                                           \
00589         OSG::osgStartLog(true,                                  \
00590                          OSG::LOG_INFO,                         \
00591                          OSG_LOG_MODULE,                        \
00592                          __FILE__,                              \
00593                          __LINE__);                             \
00594         OSG::osgLogP->doLog par;                                \
00595         OSG::osgLogP->unlock();                                 \
00596     }                                                           \
00597 }
00598 
00603 #ifdef OSG_DEBUG
00604 #define FDEBUG(par)                                             \
00605 {                                                               \
00606     OSG::initLog();                                             \
00607     if(OSG::osgLogP->checkLevel(OSG::LOG_DEBUG))                \
00608     {                                                           \
00609         OSG::osgStartLog(true,                                  \
00610                          OSG::LOG_DEBUG,OSG_LOG_MODULE,         \
00611                          __FILE__,                              \
00612                          __LINE__);                             \
00613         OSG::osgLogP->doLog par;                                \
00614         OSG::osgLogP->unlock();                                 \
00615     }                                                           \
00616 }
00617 #else
00618 #define FDEBUG(par)
00619 #endif
00620 
00625 #define FASSERT(condition, doExit)                              \
00626 {                                                               \
00627     if (!condition)                                             \
00628     {                                                           \
00629         OSG::osgLog().lock();                                   \
00630         OSG::osgLog().stream(OSG::LOG_FATAL)                    \
00631             << OSG_LOG_MODULE                                   \
00632             << ':'                                              \
00633             << __FILE__                                         \
00634             << ':'                                              \
00635             << __LINE__                                         \
00636             << " FATAL ASSERT: "                                \
00637             << (doExit ? "exit system" : "try to keep running") \
00638             << std::flush << std::endl;                         \
00639         OSG::osgLog().unlock();                                 \
00640         if(doExit)                                              \
00641             exit(-1);                                           \
00642     }                                                           \
00643 }
00644 
00649 #define FFASSERT(condition, doExit, par)                        \
00650 {                                                               \
00651     if(!condition)                                              \
00652     {                                                           \
00653         OSG::osgLog().lock();                                   \
00654         OSG::osgLog().stream(OSG::LOG_FATAL)                    \
00655             << OSG_LOG_MODULE                                   \
00656             << ':'                                              \
00657             << __FILE__                                         \
00658             << ':'                                              \
00659             << __LINE__                                         \
00660             << " FATAL ASSERT: "                                \
00661             << (doExit ? "exit system" : "try to keep running") \
00662             << std::flush << std::endl;                         \
00663         OSG::osgLogP->doLog par                                 \
00664         OSG::osgLog().unlock();                                 \
00665         if(doExit)                                              \
00666             exit(-1);                                           \
00667     }                                                           \
00668 }
00669 
00670 // C interface without line start outputs
00671 
00676 #define FPLOG(par)                                               \
00677 {                                                               \
00678    OSG::initLog();                                              \
00679    OSG::osgStartLog(false,                                       \
00680                     OSG::LOG_LOG,                               \
00681                     OSG_LOG_MODULE,                             \
00682                     __FILE__,                                   \
00683                     __LINE__);                                  \
00684    OSG::osgLogP->doLog par;                                     \
00685    OSG::osgLogP->unlock();                                      \
00686 }
00687 
00692 #define FPFPATAL(par)                                             \
00693 {                                                               \
00694     OSG::initLog();                                             \
00695     if(OSG::osgLogP->checkLevel(OSG::LOG_FPATAL))                \
00696     {                                                           \
00697         OSG::osgStartLog(false,                                  \
00698                          OSG::LOG_FPATAL,                        \
00699                          OSG_LOG_MODULE,                        \
00700                          __FILE__,                              \
00701                          __LINE__);                             \
00702         OSG::osgLogP->doLog par;                                \
00703         OSG::osgLogP->unlock();                                 \
00704     }                                                           \
00705 }
00706 
00711 #define FPWARNING(par)                                           \
00712 {                                                               \
00713     OSG::initLog();                                             \
00714     if(OSG::osgLogP->checkLevel(OSG::LOG_WARNING))              \
00715     {                                                           \
00716         OSG::osgStartLog(false,                                  \
00717                          OSG::LOG_WARNING,                      \
00718                          OSG_LOG_MODULE,                        \
00719                          __FILE__,                              \
00720                          __LINE__);                             \
00721         OSG::osgLogP->doLog par;                                \
00722         OSG::osgLogP->unlock();                                 \
00723     }                                                           \
00724 }
00725 
00730 #define FPNOTICE(par)                                            \
00731 {                                                               \
00732     OSG::initLog();                                             \
00733     if(OSG::osgLogP->checkLevel(OSG::LOG_NOTICE))               \
00734     {                                                           \
00735         OSG::osgStartLog(false,                                  \
00736                          OSG::LOG_NOTICE,                       \
00737                          OSG_LOG_MODULE,                        \
00738                          __FILE__,                              \
00739                          __LINE__);                             \
00740         OSG::osgLogP->doLog par;                                \
00741         OSG::osgLogP->unlock();                                 \
00742     }                                                           \
00743 }
00744 
00749 #define FPINFPO(par)                                              \
00750 {                                                               \
00751     OSG::initLog();                                             \
00752     if(OSG::osgLogP->checkLevel(OSG::LOG_INFPO))                 \
00753     {                                                           \
00754         OSG::osgStartLog(false,                                  \
00755                          OSG::LOG_INFPO,                         \
00756                          OSG_LOG_MODULE,                        \
00757                          __FILE__,                              \
00758                          __LINE__);                             \
00759         OSG::osgLogP->doLog par;                                \
00760         OSG::osgLogP->unlock();                                 \
00761     }                                                           \
00762 }
00763 
00768 #ifdef OSG_DEBUG
00769 #define FPDEBUG(par)                                             \
00770 {                                                               \
00771     OSG::initLog();                                             \
00772     if(OSG::osgLogP->checkLevel(OSG::LOG_DEBUG))                \
00773     {                                                           \
00774         OSG::osgStartLog(false,                                  \
00775                          OSG::LOG_DEBUG,OSG_LOG_MODULE,         \
00776                          __FILE__,                              \
00777                          __LINE__);                             \
00778         OSG::osgLogP->doLog par;                                \
00779         OSG::osgLogP->unlock();                                 \
00780     }                                                           \
00781 }
00782 #else
00783 #define FPDEBUG(par)
00784 #endif
00785 
00786 OSG_END_NAMESPACE
00787 
00788 #include <OSGLog.inl>
00789 
00790 #endif /* _OSGLOG_H_ */

Generated on Thu Aug 25 04:07:09 2005 for OpenSG by  doxygen 1.4.3