]> granicus.if.org Git - taglib/commitdiff
Simplified DebugListener class
authorTsuda Kageyu <tsuda.kageyu@gmail.com>
Sat, 8 Jun 2013 12:40:30 +0000 (21:40 +0900)
committerTsuda Kageyu <tsuda.kageyu@gmail.com>
Sat, 8 Jun 2013 12:40:30 +0000 (21:40 +0900)
taglib/toolkit/tdebug.cpp
taglib/toolkit/tdebuglistener.cpp
taglib/toolkit/tdebuglistener.h

index 95d5c55fbd5f5dc517e08a6cc98eb1579738279a..133aad391698b2f0a7df0d22876eb29693ec9f88 100644 (file)
  *   http://www.mozilla.org/MPL/                                           *
  ***************************************************************************/
 
+#include "config.h"
+
 #include "tdebug.h"
 #include "tstring.h"
 #include "tdebuglistener.h"
 
+#include <bitset>
+#include <cstdio>
+#include <cstdarg>
+
 using namespace TagLib;
 
+namespace
+{
+  String format(const char *fmt, ...)
+  {
+    va_list args;
+    va_start(args, fmt);
+
+    char buf[256];
+
+#if defined(HAVE_SNPRINTF)
+
+    vsnprintf(buf, sizeof(buf), fmt, args);
+
+#elif defined(HAVE_SPRINTF_S)
+
+    vsprintf_s(buf, fmt, args);
+
+#else
+
+    // Be careful. May cause a buffer overflow.  
+    vsprintf(buf, fmt, args);
+
+#endif
+
+    va_end(args);
+
+    return String(buf);
+  }
+}
+
 void TagLib::debug(const String &s)
 {
-  getDebugListener()->printMessage(s);
+  getDebugListener()->printMessage("TagLib: " + s + "\n");
 }
 
 void TagLib::debugData(const ByteVector &v)
 {
-  getDebugListener()->printData(v);
+  for(size_t i = 0; i < v.size(); ++i) 
+  {
+    String msg 
+      = format("*** [%d] - char '%c' - int %d, 0x%02x, 0b", i, v[i], v[i], v[i]);
+
+    std::bitset<8> b(v[i]);
+    for(int j = 7; j >= 0; --j) 
+      msg += format("%d", (b.test(j) ? 1 : 0));
+   
+    msg += "\n";
+
+    getDebugListener()->printMessage(msg);
+  }
 }
index bce94312946553ef82753d9103338f7bf16b2733..3b138e51e31995ffbd28c9fab047bd881bf04363 100644 (file)
@@ -45,41 +45,21 @@ namespace
 #ifndef NDEBUG
 # ifdef _WIN32
 
-      std::string s;
       const wstring wstr = msg.toWString();
       const int len = WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, NULL, 0, NULL, NULL);
       if(len != 0) {
-        s.resize(len);
-        WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, &s[0], len, NULL, NULL);
+        std::vector<char> buf(len);
+        WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, &buf[0], len, NULL, NULL);
+
+        std::cerr << std::string(&buf[0]);
       }
 
-      std::cerr << "TagLib: " << s << std::endl;
 
 # else
 
-      std::cerr << "TagLib: " << msg << std::endl;
+      std::cerr << msg;
 
 # 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
     }
   };
index 3dd3a73475037483aba378dfb62e17b8f87415e6..cd1c52e5621c3e5d1773167253cda77195393e11 100644 (file)
@@ -35,8 +35,8 @@ namespace TagLib
 
   /*!
    * This class enables you to handle the debug messages in your preferred 
-   * way by subclassing this class, reimplementing listen() and setting your 
-   * reimplementation as the default with setDebugListener().
+   * way by subclassing this class, reimplementing printMessage() and setting 
+   * your reimplementation as the default with setDebugListener().
    *
    * \see setDebugListener()
    */  
@@ -47,7 +47,6 @@ namespace TagLib
     virtual ~DebugListener();
 
     virtual void printMessage(const String &msg) = 0;
-    virtual void printData(const ByteVector &data) = 0;
 
   private:
     // Noncopyable