3
* Copyright(c) 2002-2008 by Alex >Comix< Cominu [comix@users.sourceforge.net]
5
* http://www.hydrogen-music.org
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.
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.
17
* You should have received a copy of the GNU General Public License
18
* along with this program; if not, write to the Free Software
19
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23
#include <hydrogen/Object.h>
24
#include <hydrogen/util.h>
40
unsigned int Object::__objects = 0;
41
bool Object::__use_log = false;
42
std::map<QString, int> Object::__object_map;
43
unsigned Logger::__log_level = 0;
48
Object::Object( const QString& class_name )
49
: __class_name( class_name )
52
__logger = Logger::get_instance();
55
int nInstances = __object_map[ __class_name ];
57
__object_map[ __class_name ] = nInstances;
65
Object::Object( const Object& obj )
68
__class_name = obj.get_class_name();
71
// __class_name = obj.getClassName;
72
__logger = Logger::get_instance();
75
int nInstances = __object_map[ __class_name ];
77
__object_map[ __class_name ] = nInstances;
90
int nInstances = __object_map[ __class_name ];
92
__object_map[ __class_name ] = nInstances;
101
* Return the number of Objects not deleted
103
int Object::get_objects_number()
109
void Object::set_logging_level(const char* level)
111
const char none[] = "None";
112
const char error[] = "Error";
113
const char warning[] = "Warning";
114
const char info[] = "Info";
115
const char debug[] = "Debug";
119
// insert hex-detecting code here. :-)
121
if( 0 == strncasecmp( level, none, sizeof(none) ) ) {
124
} else if ( 0 == strncasecmp( level, error, sizeof(error) ) ) {
125
log_level = Logger::Error;
127
} else if ( 0 == strncasecmp( level, warning, sizeof(warning) ) ) {
128
log_level = Logger::Error | Logger::Warning;
130
} else if ( 0 == strncasecmp( level, info, sizeof(info) ) ) {
131
log_level = Logger::Error | Logger::Warning | Logger::Info;
133
} else if ( 0 == strncasecmp( level, debug, sizeof(debug) ) ) {
134
log_level = Logger::Error | Logger::Warning | Logger::Info | Logger::Debug;
137
int val = H2Core::hextoi(level, -1);
139
// Probably means hex was invalid. Use -VNone instead.
140
log_level = Logger::Error;
143
if( log_level & ~0x1 ) {
151
Logger::set_log_level( log_level );
153
Logger::get_instance()->__use_file = use;
158
bool Object::is_using_verbose_log()
165
void Object::print_object_map()
168
std::cout << "[Object::print_object_map -- "
169
"object map disabled.]" << std::endl;
173
std::cout << "[Object::print_object_map]" << std::endl;
175
map<QString, int>::iterator iter = __object_map.begin();
178
int nInstances = ( *iter ).second;
179
QString sObject = ( *iter ).first;
180
if ( nInstances != 0 ) {
181
std::cout << nInstances << "\t" << sObject.toLocal8Bit().constData() << std::endl;
183
nTotal += nInstances;
185
} while ( iter != __object_map.end() );
187
std::cout << "Total : " << nTotal << " objects." << std::endl;
192
////////////////////////
195
Logger* Logger::__instance = NULL;
197
pthread_t loggerThread;
199
void* loggerThread_func( void* param )
201
if ( param == NULL ) {
208
// ::SetConsoleTitle( "Hydrogen debug log" );
209
freopen( "CONOUT$", "wt", stdout );
212
Logger *pLogger = ( Logger* )param;
214
FILE *pLogFile = NULL;
215
if ( pLogger->__use_file ) {
217
QString sLogFilename = QDir::homePath().append( "/Library/Hydrogen/hydrogen.log" );
219
QString sLogFilename = QDir::homePath().append( "/.hydrogen/hydrogen.log" );
222
pLogFile = fopen( sLogFilename.toLocal8Bit(), "w" );
223
if ( pLogFile == NULL ) {
224
std::cerr << "Error: can't open log file for writing..." << std::endl;
226
fprintf( pLogFile, "Start logger" );
230
while ( pLogger->__running ) {
237
Logger::queue_t& queue = pLogger->__msg_queue;
238
Logger::queue_t::iterator it, last;
240
for( it = last = queue.begin() ; it != queue.end() ; ++it ) {
242
printf( it->toLocal8Bit() );
244
fprintf( pLogFile, it->toLocal8Bit() );
248
// See Object.h for documentation on __mutex and when
249
// it should be locked.
250
queue.erase( queue.begin(), last );
251
pthread_mutex_lock( &pLogger->__mutex );
252
if( ! queue.empty() ) queue.pop_front();
253
pthread_mutex_unlock( &pLogger->__mutex );
257
fprintf( pLogFile, "Stop logger" );
271
pthread_exit( NULL );
275
void Logger::create_instance()
277
if ( __instance == 0 ) {
278
__instance = new Logger;
286
: __use_file( false )
291
pthread_attr_init( &attr );
292
pthread_mutex_init( &__mutex, NULL );
293
pthread_create( &loggerThread, &attr, loggerThread_func, this );
302
pthread_join( loggerThread, NULL );
306
void Logger::log( unsigned level,
307
const char* funcname,
308
const QString& class_name,
311
if( level == None ) return;
313
const char* prefix[] = { "", "(E) ", "(W) ", "(I) ", "(D) " };
315
const char* color[] = { "", "", "", "", "" };
317
const char* color[] = { "", "\033[31m", "\033[36m", "\033[32m", "" };
343
QString tmp = QString("%1%2%3\t%4 %5 \033[0m\n")
350
pthread_mutex_lock( &__mutex);
351
__msg_queue.push_back( tmp );
352
pthread_mutex_unlock( &__mutex );