]> granicus.if.org Git - taglib/commitdiff
Enabled users to define custom debug message listeners
authorTsuda Kageyu <tsuda.kageyu@gmail.com>
Fri, 7 Jun 2013 17:51:20 +0000 (02:51 +0900)
committerTsuda Kageyu <tsuda.kageyu@gmail.com>
Fri, 7 Jun 2013 18:05:50 +0000 (03:05 +0900)
taglib/CMakeLists.txt
taglib/toolkit/tdebug.cpp
taglib/toolkit/tdebug.h
taglib/toolkit/tdebuglistener.cpp [new file with mode: 0644]
taglib/toolkit/tdebuglistener.h [new file with mode: 0644]

index 352f7e2ed59c061d044903f805a445371b6296a8..23fa60df6c56458955f4d4f9ef4d199904da376d 100755 (executable)
@@ -51,6 +51,7 @@ set(tag_HDRS
   toolkit/tmap.tcc
   toolkit/tpropertymap.h
   toolkit/trefcounter.h
+  toolkit/tdebuglistener.h
   mpeg/mpegfile.h
   mpeg/mpegproperties.h
   mpeg/mpegheader.h
@@ -291,6 +292,7 @@ set(toolkit_SRCS
   toolkit/tdebug.cpp
   toolkit/tpropertymap.cpp
   toolkit/trefcounter.cpp
+  toolkit/tdebuglistener.cpp
   toolkit/unicode.cpp
 )
 
index 522b68c9794533e0979658228fab6b63f39041bd..129aee840f40eed3eb5f506dcce80a714275041f 100644 (file)
  *   http://www.mozilla.org/MPL/                                           *
  ***************************************************************************/
 
-#ifndef NDEBUG
-#include <iostream>
-#include <bitset>
-
 #include "tdebug.h"
 #include "tstring.h"
+#include "tdebuglistener.h"
 
 using namespace TagLib;
 
 void TagLib::debug(const String &s)
 {
-  std::cerr << "TagLib: " << s << std::endl;
-}
-
-void TagLib::debugData(const ByteVector &v)
-{
-  for(uint 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;
-  }
+  getDebugListener()->listen(s);
 }
-#endif
index 5204fe707d91615b05f4af762cc9785ddf9ce509..e0c6615f4e48a208cb290f641f7a493be02a78d4 100644 (file)
 namespace TagLib {
 
   class String;
-  class ByteVector;
 
 #ifndef DO_NOT_DOCUMENT
-#ifndef NDEBUG
 
   /*!
-   * A simple function that prints debugging output to cerr if debugging is
-   * not disabled.
+   * A simple function that outputs the debug messages to the listener. 
+   * The default listener redirects the messages to \a stderr when NDEBUG is 
+   * not defined.
    *
    * \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
@@ -45,27 +44,7 @@ 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);
-
-#else
-
-  // Define these to an empty statement if debugging is disabled.
-
-#define debug(x)
-#define debugData(x)
-
-#endif
-#endif
 }
 
 #endif
+#endif
diff --git a/taglib/toolkit/tdebuglistener.cpp b/taglib/toolkit/tdebuglistener.cpp
new file mode 100644 (file)
index 0000000..499134a
--- /dev/null
@@ -0,0 +1,81 @@
+/***************************************************************************
+    copyright            : (C) 2013 by Tsuda Kageyu
+    email                : tsuda.kageyu@gmail.com
+ ***************************************************************************/
+
+/***************************************************************************
+ *   This library is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU Lesser General Public License version   *
+ *   2.1 as published by the Free Software Foundation.                     *
+ *                                                                         *
+ *   This library is distributed in the hope that it will be useful, but   *
+ *   WITHOUT ANY WARRANTY; without even the implied warranty of            *
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
+ *   Lesser General Public License for more details.                       *
+ *                                                                         *
+ *   You should have received a copy of the GNU Lesser General Public      *
+ *   License along with this library; if not, write to the Free Software   *
+ *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA         *
+ *   02110-1301  USA                                                       *
+ *                                                                         *
+ *   Alternatively, this file is available under the Mozilla Public        *
+ *   License Version 1.1.  You may obtain a copy of the License at         *
+ *   http://www.mozilla.org/MPL/                                           *
+ ***************************************************************************/
+
+#include "tdebuglistener.h"
+
+#ifndef NDEBUG
+# include <iostream>
+# ifdef _WIN32
+#   include <windows.h>
+# endif
+#endif
+
+using namespace TagLib;
+
+namespace
+{
+  class DefaultListener : public DebugListener
+  {
+  public:
+    virtual void listen(const String &msg)
+    {
+#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::cerr << "TagLib: " << s << std::endl;
+
+# else
+
+      std::cerr << "TagLib: " << msg << std::endl;
+
+# endif 
+#endif
+    }
+  };
+
+  DefaultListener defaultListener;
+  DebugListener *traceListener = &defaultListener;
+}
+
+void TagLib::setDebugListener(DebugListener *listener)
+{
+  if(listener)
+    traceListener = listener;
+  else
+    traceListener = &defaultListener;
+}
+
+DebugListener *TagLib::getDebugListener()
+{
+  return traceListener;
+}
diff --git a/taglib/toolkit/tdebuglistener.h b/taglib/toolkit/tdebuglistener.h
new file mode 100644 (file)
index 0000000..5d66c91
--- /dev/null
@@ -0,0 +1,71 @@
+/***************************************************************************
+    copyright            : (C) 2013 by Tsuda Kageyu
+    email                : tsuda.kageyu@gmail.com
+ ***************************************************************************/
+
+/***************************************************************************
+ *   This library is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU Lesser General Public License version   *
+ *   2.1 as published by the Free Software Foundation.                     *
+ *                                                                         *
+ *   This library is distributed in the hope that it will be useful, but   *
+ *   WITHOUT ANY WARRANTY; without even the implied warranty of            *
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
+ *   Lesser General Public License for more details.                       *
+ *                                                                         *
+ *   You should have received a copy of the GNU Lesser General Public      *
+ *   License along with this library; if not, write to the Free Software   *
+ *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA         *
+ *   02110-1301  USA                                                       *
+ *                                                                         *
+ *   Alternatively, this file is available under the Mozilla Public        *
+ *   License Version 1.1.  You may obtain a copy of the License at         *
+ *   http://www.mozilla.org/MPL/                                           *
+ ***************************************************************************/
+
+#ifndef TAGLIB_DEBUGLISTENER_H
+#define TAGLIB_DEBUGLISTENER_H
+
+#include "taglib_export.h"
+#include "tstring.h"
+
+namespace TagLib 
+{
+  //! An abstraction for the listener to the debug messages.
+
+  /*!
+   * 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().
+   *
+   * \see setDebugListener()
+   */  
+  class TAGLIB_EXPORT DebugListener
+  {
+  public:
+    virtual void listen(const String &msg) = 0;
+  };
+
+  /*!
+   * Sets the listener that decides how the debug messages are redirected.
+   * If the parameter \a listener is null, the previous listener is released 
+   * and default stderr listener is restored.   
+   *
+   * \note The caller is responsible for deleting the previous listener
+   * as needed after it is released.
+   *
+   * \see DebugListener
+   */
+  void setDebugListener(DebugListener *listener);
+
+#ifndef DO_NOT_DOCUMENT
+
+  /*!
+   * \internal
+   */
+  DebugListener *getDebugListener();
+
+#endif
+}
+
+#endif