Merge branch 'export-dialog' into cairocanvas
[ardour.git] / libs / pbd / debug.cc
1 /*
2     Copyright (C) 2009 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <cstring>
21 #include <cstdlib>
22 #include <iostream>
23 #include <map>
24 #include <vector>
25 #include <algorithm>
26
27 #include <boost/tokenizer.hpp>
28
29 #include "pbd/debug.h"
30
31 #include "i18n.h"
32
33 using namespace std;
34 static uint64_t _debug_bit = 1;
35
36 typedef std::map<const char*,uint64_t> DebugMap;
37
38 namespace PBD {
39         DebugMap & _debug_bit_map()
40         {
41                 static DebugMap map;
42                 return map;
43         }
44 }
45
46 uint64_t PBD::DEBUG::Stateful = PBD::new_debug_bit ("stateful");
47 uint64_t PBD::DEBUG::Properties = PBD::new_debug_bit ("properties");
48 uint64_t PBD::DEBUG::FileManager = PBD::new_debug_bit ("filemanager");
49 uint64_t PBD::DEBUG::Pool = PBD::new_debug_bit ("pool");
50 uint64_t PBD::DEBUG::EventLoop = PBD::new_debug_bit ("eventloop");
51 uint64_t PBD::DEBUG::AbstractUI = PBD::new_debug_bit ("abstractui");
52 uint64_t PBD::DEBUG::FileUtils = PBD::new_debug_bit ("fileutils");
53
54 uint64_t PBD::debug_bits = 0x0;
55
56 uint64_t
57 PBD::new_debug_bit (const char* name)
58 {
59         uint64_t ret;
60         _debug_bit_map().insert (make_pair (name, _debug_bit));
61         ret = _debug_bit;
62         _debug_bit <<= 1;
63         return ret;
64 }
65
66 void
67 PBD::debug_print (const char* prefix, string str)
68 {
69         cerr << prefix << ": " << str;
70 }
71
72 void
73 PBD::set_debug_bits (uint64_t bits)
74 {
75         debug_bits = bits;
76 }
77
78 int
79 PBD::parse_debug_options (const char* str)
80 {
81         string in_str = str;
82         typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
83         boost::char_separator<char> sep (",");
84         tokenizer tokens (in_str, sep);
85         uint64_t bits = 0;
86
87         for (tokenizer::iterator tok_iter = tokens.begin(); tok_iter != tokens.end(); ++tok_iter) {
88                 if (*tok_iter == "list") {
89                         list_debug_options ();
90                         return 1;
91                 }
92
93                 if (*tok_iter == "all") {
94                         PBD::set_debug_bits (~0ULL);
95                         return 0;
96                 }
97
98                 for (map<const char*,uint64_t>::iterator i = _debug_bit_map().begin(); i != _debug_bit_map().end(); ++i) {
99                         const char* cstr = (*tok_iter).c_str();
100
101                         if (strncasecmp (cstr, i->first, strlen (cstr)) == 0) {
102                                 bits |= i->second;
103                         }
104                 }
105         }
106         
107         PBD::set_debug_bits (bits);
108         return 0;
109 }
110
111 void
112 PBD::list_debug_options ()
113 {
114         cout << _("The following debug options are available. Separate multiple options with commas.\nNames are case-insensitive and can be abbreviated.") << endl << endl;
115         cout << '\t' << X_("all") << endl; 
116
117         vector<string> options;
118
119         for (map<const char*,uint64_t>::iterator i = _debug_bit_map().begin(); i != _debug_bit_map().end(); ++i) {
120                 options.push_back (i->first);
121         }
122
123         sort (options.begin(), options.end());
124
125         for (vector<string>::iterator i = options.begin(); i != options.end(); ++i) {
126                 cout << "\t" << (*i) << endl;
127         }
128 }