2010-03-01 19:00:00 -05:00
/*
Copyright ( C ) 2009 Paul Davis
This program is free software ; you can redistribute it and / or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation ; either version 2 of the License , or
( at your option ) any later version .
This program is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU General Public License for more details .
You should have received a copy of the GNU General Public License
along with this program ; if not , write to the Free Software
Foundation , Inc . , 675 Mass Ave , Cambridge , MA 0213 9 , USA .
*/
# include <cstring>
# include <cstdlib>
# include <iostream>
# include <map>
2012-01-08 11:38:49 -05:00
# include <vector>
# include <algorithm>
2010-03-01 19:00:00 -05:00
2013-07-11 12:36:16 -04:00
# include <boost/tokenizer.hpp>
2010-03-01 19:00:00 -05:00
# include "pbd/debug.h"
# include "i18n.h"
using namespace std ;
static uint64_t _debug_bit = 1 ;
2013-03-20 17:18:55 -04:00
typedef std : : map < const char * , uint64_t > DebugMap ;
namespace PBD {
2013-03-24 15:18:20 -04:00
DebugMap & _debug_bit_map ( )
{
static DebugMap map ;
return map ;
}
2013-03-20 17:18:55 -04:00
}
2010-03-01 19:00:00 -05:00
uint64_t PBD : : DEBUG : : Stateful = PBD : : new_debug_bit ( " stateful " ) ;
uint64_t PBD : : DEBUG : : Properties = PBD : : new_debug_bit ( " properties " ) ;
2010-05-16 16:54:50 -04:00
uint64_t PBD : : DEBUG : : FileManager = PBD : : new_debug_bit ( " filemanager " ) ;
2011-01-08 10:19:32 -05:00
uint64_t PBD : : DEBUG : : Pool = PBD : : new_debug_bit ( " pool " ) ;
2012-04-24 12:45:38 -04:00
uint64_t PBD : : DEBUG : : EventLoop = PBD : : new_debug_bit ( " eventloop " ) ;
uint64_t PBD : : DEBUG : : AbstractUI = PBD : : new_debug_bit ( " abstractui " ) ;
2013-07-13 16:55:40 -04:00
uint64_t PBD : : DEBUG : : FileUtils = PBD : : new_debug_bit ( " fileutils " ) ;
2014-12-22 18:53:19 -05:00
uint64_t PBD : : DEBUG : : Configuration = PBD : : new_debug_bit ( " configuration " ) ;
2010-03-01 19:00:00 -05:00
uint64_t PBD : : debug_bits = 0x0 ;
uint64_t
PBD : : new_debug_bit ( const char * name )
{
uint64_t ret ;
2013-03-24 15:18:20 -04:00
_debug_bit_map ( ) . insert ( make_pair ( name , _debug_bit ) ) ;
2010-03-01 19:00:00 -05:00
ret = _debug_bit ;
_debug_bit < < = 1 ;
return ret ;
}
void
PBD : : debug_print ( const char * prefix , string str )
{
cerr < < prefix < < " : " < < str ;
}
void
PBD : : set_debug_bits ( uint64_t bits )
{
debug_bits = bits ;
}
int
PBD : : parse_debug_options ( const char * str )
{
2014-04-14 15:26:48 -04:00
string in_str = str ;
2013-07-11 12:36:16 -04:00
typedef boost : : tokenizer < boost : : char_separator < char > > tokenizer ;
boost : : char_separator < char > sep ( " , " ) ;
2014-04-14 15:26:48 -04:00
tokenizer tokens ( in_str , sep ) ;
2010-03-01 19:00:00 -05:00
uint64_t bits = 0 ;
2013-07-11 12:36:16 -04:00
for ( tokenizer : : iterator tok_iter = tokens . begin ( ) ; tok_iter ! = tokens . end ( ) ; + + tok_iter ) {
if ( * tok_iter = = " list " ) {
2010-03-01 19:00:00 -05:00
list_debug_options ( ) ;
return 1 ;
}
2013-07-11 12:36:16 -04:00
if ( * tok_iter = = " all " ) {
2010-03-01 19:00:00 -05:00
PBD : : set_debug_bits ( ~ 0ULL ) ;
return 0 ;
}
2013-03-24 15:18:20 -04:00
for ( map < const char * , uint64_t > : : iterator i = _debug_bit_map ( ) . begin ( ) ; i ! = _debug_bit_map ( ) . end ( ) ; + + i ) {
2013-07-11 12:36:16 -04:00
const char * cstr = ( * tok_iter ) . c_str ( ) ;
if ( strncasecmp ( cstr , i - > first , strlen ( cstr ) ) = = 0 ) {
2010-03-01 19:00:00 -05:00
bits | = i - > second ;
}
}
}
PBD : : set_debug_bits ( bits ) ;
return 0 ;
}
void
PBD : : list_debug_options ( )
{
2012-01-08 20:59:52 -05:00
cout < < _ ( " The following debug options are available. Separate multiple options with commas. \n Names are case-insensitive and can be abbreviated. " ) < < endl < < endl ;
2012-01-08 11:38:49 -05:00
cout < < ' \t ' < < X_ ( " all " ) < < endl ;
vector < string > options ;
2010-03-01 19:00:00 -05:00
2013-03-24 15:18:20 -04:00
for ( map < const char * , uint64_t > : : iterator i = _debug_bit_map ( ) . begin ( ) ; i ! = _debug_bit_map ( ) . end ( ) ; + + i ) {
2012-01-08 11:38:49 -05:00
options . push_back ( i - > first ) ;
2010-03-01 19:00:00 -05:00
}
2012-01-08 11:38:49 -05:00
sort ( options . begin ( ) , options . end ( ) ) ;
for ( vector < string > : : iterator i = options . begin ( ) ; i ! = options . end ( ) ; + + i ) {
cout < < " \t " < < ( * i ) < < endl ;
}
2010-03-01 19:00:00 -05:00
}