MSVC project changes needed to support the new 'mp3 import' stuff
[ardour.git] / libs / pbd / debug.cc
1 /*
2  * Copyright (C) 2010-2012 Carl Hetherington <carl@carlh.net>
3  * Copyright (C) 2010-2016 Paul Davis <paul@linuxaudiosystems.com>
4  * Copyright (C) 2015-2016 Robin Gareus <robin@gareus.org>
5  * Copyright (C) 2015 Tim Mayberry <mojofunk@gmail.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #include <cstring>
23 #include <cstdio>
24 #include <cstdlib>
25 #include <iostream>
26 #include <map>
27 #include <vector>
28 #include <algorithm>
29
30 #include <boost/tokenizer.hpp>
31
32 #include "pbd/debug.h"
33
34 #include "pbd/i18n.h"
35
36 using namespace std;
37 using PBD::DebugBits;
38
39 static uint64_t _debug_bit = 0;
40
41 typedef std::map<const char*,DebugBits> DebugMap;
42
43 namespace PBD {
44         DebugMap & _debug_bit_map()
45         {
46                 static DebugMap map;
47                 return map;
48         }
49 }
50
51 DebugBits PBD::DEBUG::Stateful = PBD::new_debug_bit ("stateful");
52 DebugBits PBD::DEBUG::Properties = PBD::new_debug_bit ("properties");
53 DebugBits PBD::DEBUG::FileManager = PBD::new_debug_bit ("filemanager");
54 DebugBits PBD::DEBUG::Pool = PBD::new_debug_bit ("pool");
55 DebugBits PBD::DEBUG::EventLoop = PBD::new_debug_bit ("eventloop");
56 DebugBits PBD::DEBUG::AbstractUI = PBD::new_debug_bit ("abstractui");
57 DebugBits PBD::DEBUG::FileUtils = PBD::new_debug_bit ("fileutils");
58 DebugBits PBD::DEBUG::Configuration = PBD::new_debug_bit ("configuration");
59 DebugBits PBD::DEBUG::UndoHistory = PBD::new_debug_bit ("undohistory");
60 DebugBits PBD::DEBUG::Timing = PBD::new_debug_bit ("timing");
61 DebugBits PBD::DEBUG::Threads = PBD::new_debug_bit ("threads");
62 DebugBits PBD::DEBUG::Locale = PBD::new_debug_bit ("locale");
63 DebugBits PBD::DEBUG::StringConvert = PBD::new_debug_bit ("stringconvert");
64 DebugBits PBD::DEBUG::DebugTimestamps = PBD::new_debug_bit ("debugtimestamps");
65
66 /* These are debug bits that are used by backends. Since these are loaded dynamically,
67    after command-line parsing, defining them in code that is part of the backend
68    doesn't make them available for command line parsing. Put them here.
69
70    This is sort of a hack, because it means that the debug bits aren't defined
71    with the code in which they are relevant. But providing access to debug bits
72    from dynamically loaded code, for use in command line parsing, is way above the pay grade
73    of this debug tracing scheme.
74 */
75 DebugBits PBD::DEBUG::WavesMIDI = PBD::new_debug_bit ("WavesMIDI");
76 DebugBits PBD::DEBUG::WavesAudio = PBD::new_debug_bit ("WavesAudio");
77
78 DebugBits PBD::debug_bits;
79
80 DebugBits
81 PBD::new_debug_bit (const char* name)
82 {
83         DebugBits ret;
84         DebugMap::iterator i =_debug_bit_map().find (name);
85
86         if (i != _debug_bit_map().end()) {
87                 return i->second;
88         }
89
90         if (_debug_bit >= debug_bits.size()) {
91                 cerr << "Too many debug bits defined, offender was " << name << endl;
92                 abort (); /*NOTREACHED*/
93         }
94
95         ret.set (_debug_bit++, 1);
96         _debug_bit_map().insert (make_pair (name, ret));
97         return ret;
98 }
99
100 void
101 PBD::debug_print (const char* prefix, string str)
102 {
103         if ((PBD::debug_bits & DEBUG::DebugTimestamps).any()) {
104                 printf ("%ld %s: %s", g_get_monotonic_time(), prefix, str.c_str());
105         } else {
106                 printf ("%s: %s", prefix, str.c_str());
107         }
108 }
109
110 int
111 PBD::parse_debug_options (const char* str)
112 {
113         string in_str = str;
114         typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
115         boost::char_separator<char> sep (",");
116         tokenizer tokens (in_str, sep);
117         DebugBits bits;
118
119         for (tokenizer::iterator tok_iter = tokens.begin(); tok_iter != tokens.end(); ++tok_iter) {
120                 if (*tok_iter == "list") {
121                         list_debug_options ();
122                         return 1;
123                 }
124
125                 if (*tok_iter == "all") {
126                         debug_bits.set (); /* sets all bits */
127                         return 0;
128                 }
129
130                 for (map<const char*,DebugBits>::iterator i = _debug_bit_map().begin(); i != _debug_bit_map().end(); ++i) {
131                         const char* cstr = (*tok_iter).c_str();
132
133                         if (strncasecmp (cstr, i->first, strlen (cstr)) == 0) {
134                                 bits |= i->second;
135                                 cout << i->first << " set ... debug bits now set to " << bits << " using " << i->second << endl;
136                         }
137                 }
138         }
139
140         debug_bits = bits;
141
142         return 0;
143 }
144
145 void
146 PBD::list_debug_options ()
147 {
148         cout << _("The following debug options are available. Separate multiple options with commas.\nNames are case-insensitive and can be abbreviated.") << endl << endl;
149         cout << '\t' << X_("all") << endl;
150
151         vector<string> options;
152
153         for (map<const char*,DebugBits>::iterator i = _debug_bit_map().begin(); i != _debug_bit_map().end(); ++i) {
154                 options.push_back (i->first);
155         }
156
157         sort (options.begin(), options.end());
158
159         for (vector<string>::iterator i = options.begin(); i != options.end(); ++i) {
160                 cout << "\t" << (*i) << endl;
161         }
162 }