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:
gnash::LogFile::getDefaultInstance
static LogFile & getDefaultInstance()
Definition: log.cpp:77
gnash::processLog_trace
void processLog_trace(const boost::format &fmt)
Definition: log.cpp:119
gnash::key::l
@ l
Definition: GnashKey.h:158
gnash::LogFile::LogLevel
LogLevel
Definition: log.h:71
gnash::processLog_action
void processLog_action(const boost::format &fmt)
Definition: log.cpp:231
gnash::LogFile::removeLog
bool removeLog()
Remove the log file.
Definition: log.cpp:365
gnash::log_swferror
void log_swferror(StringType msg, Args... args)
Definition: log.h:325
gnash::processLog_security
void processLog_security(const boost::format &fmt)
Definition: log.cpp:198
gnash::LogFile::FileState
FileState
Definition: log.h:78
gnash::log_action
void log_action(StringType msg, Args... args)
Definition: log.h:307
gnash::LogFile::getStamp
bool getStamp() const
Definition: log.h:163
dsodefs.h
gnash::log_debug
void log_debug(StringType msg, Args... args)
Definition: log.h:301
gnash::LogFile
Definition: log.h:64
rc.h
gnash::LogFile::setLogFilename
void setLogFilename(const std::string &fname)
Set log filename.
Definition: log.cpp:274
gnash::HostFunctionReport::HostFunctionReport
HostFunctionReport()
Definition: log.h:414
threadMap
std::map< std::thread::id, int > threadMap
Definition: log.cpp:90
gnash::LogFile::getWriteDisk
bool getWriteDisk() const
Definition: log.h:170
gnash
Anonymous namespace for callbacks, local functions, event handlers etc.
Definition: dbus_ext.cpp:41
gnash::LogFile::LOG_SILENT
@ LOG_SILENT
Definition: log.h:72
gnash::LogFile::LOG_EXTRA
@ LOG_EXTRA
Definition: log.h:75
gnash::HostFunctionReport::~HostFunctionReport
~HostFunctionReport()
Definition: log.h:426
gnash::processLog_unimpl
void processLog_unimpl(const boost::format &fmt)
Definition: log.cpp:187
gnash::key::s
@ s
Definition: GnashKey.h:165
gnash::LogFile::OPEN
@ OPEN
Definition: log.h:80
gnash::LogFile::log
void log(const std::string &label, const std::string &msg)
Intended for use by log_*(). Thread-safe (locks _ioMutex)
Definition: log.cpp:268
gnash::log_network
void log_network(StringType msg, Args... args)
Definition: log.h:277
gnash::log_error
void log_error(StringType msg, Args... args)
Definition: log.h:283
gnash::LogFile::LOG_NORMAL
@ LOG_NORMAL
Definition: log.h:73
gnash::log_trace
void log_trace(StringType msg, Args... args)
Definition: log.h:295
gnash::LogFile::setStamp
void setStamp(bool b)
Definition: log.h:159
gnash::key::t
@ t
Definition: GnashKey.h:166
gnash::clocktime::getTicks
DSOEXPORT std::uint64_t getTicks()
Wall clock timer, returns current POSIX time in milliseconds.
Definition: ClockTime.cpp:61
ClockTime.h
gnash::processLog_network
void processLog_network(const boost::format &fmt)
Definition: log.cpp:165
N_
#define N_(String)
Definition: log.h:45
gnash::LogFile::setActionDump
void setActionDump(int x)
Definition: log.h:135
gnash::processLog_abc
void processLog_abc(const boost::format &fmt)
Definition: log.cpp:142
GnashAlgorithm.h
gnash::LogFile::closeLog
bool closeLog()
Close the log file.
Definition: log.cpp:351
gnash::log_abc
void log_abc(StringType msg, Args... args)
Definition: log.h:337
gnash::LogFile::~LogFile
~LogFile()
Definition: log.cpp:301
length
@ length
Definition: klash_part.cpp:329
gnash::processLog_swferror
void processLog_swferror(const boost::format &fmt)
Definition: log.cpp:209
gnash::processLog_aserror
void processLog_aserror(const boost::format &fmt)
Definition: log.cpp:220
gnash::key::p
@ p
Definition: GnashKey.h:162
DEFAULT_LOGFILE
#define DEFAULT_LOGFILE
Definition: log.h:40
gettext.h
gnash::processLog_debug
void processLog_debug(const boost::format &fmt)
Definition: log.cpp:130
gnash::processLog_parse
void processLog_parse(const boost::format &fmt)
Definition: log.cpp:154
gnash::log_aserror
void log_aserror(StringType msg, Args... args)
Definition: log.h:331
gnash::log_parse
void log_parse(StringType msg, Args... args)
Definition: log.h:313
log.h
gnash::HostFunctionReport
Definition: log.h:410
gnash::LogFile::INPROGRESS
@ INPROGRESS
Definition: log.h:81
gnash::dbglogfile
LogFile & dbglogfile
Definition: fileio.cpp:76
gnashconfig.h
gnash::processLog_error
void processLog_error(const boost::format &fmt)
Definition: log.cpp:176
gnash::log_security
void log_security(StringType msg, Args... args)
Definition: log.h:319
gnash::LogFile::LOG_DEBUG
@ LOG_DEBUG
Definition: log.h:74
gnash::LogFile::getParserDump
int getParserDump() const
Definition: log.h:155
gnash::LogFile::setVerbosity
void setVerbosity(int x)
Definition: log.h:127
DSOEXPORT
#define DSOEXPORT
Definition: dsodefs.h:55
gnash::log_impl
void log_impl(boost::format &fmt, FuncType func)
Definition: log.h:251
gnash::LogFile::setNetwork
void setNetwork(int x)
Definition: log.h:139
gnash::LogFile::getActionDump
int getActionDump() const
Definition: log.h:143
gnash::LogFile::registerLogCallback
void registerLogCallback(logListener l)
Definition: log.h:176
gnash::key::o
@ o
Definition: GnashKey.h:161
gnash::log_unimpl
void log_unimpl(StringType msg, Args... args)
Definition: log.h:289
gnash::LogFile::getNetwork
int getNetwork() const
Definition: log.h:147
gnash::hexify
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
x
std::int32_t x
Definition: BitmapData_as.cpp:434
gnash::LogFile::setVerbosity
void setVerbosity()
Definition: log.h:123
gnash::LogFile::setWriteDisk
void setWriteDisk(bool b)
Set whether to write logs to file.
Definition: log.cpp:281
gnash::key::b
@ b
Definition: GnashKey.h:148
gnash::HostFunctionReport::HostFunctionReport
HostFunctionReport(const char *func)
Definition: log.h:418
gnash::LogFile::setParserDump
void setParserDump(int x)
Definition: log.h:151
gnash::LogFile::getVerbosity
int getVerbosity() const
Definition: log.h:131
startTicks
std::uint64_t startTicks
Definition: log.cpp:89
gnash::operator<<
std::ostream & operator<<(std::ostream &o, const URL &u)
Definition: URL.cpp:447
gnash::LogFile::CLOSED
@ CLOSED
Definition: log.h:79