1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
|
//-----------------------------------------------------------------------------
/** @file libboardgame_base/Log.h
@author Markus Enzenberger
@copyright GNU General Public License version 3 or later */
//-----------------------------------------------------------------------------
#ifndef LIBBOARDGAME_BASE_LOG_H
#define LIBBOARDGAME_BASE_LOG_H
#include <sstream>
#include <string>
namespace libboardgame_base {
using namespace std;
//-----------------------------------------------------------------------------
#if (defined ANDROID || defined __ANDROID__) && ! defined LIBBOARDGAME_DEBUG
#define LIBBOARDGAME_DISABLE_LOG 1
#endif
#ifndef LIBBOARDGAME_DISABLE_LOG
extern ostream* _log_stream;
#endif
inline void disable_logging()
{
#ifndef LIBBOARDGAME_DISABLE_LOG
_log_stream = nullptr;
#endif
}
inline ostream* get_log_stream()
{
#ifndef LIBBOARDGAME_DISABLE_LOG
return _log_stream;
#else
return nullptr;
#endif
}
inline void flush_log()
{
#ifndef LIBBOARDGAME_DISABLE_LOG
if (_log_stream != nullptr)
_log_stream->flush();
#endif
}
//-----------------------------------------------------------------------------
#ifndef LIBBOARDGAME_DISABLE_LOG
/** Initializes the logging functionality.
This is necessary to call on some platforms at the start of the program
before any calls to log().
@see LogInitializer */
void _log_init();
/** Closes the logging functionality.
This is necessary to call on some platforms before the program exits.
@see LogInitializer */
void _log_close();
/** Write a string to the log stream.
Appends a newline if the output has no newline at the end. */
void _log(const string& s);
/** Write a number of arguments to the log stream.
Writes to a buffer first so there is only a single write to the log
stream. Appends a newline if the output has no newline at the end. */
template<typename ...Ts>
void _log(const Ts&... args)
{
if (! _log_stream)
return;
ostringstream buffer;
(buffer << ... << args);
_log(buffer.str());
}
#endif // ! LIBBOARDGAME_DISABLE_LOG
//-----------------------------------------------------------------------------
class LogInitializer
{
public:
LogInitializer()
{
#ifndef LIBBOARDGAME_DISABLE_LOG
_log_init();
#endif
}
~LogInitializer()
{
#ifndef LIBBOARDGAME_DISABLE_LOG
_log_close();
#endif
}
};
//-----------------------------------------------------------------------------
} // namespace libboardgame_base
//-----------------------------------------------------------------------------
#ifndef LIBBOARDGAME_DISABLE_LOG
#define LIBBOARDGAME_LOG(...) libboardgame_base::_log(__VA_ARGS__)
#else
#define LIBBOARDGAME_LOG(...) (static_cast<void>(0))
#endif
//-----------------------------------------------------------------------------
#endif // LIBBOARDGAME_BASE_LOG_H
|