~ubuntu-branches/ubuntu/saucy/gnash/saucy-proposed

« back to all changes in this revision

Viewing changes to libbase/log.h

  • Committer: Bazaar Package Importer
  • Author(s): Sindhudweep Narayan Sarkar
  • Date: 2009-10-07 00:06:10 UTC
  • mfrom: (1.1.12 upstream)
  • Revision ID: james.westby@ubuntu.com-20091007000610-mj9rwqe774gizn1j
Tags: 0.8.6-0ubuntu1
new upstream release 0.8.6 (LP: #435897)

Show diffs side-by-side

added added

removed removed

Lines of Context:
40
40
 
41
41
// Macro to prevent repeated logging calls for the same
42
42
// event
43
 
#define LOG_ONCE(x) { static bool warned = false; if (!warned) { warned = true; x; } }
 
43
#define LOG_ONCE(x) { \
 
44
    static bool warned = false; \
 
45
    if (!warned) { warned = true; x; } \
 
46
}
44
47
 
45
 
// Define to switch between printf-style log formatting
46
 
// and boost::format
47
48
# include <boost/preprocessor/arithmetic/inc.hpp>
48
49
# include <boost/preprocessor/repetition/enum_params.hpp>
49
50
# include <boost/preprocessor/repetition/repeat.hpp>
50
51
# include <boost/preprocessor/repetition/repeat_from_to.hpp>
51
52
# include <boost/preprocessor/seq/for_each.hpp>
52
53
 
 
54
// Mingw32 (win32 console) doesn't use the standard GCC defines that
 
55
// Gnash used for debug messages, so make it so...
 
56
#ifndef __FUNCDNAME__
 
57
#define __FUNCDNAME__ __FUNCTION__
 
58
#endif
 
59
 
53
60
namespace gnash {
54
61
 
55
 
#define GNASH_DEBUG_LEVEL 2
56
 
 
57
62
// This is a basic file logging class
58
 
class DSOEXPORT LogFile {
 
63
class DSOEXPORT LogFile
 
64
{
59
65
public:
60
66
 
61
67
    static LogFile& getDefaultInstance();
62
68
 
63
69
    ~LogFile();
64
70
 
 
71
    enum LogLevel {
 
72
        LOG_SILENT,
 
73
        LOG_NORMAL,
 
74
        LOG_DEBUG,
 
75
        LOG_EXTRA
 
76
    };
 
77
 
65
78
    enum FileState {
66
79
        CLOSED,
67
80
        OPEN,
107
120
    void setLogFilename(const std::string& fname);
108
121
 
109
122
    // accessors for the verbose level
110
 
    void setVerbosity () {
111
 
        _verbose++;
 
123
    void setVerbosity() {
 
124
        ++_verbose;
112
125
    }
113
126
 
114
 
    void setVerbosity (int x) {
 
127
    void setVerbosity(int x) {
115
128
        _verbose = x;
116
129
    }
117
130
 
118
 
    int getVerbosity () {
 
131
    int getVerbosity() const {
119
132
        return _verbose;
120
133
    }
121
134
    
122
 
    void setActionDump (int x) {
 
135
    void setActionDump(int x) {
123
136
        _actiondump = x;
124
137
    }
125
138
 
126
 
    int getActionDump () {
 
139
    void setNetwork(int x) {
 
140
        _network = x;
 
141
    }
 
142
 
 
143
    int getActionDump() const {
127
144
        return _actiondump;
128
145
    }
129
146
    
 
147
    int getNetwork() const {
 
148
        return _network;
 
149
    }
 
150
    
130
151
    void setParserDump (int x) {
131
152
        _parserdump = x;
132
153
    }
133
154
 
134
 
    int getParserDump () {
 
155
    int getParserDump() const {
135
156
        return _parserdump;
136
157
    }
137
158
    
139
160
        _stamp = b;
140
161
    }
141
162
 
142
 
    bool getStamp () {
 
163
    bool getStamp() const {
143
164
        return _stamp;
144
165
    }
145
166
 
146
167
    /// Set whether to write logs to file
147
 
    void setWriteDisk (bool b);
 
168
    void setWriteDisk(bool b);
148
169
 
149
 
    bool getWriteDisk () {
 
170
    bool getWriteDisk() const {
150
171
        return _write;
151
172
    }
152
173
    
183
204
    boost::mutex _ioMutex;
184
205
 
185
206
    /// Stream to write to stdout.
186
 
    std::ofstream        _outstream;
 
207
    std::ofstream _outstream;
187
208
 
188
209
    /// How much output is required: 2 or more gives debug output.
189
 
    int          _verbose;
 
210
    int _verbose;
190
211
 
191
212
    /// Whether to dump all SWF actions
192
 
    bool                 _actiondump;
 
213
    bool _actiondump;
 
214
 
 
215
    /// Whether to dump all networking actions
 
216
    bool _network;
193
217
 
194
218
    /// Whether to dump parser output
195
 
    bool                 _parserdump;
 
219
    bool _parserdump;
196
220
 
197
221
    /// The state of the log file.
198
222
    FileState _state;
199
223
 
200
 
    bool                 _stamp;
 
224
    bool _stamp;
201
225
 
202
226
    /// Whether to write the log file to disk.
203
 
    bool                 _write;
 
227
    bool _write;
204
228
 
205
229
    std::string _filespec;
206
230
 
210
234
 
211
235
};
212
236
 
 
237
DSOEXPORT void processLog_network(const boost::format& fmt);
 
238
DSOEXPORT void processLog_error(const boost::format& fmt);
 
239
DSOEXPORT void processLog_unimpl(const boost::format& fmt);
 
240
DSOEXPORT void processLog_trace(const boost::format& fmt);
 
241
DSOEXPORT void processLog_debug(const boost::format& fmt);
 
242
DSOEXPORT void processLog_action(const boost::format& fmt);
 
243
DSOEXPORT void processLog_parse(const boost::format& fmt);
 
244
DSOEXPORT void processLog_security(const boost::format& fmt);
 
245
DSOEXPORT void processLog_swferror(const boost::format& fmt);
 
246
DSOEXPORT void processLog_amferror(const boost::format& fmt);
 
247
DSOEXPORT void processLog_aserror(const boost::format& fmt);
 
248
DSOEXPORT void processLog_abc(const boost::format& fmt);
 
249
 
213
250
/// This heap of steaming preprocessor code magically converts
214
251
/// printf-style statements into boost::format messages using templates.
215
252
//
225
262
/// This is a sequence of different log message types to be used in
226
263
/// the code. Append the name to log_ to call the function, e.g. 
227
264
/// log_error, log_unimpl.
228
 
#define LOG_TYPES (error) (debug) (unimpl) (aserror) (swferror) (amferror) (security) (action) (parse) (trace)
 
265
#define LOG_TYPES (error) (debug) (unimpl) (aserror) (swferror) \
 
266
    (amferror) (security) (action) (parse) (trace) (abc) (network)
229
267
 
230
268
/// This actually creates the template functions using the TOKENIZE
231
269
/// functions above. The templates look like this:
232
270
//
233
 
/// template< typename T0 , typename T1 , typename T2 , typename T3 > 
234
 
/// void
235
 
/// log_security (const T0& t0, const T1& t1, const T2& t2, const T3& t3)
 
271
/// template<typename T0 , typename T1 , typename T2>
 
272
/// void log_error(const T0& t0 , const T1& t1 , const T2& t2)
236
273
/// {
237
 
///     if (_verbosity == 0) return;
238
 
///     processLog_security(myFormat(t0) % t1 % t2 % t3);
 
274
///     if (LogFile::getDefaultInstance().getVerbosity() == 0) return;
 
275
///     boost::format f(t0);
 
276
///     using namespace boost::io;
 
277
///     f.exceptions(all_error_bits ^ (too_many_args_bit |
 
278
///                                    too_few_args_bit |
 
279
///                                    bad_format_string_bit));
 
280
///     processLog_error(f % t1 % t2);
239
281
/// }
240
 
//
 
282
///
241
283
/// Only not as nicely indented.
242
284
///
243
 
/// Use "g++ -E log.h" or "gcc log.h" to check.
 
285
/// Use "g++ -E log.h" or "cpp log.h" to check.
244
286
#define LOG_TEMPLATES(z, n, data)\
245
 
    template< \
246
 
         BOOST_PP_ENUM_PARAMS(\
247
 
         BOOST_PP_INC(n), typename T)\
248
 
     >\
249
 
    void log_##data (\
250
 
        BOOST_PP_REPEAT(\
251
 
        BOOST_PP_INC(n), \
252
 
        TOKENIZE_ARGS, t)\
253
 
    ) { \
 
287
template<BOOST_PP_ENUM_PARAMS(BOOST_PP_INC(n), typename T)>\
 
288
void log_##data(BOOST_PP_REPEAT(BOOST_PP_INC(n), TOKENIZE_ARGS, t)) \
 
289
{\
254
290
    if (LogFile::getDefaultInstance().getVerbosity() == 0) return; \
255
 
    processLog_##data(logFormat(t0) \
256
 
    BOOST_PP_REPEAT_FROM_TO(1, \
257
 
        BOOST_PP_INC(n), \
258
 
        TOKENIZE_FORMAT, t));\
259
 
    }\
 
291
    boost::format f(t0); \
 
292
    using namespace boost::io; \
 
293
    f.exceptions(all_error_bits ^ (too_many_args_bit | \
 
294
                                   too_few_args_bit | \
 
295
                                   bad_format_string_bit)); \
 
296
    processLog_##data(f BOOST_PP_REPEAT_FROM_TO(1, \
 
297
            BOOST_PP_INC(n), \
 
298
            TOKENIZE_FORMAT, t));\
 
299
}
260
300
 
261
301
/// Defines the maximum number of template arguments
262
302
//
263
303
/// The preprocessor generates templates with 1..ARG_NUMBER
264
304
/// arguments.
265
 
#define ARG_NUMBER 16
 
305
#define ARG_NUMBER 10
266
306
 
267
307
/// Calls the macro LOG_TEMPLATES an ARG_NUMBER number
268
308
/// of times, each time adding an extra typename argument to the
279
319
#undef LOG_TEMPLATES
280
320
#undef ARG_NUMBER
281
321
 
282
 
DSOEXPORT void processLog_error(const boost::format& fmt);
283
 
DSOEXPORT void processLog_unimpl(const boost::format& fmt);
284
 
DSOEXPORT void processLog_trace(const boost::format& fmt);
285
 
DSOEXPORT void processLog_debug(const boost::format& fmt);
286
 
DSOEXPORT void processLog_action(const boost::format& fmt);
287
 
DSOEXPORT void processLog_parse(const boost::format& fmt);
288
 
DSOEXPORT void processLog_security(const boost::format& fmt);
289
 
DSOEXPORT void processLog_swferror(const boost::format& fmt);
290
 
DSOEXPORT void processLog_amferror(const boost::format& fmt);
291
 
DSOEXPORT void processLog_aserror(const boost::format& fmt);
292
 
 
293
 
/// A fault-tolerant boost::format object for logging
294
 
//
295
 
/// Generally to be used in the LogFile macro BF(), which will also
296
 
/// be recognized by gettext for internationalization and is less
297
 
/// effort to type.
298
 
DSOEXPORT boost::format logFormat(const std::string &str);
299
 
 
300
322
/// Convert a sequence of bytes to hex or ascii format.
301
323
//
302
324
/// @param bytes    the array of bytes to process
304
326
///                 for checking that length does not exceed the array size.
305
327
/// @param ascii    whether to return in ascii or space-separated hex format.
306
328
/// @return         a string representation of the byte sequence.
307
 
DSOEXPORT std::string hexify(const unsigned char *bytes, size_t length, bool ascii);
 
329
DSOEXPORT std::string hexify(const unsigned char *bytes, size_t length,
 
330
        bool ascii);
308
331
 
309
332
// Define to 0 to completely remove parse debugging at compile-time
310
333
#ifndef VERBOSE_PARSE
331
354
#define VERBOSE_MALFORMED_AMF 1
332
355
#endif
333
356
 
 
357
// Define to 0 this to remove Networking verbosity at compile-time
 
358
#ifndef VERBOSE_NETWORKING
 
359
#define VERBOSE_NETWORKING 1
 
360
#endif
334
361
 
335
362
#if VERBOSE_PARSE
336
363
#define IF_VERBOSE_PARSE(x) do { if ( LogFile::getDefaultInstance().getParserDump() ) { x; } } while (0);
344
371
#define IF_VERBOSE_ACTION(x)
345
372
#endif
346
373
 
 
374
#if VERBOSE_ACTION
 
375
#define IF_VERBOSE_NETWORK(x) do { if ( LogFile::getDefaultInstance().getNetwork() ) { x; } } while (0);
 
376
#else
 
377
#define IF_VERBOSE_NETWORK(x)
 
378
#endif
 
379
 
347
380
#if VERBOSE_ASCODING_ERRORS
348
381
// TODO: check if it's worth to check verbosity level too...
349
382
#define IF_VERBOSE_ASCODING_ERRORS(x) { if ( gnash::RcInitFile::getDefaultInstance().showASCodingErrors() ) { x; } }
386
419
    }
387
420
 
388
421
    ~__Host_Function_Report__(void) {
389
 
        if (LogFile::getDefaultInstance().getVerbosity() >= GNASH_DEBUG_LEVEL + 1) {
 
422
        if (LogFile::getDefaultInstance().getVerbosity() > LogFile::LOG_DEBUG) {
390
423
            log_debug("%s returning", func);
391
424
        }
392
425
    }