2 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Free Software
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.
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.
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
23 #include "gnashconfig.h"
26 #include "rc.h" // for IF_VERBOSE_* implementation
27 #include "dsodefs.h" // for DSOEXPORT
30 #include <boost/thread/thread.hpp>
31 #include <boost/thread/mutex.hpp>
32 #include <boost/format.hpp>
34 // the default name for the debug log
35 #define DEFAULT_LOGFILE "gnash-dbg.log"
37 // Support compilation with (or without) native language support
39 #define _(String) gettext (String)
40 #define N_(String) gettext_noop (String)
42 // Macro to prevent repeated logging calls for the same
44 #define LOG_ONCE(x) { \
45 static bool warned = false; \
46 if (!warned) { warned = true; x; } \
49 # include <boost/preprocessor/arithmetic/inc.hpp>
50 # include <boost/preprocessor/repetition/enum_params.hpp>
51 # include <boost/preprocessor/repetition/repeat.hpp>
52 # include <boost/preprocessor/repetition/repeat_from_to.hpp>
53 # include <boost/preprocessor/seq/for_each.hpp>
55 // Mingw32 (win32 console) doesn't use the standard GCC defines that
56 // Gnash used for debug messages, so make it so...
58 #define __FUNCDNAME__ __FUNCTION__
63 // This is a basic file logging class
64 class DSOEXPORT LogFile
68 static LogFile
& getDefaultInstance();
86 /// Intended for use by log_*(). Thread-safe (locks _ioMutex)
89 /// The label string ie: "ERROR" for "ERROR: <msg>"
92 /// The message string ie: "bah" for "ERROR: bah"
94 void log(const std::string
& label
, const std::string
& msg
);
96 /// Intended for use by log_*(). Thread-safe (locks _ioMutex)
99 /// The message to print
101 void log(const std::string
& msg
);
103 /// Remove the log file
105 /// Does NOT lock _ioMutex (should it?)
109 /// Close the log file
111 /// Locks _ioMutex to prevent race conditions accessing _outstream
117 /// If a log file is opened already, it will be closed
118 /// by this call, and will be reopened on next use
121 void setLogFilename(const std::string
& fname
);
123 // accessors for the verbose level
124 void setVerbosity() {
128 void setVerbosity(int x
) {
132 int getVerbosity() const {
136 void setActionDump(int x
) {
140 void setNetwork(int x
) {
144 int getActionDump() const {
148 int getNetwork() const {
152 void setParserDump (int x
) {
156 int getParserDump() const {
160 void setStamp (bool b
) {
164 bool getStamp() const {
168 /// Set whether to write logs to file
169 void setWriteDisk(bool b
);
171 bool getWriteDisk() const {
175 typedef void (*logListener
)(const std::string
& s
);
177 void registerLogCallback(logListener l
) { _listener
= l
; }
181 /// Open the specified file to write logs on disk
183 /// Locks _ioMutex to prevent race conditions accessing _outstream
185 /// @return true on success, false on failure
187 bool openLog(const std::string
& filespec
);
190 /// Open the RcInitFile-specified log file if log write
193 /// This method is called before any attempt to write is made.
194 /// It will return true if the file was opened, false if wasn't
195 /// (either not requested or error).
197 /// On error, will print a message on stderr
199 bool openLogIfNeeded();
201 // Use getDefaultInstance for getting the singleton
204 /// Mutex for locking I/O during logfile access.
205 boost::mutex _ioMutex
;
207 /// Stream to write to stdout.
208 std::ofstream _outstream
;
210 /// How much output is required: 2 or more gives debug output.
213 /// Whether to dump all SWF actions
216 /// Whether to dump all networking actions
219 /// Whether to dump parser output
222 /// The state of the log file.
227 /// Whether to write the log file to disk.
230 std::string _filespec
;
232 std::string _logFilename
;
234 logListener _listener
;
238 DSOEXPORT
void processLog_network(const boost::format
& fmt
);
239 DSOEXPORT
void processLog_error(const boost::format
& fmt
);
240 DSOEXPORT
void processLog_unimpl(const boost::format
& fmt
);
241 DSOEXPORT
void processLog_trace(const boost::format
& fmt
);
242 DSOEXPORT
void processLog_debug(const boost::format
& fmt
);
243 DSOEXPORT
void processLog_action(const boost::format
& fmt
);
244 DSOEXPORT
void processLog_parse(const boost::format
& fmt
);
245 DSOEXPORT
void processLog_security(const boost::format
& fmt
);
246 DSOEXPORT
void processLog_swferror(const boost::format
& fmt
);
247 DSOEXPORT
void processLog_aserror(const boost::format
& fmt
);
248 DSOEXPORT
void processLog_abc(const boost::format
& fmt
);
250 /// This heap of steaming preprocessor code magically converts
251 /// printf-style statements into boost::format messages using templates.
253 /// Macro to feed boost::format strings to the boost::format object,
254 /// producing code like this: "% t1 % t2 % t3 ..."
255 #define TOKENIZE_FORMAT(z, n, t) % t##n
257 /// Macro to add a number of arguments to the templated function
258 /// corresponding to the number of template arguments. Produces code
259 /// like this: "const T0& t0, const T1& t1, const T2& t2 ..."
260 #define TOKENIZE_ARGS(z, n, t) BOOST_PP_COMMA_IF(n) const T##n& t##n
262 /// This is a sequence of different log message types to be used in
263 /// the code. Append the name to log_ to call the function, e.g.
264 /// log_error, log_unimpl.
265 #define LOG_TYPES (error) (debug) (unimpl) (aserror) (swferror) \
266 (security) (action) (parse) (trace) (abc) (network)
268 /// This actually creates the template functions using the TOKENIZE
269 /// functions above. The templates look like this:
271 /// template<typename T0 , typename T1 , typename T2>
272 /// void log_error(const T0& t0 , const T1& t1 , const T2& t2)
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);
283 /// Only not as nicely indented.
285 /// Use "g++ -E log.h" or "cpp log.h" to check.
286 #define LOG_TEMPLATES(z, n, data)\
287 template<BOOST_PP_ENUM_PARAMS(BOOST_PP_INC(n), typename T)>\
288 inline void log_##data(BOOST_PP_REPEAT(BOOST_PP_INC(n), TOKENIZE_ARGS, t)) \
290 if (LogFile::getDefaultInstance().getVerbosity() == 0) return; \
291 boost::format f(t0); \
292 using namespace boost::io; \
293 f.exceptions(all_error_bits ^ (too_many_args_bit | \
295 bad_format_string_bit)); \
296 processLog_##data(f BOOST_PP_REPEAT_FROM_TO(1, \
298 TOKENIZE_FORMAT, t));\
301 /// Defines the maximum number of template arguments
303 /// The preprocessor generates templates with 1..ARG_NUMBER
305 #define ARG_NUMBER 10
307 /// Calls the macro LOG_TEMPLATES an ARG_NUMBER number
308 /// of times, each time adding an extra typename argument to the
310 #define GENERATE_LOG_TYPES(r, _, t) \
311 BOOST_PP_REPEAT(ARG_NUMBER, LOG_TEMPLATES, t)
313 /// Calls the template generator for each log type in the
314 /// sequence LOG_TYPES.
315 BOOST_PP_SEQ_FOR_EACH(GENERATE_LOG_TYPES
, _
, LOG_TYPES
)
318 #undef TOKENIZE_FORMAT
319 #undef GENERATE_LOG_TYPES
323 /// Convert a sequence of bytes to hex or ascii format.
325 /// @param bytes the array of bytes to process
326 /// @param length the number of bytes to read. Callers are responsible
327 /// for checking that length does not exceed the array size.
328 /// @param ascii whether to return in ascii or space-separated hex format.
329 /// @return a string representation of the byte sequence.
330 DSOEXPORT
std::string
hexify(const unsigned char *bytes
, size_t length
,
333 // Define to 0 to completely remove parse debugging at compile-time
334 #ifndef VERBOSE_PARSE
335 #define VERBOSE_PARSE 1
338 // Define to 0 to completely remove action debugging at compile-time
339 #ifndef VERBOSE_ACTION
340 #define VERBOSE_ACTION 1
343 // Define to 0 to remove ActionScript errors verbosity at compile-time
344 #ifndef VERBOSE_ASCODING_ERRORS
345 #define VERBOSE_ASCODING_ERRORS 1
348 // Define to 0 this to remove invalid SWF verbosity at compile-time
349 #ifndef VERBOSE_MALFORMED_SWF
350 #define VERBOSE_MALFORMED_SWF 1
353 // Define to 0 this to remove Networking verbosity at compile-time
354 #ifndef VERBOSE_NETWORKING
355 #define VERBOSE_NETWORKING 1
359 #define IF_VERBOSE_PARSE(x) do { if ( LogFile::getDefaultInstance().getParserDump() ) { x; } } while (0);
361 #define IF_VERBOSE_PARSE(x)
365 #define IF_VERBOSE_ACTION(x) do { if ( LogFile::getDefaultInstance().getActionDump() ) { x; } } while (0);
367 #define IF_VERBOSE_ACTION(x)
371 #define IF_VERBOSE_NETWORK(x) do { if ( LogFile::getDefaultInstance().getNetwork() ) { x; } } while (0);
373 #define IF_VERBOSE_NETWORK(x)
376 #if VERBOSE_ASCODING_ERRORS
377 // TODO: check if it's worth to check verbosity level too...
378 #define IF_VERBOSE_ASCODING_ERRORS(x) { if ( gnash::RcInitFile::getDefaultInstance().showASCodingErrors() ) { x; } }
380 #define IF_VERBOSE_ASCODING_ERRORS(x)
383 #if VERBOSE_MALFORMED_SWF
384 // TODO: check if it's worth to check verbosity level too...
385 #define IF_VERBOSE_MALFORMED_SWF(x) { if ( gnash::RcInitFile::getDefaultInstance().showMalformedSWFErrors() ) { x; } }
387 #define IF_VERBOSE_MALFORMED_SWF(x)
390 class DSOEXPORT HostFunctionReport
393 // Only print function tracing messages when multiple -v
394 // options have been supplied.
395 HostFunctionReport() {
396 log_debug("entering");
399 HostFunctionReport(const char* func
) : _func(func
) {
401 log_debug("%s enter", func
);
404 log_debug("No Function Name! enter");
407 ~HostFunctionReport() {
408 log_debug("%s returning", _func
);
414 #ifndef HAVE_FUNCTION
416 #define dummystr(x) # x
417 #define dummyestr(x) dummystr(x)
418 #define __FUNCTION__ __FILE__":"dummyestr(__LINE__)
420 #define __FUNCTION__ __func__
424 #ifndef HAVE_PRETTY_FUNCTION
425 #define __PRETTY_FUNCTION__ __FUNCTION__
428 #if defined(__cplusplus) && defined(__GNUC__)
429 #define GNASH_REPORT_FUNCTION \
430 const gnash::HostFunctionReport hfr(__PRETTY_FUNCTION__)
431 #define GNASH_REPORT_RETURN
433 #define GNASH_REPORT_FUNCTION \
434 gnash::log_debug("entering")
436 #define GNASH_REPORT_RETURN \
437 gnash::log_debug("returning")
443 #endif // GNASH_LOG_H
448 // indent-tabs-mode: nil