Merge branch 'master' into windows
[ardour.git] / libs / pbd / debug.cc
index 4f97ccff86ca080e9ea6dbe02dd5a310e33f0909..fba457c83eeb256012b2b2f36dfae97a406c8861 100644 (file)
 #include <cstdlib>
 #include <iostream>
 #include <map>
+#include <vector>
+#include <algorithm>
+
+#include <boost/tokenizer.hpp>
 
 #include "pbd/debug.h"
 
 
 using namespace std;
 static uint64_t _debug_bit = 1;
-static std::map<const char*,uint64_t> _debug_bit_map;
+
+typedef std::map<const char*,uint64_t> DebugMap;
+
+namespace PBD {
+       DebugMap & _debug_bit_map()
+       {
+               static DebugMap map;
+               return map;
+       }
+}
 
 uint64_t PBD::DEBUG::Stateful = PBD::new_debug_bit ("stateful");
 uint64_t PBD::DEBUG::Properties = PBD::new_debug_bit ("properties");
 uint64_t PBD::DEBUG::FileManager = PBD::new_debug_bit ("filemanager");
+uint64_t PBD::DEBUG::Pool = PBD::new_debug_bit ("pool");
+uint64_t PBD::DEBUG::EventLoop = PBD::new_debug_bit ("eventloop");
+uint64_t PBD::DEBUG::AbstractUI = PBD::new_debug_bit ("abstractui");
+uint64_t PBD::DEBUG::FileUtils = PBD::new_debug_bit ("fileutils");
 
 uint64_t PBD::debug_bits = 0x0;
 
@@ -40,7 +57,7 @@ uint64_t
 PBD::new_debug_bit (const char* name)
 {
         uint64_t ret;
-        _debug_bit_map.insert (make_pair (name, _debug_bit));
+        _debug_bit_map().insert (make_pair (name, _debug_bit));
         ret = _debug_bit;
         _debug_bit <<= 1;
         return ret;
@@ -61,36 +78,31 @@ PBD::set_debug_bits (uint64_t bits)
 int
 PBD::parse_debug_options (const char* str)
 {
-       char* p;
-       char* sp;
+       typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
+       boost::char_separator<char> sep (",");
+       tokenizer tokens (string(str), sep);
        uint64_t bits = 0;
-       char* copy = strdup (str);
-
-       p = strtok_r (copy, ",", &sp);
 
-       while (p) {
-               if (strcasecmp (p, "list") == 0) {
+       for (tokenizer::iterator tok_iter = tokens.begin(); tok_iter != tokens.end(); ++tok_iter) {
+               if (*tok_iter == "list") {
                        list_debug_options ();
-                       free (copy);
                        return 1;
                }
 
-               if (strcasecmp (p, "all") == 0) {
+               if (*tok_iter == "all") {
                        PBD::set_debug_bits (~0ULL);
-                       free (copy);
                        return 0;
                }
 
-                for (map<const char*,uint64_t>::iterator i = _debug_bit_map.begin(); i != _debug_bit_map.end(); ++i) {
-                        if (strncasecmp (p, i->first, strlen (p)) == 0) {
+               for (map<const char*,uint64_t>::iterator i = _debug_bit_map().begin(); i != _debug_bit_map().end(); ++i) {
+                       const char* cstr = (*tok_iter).c_str();
+
+                        if (strncasecmp (cstr, i->first, strlen (cstr)) == 0) {
                                 bits |= i->second;
                         }
                 }
-
-               p = strtok_r (0, ",", &sp);
        }
        
-       free (copy);
        PBD::set_debug_bits (bits);
        return 0;
 }
@@ -98,10 +110,18 @@ PBD::parse_debug_options (const char* str)
 void
 PBD::list_debug_options ()
 {
-       cout << _("The following debug options are available. Separate multipe options with commas.\nNames are case-insensitive and can be abbreviated.") << endl << endl;
-       cout << "\tAll" << endl;
+       cout << _("The following debug options are available. Separate multiple options with commas.\nNames are case-insensitive and can be abbreviated.") << endl << endl;
+       cout << '\t' << X_("all") << endl; 
+
+       vector<string> options;
 
-        for (map<const char*,uint64_t>::iterator i = _debug_bit_map.begin(); i != _debug_bit_map.end(); ++i) {
-                cout << "\t" << i->first << endl;
+       for (map<const char*,uint64_t>::iterator i = _debug_bit_map().begin(); i != _debug_bit_map().end(); ++i) {
+               options.push_back (i->first);
         }
+
+       sort (options.begin(), options.end());
+
+       for (vector<string>::iterator i = options.begin(); i != options.end(); ++i) {
+                cout << "\t" << (*i) << endl;
+       }
 }