mirror of
				https://github.com/vcmi/vcmi.git
				synced 2025-10-31 00:07:39 +02:00 
			
		
		
		
	Fix build, reduce includes of CConsoleHandler
This commit is contained in:
		| @@ -50,7 +50,7 @@ std::shared_ptr<CMainMenu> GameInstance::mainmenu() | ||||
| 		return nullptr; | ||||
|  | ||||
| 	if (!mainMenuInstance) | ||||
| 		mainMenuInstance = std::shared_ptr<CMainMenu>(new CMainMenu()); | ||||
| 		mainMenuInstance = std::make_shared<CMainMenu>(); | ||||
|  | ||||
| 	return mainMenuInstance; | ||||
| } | ||||
|   | ||||
| @@ -34,6 +34,7 @@ | ||||
| #include "../client/windows/CMessage.h" | ||||
| #include "../client/windows/InfoWindows.h" | ||||
|  | ||||
| #include "../lib/CConsoleHandler.h" | ||||
| #include "../lib/CThreadHelper.h" | ||||
| #include "../lib/ExceptionsCommon.h" | ||||
| #include "../lib/filesystem/Filesystem.h" | ||||
| @@ -227,12 +228,12 @@ int main(int argc, char * argv[]) | ||||
| 	CConsoleHandler console(callbackFunction); | ||||
| 	console.start(); | ||||
|  | ||||
| 	CBasicLogConfigurator logConfig(logPath, &console); | ||||
| 	CBasicLogConfigurator logConfigurator(logPath, &console); | ||||
| #else | ||||
| 	CBasicLogConfigurator logConfig(logPath, nullptr); | ||||
| 	CBasicLogConfigurator logConfigurator(logPath, nullptr); | ||||
| #endif | ||||
|  | ||||
| 	logConfig.configureDefault(); | ||||
| 	logConfigurator.configureDefault(); | ||||
| 	logGlobal->info("Starting client of '%s'", GameConstants::VCMI_VERSION); | ||||
| 	logGlobal->info("Creating console and configuring logger: %d ms", pomtime.getDiff()); | ||||
| 	logGlobal->info("The log file will be saved to %s", logPath); | ||||
| @@ -289,7 +290,7 @@ int main(int argc, char * argv[]) | ||||
| 	setSettingInteger("general/saveFrequency", "savefrequency", 1); | ||||
|  | ||||
| 	// Initialize logging based on settings | ||||
| 	logConfig.configure(); | ||||
| 	logConfigurator.configure(); | ||||
| 	logGlobal->debug("settings = %s", settings.toJsonNode().toString()); | ||||
|  | ||||
| 	// Some basic data validation to produce better error messages in cases of incorrect install | ||||
|   | ||||
| @@ -14,6 +14,7 @@ | ||||
| #include <QDir> | ||||
|  | ||||
| #include "../lib/CConfigHandler.h" | ||||
| #include "../lib/CConsoleHandler.h" | ||||
| #include "../lib/VCMIDirs.h" | ||||
| #include "../lib/filesystem/Filesystem.h" | ||||
| #include "../lib/logging/CBasicLogConfigurator.h" | ||||
| @@ -32,12 +33,12 @@ void MainWindow::load() | ||||
|  | ||||
| #ifndef VCMI_MOBILE | ||||
| 	console = std::make_unique<CConsoleHandler>(); | ||||
| 	CBasicLogConfigurator logConfig(VCMIDirs::get().userLogsPath() / "VCMI_Launcher_log.txt", console.get()); | ||||
| 	CBasicLogConfigurator logConfigurator(VCMIDirs::get().userLogsPath() / "VCMI_Launcher_log.txt", console.get()); | ||||
| #else | ||||
| 	CBasicLogConfigurator logConfig(VCMIDirs::get().userLogsPath() / "VCMI_Launcher_log.txt", nullptr); | ||||
| 	CBasicLogConfigurator logConfigurator(VCMIDirs::get().userLogsPath() / "VCMI_Launcher_log.txt", nullptr); | ||||
| #endif | ||||
|  | ||||
| 	logConfig.configureDefault(); | ||||
| 	logConfigurator.configureDefault(); | ||||
|  | ||||
| 	try | ||||
| 	{ | ||||
|   | ||||
| @@ -15,8 +15,6 @@ | ||||
|  | ||||
| #include <boost/stacktrace.hpp> | ||||
|  | ||||
| VCMI_LIB_NAMESPACE_END | ||||
|  | ||||
| #if defined(NDEBUG) && !defined(VCMI_ANDROID) | ||||
| #define USE_ON_TERMINATE | ||||
| #endif | ||||
| @@ -27,29 +25,29 @@ VCMI_LIB_NAMESPACE_END | ||||
| #endif | ||||
|  | ||||
| #ifndef VCMI_WINDOWS | ||||
| 	#define CONSOLE_GREEN "\x1b[1;32m" | ||||
| 	#define CONSOLE_RED "\x1b[1;31m" | ||||
| 	#define CONSOLE_MAGENTA "\x1b[1;35m" | ||||
| 	#define CONSOLE_YELLOW "\x1b[1;33m" | ||||
| 	#define CONSOLE_WHITE "\x1b[1;37m" | ||||
| 	#define CONSOLE_GRAY "\x1b[1;30m" | ||||
| 	#define CONSOLE_TEAL "\x1b[1;36m" | ||||
| constexpr const char * CONSOLE_GREEN = "\x1b[1;32m"; | ||||
| constexpr const char * CONSOLE_RED = "\x1b[1;31m"; | ||||
| constexpr const char * CONSOLE_MAGENTA = "\x1b[1;35m"; | ||||
| constexpr const char * CONSOLE_YELLOW = "\x1b[1;33m"; | ||||
| constexpr const char * CONSOLE_WHITE = "\x1b[1;37m"; | ||||
| constexpr const char * CONSOLE_GRAY = "\x1b[1;30m"; | ||||
| constexpr const char * CONSOLE_TEAL = "\x1b[1;36m"; | ||||
| #else | ||||
| 	#include <windows.h> | ||||
| 	#include <dbghelp.h> | ||||
| #include <windows.h> | ||||
| #include <dbghelp.h> | ||||
| #ifndef __MINGW32__ | ||||
| 	#pragma comment(lib, "dbghelp.lib") | ||||
| #endif | ||||
| 	HANDLE handleIn; | ||||
| 	HANDLE handleOut; | ||||
| 	HANDLE handleErr; | ||||
| 	#define CONSOLE_GREEN FOREGROUND_GREEN | FOREGROUND_INTENSITY | ||||
| 	#define CONSOLE_RED FOREGROUND_RED | FOREGROUND_INTENSITY | ||||
| 	#define CONSOLE_MAGENTA FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY | ||||
| 	#define CONSOLE_YELLOW FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY | ||||
| 	#define CONSOLE_WHITE FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY | ||||
| 	#define CONSOLE_GRAY FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | ||||
| 	#define CONSOLE_TEAL FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY | ||||
| HANDLE handleIn; | ||||
| HANDLE handleOut; | ||||
| HANDLE handleErr; | ||||
| constexpr int32_t CONSOLE_GREEN = FOREGROUND_GREEN | FOREGROUND_INTENSITY; | ||||
| constexpr int32_t CONSOLE_RED = FOREGROUND_RED | FOREGROUND_INTENSITY; | ||||
| constexpr int32_t CONSOLE_MAGENTA = FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY; | ||||
| constexpr int32_t CONSOLE_YELLOW = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY; | ||||
| constexpr int32_t CONSOLE_WHITE = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY; | ||||
| constexpr int32_t CONSOLE_GRAY = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE; | ||||
| constexpr int32_t CONSOLE_TEAL = FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY; | ||||
| #endif | ||||
|  | ||||
| VCMI_LIB_NAMESPACE_BEGIN | ||||
| @@ -190,7 +188,7 @@ LONG WINAPI onUnhandledException(EXCEPTION_POINTERS* exception) | ||||
| } | ||||
| #endif | ||||
|  | ||||
| void CConsoleHandler::setColor(EConsoleTextColor::EConsoleTextColor color) | ||||
| void CConsoleHandler::setColor(EConsoleTextColor color) | ||||
| { | ||||
| 	TColor colorCode; | ||||
| 	switch(color) | ||||
| @@ -259,8 +257,8 @@ int CConsoleHandler::run() | ||||
| 		boost::this_thread::interruption_point(); | ||||
| #else | ||||
| 		std::getline(std::cin, buffer); | ||||
| 		if ( cb && *cb ) | ||||
| 			(*cb)(buffer, false); | ||||
| 		if ( cb ) | ||||
| 			cb(buffer, false); | ||||
| #endif | ||||
| 	} | ||||
| 	return -1; | ||||
| @@ -270,7 +268,7 @@ CConsoleHandler::CConsoleHandler() | ||||
| 	:CConsoleHandler(std::function<void(const std::string &, bool)>{}) | ||||
| {} | ||||
|  | ||||
| CConsoleHandler::CConsoleHandler(std::function<void(const std::string &, bool)> callback) | ||||
| CConsoleHandler::CConsoleHandler(const std::function<void(const std::string &, bool)> & callback) | ||||
| 	:cb(callback) | ||||
| { | ||||
| #ifdef VCMI_WINDOWS | ||||
| @@ -310,7 +308,7 @@ void CConsoleHandler::end() | ||||
| #ifndef VCMI_WINDOWS | ||||
| 		thread.interrupt(); | ||||
| #else | ||||
| 		TerminateThread(thread->native_handle(),0); | ||||
| 		TerminateThread(thread.native_handle(),0); | ||||
| #endif | ||||
| 		thread.join(); | ||||
| 	} | ||||
|   | ||||
| @@ -11,10 +11,8 @@ | ||||
|  | ||||
| VCMI_LIB_NAMESPACE_BEGIN | ||||
|  | ||||
| namespace EConsoleTextColor | ||||
| { | ||||
| /** The color enum is used for colored text console output. */ | ||||
| enum EConsoleTextColor | ||||
| enum class EConsoleTextColor : int8_t | ||||
| { | ||||
| 	DEFAULT = -1, | ||||
| 	GREEN, | ||||
| @@ -25,19 +23,18 @@ enum EConsoleTextColor | ||||
| 	GRAY, | ||||
| 	TEAL = -2 | ||||
| }; | ||||
| } | ||||
|  | ||||
| /// Class which wraps the native console. It can print text based on | ||||
| /// the chosen color | ||||
| class DLL_LINKAGE CConsoleHandler | ||||
| { | ||||
| public: | ||||
| 	CConsoleHandler(std::function<void(const std::string &, bool)> callback); | ||||
| 	CConsoleHandler(const std::function<void(const std::string &, bool)> & callback); | ||||
| 	CConsoleHandler(); | ||||
| 	~CConsoleHandler(); | ||||
| 	void start(); //starts listening thread | ||||
|  | ||||
| 	template<typename T> void print(const T &data, bool addNewLine = false, EConsoleTextColor::EConsoleTextColor color = EConsoleTextColor::DEFAULT, bool printToStdErr = false) | ||||
| 	template<typename T> void print(const T &data, bool addNewLine = false, EConsoleTextColor color = EConsoleTextColor::DEFAULT, bool printToStdErr = false) | ||||
| 	{ | ||||
| 		TLockGuard _(smx); | ||||
| #ifndef VCMI_WINDOWS | ||||
| @@ -82,17 +79,17 @@ private: | ||||
| #ifndef VCMI_WINDOWS | ||||
| 	using TColor = std::string; | ||||
| #else | ||||
| 	typedef WORD TColor; | ||||
| 	using TColor = int32_t; | ||||
| #endif | ||||
|  | ||||
| 	int run(); | ||||
|  | ||||
| 	void end(); //kills listening thread | ||||
|  | ||||
| 	void setColor(EConsoleTextColor::EConsoleTextColor color); //sets color of text appropriate for given logging level | ||||
| 	void setColor(EConsoleTextColor color); //sets color of text appropriate for given logging level | ||||
|  | ||||
| 	static TColor defColor; | ||||
| 	static TColor defErrColor; | ||||
| 	TColor defColor; | ||||
| 	TColor defErrColor; | ||||
|  | ||||
| 	//function to be called when message is received - string: message, bool: whether call was made from in-game console | ||||
| 	std::function<void(const std::string &, bool)> cb; | ||||
|   | ||||
| @@ -12,6 +12,7 @@ | ||||
| #include "CLogger.h" | ||||
|  | ||||
| #include "../CConfigHandler.h" | ||||
| #include "../CConsoleHandler.h" | ||||
|  | ||||
| VCMI_LIB_NAMESPACE_BEGIN | ||||
|  | ||||
| @@ -122,9 +123,9 @@ ELogLevel::ELogLevel CBasicLogConfigurator::getLogLevel(const std::string & leve | ||||
| 		throw std::runtime_error("Log level " + level + " unknown."); | ||||
| } | ||||
|  | ||||
| EConsoleTextColor::EConsoleTextColor CBasicLogConfigurator::getConsoleColor(const std::string & colorName) | ||||
| EConsoleTextColor CBasicLogConfigurator::getConsoleColor(const std::string & colorName) | ||||
| { | ||||
| 	static const std::map<std::string, EConsoleTextColor::EConsoleTextColor> colorMap = | ||||
| 	static const std::map<std::string, EConsoleTextColor> colorMap = | ||||
| 	{ | ||||
| 		{"default", EConsoleTextColor::DEFAULT}, | ||||
| 		{"green", EConsoleTextColor::GREEN}, | ||||
|   | ||||
| @@ -10,12 +10,11 @@ | ||||
|  | ||||
| #pragma once | ||||
|  | ||||
| #include "../CConsoleHandler.h" | ||||
|  | ||||
| VCMI_LIB_NAMESPACE_BEGIN | ||||
|  | ||||
| class CConsoleHandler; | ||||
| class JsonNode; | ||||
| enum class EConsoleTextColor : int8_t; | ||||
|  | ||||
| /// The class CBasicLogConfigurator reads log properties from settings.json and | ||||
| /// sets up the logging system. Make sure that you use the same configurator object to | ||||
| @@ -42,7 +41,7 @@ private: | ||||
| 	static ELogLevel::ELogLevel getLogLevel(const std::string & level); | ||||
| 	// Gets EConsoleTextColor enum from strings. (Should be moved to CLogger as a separate function?) | ||||
| 	// Throws: std::runtime_error | ||||
| 	static EConsoleTextColor::EConsoleTextColor getConsoleColor(const std::string & colorName); | ||||
| 	static EConsoleTextColor getConsoleColor(const std::string & colorName); | ||||
|  | ||||
| 	boost::filesystem::path filePath; | ||||
| 	CConsoleHandler * console; | ||||
|   | ||||
| @@ -10,6 +10,7 @@ | ||||
| #include "StdInc.h" | ||||
| #include "CLogger.h" | ||||
| #include "../CThreadHelper.h" | ||||
| #include "../CConsoleHandler.h" | ||||
|  | ||||
| #ifdef VCMI_ANDROID | ||||
| #include <android/log.h> | ||||
| @@ -313,13 +314,13 @@ CColorMapping::CColorMapping() | ||||
| 	levelMap[ELogLevel::ERROR] = EConsoleTextColor::RED; | ||||
| } | ||||
|  | ||||
| void CColorMapping::setColorFor(const CLoggerDomain & domain, ELogLevel::ELogLevel level, EConsoleTextColor::EConsoleTextColor color) | ||||
| void CColorMapping::setColorFor(const CLoggerDomain & domain, ELogLevel::ELogLevel level, EConsoleTextColor color) | ||||
| { | ||||
| 	assert(level != ELogLevel::NOT_SET); | ||||
| 	map[domain.getName()][level] = color; | ||||
| } | ||||
|  | ||||
| EConsoleTextColor::EConsoleTextColor CColorMapping::getColorFor(const CLoggerDomain & domain, ELogLevel::ELogLevel level) const | ||||
| EConsoleTextColor CColorMapping::getColorFor(const CLoggerDomain & domain, ELogLevel::ELogLevel level) const | ||||
| { | ||||
| 	CLoggerDomain currentDomain = domain; | ||||
| 	while(true) | ||||
| @@ -400,7 +401,7 @@ void CLogConsoleTarget::write(const LogRecord & record) | ||||
| 	const bool printToStdErr = record.level >= ELogLevel::WARN; | ||||
| 	if(console) | ||||
| 	{ | ||||
| 		const EConsoleTextColor::EConsoleTextColor textColor = | ||||
| 		const EConsoleTextColor textColor = | ||||
| 			coloredOutputEnabled ? colorMapping.getColorFor(record.domain, record.level) : EConsoleTextColor::DEFAULT; | ||||
|  | ||||
| 		console->print(message, true, textColor, printToStdErr); | ||||
|   | ||||
| @@ -9,14 +9,14 @@ | ||||
|  */ | ||||
| #pragma once | ||||
|  | ||||
| #include "../CConsoleHandler.h" | ||||
|  | ||||
| VCMI_LIB_NAMESPACE_BEGIN | ||||
|  | ||||
| class CLogger; | ||||
| class CConsoleHandler; | ||||
| struct LogRecord; | ||||
| class ILogTarget; | ||||
|  | ||||
| enum class EConsoleTextColor : int8_t; | ||||
|  | ||||
| namespace ELogLevel | ||||
| { | ||||
| @@ -157,11 +157,11 @@ class DLL_LINKAGE CColorMapping | ||||
| public: | ||||
| 	CColorMapping(); | ||||
|  | ||||
| 	void setColorFor(const CLoggerDomain & domain, ELogLevel::ELogLevel level, EConsoleTextColor::EConsoleTextColor color); | ||||
| 	EConsoleTextColor::EConsoleTextColor getColorFor(const CLoggerDomain & domain, ELogLevel::ELogLevel level) const; | ||||
| 	void setColorFor(const CLoggerDomain & domain, ELogLevel::ELogLevel level, EConsoleTextColor color); | ||||
| 	EConsoleTextColor getColorFor(const CLoggerDomain & domain, ELogLevel::ELogLevel level) const; | ||||
|  | ||||
| private: | ||||
| 	std::map<std::string, std::map<ELogLevel::ELogLevel, EConsoleTextColor::EConsoleTextColor> > map; | ||||
| 	std::map<std::string, std::map<ELogLevel::ELogLevel, EConsoleTextColor> > map; | ||||
| }; | ||||
|  | ||||
| /// This target is a logging target which writes message to the console. | ||||
|   | ||||
| @@ -11,6 +11,7 @@ | ||||
|  | ||||
| #include "LobbyServer.h" | ||||
|  | ||||
| #include "../lib/CConsoleHandler.h" | ||||
| #include "../lib/logging/CBasicLogConfigurator.h" | ||||
| #include "../lib/filesystem/CFilesystemLoader.h" | ||||
| #include "../lib/filesystem/Filesystem.h" | ||||
| @@ -26,8 +27,8 @@ int main(int argc, const char * argv[]) | ||||
| #ifndef VCMI_IOS | ||||
| 	CConsoleHandler console; | ||||
| #endif | ||||
| 	CBasicLogConfigurator logConfig(VCMIDirs::get().userLogsPath() / "VCMI_Lobby_log.txt", &console); | ||||
| 	logConfig.configureDefault(); | ||||
| 	CBasicLogConfigurator logConfigurator(VCMIDirs::get().userLogsPath() / "VCMI_Lobby_log.txt", &console); | ||||
| 	logConfigurator.configureDefault(); | ||||
|  | ||||
| 	auto databasePath = VCMIDirs::get().userDataPath() / "vcmiLobby.db"; | ||||
| 	logGlobal->info("Opening database %s", databasePath.string()); | ||||
|   | ||||
| @@ -23,6 +23,7 @@ | ||||
| #include "../lib/GameLibrary.h" | ||||
| #include "../lib/logging/CBasicLogConfigurator.h" | ||||
| #include "../lib/CConfigHandler.h" | ||||
| #include "../lib/CConsoleHandler.h" | ||||
| #include "../lib/filesystem/Filesystem.h" | ||||
| #include "../lib/filesystem/CMemoryBuffer.h" | ||||
| #include "../lib/GameConstants.h" | ||||
|   | ||||
| @@ -72,14 +72,14 @@ int main(int argc, const char * argv[]) | ||||
| 	boost::filesystem::current_path(boost::filesystem::system_complete(argv[0]).parent_path()); | ||||
|  | ||||
| 	CConsoleHandler console; | ||||
| 	CBasicLogConfigurator logConfig(VCMIDirs::get().userLogsPath() / "VCMI_Server_log.txt", &console); | ||||
| 	logConfig.configureDefault(); | ||||
| 	CBasicLogConfigurator logConfigurator(VCMIDirs::get().userLogsPath() / "VCMI_Server_log.txt", &console); | ||||
| 	logConfigurator.configureDefault(); | ||||
| 	logGlobal->info(SERVER_NAME); | ||||
|  | ||||
| 	boost::program_options::variables_map opts; | ||||
| 	handleCommandOptions(argc, argv, opts); | ||||
| 	preinitDLL(false); | ||||
| 	logConfig.configure(); | ||||
| 	logConfigurator.configure(); | ||||
|  | ||||
| 	loadDLLClasses(); | ||||
| 	std::srand(static_cast<uint32_t>(time(nullptr))); | ||||
| @@ -98,7 +98,7 @@ int main(int argc, const char * argv[]) | ||||
| 		// CVCMIServer destructor must be called here - before LIBRARY cleanup | ||||
| 	} | ||||
|  | ||||
| 	logConfig.deconfigure(); | ||||
| 	logConfigurator.deconfigure(); | ||||
| 	vstd::clear_pointer(LIBRARY); | ||||
|  | ||||
| 	return 0; | ||||
|   | ||||
		Reference in New Issue
	
	Block a user