]> granicus.if.org Git - taglib/commitdiff
Enabled users to define custom debug message listeners
authorTsuda Kageyu <tsuda.kageyu@gmail.com>
Sat, 8 Jun 2013 00:59:36 +0000 (09:59 +0900)
committerTsuda Kageyu <tsuda.kageyu@gmail.com>
Sat, 8 Jun 2013 00:59:36 +0000 (09:59 +0900)
taglib/toolkit/tdebug.cpp
taglib/toolkit/tdebug.h
taglib/toolkit/tdebuglistener.cpp
taglib/toolkit/tdebuglistener.h

index 129aee840f40eed3eb5f506dcce80a714275041f..95d5c55fbd5f5dc517e08a6cc98eb1579738279a 100644 (file)
@@ -31,5 +31,10 @@ using namespace TagLib;
 
 void TagLib::debug(const String &s)
 {
-  getDebugListener()->listen(s);
+  getDebugListener()->printMessage(s);
+}
+
+void TagLib::debugData(const ByteVector &v)
+{
+  getDebugListener()->printData(v);
 }
index e0c6615f4e48a208cb290f641f7a493be02a78d4..bd94d159fca93f92b68310c7ba1f1519b3b7e561 100644 (file)
@@ -29,6 +29,7 @@
 namespace TagLib {
 
   class String;
+  class ByteVector;
 
 #ifndef DO_NOT_DOCUMENT
 
@@ -44,6 +45,17 @@ namespace TagLib {
    * \internal
    */
   void debug(const String &s);
+  
+  /*!
+   * For debugging binary data.
+   *
+   * \warning Do not use this outside of TagLib, it could lead to undefined
+   * symbols in your build if TagLib is built with NDEBUG defined and your
+   * application is not.
+   *
+   * \internal
+   */
+  void debugData(const ByteVector &v);
 }
 
 #endif
index 499134aa1fbb756716898232581b6b2706183684..bce94312946553ef82753d9103338f7bf16b2733 100644 (file)
@@ -27,6 +27,7 @@
 
 #ifndef NDEBUG
 # include <iostream>
+# include <bitset>
 # ifdef _WIN32
 #   include <windows.h>
 # endif
@@ -39,7 +40,7 @@ namespace
   class DefaultListener : public DebugListener
   {
   public:
-    virtual void listen(const String &msg)
+    virtual void printMessage(const String &msg)
     {
 #ifndef NDEBUG
 # ifdef _WIN32
@@ -59,23 +60,51 @@ namespace
       std::cerr << "TagLib: " << msg << std::endl;
 
 # endif 
+#endif
+    }
+
+    virtual void printData(const ByteVector &v)
+    {
+#ifndef NDEBUG
+
+      for(size_t i = 0; i < v.size(); i++) 
+      {
+        std::cout << "*** [" << i << "] - '" << char(v[i]) << "' - int " << int(v[i])
+          << std::endl;
+
+        std::bitset<8> b(v[i]);
+
+        for(int j = 0; j < 8; j++)
+          std::cout << i << ":" << j << " " << b.test(j) << std::endl;
+
+        std::cout << std::endl;
+      }
+
 #endif
     }
   };
 
   DefaultListener defaultListener;
-  DebugListener *traceListener = &defaultListener;
+  DebugListener *debugListener = &defaultListener;
+}
+
+TagLib::DebugListener::DebugListener()
+{
+}
+
+TagLib::DebugListener::~DebugListener()
+{
 }
 
 void TagLib::setDebugListener(DebugListener *listener)
 {
   if(listener)
-    traceListener = listener;
+    debugListener = listener;
   else
-    traceListener = &defaultListener;
+    debugListener = &defaultListener;
 }
 
 DebugListener *TagLib::getDebugListener()
 {
-  return traceListener;
+  return debugListener;
 }
index 5d66c91a8a8700c592dce79e000bd33273b90d77..3dd3a73475037483aba378dfb62e17b8f87415e6 100644 (file)
@@ -43,7 +43,16 @@ namespace TagLib
   class TAGLIB_EXPORT DebugListener
   {
   public:
-    virtual void listen(const String &msg) = 0;
+    DebugListener();
+    virtual ~DebugListener();
+
+    virtual void printMessage(const String &msg) = 0;
+    virtual void printData(const ByteVector &data) = 0;
+
+  private:
+    // Noncopyable
+    DebugListener(const DebugListener &);
+    DebugListener &operator=(const DebugListener &);
   };
 
   /*!
@@ -56,14 +65,14 @@ namespace TagLib
    *
    * \see DebugListener
    */
-  void setDebugListener(DebugListener *listener);
+  TAGLIB_EXPORT void setDebugListener(DebugListener *listener);
 
 #ifndef DO_NOT_DOCUMENT
 
   /*!
    * \internal
    */
-  DebugListener *getDebugListener();
+  TAGLIB_EXPORT DebugListener *getDebugListener();
 
 #endif
 }