Gnash  0.8.11dev
log.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
3 // Free Software Foundation, Inc
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 
19 #ifndef GNASH_LOG_H
20 #define GNASH_LOG_H
21 
22 #ifdef HAVE_CONFIG_H
23 #include "gnashconfig.h"
24 #endif
25 
26 #include "rc.h" // for IF_VERBOSE_* implementation
27 #include "dsodefs.h" // for DSOEXPORT
28 
29 #include <fstream>
30 #include <boost/thread/mutex.hpp>
31 #include <boost/format.hpp>
32 
33 // This is needed so we can print to the Android log file, which can
34 // be retrieved with logcat.
35 #ifdef __ANDROID__
36 #include <android/log.h>
37 #endif
38 
39 // the default name for the debug log
40 #define DEFAULT_LOGFILE "gnash-dbg.log"
41 
42 // Support compilation with (or without) native language support
43 #include "gettext.h"
44 #define _(String) gettext (String)
45 #define N_(String) gettext_noop (String)
46 
47 // Macro to prevent repeated logging calls for the same
48 // event
49 #define LOG_ONCE(x) { \
50  static bool warned = false; \
51  if (!warned) { warned = true; x; } \
52 }
53 
54 # include <boost/preprocessor/arithmetic/inc.hpp>
55 # include <boost/preprocessor/repetition/enum_params.hpp>
56 # include <boost/preprocessor/repetition/repeat.hpp>
57 # include <boost/preprocessor/repetition/repeat_from_to.hpp>
58 # include <boost/preprocessor/seq/for_each.hpp>
59 
60 // Mingw32 (win32 console) doesn't use the standard GCC defines that
61 // Gnash used for debug messages, so make it so...
62 #ifndef __FUNCDNAME__
63 #define __FUNCDNAME__ __FUNCTION__
64 #endif
65 
66 namespace gnash {
67 
68 // This is a basic file logging class
70 {
71 public:
72 
73  static LogFile& getDefaultInstance();
74 
75  ~LogFile();
76 
77  enum LogLevel {
81  LOG_EXTRA
82  };
83 
84  enum FileState {
88  IDLE
89  };
90 
92  //
99  void log(const std::string& label, const std::string& msg);
100 
102  //
106  void log(const std::string& msg);
107 
109  //
112  bool removeLog();
113 
115  //
118  bool closeLog();
119 
121  //
126  void setLogFilename(const std::string& fname);
127 
128  // accessors for the verbose level
129  void setVerbosity() {
130  ++_verbose;
131  }
132 
133  void setVerbosity(int x) {
134  _verbose = x;
135  }
136 
137  int getVerbosity() const {
138  return _verbose;
139  }
140 
141  void setActionDump(int x) {
142  _actiondump = x;
143  }
144 
145  void setNetwork(int x) {
146  _network = x;
147  }
148 
149  int getActionDump() const {
150  return _actiondump;
151  }
152 
153  int getNetwork() const {
154  return _network;
155  }
156 
157  void setParserDump (int x) {
158  _parserdump = x;
159  }
160 
161  int getParserDump() const {
162  return _parserdump;
163  }
164 
165  void setStamp (bool b) {
166  _stamp = b;
167  }
168 
169  bool getStamp() const {
170  return _stamp;
171  }
172 
174  void setWriteDisk(bool b);
175 
176  bool getWriteDisk() const {
177  return _write;
178  }
179 
180  typedef void (*logListener)(const std::string& s);
181 
182  void registerLogCallback(logListener l) { _listener = l; }
183 
184 private:
185 
187  //
192  bool openLog(const std::string& filespec);
193 
197  //
204  bool openLogIfNeeded();
205 
206  // Use getDefaultInstance for getting the singleton
207  LogFile ();
208 
210  boost::mutex _ioMutex;
211 
213  std::ofstream _outstream;
214 
216  int _verbose;
217 
219  bool _actiondump;
220 
222  bool _network;
223 
225  bool _parserdump;
226 
228  FileState _state;
229 
230  bool _stamp;
231 
233  bool _write;
234 
235  std::string _filespec;
236 
237  std::string _logFilename;
238 
239  logListener _listener;
240 
241 };
242 
243 DSOEXPORT void processLog_network(const boost::format& fmt);
244 DSOEXPORT void processLog_error(const boost::format& fmt);
245 DSOEXPORT void processLog_unimpl(const boost::format& fmt);
246 DSOEXPORT void processLog_trace(const boost::format& fmt);
247 DSOEXPORT void processLog_debug(const boost::format& fmt);
248 DSOEXPORT void processLog_action(const boost::format& fmt);
249 DSOEXPORT void processLog_parse(const boost::format& fmt);
250 DSOEXPORT void processLog_security(const boost::format& fmt);
251 DSOEXPORT void processLog_swferror(const boost::format& fmt);
252 DSOEXPORT void processLog_aserror(const boost::format& fmt);
253 DSOEXPORT void processLog_abc(const boost::format& fmt);
254 
257 //
260 #define TOKENIZE_FORMAT(z, n, t) % t##n
261 
265 #define TOKENIZE_ARGS(z, n, t) BOOST_PP_COMMA_IF(n) const T##n& t##n
266 
270 #define LOG_TYPES (error) (debug) (unimpl) (aserror) (swferror) \
271  (security) (action) (parse) (trace) (abc) (network)
272 
275 //
291 #define LOG_TEMPLATES(z, n, data)\
292 template<BOOST_PP_ENUM_PARAMS(BOOST_PP_INC(n), typename T)>\
293 inline void log_##data(BOOST_PP_REPEAT(BOOST_PP_INC(n), TOKENIZE_ARGS, t)) \
294 {\
295  if (LogFile::getDefaultInstance().getVerbosity() == 0) return; \
296  boost::format f(t0); \
297  using namespace boost::io; \
298  f.exceptions(all_error_bits ^ (too_many_args_bit | \
299  too_few_args_bit | \
300  bad_format_string_bit)); \
301  processLog_##data(f BOOST_PP_REPEAT_FROM_TO(1, \
302  BOOST_PP_INC(n), \
303  TOKENIZE_FORMAT, t));\
304 }
305 
307 //
310 #define ARG_NUMBER 10
311 
315 #define GENERATE_LOG_TYPES(r, _, t) \
316  BOOST_PP_REPEAT(ARG_NUMBER, LOG_TEMPLATES, t)
317 
320 BOOST_PP_SEQ_FOR_EACH(GENERATE_LOG_TYPES, _, LOG_TYPES)
321 
322 #undef TOKENIZE_ARGS
323 #undef TOKENIZE_FORMAT
324 #undef GENERATE_LOG_TYPES
325 #undef LOG_TEMPLATES
326 #undef ARG_NUMBER
327 
329 //
335 DSOEXPORT std::string hexify(const unsigned char *bytes, size_t length,
336  bool ascii);
337 
338 // Define to 0 to completely remove parse debugging at compile-time
339 #ifndef VERBOSE_PARSE
340 #define VERBOSE_PARSE 1
341 #endif
342 
343 // Define to 0 to completely remove action debugging at compile-time
344 #ifndef VERBOSE_ACTION
345 #define VERBOSE_ACTION 1
346 #endif
347 
348 // Define to 0 to remove ActionScript errors verbosity at compile-time
349 #ifndef VERBOSE_ASCODING_ERRORS
350 #define VERBOSE_ASCODING_ERRORS 1
351 #endif
352 
353 // Define to 0 this to remove invalid SWF verbosity at compile-time
354 #ifndef VERBOSE_MALFORMED_SWF
355 #define VERBOSE_MALFORMED_SWF 1
356 #endif
357 
358 // Define to 0 this to remove Networking verbosity at compile-time
359 #ifndef VERBOSE_NETWORKING
360 #define VERBOSE_NETWORKING 1
361 #endif
362 
363 #if VERBOSE_PARSE
364 #define IF_VERBOSE_PARSE(x) do { if ( LogFile::getDefaultInstance().getParserDump() ) { x; } } while (0);
365 #else
366 #define IF_VERBOSE_PARSE(x)
367 #endif
368 
369 #if VERBOSE_ACTION
370 #define IF_VERBOSE_ACTION(x) do { if ( LogFile::getDefaultInstance().getActionDump() ) { x; } } while (0);
371 #else
372 #define IF_VERBOSE_ACTION(x)
373 #endif
374 
375 #if VERBOSE_ACTION
376 #define IF_VERBOSE_NETWORK(x) do { if ( LogFile::getDefaultInstance().getNetwork() ) { x; } } while (0);
377 #else
378 #define IF_VERBOSE_NETWORK(x)
379 #endif
380 
381 #if VERBOSE_ASCODING_ERRORS
382 // TODO: check if it's worth to check verbosity level too...
383 #define IF_VERBOSE_ASCODING_ERRORS(x) { if ( gnash::RcInitFile::getDefaultInstance().showASCodingErrors() ) { x; } }
384 #else
385 #define IF_VERBOSE_ASCODING_ERRORS(x)
386 #endif
387 
388 #if VERBOSE_MALFORMED_SWF
389 // TODO: check if it's worth to check verbosity level too...
390 #define IF_VERBOSE_MALFORMED_SWF(x) { if ( gnash::RcInitFile::getDefaultInstance().showMalformedSWFErrors() ) { x; } }
391 #else
392 #define IF_VERBOSE_MALFORMED_SWF(x)
393 #endif
394 
396 {
397 public:
398  // Only print function tracing messages when multiple -v
399  // options have been supplied.
400  HostFunctionReport() : _func(0) {
401  log_debug("entering");
402  }
403 
404  HostFunctionReport(const char* func) : _func(func) {
405  if (func) {
406  log_debug("%s enter", func);
407  }
408  else {
409  log_debug("No Function Name! enter");
410  }
411  }
413  log_debug("%s returning", _func);
414  }
415 private:
416  const char* _func;
417 };
418 
419 #ifndef HAVE_FUNCTION
420  #ifndef HAVE_func
421  #define dummystr(x) # x
422  #define dummyestr(x) dummystr(x)
423  #define __FUNCTION__ __FILE__":"dummyestr(__LINE__)
424  #else
425  #define __FUNCTION__ __func__
426  #endif
427 #endif
428 
429 #ifndef HAVE_PRETTY_FUNCTION
430  #define __PRETTY_FUNCTION__ __FUNCTION__
431 #endif
432 
433 #if defined(__cplusplus) && defined(__GNUC__)
434 #define GNASH_REPORT_FUNCTION \
435  const gnash::HostFunctionReport hfr(__PRETTY_FUNCTION__)
436 #define GNASH_REPORT_RETURN
437 #else
438 #define GNASH_REPORT_FUNCTION \
439  gnash::log_debug("entering")
440 
441 #define GNASH_REPORT_RETURN \
442  gnash::log_debug("returning")
443 #endif
444 
445 }
446 
447 #endif // GNASH_LOG_H
448 
449 
450 // Local Variables:
451 // mode: C++
452 // indent-tabs-mode: nil
453 // End:
void setActionDump(int x)
Definition: log.h:141
void setVerbosity(int x)
Definition: log.h:133
HostFunctionReport()
Definition: log.h:400
void setParserDump(int x)
Definition: log.h:157
Definition: log.h:80
void processLog_action(const boost::format &fmt)
Definition: log.cpp:235
SWFStream & s
Definition: DefineBitsTag.cpp:73
Definition: log.h:79
bool getWriteDisk() const
Definition: log.h:176
void registerLogCallback(logListener l)
Definition: log.h:182
Definition: GnashKey.h:158
int getVerbosity() const
Definition: log.h:137
Definition: log.h:85
std::string hexify(const unsigned char *p, size_t length, bool ascii)
Convert a sequence of bytes to hex or ascii format.
Definition: log.cpp:51
~HostFunctionReport()
Definition: log.h:412
Definition: log.h:86
int getNetwork() const
Definition: log.h:153
void processLog_abc(const boost::format &fmt)
Definition: log.cpp:146
void
Definition: npapi/plugin.cpp:1286
void processLog_trace(const boost::format &fmt)
Definition: log.cpp:123
#define _(String)
Definition: log.h:44
void setNetwork(int x)
Definition: log.h:145
void processLog_network(const boost::format &fmt)
Definition: log.cpp:169
void processLog_error(const boost::format &fmt)
Definition: log.cpp:180
void processLog_security(const boost::format &fmt)
Definition: log.cpp:202
void setVerbosity()
Definition: log.h:129
Definition: klash_part.cpp:329
void processLog_parse(const boost::format &fmt)
Definition: log.cpp:158
boost::int32_t x
Definition: BitmapData_as.cpp:434
Definition: GnashKey.h:148
#define DSOEXPORT
Definition: dsodefs.h:55
Definition: log.h:395
int getParserDump() const
Definition: log.h:161
void processLog_aserror(const boost::format &fmt)
Definition: log.cpp:224
FileState
Definition: log.h:84
Definition: log.h:69
LogLevel
Definition: log.h:77
void setStamp(bool b)
Definition: log.h:165
Definition: log.h:78
#define GENERATE_LOG_TYPES(r, _, t)
Definition: log.h:315
int getActionDump() const
Definition: log.h:149
Definition: log.h:87
bool getStamp() const
Definition: log.h:169
void processLog_debug(const boost::format &fmt)
Definition: log.cpp:134
#define LOG_TYPES
Definition: log.h:270
void processLog_swferror(const boost::format &fmt)
Definition: log.cpp:213
HostFunctionReport(const char *func)
Definition: log.h:404
void processLog_unimpl(const boost::format &fmt)
Definition: log.cpp:191