Move FileManager code into libpbd. Use it for SMF read/write.
[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
25 #include "pbd/debug.h"
26
27 #include "i18n.h"
28
29 using namespace std;
30 static uint64_t _debug_bit = 1;
31 static std::map<const char*,uint64_t> _debug_bit_map;
32
33 uint64_t PBD::DEBUG::Stateful = PBD::new_debug_bit ("stateful");
34 uint64_t PBD::DEBUG::Properties = PBD::new_debug_bit ("properties");
35 uint64_t PBD::DEBUG::FileManager = PBD::new_debug_bit ("filemanager");
36
37 uint64_t PBD::debug_bits = 0x0;
38
39 uint64_t
40 PBD::new_debug_bit (const char* name)
41 {
42         uint64_t ret;
43         _debug_bit_map.insert (make_pair (name, _debug_bit));
44         ret = _debug_bit;
45         _debug_bit <<= 1;
46         return ret;
47 }
48
49 void
50 PBD::debug_print (const char* prefix, string str)
51 {
52         cerr << prefix << ": " << str;
53 }
54
55 void
56 PBD::set_debug_bits (uint64_t bits)
57 {
58         debug_bits = bits;
59 }
60
61 int
62 PBD::parse_debug_options (const char* str)
63 {
64         char* p;
65         char* sp;
66         uint64_t bits = 0;
67         char* copy = strdup (str);
68
69         p = strtok_r (copy, ",", &sp);
70
71         while (p) {
72                 if (strcasecmp (p, "list") == 0) {
73                         list_debug_options ();
74                         free (copy);
75                         return 1;
76                 }
77
78                 if (strcasecmp (p, "all") == 0) {
79                         PBD::set_debug_bits (~0ULL);
80                         free (copy);
81                         return 0;
82                 }
83
84                 for (map<const char*,uint64_t>::iterator i = _debug_bit_map.begin(); i != _debug_bit_map.end(); ++i) {
85                         if (strncasecmp (p, i->first, strlen (p)) == 0) {
86                                 bits |= i->second;
87                         }
88                 }
89
90                 p = strtok_r (0, ",", &sp);
91         }
92         
93         free (copy);
94         PBD::set_debug_bits (bits);
95         return 0;
96 }
97
98 void
99 PBD::list_debug_options ()
100 {
101         cout << _("The following debug options are available. Separate multipe options with commas.\nNames are case-insensitive and can be abbreviated.") << endl << endl;
102         cout << "\tAll" << endl;
103
104         for (map<const char*,uint64_t>::iterator i = _debug_bit_map.begin(); i != _debug_bit_map.end(); ++i) {
105                 cout << "\t" << i->first << endl;
106         }
107 }