~ubuntu-branches/ubuntu/precise/hydrogen/precise

« back to all changes in this revision

Viewing changes to .pc/1005_werror_format_security.diff/libs/hydrogen/src/object.cpp

  • Committer: Package Import Robot
  • Author(s): Matthias Klose
  • Date: 2011-12-06 09:56:34 UTC
  • Revision ID: package-import@ubuntu.com-20111206095634-3cb3pwj0us0f5nwg
Tags: 0.9.5-3ubuntu1
Fix build error with -Werror=format-security. Closes: #643406.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
#include <hydrogen/util.h>
 
25
 
 
26
#include <QDir>
 
27
#include <iostream>
 
28
#include <pthread.h>
 
29
#include <cassert>
 
30
#include <strings.h>
 
31
 
 
32
#ifdef WIN32
 
33
#include <windows.h>
 
34
#else
 
35
#include <unistd.h>
 
36
#endif
 
37
 
 
38
using namespace std;
 
39
 
 
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;
 
44
 
 
45
/**
 
46
 * Constructor
 
47
 */
 
48
Object::Object( const QString& class_name )
 
49
                : __class_name( class_name )
 
50
{
 
51
        ++__objects;
 
52
        __logger = Logger::get_instance();
 
53
 
 
54
        if ( __use_log ) {
 
55
                int nInstances = __object_map[ __class_name ];
 
56
                ++nInstances;
 
57
                __object_map[ __class_name ] = nInstances;
 
58
        }
 
59
}
 
60
 
 
61
 
 
62
/**
 
63
 * Copy constructor
 
64
 */
 
65
Object::Object( const Object& obj )
 
66
{
 
67
 
 
68
        __class_name = obj.get_class_name();
 
69
 
 
70
        ++__objects;
 
71
//      __class_name = obj.getClassName;
 
72
        __logger = Logger::get_instance();
 
73
 
 
74
        if ( __use_log ) {
 
75
                int nInstances = __object_map[ __class_name ];
 
76
                ++nInstances;
 
77
                __object_map[ __class_name ] = nInstances;
 
78
        }
 
79
}
 
80
 
 
81
 
 
82
/**
 
83
 * Destructor
 
84
 */
 
85
Object::~Object()
 
86
{
 
87
        --__objects;
 
88
 
 
89
        if ( __use_log ) {
 
90
                int nInstances = __object_map[ __class_name ];
 
91
                --nInstances;
 
92
                __object_map[ __class_name ] = nInstances;
 
93
        }
 
94
}
 
95
 
 
96
 
 
97
 
 
98
 
 
99
 
 
100
/**
 
101
 * Return the number of Objects not deleted
 
102
 */
 
103
int Object::get_objects_number()
 
104
{
 
105
        return __objects;
 
106
}
 
107
 
 
108
 
 
109
void Object::set_logging_level(const char* level)
 
110
{
 
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";
 
116
        bool use;
 
117
        unsigned log_level;
 
118
 
 
119
        // insert hex-detecting code here.  :-)
 
120
 
 
121
        if( 0 == strncasecmp( level, none, sizeof(none) ) ) {
 
122
                log_level = 0;
 
123
                use = false;
 
124
        } else if ( 0 == strncasecmp( level, error, sizeof(error) ) ) {
 
125
                log_level = Logger::Error;
 
126
                use = true;
 
127
        } else if ( 0 == strncasecmp( level, warning, sizeof(warning) ) ) {
 
128
                log_level = Logger::Error | Logger::Warning;
 
129
                use = true;
 
130
        } else if ( 0 == strncasecmp( level, info, sizeof(info) ) ) {
 
131
                log_level = Logger::Error | Logger::Warning | Logger::Info;
 
132
                use = true;
 
133
        } else if ( 0 == strncasecmp( level, debug, sizeof(debug) ) ) {
 
134
                log_level = Logger::Error | Logger::Warning | Logger::Info | Logger::Debug;
 
135
                use = true;
 
136
        } else {
 
137
                int val = H2Core::hextoi(level, -1);
 
138
                if( val == 0 ) {
 
139
                        // Probably means hex was invalid.  Use -VNone instead.
 
140
                        log_level = Logger::Error;
 
141
                } else {
 
142
                        log_level = val;
 
143
                        if( log_level & ~0x1 ) {
 
144
                                use = true;
 
145
                        } else {
 
146
                                use = false;
 
147
                        }
 
148
                }
 
149
        }
 
150
 
 
151
        Logger::set_log_level( log_level );
 
152
        __use_log = use;
 
153
        Logger::get_instance()->__use_file = use;
 
154
}
 
155
 
 
156
 
 
157
 
 
158
bool Object::is_using_verbose_log()
 
159
{
 
160
        return __use_log;
 
161
}
 
162
 
 
163
 
 
164
 
 
165
void Object::print_object_map()
 
166
{
 
167
        if (!__use_log) {
 
168
                std::cout << "[Object::print_object_map -- "
 
169
                        "object map disabled.]" << std::endl;
 
170
                return;
 
171
        }
 
172
 
 
173
        std::cout << "[Object::print_object_map]" << std::endl;
 
174
 
 
175
        map<QString, int>::iterator iter = __object_map.begin();
 
176
        int nTotal = 0;
 
177
        do {
 
178
                int nInstances = ( *iter ).second;
 
179
                QString sObject = ( *iter ).first;
 
180
                if ( nInstances != 0 ) {
 
181
                        std::cout << nInstances << "\t" << sObject.toLocal8Bit().constData() << std::endl;
 
182
                }
 
183
                nTotal += nInstances;
 
184
                iter++;
 
185
        } while ( iter != __object_map.end() );
 
186
 
 
187
        std::cout << "Total : " << nTotal << " objects." << std::endl;
 
188
}
 
189
 
 
190
 
 
191
 
 
192
////////////////////////
 
193
 
 
194
 
 
195
Logger* Logger::__instance = NULL;
 
196
 
 
197
pthread_t loggerThread;
 
198
 
 
199
void* loggerThread_func( void* param )
 
200
{
 
201
        if ( param == NULL ) {
 
202
                // ??????
 
203
                return NULL;
 
204
        }
 
205
 
 
206
#ifdef WIN32
 
207
        ::AllocConsole();
 
208
//      ::SetConsoleTitle( "Hydrogen debug log" );
 
209
        freopen( "CONOUT$", "wt", stdout );
 
210
#endif
 
211
 
 
212
        Logger *pLogger = ( Logger* )param;
 
213
 
 
214
        FILE *pLogFile = NULL;
 
215
        if ( pLogger->__use_file ) {
 
216
#ifdef Q_OS_MACX
 
217
                QString sLogFilename = QDir::homePath().append( "/Library/Hydrogen/hydrogen.log" );
 
218
#else
 
219
                QString sLogFilename = QDir::homePath().append( "/.hydrogen/hydrogen.log" );
 
220
#endif
 
221
 
 
222
                pLogFile = fopen( sLogFilename.toLocal8Bit(), "w" );
 
223
                if ( pLogFile == NULL ) {
 
224
                        std::cerr << "Error: can't open log file for writing..." << std::endl;
 
225
                } else {
 
226
                        fprintf( pLogFile, "Start logger" );
 
227
                }
 
228
        }
 
229
 
 
230
        while ( pLogger->__running ) {
 
231
#ifdef WIN32
 
232
                Sleep( 1000 );
 
233
#else
 
234
                usleep( 999999 );
 
235
#endif
 
236
 
 
237
                Logger::queue_t& queue = pLogger->__msg_queue;
 
238
                Logger::queue_t::iterator it, last;
 
239
                QString tmpString;
 
240
                for( it = last = queue.begin() ; it != queue.end() ; ++it ) {
 
241
                        last = it;
 
242
                        printf( it->toLocal8Bit() );
 
243
                        if( pLogFile ) {
 
244
                                fprintf( pLogFile, it->toLocal8Bit() );
 
245
                                fflush( pLogFile );
 
246
                        }
 
247
                }
 
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 );
 
254
        }
 
255
 
 
256
        if ( pLogFile ) {
 
257
                fprintf( pLogFile, "Stop logger" );
 
258
                fclose( pLogFile );
 
259
        }
 
260
#ifdef WIN32
 
261
        ::FreeConsole();
 
262
#endif
 
263
 
 
264
 
 
265
#ifdef WIN32
 
266
        Sleep( 1000 );
 
267
#else
 
268
        usleep( 100000 );
 
269
#endif
 
270
 
 
271
        pthread_exit( NULL );
 
272
        return NULL;
 
273
}
 
274
 
 
275
void Logger::create_instance()
 
276
{
 
277
        if ( __instance == 0 ) {
 
278
                __instance = new Logger;
 
279
        }
 
280
}
 
281
 
 
282
/**
 
283
 * Constructor
 
284
 */
 
285
Logger::Logger()
 
286
                : __use_file( false )
 
287
                , __running( true )
 
288
{
 
289
        __instance = this;
 
290
        pthread_attr_t attr;
 
291
        pthread_attr_init( &attr );
 
292
        pthread_mutex_init( &__mutex, NULL );
 
293
        pthread_create( &loggerThread, &attr, loggerThread_func, this );
 
294
}
 
295
 
 
296
/**
 
297
 * Destructor
 
298
 */
 
299
Logger::~Logger()
 
300
{
 
301
        __running = false;
 
302
        pthread_join( loggerThread, NULL );
 
303
 
 
304
}
 
305
 
 
306
void Logger::log( unsigned level,
 
307
                  const char* funcname,
 
308
                  const QString& class_name,
 
309
                  const QString& msg )
 
310
{
 
311
        if( level == None ) return;
 
312
 
 
313
        const char* prefix[] = { "", "(E) ", "(W) ", "(I) ", "(D) " };
 
314
#ifdef WIN32
 
315
        const char* color[] = { "", "", "", "", "" };
 
316
#else
 
317
        const char* color[] = { "", "\033[31m", "\033[36m", "\033[32m", "" };
 
318
#endif // WIN32
 
319
 
 
320
        int i;
 
321
        switch(level) {
 
322
        case None:
 
323
                assert(false);
 
324
                i = 0;
 
325
                break;
 
326
        case Error:
 
327
                i = 1;
 
328
                break;
 
329
        case Warning:
 
330
                i = 2;
 
331
                break;
 
332
        case Info:
 
333
                i = 3;
 
334
                break;
 
335
        case Debug:
 
336
                i = 4;
 
337
                break;
 
338
        default:
 
339
                i = 0;
 
340
                break;
 
341
        }
 
342
 
 
343
        QString tmp = QString("%1%2%3\t%4 %5 \033[0m\n")
 
344
                .arg(color[i])
 
345
                .arg(prefix[i])
 
346
                .arg(class_name)
 
347
                .arg(funcname)
 
348
                .arg(msg);
 
349
 
 
350
        pthread_mutex_lock( &__mutex);
 
351
        __msg_queue.push_back( tmp );
 
352
        pthread_mutex_unlock( &__mutex );
 
353
}