Merge branch 'patches' of https://github.com/jdekozak/ardour
[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 "pbd/debug.h"
28
29 #include "i18n.h"
30
31 using namespace std;
32 static uint64_t _debug_bit = 1;
33
34 typedef std::map<const char*,uint64_t> DebugMap;
35
36 namespace PBD {
37         DebugMap _debug_bit_map;
38 }
39
40 uint64_t PBD::DEBUG::Stateful = PBD::new_debug_bit ("stateful");
41 uint64_t PBD::DEBUG::Properties = PBD::new_debug_bit ("properties");
42 uint64_t PBD::DEBUG::FileManager = PBD::new_debug_bit ("filemanager");
43 uint64_t PBD::DEBUG::Pool = PBD::new_debug_bit ("pool");
44 uint64_t PBD::DEBUG::EventLoop = PBD::new_debug_bit ("eventloop");
45 uint64_t PBD::DEBUG::AbstractUI = PBD::new_debug_bit ("abstractui");
46
47 uint64_t PBD::debug_bits = 0x0;
48
49 uint64_t
50 PBD::new_debug_bit (const char* name)
51 {
52         uint64_t ret;
53         _debug_bit_map.insert (make_pair (name, _debug_bit));
54         ret = _debug_bit;
55         _debug_bit <<= 1;
56         return ret;
57 }
58
59 void
60 PBD::debug_print (const char* prefix, string str)
61 {
62         cerr << prefix << ": " << str;
63 }
64
65 void
66 PBD::set_debug_bits (uint64_t bits)
67 {
68         debug_bits = bits;
69 }
70
71 int
72 PBD::parse_debug_options (const char* str)
73 {
74         char* p;
75         char* sp;
76         uint64_t bits = 0;
77         char* copy = strdup (str);
78
79         p = strtok_r (copy, ",", &sp);
80
81         while (p) {
82                 if (strcasecmp (p, "list") == 0) {
83                         list_debug_options ();
84                         free (copy);
85                         return 1;
86                 }
87
88                 if (strcasecmp (p, "all") == 0) {
89                         PBD::set_debug_bits (~0ULL);
90                         free (copy);
91                         return 0;
92                 }
93
94                 for (map<const char*,uint64_t>::iterator i = _debug_bit_map.begin(); i != _debug_bit_map.end(); ++i) {
95                         if (strncasecmp (p, i->first, strlen (p)) == 0) {
96                                 bits |= i->second;
97                         }
98                 }
99
100                 p = strtok_r (0, ",", &sp);
101         }
102         
103         free (copy);
104         PBD::set_debug_bits (bits);
105         return 0;
106 }
107
108 void
109 PBD::list_debug_options ()
110 {
111         cout << _("The following debug options are available. Separate multiple options with commas.\nNames are case-insensitive and can be abbreviated.") << endl << endl;
112         cout << '\t' << X_("all") << endl; 
113
114         vector<string> options;
115
116         for (map<const char*,uint64_t>::iterator i = _debug_bit_map.begin(); i != _debug_bit_map.end(); ++i) {
117                 options.push_back (i->first);
118         }
119
120         sort (options.begin(), options.end());
121
122         for (vector<string>::iterator i = options.begin(); i != options.end(); ++i) {
123                 cout << "\t" << (*i) << endl;
124         }
125 }