Debian Codegraph @

src/engine/logging.cpp

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 #include <array>
22 #include <cassert>
23 #include <ctime>
24
25 #if defined( _WIN32 )
26 #define WIN32_LEAN_AND_MEAN
27 #include <windows.h>
28 #endif
29
30 #if defined( MACOS_APP_BUNDLE )
31 #include <syslog.h>
32 #endif
33
34 #include "logging.h"
35 #include "system.h"
36
37 namespace
38 {
39 int debugLevel = DBG_ALL_WARN;
40 bool textSupportMode = false;
41
42 #if defined( _WIN32 )
43 // Sets the Windows console codepage to the system codepage
44 class ConsoleCPSwitcher
45 {
46 public:
47 ConsoleCPSwitcher()
48 : _consoleOutputCP( GetConsoleOutputCP() )
49 {
50 if ( _consoleOutputCP > 0 ) {
51 SetConsoleOutputCP( GetACP() );
52 }
53 }
54
55 ConsoleCPSwitcher( const ConsoleCPSwitcher & ) = delete;
56
57 ~ConsoleCPSwitcher()
58 {
59 if ( _consoleOutputCP > 0 ) {
60 SetConsoleOutputCP( _consoleOutputCP );
61 }
62 }
63
64 ConsoleCPSwitcher & operator=( const ConsoleCPSwitcher & ) = delete;
65
66 private:
67 const UINT _consoleOutputCP;
68 };
69
70 const ConsoleCPSwitcher consoleCPSwitcher;
71 #endif
72 }
73
74 namespace Logging
75 {
76 #if defined( TARGET_NINTENDO_SWITCH ) || defined( _WIN32 )
77 std::ofstream logFile;
78 // This mutex protects operations with logFile
79 std::mutex logMutex;
80 #endif
81
82 const char * GetDebugOptionName( const int name )
83 {
84 if ( name & DBG_ENGINE )
85 return "DBG_ENGINE";
86 else if ( name & DBG_GAME )
87 return "DBG_GAME";
88 else if ( name & DBG_BATTLE )
89 return "DBG_BATTLE";
90 else if ( name & DBG_AI )
91 return "DBG_AI";
92 else if ( name & DBG_NETWORK )
93 return "DBG_NETWORK";
94 else if ( name & DBG_OTHER )
95 return "DBG_OTHER";
96 else if ( name & DBG_DEVEL )
97 return "DBG_DEVEL";
98
99 return "";
100 }
101
102 std::string GetTimeString()
103 {
104 const tm tmi = System::GetTM( std::time( nullptr ) );
105
106 std::array<char, 256> buf;
107
108 const size_t writtenBytes = std::strftime( buf.data(), buf.size(), "%d.%m.%Y %H:%M:%S", &tmi );
109 if ( writtenBytes == 0 ) {
110 assert( 0 );
111 return "<TIMESTAMP ERROR>";
112 }
113
114 return std::string( buf.data() );
115 }
116
117 void InitLog()
118 {
119 #if defined( TARGET_NINTENDO_SWITCH )
120 const std::scoped_lock<std::mutex> lock( logMutex );
121
122 logFile.open( "fheroes2.log", std::ofstream::app );
123 #elif defined( _WIN32 )
124 const std::scoped_lock<std::mutex> lock( logMutex );
125
126 const std::string configDir = System::GetConfigDirectory( "fheroes2" );
127
128 System::MakeDirectory( configDir );
129
130 logFile.open( System::concatPath( configDir, "fheroes2.log" ), std::ofstream::app );
131 #elif defined( MACOS_APP_BUNDLE )
132 openlog( "fheroes2", LOG_CONS | LOG_NDELAY, LOG_USER );
133
134 setlogmask( LOG_UPTO( LOG_WARNING ) );
135 #endif
136 }
137
138 void setDebugLevel( const int level )
139 {
140 debugLevel = level;
141 }
142
143 int getDebugLevel()
144 {
145 return debugLevel;
146 }
147
148 void setTextSupportMode( const bool enableTextSupportMode )
149 {
150 textSupportMode = enableTextSupportMode;
151 }
152
153 bool isTextSupportModeEnabled()
154 {
155 return textSupportMode;
156 }
157 }
158
159 bool IS_DEBUG( const int name, const int level )
160 {
161 return ( ( DBG_ENGINE & name ) && ( ( DBG_ENGINE & debugLevel ) >> 2 ) >= level ) || ( ( DBG_GAME & name ) && ( ( DBG_GAME & debugLevel ) >> 4 ) >= level )
162 || ( ( DBG_BATTLE & name ) && ( ( DBG_BATTLE & debugLevel ) >> 6 ) >= level ) || ( ( DBG_AI & name ) && ( ( DBG_AI & debugLevel ) >> 8 ) >= level )
163 || ( ( DBG_NETWORK & name ) && ( ( DBG_NETWORK & debugLevel ) >> 10 ) >= level ) || ( ( DBG_DEVEL & name ) && ( ( DBG_DEVEL & debugLevel ) >> 12 ) >= level );
164 }