[opengtl-commits] [235] * introduce a new debug system

[ Thread Index | Date Index | More lists.tuxfamily.org/opengtl-commits Archives ]


Revision: 235
Author:   cyrille
Date:     2008-06-25 21:47:28 +0200 (Wed, 25 Jun 2008)

Log Message:
-----------
* introduce a new debug system

Modified Paths:
--------------
    trunk/OpenGTL/OpenCTL/OpenCTL/CMakeLists.txt
    trunk/OpenGTL/OpenCTL/tests/library/CMakeLists.txt
    trunk/OpenGTL/OpenCTL/tools/interpreter/CMakeLists.txt
    trunk/OpenGTL/OpenGTL/GTLCore/CMakeLists.txt
    trunk/OpenGTL/OpenGTL/GTLCore/Debug.cpp
    trunk/OpenGTL/OpenGTL/GTLCore/Debug.h
    trunk/OpenGTL/OpenGTL/GTLCore/Visitor_p.cpp
    trunk/OpenGTL/OpenShiva/tools/interpreter/CMakeLists.txt


Modified: trunk/OpenGTL/OpenCTL/OpenCTL/CMakeLists.txt
===================================================================
--- trunk/OpenGTL/OpenCTL/OpenCTL/CMakeLists.txt	2008-06-25 19:46:48 UTC (rev 234)
+++ trunk/OpenGTL/OpenCTL/OpenCTL/CMakeLists.txt	2008-06-25 19:47:28 UTC (rev 235)
@@ -19,6 +19,7 @@
 target_link_libraries(OpenCTL GTLCore )
 
 add_definitions( "${LLVM_COMPILE_FLAGS}")
+add_definitions( -DCOUMPONENT_NAME=\"\\\"OpenCTL\\\"\" )
 
 if (NOT APPLE)
   add_definitions( -D_OPENCTL_CTL_STD_LIB_BUILD_DIR_=\\"${CMAKE_CURRENT_BINARY_DIR}/../CtlStdLib/\\")

Modified: trunk/OpenGTL/OpenCTL/tests/library/CMakeLists.txt
===================================================================
--- trunk/OpenGTL/OpenCTL/tests/library/CMakeLists.txt	2008-06-25 19:46:48 UTC (rev 234)
+++ trunk/OpenGTL/OpenCTL/tests/library/CMakeLists.txt	2008-06-25 19:47:28 UTC (rev 235)
@@ -4,6 +4,7 @@
   TestOpenCTL.cpp
     )
 
+add_definitions( -DCOUMPONENT_NAME=\"\\\"TestOpenCTL\\\"\" )
 add_executable(TestOpenCTL ${TestOpenCTL_SRCS})
 target_link_libraries(TestOpenCTL OpenCTL GTLTest )
 add_test(TestOpenCTL TestOpenCTL)

Modified: trunk/OpenGTL/OpenCTL/tools/interpreter/CMakeLists.txt
===================================================================
--- trunk/OpenGTL/OpenCTL/tools/interpreter/CMakeLists.txt	2008-06-25 19:46:48 UTC (rev 234)
+++ trunk/OpenGTL/OpenCTL/tools/interpreter/CMakeLists.txt	2008-06-25 19:47:28 UTC (rev 235)
@@ -4,6 +4,7 @@
   CtlI.cpp
     )
 
+add_definitions( -DCOUMPONENT_NAME=\"\\\"CTLi\\\"\" )
 add_executable(ctli ${ctlexec_SRCS})
 target_link_libraries(ctli OpenCTL )
 install( TARGETS ctli DESTINATION ${BIN_INSTALL_DIR} )

Modified: trunk/OpenGTL/OpenGTL/GTLCore/CMakeLists.txt
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/CMakeLists.txt	2008-06-25 19:46:48 UTC (rev 234)
+++ trunk/OpenGTL/OpenGTL/GTLCore/CMakeLists.txt	2008-06-25 19:47:28 UTC (rev 235)
@@ -59,8 +59,10 @@
 set_target_properties(GTLCore PROPERTIES VERSION ${OPENGTL_LIB_VERSION} SOVERSION ${OPENGTL_LIB_SOVERSION} )
 
 # Set some LLVM required properties
-set_target_properties( GTLCore PROPERTIES COMPILE_FLAGS "${LLVM_COMPILE_FLAGS}")
+add_definitions( "${LLVM_COMPILE_FLAGS}")
+add_definitions( -DCOUMPONENT_NAME=\"\\\"GTLCore\\\"\" )
 
+
 # installation
 install(TARGETS GTLCore  DESTINATION ${LIB_INSTALL_DIR} )
 install( FILES Parameter.h Function.h Array.h Buffer.h ErrorMessage.h PixelDescription.h Region.h RegionF.h ScopedName.h String.h Type.h Value.h Version.h Macros.h DESTINATION ${INCLUDE_INSTALL_DIR}/GTLCore )

Modified: trunk/OpenGTL/OpenGTL/GTLCore/Debug.cpp
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/Debug.cpp	2008-06-25 19:46:48 UTC (rev 234)
+++ trunk/OpenGTL/OpenGTL/GTLCore/Debug.cpp	2008-06-25 19:47:28 UTC (rev 235)
@@ -18,14 +18,208 @@
  */
 
 #include "Debug.h"
+
+#include <map>
+#include <fstream>
+
+#include <llvm/System/Path.h>
+
+#include "Macros_p.h"
 #include "ScopedName.h"
 #include "Token_p.h"
 #include "Type.h"
 
 using namespace GTLCore;
 
+#ifndef _NDEBUG_
+
+struct FunctionDebugInfo {
+  FunctionDebugInfo() : enabled( true ) {}
+  bool enabled;
+};
+
+struct FileDebugInfo {
+  FileDebugInfo() : enabled( true ) {}
+  bool enabled;
+  std::map<GTLCore::String, FunctionDebugInfo> functionsDebugInfo;
+};
+
+struct LibraryDebugInfo {
+  LibraryDebugInfo() : enabled( true ) {}
+  bool enabled;
+  std::map<GTLCore::String, FileDebugInfo> filesDebugInfo;
+};
+
+struct Debug::Private {
+  Private();
+  ~Private();
+  // Static
+  static Debug::Private* instance();
+  static Debug::Private* s_instance;
+  // Helper members
+  static bool isEnabled( const std::map< GTLCore::String, LibraryDebugInfo >& infos, const GTLCore::String& _libraryName, const GTLCore::String& _fileName, const GTLCore::String& _functionName );
+  static String extractFunctionName( const String& );
+  static void readConfigFile( const String& _fileName, std::map< GTLCore::String, LibraryDebugInfo >& destination);
+  static std::ostream& report( std::ostream&, const std::map< GTLCore::String, LibraryDebugInfo >& _infos, const String& _libraryName, const String& _fileName, int _line, const String& _functionName );
+  // Fields
+  std::ostream* m_debugStream;
+  std::ostream* m_warningStream;
+  std::ostream* m_errorStream;
+  std::ostream* m_voidStream;
+  std::map< GTLCore::String, LibraryDebugInfo > m_librariesDebugInfo;
+  std::map< GTLCore::String, LibraryDebugInfo > m_librariesWarningInfo;
+  std::map< GTLCore::String, LibraryDebugInfo > m_librariesErrorInfo;
+};
+
+Debug::Private* Debug::Private::s_instance = 0;
+
+STATIC_DELETER( Debug )
+{
+  delete Debug::Private::s_instance;
+}
+
+class NullStream : public std::ostream {
+};
+
+Debug::Private::Private()
+{
+  m_debugStream = &std::cerr;
+  m_warningStream = &std::cerr;
+  m_errorStream = &std::cerr;
+  m_voidStream = new NullStream;
+//   m_librariesDebugInfo[ "GTLCore" ].enabled = false;
+  // Read ${HOME}/.OpenGTLDebugConfig
+  readConfigFile( ".OpenGTLDebugConfig", m_librariesDebugInfo );
+  readConfigFile( ".OpenGTLWarningConfig", m_librariesWarningInfo );
+  readConfigFile( ".OpenGTLErrorConfig", m_librariesErrorInfo );
+}
+
+Debug::Private::~Private()
+{
+  delete m_voidStream;
+}
+
+void Debug::Private::readConfigFile( const String& _fileName, std::map< GTLCore::String, LibraryDebugInfo >& _destination)
+{
+  llvm::sys::Path path = llvm::sys::Path::GetUserHomeDirectory();
+  path.appendComponent( _fileName);
+  if( path.exists() )
+  {
+    std::ifstream in;
+    in.open(path.toString().c_str() );
+    if(in)
+    {
+      GTLCore::String str;
+      std::getline(in,str);
+      while ( in ) {
+        if( str[0] != '#' )
+        {
+          std::list< String > splited = str.split( " =,", false );
+          if( splited.size() >= 2 and splited.size() <= 4 )
+          {
+            std::list< String >::iterator it = --splited.end();
+            bool status = (*(--splited.end()) == "true");
+            it = splited.begin();
+            LibraryDebugInfo& ldi = _destination[ *it ];
+            if( splited.size() == 2 )
+            {
+              ldi.enabled = status;
+            } else {
+              FileDebugInfo& fdi = ldi.filesDebugInfo[ *(++it) ];
+              if( splited.size() == 3 )
+              {
+                fdi.enabled = status;
+              } else {
+                FunctionDebugInfo& fundi = fdi.functionsDebugInfo[ *(++it) ];
+                fundi.enabled = status;
+              }
+            }
+          }
+        }
+        std::getline(in,str);
+      }
+    }
+  }
+}
+
+bool Debug::Private::isEnabled( const std::map< GTLCore::String, LibraryDebugInfo >& infos, const GTLCore::String& _libraryName, const GTLCore::String& _fileName, const GTLCore::String& _functionName )
+{
+  std::map< GTLCore::String, LibraryDebugInfo >::const_iterator ldi = infos.find(_libraryName );
+  if( ldi == infos.end() ) return true;
+  if( not ldi->second.enabled ) return false;
+  std::map< GTLCore::String, FileDebugInfo>::const_iterator fdi = ldi->second.filesDebugInfo.find( _fileName );
+  if( fdi == ldi->second.filesDebugInfo.end() ) return true;
+  if( not fdi->second.enabled ) return false;
+  std::map< GTLCore::String, FunctionDebugInfo>::const_iterator fundi = fdi->second.functionsDebugInfo.find( _functionName );
+  if( fundi == fdi->second.functionsDebugInfo.end() ) return true;
+  return fundi->second.enabled;
+}
+
+String Debug::Private::extractFunctionName( const String& _str)
+{
+  int posBeg = 0;
+  int posEnd = 0;
+  for( size_t i = 0; i < _str.length(); ++i)
+  {
+    if( _str[i] == '(' )
+    {
+      posEnd = i;
+      break;
+    }
+  }
+  for( size_t i = posEnd; i > 0; --i)
+  {
+    if( _str[i] == ' ' )
+    {
+      posBeg = i + 1;
+      break;
+    }
+  }
+  return _str.substr( posBeg, posEnd - posBeg );
+}
+
+std::ostream& Debug::Private::report( std::ostream& _stream, const std::map< GTLCore::String, LibraryDebugInfo >& _infos, const String& _libraryName, const String& _fileName, int _line, const String& _functionName )
+{
+  GTLCore::String fileName = llvm::sys::Path(_fileName).getLast();
+  GTLCore::String functionName = Private::extractFunctionName( _functionName );
+  if( isEnabled( _infos, _libraryName, fileName, functionName ) )
+  {
+    _stream << _libraryName << " (Debug): in " << fileName << " at " << _line << " in " << functionName << " : ";
+    return _stream;
+  } else {
+    return *Private::instance()->m_voidStream;
+  }
+}
+
+std::ostream& Debug::debug( const GTLCore::String& _libraryName, const GTLCore::String& _fileName, int _line, const GTLCore::String& _functionName )
+{
+  Private* p = Private::instance();
+  return Private::report( *p->m_debugStream, p->m_librariesDebugInfo, _libraryName, _fileName, _line, _functionName );
+}
+
+std::ostream& Debug::warning( const GTLCore::String& _libraryName, const GTLCore::String& _fileName, int _line, const GTLCore::String& _functionName )
+{
+  Private* p = Private::instance();
+  return Private::report( *p->m_warningStream, p->m_librariesWarningInfo, _libraryName, _fileName, _line, _functionName );
+}
+
+std::ostream& Debug::error( const GTLCore::String& _libraryName, const GTLCore::String& _fileName, int _line, const GTLCore::String& _functionName )
+{
+  Private* p = Private::instance();
+  return Private::report( *p->m_errorStream, p->m_librariesErrorInfo, _libraryName, _fileName, _line, _functionName );
+}
+
+
+Debug::Private* Debug::Private::instance()
+{
+  if( s_instance == 0) s_instance = new Debug::Private;
+  return s_instance;
+}
+  
+#endif
+
 namespace GTLCore {
-
+  
   std::ostream& operator<< (std::ostream& ostr, const GTLCore::ScopedName& name)
   {
     return ostr << name.toString() ;

Modified: trunk/OpenGTL/OpenGTL/GTLCore/Debug.h
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/Debug.h	2008-06-25 19:46:48 UTC (rev 234)
+++ trunk/OpenGTL/OpenGTL/GTLCore/Debug.h	2008-06-25 19:47:28 UTC (rev 235)
@@ -22,31 +22,64 @@
 
 #include <iostream>
 #include <stdlib.h>
+#include <GTLCore/String.h>
 
+class DebugDeleter;
+
 namespace GTLCore {
+  class Debug {
+    friend class ::DebugDeleter;
+    public:
+      Debug();
+      ~Debug();
+      static std::ostream& debug( const String& _libraryName, const String& _fileName, int _line, const String& _functionName );
+      static std::ostream& warning( const String& _libraryName, const String& _fileName, int _line, const String& _functionName );
+      static std::ostream& error( const String& _libraryName, const String& _fileName, int _line, const String& _functionName );
+    private:
+      struct Private;
+  };
   class ScopedName;
   class Type;
   class Token;
 
-  std::ostream& operator<< (std::ostream& ostr, const GTLCore::ScopedName& name);
-  std::ostream& operator<< (std::ostream& ostr, const GTLCore::Type& type);
-  std::ostream& operator<< (std::ostream& ostr, const GTLCore::Token& token);
+  std::ostream& operator<< (std::ostream& ostr, const ScopedName& name);
+  std::ostream& operator<< (std::ostream& ostr, const Type& type);
+  std::ostream& operator<< (std::ostream& ostr, const Token& token);
 }
 
 
-
 #define GTL_WARNING(msg) \
-  std::cerr << "OpenGTL (Warning): in " << __FILE__ << " at " << __LINE__ << " : " << msg << std::endl;
+  GTLCore::Debug::warning(COUMPONENT_NAME, __FILE__, __LINE__, FUNC_INFO ) << msg << std::endl;
 
+#define GTL_ERROR(msg) \
+  GTLCore::Debug::error(COUMPONENT_NAME, __FILE__, __LINE__, FUNC_INFO ) << msg << std::endl;
+
+#define GTL_ABORT(msg) \
+  GTL_ERROR(msg); \
+  abort();
+
 #ifndef _NDEBUG_
 
+#if (defined(__GNUC__) && !( defined(__sun) || defined(sun) )) || ( ( defined(hpux) || defined(__hpux) ) && ( defined(__HP_aCC) || __cplusplus >= 199707L )  )
+#  define FUNC_INFO __PRETTY_FUNCTION__
+#elif defined(_MSC_VER) && _MSC_VER > 1300
+#  define FUNC_INFO __FUNCSIG__
+#else
+#  define FUNC_INFO ""
+#endif
+
+
+
 #include <assert.h>
 
 #define GTL_DEBUG(msg) \
-  std::cerr << "OpenGTL (Debug): in " << __FILE__ << " at " << __LINE__ << " : " << msg << std::endl;
+  GTLCore::Debug::debug(COUMPONENT_NAME, __FILE__, __LINE__, FUNC_INFO ) << msg << std::endl;
 
 #define GTL_ASSERT(assrt) \
-  assert(assrt);
+  if( not (assrt ) ) \
+  { \
+    GTL_ABORT( #assrt ); \
+  }
 
 #else
 
@@ -54,9 +87,5 @@
 #define GTL_ASSERT(assrt)
 
 #endif
-             
-#define GTL_ABORT(msg) \
-  std::cerr << "OpenGTL (Abort): in " << __FILE__ << " at " << __LINE__ << " : " << msg << std::endl; \
-  abort();
 
 #endif

Modified: trunk/OpenGTL/OpenGTL/GTLCore/Visitor_p.cpp
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/Visitor_p.cpp	2008-06-25 19:46:48 UTC (rev 234)
+++ trunk/OpenGTL/OpenGTL/GTLCore/Visitor_p.cpp	2008-06-25 19:47:28 UTC (rev 235)
@@ -29,6 +29,7 @@
 #include "CodeGenerator_p.h"
 #include "Debug.h"
 #include "Macros.h"
+#include "Macros_p.h"
 #include "Type.h"
 #include "Type_p.h"
 #include "ErrorMessage.h"

Modified: trunk/OpenGTL/OpenShiva/tools/interpreter/CMakeLists.txt
===================================================================
--- trunk/OpenGTL/OpenShiva/tools/interpreter/CMakeLists.txt	2008-06-25 19:46:48 UTC (rev 234)
+++ trunk/OpenGTL/OpenShiva/tools/interpreter/CMakeLists.txt	2008-06-25 19:47:28 UTC (rev 235)
@@ -4,6 +4,8 @@
   Shiva.cpp
     )
 
+add_definitions( -DCOUMPONENT_NAME=\"\\\"Shiva\\\"\" )
+
 add_executable(shiva ${shiva_SRCS})
 target_link_libraries(shiva OpenShiva )
 install( TARGETS shiva DESTINATION ${BIN_INSTALL_DIR} )


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/