Debian Codegraph @

src/engine/logging.h

raw symbols SCIP
1 /***************************************************************************
2 * fheroes2: https://github.com/ihhub/fheroes2 *
3 * Copyright (C) 2021 - 2025 *
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 2 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 *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20
21 #pragma once
22
23 #include <iostream>
24 #include <sstream> // IWYU pragma: keep
25 #include <string>
26
27 enum
28 {
29 DBG_WARN = 0x0001,
30 DBG_INFO = 0x0002,
31 DBG_TRACE = 0x0003,
32
33 DBG_ENGINE = 0x000C,
34 DBG_GAME = 0x0030,
35 DBG_BATTLE = 0x00C0,
36 DBG_AI = 0x0300,
37 DBG_NETWORK = 0x0C00,
38 DBG_OTHER = 0x3000,
39 DBG_DEVEL = 0xC000,
40
41 DBG_ENGINE_WARN = 0x0004,
42 DBG_GAME_WARN = 0x0010,
43 DBG_BATTLE_WARN = 0x0040,
44 DBG_AI_WARN = 0x0100,
45 DBG_NETWORK_WARN = 0x0400,
46 DBG_OTHER_WARN = 0x1000,
47
48 DBG_ENGINE_INFO = 0x0008,
49 DBG_GAME_INFO = 0x0020,
50 DBG_BATTLE_INFO = 0x0080,
51 DBG_AI_INFO = 0x0200,
52 DBG_NETWORK_INFO = 0x0800,
53 DBG_OTHER_INFO = 0x2000,
54
55 DBG_ENGINE_TRACE = DBG_ENGINE,
56 DBG_GAME_TRACE = DBG_GAME,
57 DBG_BATTLE_TRACE = DBG_BATTLE,
58 DBG_AI_TRACE = DBG_AI,
59 DBG_NETWORK_TRACE = DBG_NETWORK,
60 DBG_OTHER_TRACE = DBG_OTHER,
61
62 DBG_ALL = DBG_ENGINE | DBG_GAME | DBG_BATTLE | DBG_AI | DBG_NETWORK | DBG_OTHER,
63
64 DBG_ALL_WARN = DBG_ENGINE_WARN | DBG_GAME_WARN | DBG_BATTLE_WARN | DBG_AI_WARN | DBG_NETWORK_WARN | DBG_OTHER_WARN,
65 DBG_ALL_INFO = DBG_ENGINE_INFO | DBG_GAME_INFO | DBG_BATTLE_INFO | DBG_AI_INFO | DBG_NETWORK_INFO | DBG_OTHER_INFO,
66 DBG_ALL_TRACE = DBG_ENGINE_TRACE | DBG_GAME_TRACE | DBG_BATTLE_TRACE | DBG_AI_TRACE | DBG_NETWORK_TRACE | DBG_OTHER_TRACE
67 };
68
69 #if defined( TARGET_NINTENDO_SWITCH ) || defined( _WIN32 )
70 #include <fstream>
71 #include <mutex>
72
73 namespace Logging
74 {
75 extern std::ofstream logFile;
76 // This mutex protects operations with logFile
77 extern std::mutex logMutex;
78 }
79 #endif
80
81 namespace Logging
82 {
83 const char * GetDebugOptionName( const int name );
84
85 std::string GetTimeString();
86
87 // Initialize logging. Some systems require writing logging information into a file.
88 void InitLog();
89
90 void setDebugLevel( const int level );
91 int getDebugLevel();
92
93 void setTextSupportMode( const bool enableTextSupportMode );
94 bool isTextSupportModeEnabled();
95 }
96
97 #if defined( _WIN32 ) && defined( WITH_DEBUG )
98 #define COUT( x ) \
99 { \
100 const std::scoped_lock<std::mutex> _logfile_lock( Logging::logMutex ); /* The name was chosen on purpose to avoid name collisions with outer code blocks. */ \
101 \
102 Logging::logFile << x << std::endl; \
103 Logging::logFile.flush(); \
104 std::cerr << x << std::endl; \
105 }
106 #elif defined( TARGET_NINTENDO_SWITCH ) || defined( _WIN32 )
107 #define COUT( x ) \
108 { \
109 const std::scoped_lock<std::mutex> _logfile_lock( Logging::logMutex ); /* The name was chosen on purpose to avoid name collisions with outer code blocks. */ \
110 \
111 Logging::logFile << x << std::endl; \
112 Logging::logFile.flush(); \
113 }
114 #elif defined( TARGET_PS_VITA )
115 #include <psp2/kernel/clib.h>
116 #define COUT( x ) \
117 { \
118 std::ostringstream _log_strstream; /* The name was chosen on purpose to avoid name collisions with outer code blocks. */ \
119 _log_strstream << x << std::endl; \
120 sceClibPrintf( "%s", _log_strstream.str().c_str() ); \
121 }
122 #elif defined( MACOS_APP_BUNDLE )
123 #include <syslog.h>
124 #define COUT( x ) \
125 { \
126 std::ostringstream _log_strstream; /* The name was chosen on purpose to avoid name collisions with outer code blocks. */ \
127 _log_strstream << x; \
128 syslog( LOG_WARNING, "fheroes2_log: %s", _log_strstream.str().c_str() ); \
129 }
130 #elif defined( ANDROID )
131 #include <android/log.h>
132 #define COUT( x ) \
133 { \
134 std::ostringstream _log_strstream; /* The name was chosen on purpose to avoid name collisions with outer code blocks. */ \
135 _log_strstream << x; \
136 __android_log_print( ANDROID_LOG_INFO, "fheroes2", "%s", _log_strstream.str().c_str() ); \
137 }
138 #elif defined( __EMSCRIPTEN__ )
139 #include <emscripten/console.h>
140 #define COUT( x ) \
141 { \
142 std::ostringstream _log_strstream; /* The name was chosen on purpose to avoid name collisions with outer code blocks. */ \
143 _log_strstream << x; \
144 emscripten_out( _log_strstream.str().c_str() ); \
145 }
146 #else
147 // Default: log to stderr
148 #define COUT( x ) \
149 { \
150 std::cerr << x << std::endl; \
151 }
152 #endif
153
154 #define VERBOSE_LOG( x ) \
155 { \
156 COUT( Logging::GetTimeString() << ": [VERBOSE]\t" << __FUNCTION__ << ": " << x ); \
157 }
158
159 #define ERROR_LOG( x ) \
160 { \
161 COUT( Logging::GetTimeString() << ": [ERROR]\t" << __FUNCTION__ << ": " << x ); \
162 }
163
164 #ifdef WITH_DEBUG
165 #define DEBUG_LOG( x, y, z ) \
166 if ( IS_DEBUG( x, y ) ) { \
167 COUT( Logging::GetTimeString() << ": [" << Logging::GetDebugOptionName( x ) << "]\t" << __FUNCTION__ << ": " << z ); \
168 }
169 #else
170 #define DEBUG_LOG( x, y, z )
171 #endif
172 #define IS_DEVEL() IS_DEBUG( DBG_DEVEL, DBG_INFO )
173
174 bool IS_DEBUG( const int name, const int level );
175
176 namespace Logging
177 {
178 // This structure simply adds text separators. It is used for Text Support Mode only.
179 struct TextSupportLogger
180 {
181 TextSupportLogger(){ COUT( "----------" ) }
182
183 TextSupportLogger( const TextSupportLogger & )
184 = delete;
185 TextSupportLogger( const TextSupportLogger && ) = delete;
186 TextSupportLogger & operator=( const TextSupportLogger & ) = delete;
187 TextSupportLogger & operator=( const TextSupportLogger && ) = delete;
188
189 ~TextSupportLogger()
190 {
191 COUT( "----------" )
192 }
193 };
194 }
195
196 // Put this macro at the beginning of code block (eg. function) which is responsible for text support mode output.
197 #define START_TEXT_SUPPORT_MODE \
198 if ( !Logging::isTextSupportModeEnabled() ) { \
199 return; \
200 } \
201 const Logging::TextSupportLogger _temp_logger; // The name was chosen on purpose to avoid collisions with other variable names within a code block.