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