1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-07-15 01:24:45 +02:00

Remove console global

This commit is contained in:
Ivan Savenko
2025-02-27 23:10:11 +00:00
parent e5a095a237
commit 782362e5ce
12 changed files with 105 additions and 97 deletions

View File

@ -15,12 +15,6 @@
#include <boost/stacktrace.hpp>
VCMI_LIB_NAMESPACE_BEGIN
std::mutex CConsoleHandler::smx;
DLL_LINKAGE CConsoleHandler * console = nullptr;
VCMI_LIB_NAMESPACE_END
#if defined(NDEBUG) && !defined(VCMI_ANDROID)
@ -245,12 +239,12 @@ void CConsoleHandler::setColor(EConsoleTextColor::EConsoleTextColor color)
#endif
}
int CConsoleHandler::run() const
int CConsoleHandler::run()
{
setThreadName("consoleHandler");
//disabling sync to make in_avail() work (othervice always returns 0)
{
TLockGuard _(smx);
std::lock_guard guard(smx);
std::ios::sync_with_stdio(false);
}
std::string buffer;
@ -262,8 +256,8 @@ int CConsoleHandler::run() const
if (std::cin.rdbuf()->in_avail())
{
if ( getline(std::cin, buffer).good() )
if ( cb && *cb )
(*cb)(buffer, false);
if ( cb )
cb(buffer, false);
}
else
boost::this_thread::sleep_for(boost::chrono::milliseconds(100));
@ -277,9 +271,13 @@ int CConsoleHandler::run() const
}
return -1;
}
CConsoleHandler::CConsoleHandler():
cb(new std::function<void(const std::string &, bool)>),
thread(nullptr)
CConsoleHandler::CConsoleHandler()
:CConsoleHandler(std::function<void(const std::string &, bool)>{})
{}
CConsoleHandler::CConsoleHandler(std::function<void(const std::string &, bool)> callback)
:cb(callback)
{
#ifdef VCMI_WINDOWS
handleIn = GetStdHandle(STD_INPUT_HANDLE);
@ -309,27 +307,24 @@ CConsoleHandler::~CConsoleHandler()
{
logGlobal->info("Killing console...");
end();
delete cb;
logGlobal->info("Killing console... done!");
}
void CConsoleHandler::end()
{
if (thread)
if (thread.joinable())
{
#ifndef VCMI_WINDOWS
thread->interrupt();
thread.interrupt();
#else
TerminateThread(thread->native_handle(),0);
#endif
thread->join();
delete thread;
thread = nullptr;
thread.join();
}
}
void CConsoleHandler::start()
{
thread = new boost::thread(std::bind(&CConsoleHandler::run,console));
thread = boost::thread(std::bind(&CConsoleHandler::run, this));
}
VCMI_LIB_NAMESPACE_END

View File

@ -16,14 +16,14 @@ namespace EConsoleTextColor
/** The color enum is used for colored text console output. */
enum EConsoleTextColor
{
DEFAULT = -1,
GREEN,
RED,
MAGENTA,
YELLOW,
WHITE,
GRAY,
TEAL = -2
DEFAULT = -1,
GREEN,
RED,
MAGENTA,
YELLOW,
WHITE,
GRAY,
TEAL = -2
};
}
@ -32,67 +32,64 @@ enum EConsoleTextColor
class DLL_LINKAGE CConsoleHandler
{
public:
CConsoleHandler();
~CConsoleHandler();
void start(); //starts listening thread
CConsoleHandler(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::EConsoleTextColor color = EConsoleTextColor::DEFAULT, bool printToStdErr = false)
{
TLockGuard _(smx);
TLockGuard _(smx);
#ifndef VCMI_WINDOWS
// with love from ffmpeg - library is trying to print some warnings from separate thread
// this results in broken console on Linux. Lock stdout to print all our data at once
flockfile(stdout);
#endif
if(color != EConsoleTextColor::DEFAULT) setColor(color);
if(printToStdErr)
{
std::cerr << data;
if(addNewLine)
{
std::cerr << std::endl;
}
else
{
std::cerr << std::flush;
}
}
else
{
std::cout << data;
if(addNewLine)
{
std::cout << std::endl;
}
else
{
std::cout << std::flush;
}
}
if(color != EConsoleTextColor::DEFAULT) setColor(color);
if(printToStdErr)
{
std::cerr << data;
if(addNewLine)
{
std::cerr << std::endl;
}
else
{
std::cerr << std::flush;
}
}
else
{
std::cout << data;
if(addNewLine)
{
std::cout << std::endl;
}
else
{
std::cout << std::flush;
}
}
if(color != EConsoleTextColor::DEFAULT) setColor(EConsoleTextColor::DEFAULT);
if(color != EConsoleTextColor::DEFAULT) setColor(EConsoleTextColor::DEFAULT);
#ifndef VCMI_WINDOWS
funlockfile(stdout);
#endif
}
//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;
private:
int run() const;
int run();
void end(); //kills listening thread
static void setColor(EConsoleTextColor::EConsoleTextColor color); //sets color of text appropriate for given logging level
void setColor(EConsoleTextColor::EConsoleTextColor color); //sets color of text appropriate for given logging level
/// FIXME: Implement CConsoleHandler as singleton, move some logic into CLogConsoleTarget, etc... needs to be discussed:)
/// Without static, application will crash complaining about mutex deleted. In short: CConsoleHandler gets deleted before
/// the logging system.
static std::mutex smx;
//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;
boost::thread * thread;
std::mutex smx;
boost::thread thread;
};
extern DLL_LINKAGE CConsoleHandler * console;
VCMI_LIB_NAMESPACE_END

View File

@ -32,7 +32,6 @@
#include "CStopWatch.h"
#include "VCMIDirs.h"
#include "filesystem/Filesystem.h"
#include "CConsoleHandler.h"
#include "rmg/CRmgTemplateStorage.h"
#include "mapObjectConstructors/CObjectClassesHandler.h"
#include "mapObjects/CObjectHandler.h"
@ -47,9 +46,8 @@ VCMI_LIB_NAMESPACE_BEGIN
GameLibrary * LIBRARY = nullptr;
DLL_LINKAGE void preinitDLL(CConsoleHandler * Console, bool extractArchives)
DLL_LINKAGE void preinitDLL(bool extractArchives)
{
console = Console;
LIBRARY = new GameLibrary();
LIBRARY->loadFilesystem(extractArchives);
settings.init("config/settings.json", "vcmi:settings");

View File

@ -119,7 +119,7 @@ public:
extern DLL_LINKAGE GameLibrary * LIBRARY;
DLL_LINKAGE void preinitDLL(CConsoleHandler * Console, bool extractArchives);
DLL_LINKAGE void preinitDLL(bool extractArchives);
DLL_LINKAGE void loadDLLClasses(bool onlyEssential = false);