~ubuntu-branches/ubuntu/lucid/lastfm/lucid

« back to all changes in this revision

Viewing changes to src/libUnicorn/logger.h

  • Committer: Bazaar Package Importer
  • Author(s): Devid Filoni
  • Date: 2008-07-14 16:46:20 UTC
  • mfrom: (1.1.7 upstream) (3.1.1 lenny)
  • Revision ID: james.westby@ubuntu.com-20080714164620-67hoz9fs177wpgmr
Tags: 1:1.5.1.31879.dfsg-1ubuntu1
* Merge from Debian unstable (LP: #248100), remaining changes:
  - debian/rules: add dh_icons call
  + debian/control:
    - switch iceweasel to firefox in Recommends field
    - modify debhelper version to >= 5.0.51~
    - modify Maintainer to Ubuntu MOTU Developers

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/***************************************************************************
2
 
 *   Copyright (C) 2005 - 2007 by                                          *
 
2
 *   Copyright (C) 2005 - 2008 by                                          *
3
3
 *      Christian Muehlhaeuser, Last.fm Ltd <chris@last.fm>                *
4
 
 *      Erik Jaelevik, Last.fm Ltd <erik@last.fm>                          *
5
4
 *                                                                         *
6
5
 *   This program is free software; you can redistribute it and/or modify  *
7
6
 *   it under the terms of the GNU General Public License as published by  *
24
23
 
25
24
#include "UnicornDllExportMacro.h"
26
25
 
27
 
 
28
 
#include <QMutex>
29
 
#include <QThread>
30
 
#include <QString>
31
 
#include <QDebug>
32
 
#include <QtGlobal>
33
 
 
34
26
#include <string>
35
27
#include <fstream>
36
28
#include <sstream>
40
32
#include <ctime>
41
33
#include <cstdlib>
42
34
 
 
35
#ifdef QT_CORE_LIB
 
36
    #include <QString>
 
37
#endif
 
38
 
 
39
#ifdef WIN32
 
40
    #define LFM_LOGGER_STRING std::wstring
 
41
    #include <windows.h>
 
42
#else
 
43
    #define LFM_LOGGER_STRING std::string
 
44
#endif
 
45
 
43
46
#define FUNCTION_NAME ( std::string( __FUNCTION__ ) )
44
47
#define LINE_NUMBER ( __LINE__ )
45
48
 
48
51
{                                                                            \
49
52
    std::ostringstream ss;                                                   \
50
53
    ss << msg;                                                               \
51
 
    Logger& lg = Logger::GetLogger();                                        \
 
54
    Logger& lg = Logger::GetLogger();                            \
52
55
    lg.Log( (Logger::Severity)level, ss.str(), FUNCTION_NAME, LINE_NUMBER ); \
53
56
}
54
57
 
 
58
#define LOGW(level, msg)                                                      \
 
59
{                                                                            \
 
60
    std::wostringstream ss;                                                   \
 
61
    ss << msg;                                                               \
 
62
    Logger& lg = Logger::GetLogger();                            \
 
63
    lg.LogW( (Logger::Severity)level, ss.str(), FUNCTION_NAME, LINE_NUMBER ); \
 
64
}
 
65
 
55
66
// Like LOG but with added line break
56
67
#define LOGL(level, msg) LOG(level, msg << "\n")
57
68
 
 
69
#define LOGWL(level, msg) LOGW(level, msg << L"\n")
 
70
 
58
71
// Global VERIFY macro
59
72
// Wrap these around statements that should always be executed to verify that
60
73
// they're always true (connects are a good example)
65
78
}
66
79
 
67
80
/*************************************************************************/ /**
68
 
    Extra inserter to handle QStrings.
69
 
******************************************************************************/
70
 
inline std::ostream&
71
 
operator<<(std::ostream& os, const QString& qs)
72
 
{
73
 
    os << qs.toAscii().data();
74
 
    return os;
75
 
}
76
 
 
77
 
/*************************************************************************/ /**
78
81
    Simple logging class
79
82
******************************************************************************/
80
83
class UNICORN_DLLEXPORT Logger
90
93
    };
91
94
 
92
95
    std::ofstream mFileOut;
93
 
    QMutex mMutex;
94
 
    QtMsgHandler mDefaultMsgHandler;
 
96
 
 
97
#ifdef WIN32
 
98
    CRITICAL_SECTION mMutex;
 
99
#else
 
100
    pthread_mutex_t mMutex;
 
101
#endif
95
102
 
96
103
    /*********************************************************************/ /**
97
104
        Ctor
98
105
    **************************************************************************/
99
 
    Logger() : mDefaultMsgHandler(NULL), mLevel(Warning) {}
 
106
    Logger() : mLevel(Warning)
 
107
    #ifdef QT_CORE_LIB
 
108
        , mDefaultMsgHandler(NULL)
 
109
    #endif
 
110
    {
 
111
        #ifdef WIN32
 
112
            InitializeCriticalSection( &mMutex );
 
113
        #endif
 
114
    }
100
115
 
101
116
    /*********************************************************************/ /**
102
117
        Dtor
105
120
    ~Logger()
106
121
    {
107
122
        mFileOut.close();
 
123
        #ifdef WIN32
 
124
            DeleteCriticalSection( &mMutex );
 
125
        #endif
108
126
    }
109
127
 
110
128
    /*********************************************************************/ /**
111
 
        Initalises the logger.
 
129
        Initialises the logger.
112
130
 
113
131
        @param[in] sFilename The file to log to.
114
132
        @param[in] bOverwrite If true, the file is wiped before starting.
115
133
    **************************************************************************/
116
134
    void
117
135
    Init(
118
 
        QString sFilename,
 
136
        LFM_LOGGER_STRING sFilename,
119
137
        bool bOverwrite = true);
 
138
        
 
139
        
 
140
    #ifdef QT_CORE_LIB
 
141
    void
 
142
    Init( 
 
143
        QString path,
 
144
        bool overwrite = true )
 
145
    {
 
146
        #ifdef WIN32
 
147
            Init( path.toStdWString(), overwrite );
 
148
        #else
 
149
            Init( path.toStdString(), overwrite );
 
150
        #endif
 
151
    }
 
152
    #endif
120
153
 
121
154
    /*********************************************************************/ /**
122
155
        Returns the static Logger object.
133
166
        std::string message,
134
167
        std::string function,
135
168
        int line );
 
169
 
 
170
    /*********************************************************************/ /**
 
171
        Used by redirected qDebug in release builds. Doesn't add any text.
 
172
    **************************************************************************/
 
173
    void
 
174
    Log(
 
175
        const char* message );
 
176
 
 
177
    #ifdef WIN32
 
178
    /*********************************************************************/ /**
 
179
        Called by LOGW macro to do the outputting of wide characters.
 
180
    **************************************************************************/
 
181
    void
 
182
    LogW(
 
183
        Severity level,
 
184
        std::wstring message,
 
185
        std::string function,
 
186
        int line );
 
187
    #endif
136
188
    
137
189
    /*********************************************************************/ /**
138
190
        Sets debug level.
150
202
    /*********************************************************************/ /**
151
203
        Gets current logfile path.
152
204
    **************************************************************************/
153
 
    QString
 
205
    LFM_LOGGER_STRING
154
206
    GetFilePath() const { return mFilePath; }
155
207
 
156
208
    /*********************************************************************/ /**
169
221
        return sTime;
170
222
    }
171
223
 
172
 
    /*********************************************************************/ /**
173
 
        Used to direct qDebug() output directly to the log, it formats itself
174
 
        correctly, and this is a temporary HACK until we redo this stuff
175
 
    **************************************************************************/
176
 
    
177
 
    void JustOutputThisHack( const char* msg );
178
 
 
179
224
private:
180
 
 
181
225
    Severity mLevel;
182
 
    QString mFilePath;
 
226
    LFM_LOGGER_STRING mFilePath;
 
227
 
 
228
public:
 
229
  #ifdef QT_CORE_LIB
 
230
    QtMsgHandler mDefaultMsgHandler;
 
231
  #endif
183
232
 
184
233
};
185
234
 
187
236
    #define __PRETTY_FUNCTION__ __FUNCTION__
188
237
#endif
189
238
 
 
239
#ifdef QT_CORE_LIB
 
240
 
 
241
#include <QThread>
190
242
#include <QDebug>
 
243
#include <QtGlobal>
 
244
 
 
245
/*************************************************************************/ /**
 
246
    Extra inserter to handle QStrings.
 
247
******************************************************************************/
 
248
inline std::ostream&
 
249
operator<<(std::ostream& os, const QString& qs)
 
250
{
 
251
    os << qs.toAscii().data();
 
252
    return os;
 
253
}
 
254
 
191
255
#ifndef QT_NO_DEBUG
192
256
 
193
257
/**
214
278
 
215
279
        m_time.start();
216
280
    }
217
 
    
 
281
 
218
282
    QDebugBlock( const QDebugBlock& that )
219
283
    {
220
284
        *this = that;
221
285
    }
222
 
    
 
286
 
223
287
    QDebugBlock &operator=( const QDebugBlock& block )
224
288
    {
225
289
        m_title = block.m_title;
226
290
        m_time = block.m_time;
227
 
        
 
291
 
228
292
        block.m_title = "";
229
 
        
 
293
 
230
294
        return *this;
231
295
    }
232
296
 
250
314
 
251
315
        return d;
252
316
    }
253
 
    
254
 
    
 
317
 
 
318
 
255
319
    /**
256
320
     * so you can do: Q_DEBUG_BLOCK << somestring;
257
321
     */
260
324
        QString const s = v.toString();
261
325
        if (!s.isEmpty())
262
326
            debug() << s;
263
 
        
 
327
 
264
328
        return *this;
265
329
    }
266
330
};
270
334
#define qDebug() QDebugBlock::debug()
271
335
 
272
336
 
 
337
#else // QT_NO_DEBUG
273
338
 
274
 
#else //Q_NO_DEBUG
275
339
    #include <QDateTime>
276
 
    
 
340
 
277
341
    #define Q_DEBUG_BLOCK qDebug()
278
342
    class QDebugBlock { public: QDebugBlock( QString ) {} };
279
 
    
 
343
 
 
344
    // If we don't do this, we won't get function names/line numbers from qDebug statements
280
345
    #define qDebug() qDebug() << QDateTime::currentDateTime().toUTC().toString( "yyMMdd hh:mm:ss" ) \
281
 
                              << '-' << QString("%1").arg( (int)QThread::currentThreadId(), 4 ) \
282
 
                              << '-' << __PRETTY_FUNCTION__ << '(' << __LINE__<< ") - L4\n  "
 
346
                              << '-' << QString( "%1" ).arg( (int)QThread::currentThreadId(), 8, 16, QChar( '0' ) ) \
 
347
                              << '-' << __PRETTY_FUNCTION__ << '(' << __LINE__<< ") - L3\n  "
 
348
 
 
349
    #define qWarning() qDebug()
283
350
#endif
284
351
 
 
352
#endif //QT_CORE_LIB
285
353
 
286
354
#endif //LOGGER_H