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 <mutex>
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 // 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 
60 namespace gnash {
61 
62 // This is a basic file logging class
64 {
65 public:
66 
67  static LogFile& getDefaultInstance();
68 
69  ~LogFile();
70 
71  enum LogLevel {
75  LOG_EXTRA
76  };
77 
78  enum FileState {
82  IDLE
83  };
84 
86  //
93  void log(const std::string& label, const std::string& msg);
94 
96  //
100  void log(const std::string& msg);
101 
103  //
106  bool removeLog();
107 
109  //
112  bool closeLog();
113 
115  //
120  void setLogFilename(const std::string& fname);
121 
122  // accessors for the verbose level
123  void setVerbosity() {
124  ++_verbose;
125  }
126 
127  void setVerbosity(int x) {
128  _verbose = x;
129  }
130 
131  int getVerbosity() const {
132  return _verbose;
133  }
134 
135  void setActionDump(int x) {
136  _actiondump = x;
137  }
138 
139  void setNetwork(int x) {
140  _network = x;
141  }
142 
143  int getActionDump() const {
144  return _actiondump;
145  }
146 
147  int getNetwork() const {
148  return _network;
149  }
150 
151  void setParserDump (int x) {
152  _parserdump = x;
153  }
154 
155  int getParserDump() const {
156  return _parserdump;
157  }
158 
159  void setStamp (bool b) {
160  _stamp = b;
161  }
162 
163  bool getStamp() const {
164  return _stamp;
165  }
166 
168  void setWriteDisk(bool b);
169 
170  bool getWriteDisk() const {
171  return _write;
172  }
173 
174  typedef void (*logListener)(const std::string& s);
175 
176  void registerLogCallback(logListener l) { _listener = l; }
177 
178 private:
179 
181  //
186  bool openLog(const std::string& filespec);
187 
191  //
198  bool openLogIfNeeded();
199 
200  // Use getDefaultInstance for getting the singleton
201  LogFile ();
202 
204  std::mutex _ioMutex;
205 
207  std::ofstream _outstream;
208 
210  int _verbose;
211 
213  bool _actiondump;
214 
216  bool _network;
217 
219  bool _parserdump;
220 
222  FileState _state;
223 
224  bool _stamp;
225 
227  bool _write;
228 
229  std::string _filespec;
230 
231  std::string _logFilename;
232 
233  logListener _listener;
234 
235 };
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_aserror(const boost::format& fmt);
247 DSOEXPORT void processLog_abc(const boost::format& fmt);
248 
249 template <typename FuncType>
250 inline void
251 log_impl(boost::format& fmt, FuncType func)
252 {
253  func(fmt);
254 }
255 
256 template<typename FuncType, typename Arg, typename... Args>
257 inline void
258 log_impl(boost::format& fmt, FuncType processFunc, Arg arg, Args... args)
259 {
260  fmt % arg;
261  log_impl(fmt, processFunc, args...);
262 }
263 
264 template<typename StringType, typename FuncType, typename... Args>
265 inline void
266 log_impl(StringType msg, FuncType func, Args... args)
267 {
268  boost::format fmt(msg);
269  using namespace boost::io;
270  fmt.exceptions(all_error_bits ^ (too_many_args_bit |
271  too_few_args_bit |
272  bad_format_string_bit));
273  log_impl(fmt, func, args...);
274 }
275 
276 template<typename StringType, typename... Args>
277 inline void log_network(StringType msg, Args... args)
278 {
279  log_impl(msg, processLog_network, args...);
280 }
281 
282 template<typename StringType, typename... Args>
283 inline void log_error(StringType msg, Args... args)
284 {
285  log_impl(msg, processLog_error, args...);
286 }
287 
288 template<typename StringType, typename... Args>
289 inline void log_unimpl(StringType msg, Args... args)
290 {
291  log_impl(msg, processLog_unimpl, args...);
292 }
293 
294 template<typename StringType, typename... Args>
295 inline void log_trace(StringType msg, Args... args)
296 {
297  log_impl(msg, processLog_trace, args...);
298 }
299 
300 template<typename StringType, typename... Args>
301 inline void log_debug(StringType msg, Args... args)
302 {
303  log_impl(msg, processLog_debug, args...);
304 }
305 
306 template<typename StringType, typename... Args>
307 inline void log_action(StringType msg, Args... args)
308 {
309  log_impl(msg, processLog_action, args...);
310 }
311 
312 template<typename StringType, typename... Args>
313 inline void log_parse(StringType msg, Args... args)
314 {
315  log_impl(msg, processLog_parse, args...);
316 }
317 
318 template<typename StringType, typename... Args>
319 inline void log_security(StringType msg, Args... args)
320 {
321  log_impl(msg, processLog_security, args...);
322 }
323 
324 template<typename StringType, typename... Args>
325 inline void log_swferror(StringType msg, Args... args)
326 {
327  log_impl(msg, processLog_swferror, args...);
328 }
329 
330 template<typename StringType, typename... Args>
331 inline void log_aserror(StringType msg, Args... args)
332 {
333  log_impl(msg, processLog_aserror, args...);
334 }
335 
336 template<typename StringType, typename... Args>
337 inline void log_abc(StringType msg, Args... args)
338 {
339  log_impl(msg, processLog_abc, args...);
340 }
341 
343 //
349 DSOEXPORT std::string hexify(const unsigned char *bytes, size_t length,
350  bool ascii);
351 
352 // Define to 0 to completely remove parse debugging at compile-time
353 #ifndef VERBOSE_PARSE
354 #define VERBOSE_PARSE 1
355 #endif
356 
357 // Define to 0 to completely remove action debugging at compile-time
358 #ifndef VERBOSE_ACTION
359 #define VERBOSE_ACTION 1
360 #endif
361 
362 // Define to 0 to remove ActionScript errors verbosity at compile-time
363 #ifndef VERBOSE_ASCODING_ERRORS
364 #define VERBOSE_ASCODING_ERRORS 1
365 #endif
366 
367 // Define to 0 this to remove invalid SWF verbosity at compile-time
368 #ifndef VERBOSE_MALFORMED_SWF
369 #define VERBOSE_MALFORMED_SWF 1
370 #endif
371 
372 // Define to 0 this to remove Networking verbosity at compile-time
373 #ifndef VERBOSE_NETWORKING
374 #define VERBOSE_NETWORKING 1
375 #endif
376 
377 #if VERBOSE_PARSE
378 #define IF_VERBOSE_PARSE(x) do { if ( LogFile::getDefaultInstance().getParserDump() ) { x; } } while (0);
379 #else
380 #define IF_VERBOSE_PARSE(x)
381 #endif
382 
383 #if VERBOSE_ACTION
384 #define IF_VERBOSE_ACTION(x) do { if ( LogFile::getDefaultInstance().getActionDump() ) { x; } } while (0);
385 #else
386 #define IF_VERBOSE_ACTION(x)
387 #endif
388 
389 #if VERBOSE_ACTION
390 #define IF_VERBOSE_NETWORK(x) do { if ( LogFile::getDefaultInstance().getNetwork() ) { x; } } while (0);
391 #else
392 #define IF_VERBOSE_NETWORK(x)
393 #endif
394 
395 #if VERBOSE_ASCODING_ERRORS
396 // TODO: check if it's worth to check verbosity level too...
397 #define IF_VERBOSE_ASCODING_ERRORS(x) { if ( gnash::RcInitFile::getDefaultInstance().showASCodingErrors() ) { x; } }
398 #else
399 #define IF_VERBOSE_ASCODING_ERRORS(x)
400 #endif
401 
402 #if VERBOSE_MALFORMED_SWF
403 // TODO: check if it's worth to check verbosity level too...
404 #define IF_VERBOSE_MALFORMED_SWF(x) { if ( gnash::RcInitFile::getDefaultInstance().showMalformedSWFErrors() ) { x; } }
405 #else
406 #define IF_VERBOSE_MALFORMED_SWF(x)
407 #endif
408 
410 {
411 public:
412  // Only print function tracing messages when multiple -v
413  // options have been supplied.
414  HostFunctionReport() : _func(nullptr) {
415  log_debug("entering");
416  }
417 
418  HostFunctionReport(const char* func) : _func(func) {
419  if (func) {
420  log_debug("%s enter", func);
421  }
422  else {
423  log_debug("No Function Name! enter");
424  }
425  }
427  log_debug("%s returning", _func);
428  }
429 private:
430  const char* _func;
431 };
432 
433 #ifndef HAVE_FUNCTION
434  #ifndef HAVE_func
435  #define dummystr(x) # x
436  #define dummyestr(x) dummystr(x)
437  #define __FUNCTION__ __FILE__ ":" dummyestr(__LINE__)
438  #else
439  #define __FUNCTION__ __func__
440  #endif
441 #endif
442 
443 #ifndef HAVE_PRETTY_FUNCTION
444  #define __PRETTY_FUNCTION__ __FUNCTION__
445 #endif
446 
447 #if defined(__cplusplus) && defined(__GNUC__)
448 #define GNASH_REPORT_FUNCTION \
449  const gnash::HostFunctionReport hfr(__PRETTY_FUNCTION__)
450 #define GNASH_REPORT_RETURN
451 #else
452 #define GNASH_REPORT_FUNCTION \
453  gnash::log_debug("entering")
454 
455 #define GNASH_REPORT_RETURN \
456  gnash::log_debug("returning")
457 #endif
458 
459 }
460 
461 #endif // GNASH_LOG_H
462 
463 
464 // Local Variables:
465 // mode: C++
466 // indent-tabs-mode: nil
467 // End:
void setActionDump(int x)
Definition: log.h:135
int getActionDump() const
Definition: log.h:143
void setVerbosity(int x)
Definition: log.h:127
HostFunctionReport()
Definition: log.h:414
void setParserDump(int x)
Definition: log.h:151
Definition: log.h:74
void processLog_action(const boost::format &fmt)
Definition: log.cpp:231
Definition: log.h:73
void registerLogCallback(logListener l)
Definition: log.h:176
int getParserDump() const
Definition: log.h:155
Definition: GnashKey.h:158
Anonymous namespace for callbacks, local functions, event handlers etc.
Definition: dbus_ext.cpp:40
Definition: log.h:79
int getNetwork() const
Definition: log.h:147
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:48
~HostFunctionReport()
Definition: log.h:426
void log_parse(StringType msg, Args... args)
Definition: log.h:313
Definition: log.h:80
void processLog_abc(const boost::format &fmt)
Definition: log.cpp:142
void processLog_trace(const boost::format &fmt)
Definition: log.cpp:119
void log_aserror(StringType msg, Args... args)
Definition: log.h:331
void setNetwork(int x)
Definition: log.h:139
void log_security(StringType msg, Args... args)
Definition: log.h:319
void processLog_network(const boost::format &fmt)
Definition: log.cpp:165
void log_unimpl(StringType msg, Args... args)
Definition: log.h:289
void log_error(StringType msg, Args... args)
Definition: log.h:283
void processLog_error(const boost::format &fmt)
Definition: log.cpp:176
void log_network(StringType msg, Args... args)
Definition: log.h:277
void log_action(StringType msg, Args... args)
Definition: log.h:307
void processLog_security(const boost::format &fmt)
Definition: log.cpp:198
void setVerbosity()
Definition: log.h:123
Definition: klash_part.cpp:329
void log_abc(StringType msg, Args... args)
Definition: log.h:337
void processLog_parse(const boost::format &fmt)
Definition: log.cpp:154
std::int32_t x
Definition: BitmapData_as.cpp:434
void log_swferror(StringType msg, Args... args)
Definition: log.h:325
Definition: GnashKey.h:148
#define DSOEXPORT
Definition: dsodefs.h:55
Definition: log.h:409
bool getWriteDisk() const
Definition: log.h:170
bool getStamp() const
Definition: log.h:163
void processLog_aserror(const boost::format &fmt)
Definition: log.cpp:220
FileState
Definition: log.h:78
void log_impl(boost::format &fmt, FuncType func)
Definition: log.h:251
Definition: log.h:63
LogLevel
Definition: log.h:71
int getVerbosity() const
Definition: log.h:131
void setStamp(bool b)
Definition: log.h:159
Definition: log.h:72
Definition: GnashKey.h:165
Definition: log.h:81
void log_debug(StringType msg, Args... args)
Definition: log.h:301
void processLog_debug(const boost::format &fmt)
Definition: log.cpp:130
void processLog_swferror(const boost::format &fmt)
Definition: log.cpp:209
HostFunctionReport(const char *func)
Definition: log.h:418
void processLog_unimpl(const boost::format &fmt)
Definition: log.cpp:187
void log_trace(StringType msg, Args... args)
Definition: log.h:295