| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#pragma once |
| 22 |
|
| 23 |
#include <iostream> |
| 24 |
#include <sstream> |
| 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 |
|
| 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 |
|
| 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 ); \ |
| 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 ); \ |
| 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; \ |
| 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; \ |
| 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; \ |
| 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; \ |
| 143 |
_log_strstream << x; \ |
| 144 |
emscripten_out( _log_strstream.str().c_str() ); \ |
| 145 |
} |
| 146 |
#else |
| 147 |
|
| 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 |
|
| 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 |
|
| 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. |