| [73] | 1 | /* |
|---|
| 2 | * Hydrogen |
|---|
| 3 | * Copyright(c) 2002-2008 by Alex >Comix< Cominu [comix@users.sourceforge.net] |
|---|
| 4 | * |
|---|
| 5 | * http://www.hydrogen-music.org |
|---|
| 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 |
|---|
| 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 |
|---|
| 20 | * |
|---|
| 21 | */ |
|---|
| 22 | |
|---|
| 23 | #include <hydrogen/Object.h> |
|---|
| 24 | |
|---|
| 25 | #include <QDir> |
|---|
| 26 | #include <iostream> |
|---|
| 27 | #include <pthread.h> |
|---|
| 28 | |
|---|
| 29 | #ifdef WIN32 |
|---|
| 30 | #include <windows.h> |
|---|
| 31 | #else |
|---|
| 32 | #include <unistd.h> |
|---|
| 33 | #endif |
|---|
| 34 | |
|---|
| 35 | using namespace std; |
|---|
| 36 | |
|---|
| 37 | unsigned int Object::__objects = 0; |
|---|
| 38 | bool Object::__use_log = false; |
|---|
| [127] | 39 | std::map<QString, int> Object::__object_map; |
|---|
| [73] | 40 | |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | /** |
|---|
| 45 | * Constructor |
|---|
| 46 | */ |
|---|
| [127] | 47 | Object::Object( const QString& class_name ) |
|---|
| [73] | 48 | : __class_name( class_name ) |
|---|
| 49 | { |
|---|
| 50 | ++__objects; |
|---|
| 51 | __logger = Logger::get_instance(); |
|---|
| 52 | |
|---|
| [539] | 53 | #ifdef CONFIG_DEBUG |
|---|
| [205] | 54 | Object::use_verbose_log( true ); |
|---|
| [539] | 55 | #endif |
|---|
| [205] | 56 | |
|---|
| [73] | 57 | if ( __use_log ) { |
|---|
| 58 | int nInstances = __object_map[ __class_name ]; |
|---|
| 59 | ++nInstances; |
|---|
| 60 | __object_map[ __class_name ] = nInstances; |
|---|
| 61 | } |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | |
|---|
| 65 | /** |
|---|
| 66 | * Copy constructor |
|---|
| 67 | */ |
|---|
| 68 | Object::Object( const Object& obj ) |
|---|
| 69 | { |
|---|
| [205] | 70 | |
|---|
| [539] | 71 | #ifdef CONFIG_DEBUG |
|---|
| [205] | 72 | use_verbose_log( true ); |
|---|
| [539] | 73 | #endif |
|---|
| [73] | 74 | __class_name = obj.get_class_name(); |
|---|
| 75 | |
|---|
| 76 | ++__objects; |
|---|
| 77 | // __class_name = obj.getClassName; |
|---|
| 78 | __logger = Logger::get_instance(); |
|---|
| 79 | |
|---|
| 80 | if ( __use_log ) { |
|---|
| 81 | int nInstances = __object_map[ __class_name ]; |
|---|
| 82 | ++nInstances; |
|---|
| 83 | __object_map[ __class_name ] = nInstances; |
|---|
| 84 | } |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | |
|---|
| 88 | /** |
|---|
| 89 | * Destructor |
|---|
| 90 | */ |
|---|
| 91 | Object::~Object() |
|---|
| 92 | { |
|---|
| 93 | --__objects; |
|---|
| 94 | |
|---|
| 95 | if ( __use_log ) { |
|---|
| 96 | int nInstances = __object_map[ __class_name ]; |
|---|
| 97 | --nInstances; |
|---|
| 98 | __object_map[ __class_name ] = nInstances; |
|---|
| 99 | } |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | |
|---|
| 106 | /** |
|---|
| 107 | * Return the number of Objects not deleted |
|---|
| 108 | */ |
|---|
| 109 | int Object::get_objects_number() |
|---|
| 110 | { |
|---|
| 111 | return __objects; |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | |
|---|
| 115 | void Object::use_verbose_log( bool bUse ) |
|---|
| 116 | { |
|---|
| 117 | __use_log = bUse; |
|---|
| 118 | Logger::get_instance()->__use_file = bUse; |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | |
|---|
| 122 | |
|---|
| 123 | bool Object::is_using_verbose_log() |
|---|
| 124 | { |
|---|
| 125 | return __use_log; |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | |
|---|
| 129 | |
|---|
| 130 | void Object::print_object_map() |
|---|
| 131 | { |
|---|
| 132 | std::cout << "[Object::print_object_map]" << std::endl; |
|---|
| 133 | |
|---|
| [127] | 134 | map<QString, int>::iterator iter = __object_map.begin(); |
|---|
| [73] | 135 | int nTotal = 0; |
|---|
| 136 | do { |
|---|
| 137 | int nInstances = ( *iter ).second; |
|---|
| [127] | 138 | QString sObject = ( *iter ).first; |
|---|
| [73] | 139 | if ( nInstances != 0 ) { |
|---|
| [127] | 140 | std::cout << nInstances << "\t" << sObject.toStdString() << std::endl; |
|---|
| [73] | 141 | } |
|---|
| 142 | nTotal += nInstances; |
|---|
| 143 | iter++; |
|---|
| 144 | } while ( iter != __object_map.end() ); |
|---|
| 145 | |
|---|
| 146 | std::cout << "Total : " << nTotal << " objects." << std::endl; |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | |
|---|
| 150 | |
|---|
| 151 | //////////////////////// |
|---|
| 152 | |
|---|
| 153 | |
|---|
| 154 | Logger* Logger::__instance = NULL; |
|---|
| 155 | |
|---|
| 156 | pthread_t loggerThread; |
|---|
| 157 | |
|---|
| 158 | void* loggerThread_func( void* param ) |
|---|
| 159 | { |
|---|
| 160 | if ( param == NULL ) { |
|---|
| 161 | // ?????? |
|---|
| 162 | return NULL; |
|---|
| 163 | } |
|---|
| 164 | |
|---|
| 165 | #ifdef WIN32 |
|---|
| 166 | ::AllocConsole(); |
|---|
| 167 | // ::SetConsoleTitle( "Hydrogen debug log" ); |
|---|
| 168 | freopen( "CONOUT$", "wt", stdout ); |
|---|
| 169 | #endif |
|---|
| 170 | |
|---|
| 171 | Logger *pLogger = ( Logger* )param; |
|---|
| 172 | |
|---|
| 173 | FILE *pLogFile = NULL; |
|---|
| 174 | if ( pLogger->__use_file ) { |
|---|
| 175 | #ifdef Q_OS_MACX |
|---|
| [127] | 176 | QString sLogFilename = QDir::homePath().append( "/Library/Hydrogen/hydrogen.log" ); |
|---|
| [73] | 177 | #else |
|---|
| [127] | 178 | QString sLogFilename = QDir::homePath().append( "/.hydrogen/hydrogen.log" ); |
|---|
| [73] | 179 | #endif |
|---|
| 180 | |
|---|
| [127] | 181 | pLogFile = fopen( sLogFilename.toAscii(), "w" ); |
|---|
| [73] | 182 | if ( pLogFile == NULL ) { |
|---|
| 183 | std::cerr << "Error: can't open log file for writing..." << std::endl; |
|---|
| 184 | } else { |
|---|
| 185 | fprintf( pLogFile, "Start logger" ); |
|---|
| 186 | } |
|---|
| 187 | } |
|---|
| 188 | |
|---|
| 189 | while ( pLogger->__running ) { |
|---|
| 190 | #ifdef WIN32 |
|---|
| 191 | Sleep( 1000 ); |
|---|
| 192 | #else |
|---|
| 193 | usleep( 1000000 ); |
|---|
| 194 | #endif |
|---|
| 195 | pthread_mutex_lock( &pLogger->__logger_mutex ); |
|---|
| 196 | |
|---|
| 197 | |
|---|
| [127] | 198 | vector<QString>::iterator it; |
|---|
| 199 | QString tmpString; |
|---|
| [73] | 200 | while ( ( it = pLogger->__msg_queue.begin() ) != pLogger->__msg_queue.end() ) { |
|---|
| 201 | tmpString = *it; |
|---|
| 202 | pLogger->__msg_queue.erase( it ); |
|---|
| [127] | 203 | printf( tmpString.toAscii() ); |
|---|
| [73] | 204 | |
|---|
| 205 | if ( pLogFile ) { |
|---|
| [127] | 206 | fprintf( pLogFile, tmpString.toAscii() ); |
|---|
| [73] | 207 | fflush( pLogFile ); |
|---|
| 208 | } |
|---|
| 209 | } |
|---|
| 210 | pthread_mutex_unlock( &pLogger->__logger_mutex ); |
|---|
| 211 | } |
|---|
| 212 | |
|---|
| 213 | if ( pLogFile ) { |
|---|
| 214 | fprintf( pLogFile, "Stop logger" ); |
|---|
| 215 | fclose( pLogFile ); |
|---|
| 216 | } |
|---|
| 217 | #ifdef WIN32 |
|---|
| 218 | ::FreeConsole(); |
|---|
| 219 | #endif |
|---|
| 220 | |
|---|
| 221 | |
|---|
| 222 | #ifdef WIN32 |
|---|
| 223 | Sleep( 1000 ); |
|---|
| 224 | #else |
|---|
| 225 | usleep( 100000 ); |
|---|
| 226 | #endif |
|---|
| 227 | |
|---|
| 228 | pthread_exit( NULL ); |
|---|
| 229 | return NULL; |
|---|
| 230 | } |
|---|
| 231 | |
|---|
| 232 | |
|---|
| 233 | Logger* Logger::get_instance() |
|---|
| 234 | { |
|---|
| 235 | if ( !__instance ) { |
|---|
| 236 | __instance = new Logger(); |
|---|
| 237 | } |
|---|
| 238 | return __instance; |
|---|
| 239 | } |
|---|
| 240 | |
|---|
| 241 | |
|---|
| 242 | /** |
|---|
| 243 | * Constructor |
|---|
| 244 | */ |
|---|
| 245 | Logger::Logger() |
|---|
| 246 | : __use_file( false ) |
|---|
| 247 | , __running( true ) |
|---|
| 248 | { |
|---|
| 249 | pthread_attr_t attr; |
|---|
| 250 | pthread_attr_init( &attr ); |
|---|
| 251 | |
|---|
| 252 | pthread_mutex_init( &__logger_mutex, NULL ); |
|---|
| 253 | |
|---|
| 254 | |
|---|
| 255 | pthread_create( &loggerThread, &attr, loggerThread_func, this ); |
|---|
| 256 | } |
|---|
| 257 | |
|---|
| 258 | |
|---|
| 259 | |
|---|
| 260 | |
|---|
| 261 | /** |
|---|
| 262 | * Destructor |
|---|
| 263 | */ |
|---|
| 264 | Logger::~Logger() |
|---|
| 265 | { |
|---|
| 266 | __running = false; |
|---|
| 267 | pthread_join( loggerThread, NULL ); |
|---|
| 268 | |
|---|
| 269 | } |
|---|
| 270 | |
|---|
| 271 | |
|---|
| 272 | |
|---|
| 273 | |
|---|
| 274 | |
|---|
| [127] | 275 | void Logger::info_log( const char* funcname, const QString& class_name, const QString& logMsg ) |
|---|
| [73] | 276 | { |
|---|
| 277 | if ( !Object::is_using_verbose_log() ) return; |
|---|
| 278 | |
|---|
| 279 | pthread_mutex_lock( &__logger_mutex ); |
|---|
| 280 | |
|---|
| [127] | 281 | QString prefix = "(I) " + class_name + "\t" + funcname; |
|---|
| 282 | |
|---|
| [73] | 283 | #ifdef WIN32 |
|---|
| [127] | 284 | __msg_queue.push_back( QString( "%1 %2\n" ).arg(prefix).arg(logMsg).toUtf8() ); |
|---|
| [73] | 285 | #else |
|---|
| [127] | 286 | __msg_queue.push_back( QString( "\033[32m%1 %2 \033[0m\n" ).arg(prefix).arg(logMsg) ); |
|---|
| [73] | 287 | #endif |
|---|
| 288 | |
|---|
| 289 | pthread_mutex_unlock( &__logger_mutex ); |
|---|
| 290 | |
|---|
| 291 | } |
|---|
| 292 | |
|---|
| 293 | |
|---|
| 294 | |
|---|
| 295 | |
|---|
| [127] | 296 | void Logger::warning_log( const char* funcname, const QString& class_name, const QString& logMsg ) |
|---|
| [73] | 297 | { |
|---|
| 298 | pthread_mutex_lock( &__logger_mutex ); |
|---|
| [127] | 299 | QString prefix = "(W) " + class_name + "\t" + funcname; |
|---|
| [73] | 300 | |
|---|
| 301 | #ifdef WIN32 |
|---|
| [127] | 302 | __msg_queue.push_back( prefix + logMsg + "\n" ); |
|---|
| [73] | 303 | #else |
|---|
| [127] | 304 | __msg_queue.push_back( QString( "\033[36m%1 %2\033[0m\n" ).arg( prefix ).arg( logMsg ) ); |
|---|
| [73] | 305 | #endif |
|---|
| 306 | |
|---|
| 307 | pthread_mutex_unlock( &__logger_mutex ); |
|---|
| 308 | } |
|---|
| 309 | |
|---|
| 310 | |
|---|
| 311 | |
|---|
| [127] | 312 | void Logger::error_log( const char* funcname, const QString& class_name, const QString& logMsg ) |
|---|
| [73] | 313 | { |
|---|
| 314 | pthread_mutex_lock( &__logger_mutex ); |
|---|
| 315 | |
|---|
| [127] | 316 | QString prefix = QString( "(E) %1\t%2" ).arg( class_name ).arg( funcname ); |
|---|
| 317 | |
|---|
| [73] | 318 | #ifdef WIN32 |
|---|
| [127] | 319 | __msg_queue.push_back( prefix + logMsg + "\n" ); |
|---|
| [73] | 320 | #else |
|---|
| [127] | 321 | __msg_queue.push_back( QString( "\033[31m%1 %2\033[0m\n" ).arg( prefix ).arg( logMsg ) ); |
|---|
| [73] | 322 | #endif |
|---|
| 323 | |
|---|
| 324 | pthread_mutex_unlock( &__logger_mutex ); |
|---|
| 325 | } |
|---|
| 326 | |
|---|
| 327 | |
|---|
| 328 | |
|---|
| 329 | |
|---|
| 330 | /* |
|---|
| 331 | #ifndef WIN32 |
|---|
| 332 | |
|---|
| 333 | |
|---|
| 334 | static const WORD bgMask( BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED | BACKGROUND_INTENSITY ); |
|---|
| 335 | |
|---|
| 336 | |
|---|
| 337 | void Logger__setREDColor() |
|---|
| 338 | { |
|---|
| 339 | CONSOLE_SCREEN_BUFFER_INFO csbi; |
|---|
| 340 | HANDLE hCon = GetStdHandle( STD_OUTPUT_HANDLE ); |
|---|
| 341 | GetConsoleScreenBufferInfo(hCon, &csbi); |
|---|
| 342 | |
|---|
| 343 | // WORD wRGBI = GetRGBI(fgColor, FOREGROUND_INTENSITY, FOREGROUND_RED, FOREGROUND_GREEN, FOREGROUND_BLUE); |
|---|
| 344 | WORD color = 10 | 255; |
|---|
| 345 | csbi.wAttributes &= bgMask; |
|---|
| 346 | csbi.wAttributes |= color; |
|---|
| 347 | |
|---|
| 348 | SetConsoleTextAttribute( hCon, csbi.wAttributes ); |
|---|
| 349 | |
|---|
| 350 | // wRGBI = GetRGBI(bgColor, BACKGROUND_INTENSITY, BACKGROUND_RED, BACKGROUND_GREEN, BACKGROUND_BLUE); |
|---|
| 351 | |
|---|
| 352 | // csbi.wAttributes &= fgMask; |
|---|
| 353 | // csbi.wAttributes |= wRGBI; |
|---|
| 354 | |
|---|
| 355 | SetConsoleTextAttribute( hCon, csbi.wAttributes ); |
|---|
| 356 | } |
|---|
| 357 | |
|---|
| 358 | |
|---|
| 359 | #endif |
|---|
| 360 | */ |
|---|
| 361 | |
|---|