2009-04-15 17:03:31 +03:00
|
|
|
/*
|
|
|
|
* CConsoleHandler.h, part of VCMI engine
|
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
2009-04-16 14:14:13 +03:00
|
|
|
*/
|
2017-07-13 10:26:03 +02:00
|
|
|
#pragma once
|
2009-04-16 14:14:13 +03:00
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
2013-04-05 13:29:46 +03:00
|
|
|
namespace EConsoleTextColor
|
|
|
|
{
|
|
|
|
/** The color enum is used for colored text console output. */
|
|
|
|
enum EConsoleTextColor
|
|
|
|
{
|
|
|
|
DEFAULT = -1,
|
|
|
|
GREEN,
|
|
|
|
RED,
|
|
|
|
MAGENTA,
|
|
|
|
YELLOW,
|
|
|
|
WHITE,
|
|
|
|
GRAY,
|
|
|
|
TEAL = -2
|
|
|
|
};
|
|
|
|
}
|
2009-06-23 11:14:49 +03:00
|
|
|
|
2011-02-22 13:52:36 +02:00
|
|
|
/// Class which wraps the native console. It can print text based on
|
|
|
|
/// the chosen color
|
2011-12-14 00:35:28 +03:00
|
|
|
class DLL_LINKAGE CConsoleHandler
|
2009-04-16 14:14:13 +03:00
|
|
|
{
|
|
|
|
public:
|
2017-07-17 23:04:00 +02:00
|
|
|
CConsoleHandler();
|
|
|
|
~CConsoleHandler();
|
2013-04-05 13:29:46 +03:00
|
|
|
void start(); //starts listening thread
|
2009-06-23 11:14:49 +03:00
|
|
|
|
2013-04-09 17:31:36 +03:00
|
|
|
template<typename T> void print(const T &data, bool addNewLine = false, EConsoleTextColor::EConsoleTextColor color = EConsoleTextColor::DEFAULT, bool printToStdErr = false)
|
2009-04-16 14:14:13 +03:00
|
|
|
{
|
2013-04-09 17:31:36 +03:00
|
|
|
TLockGuard _(smx);
|
2014-08-26 13:19:04 +03:00
|
|
|
#ifndef VCMI_WINDOWS
|
2012-11-13 14:52:23 +03:00
|
|
|
// 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
|
2013-04-05 13:29:46 +03:00
|
|
|
if(color != EConsoleTextColor::DEFAULT) setColor(color);
|
|
|
|
if(printToStdErr)
|
|
|
|
{
|
2013-04-09 17:31:36 +03:00
|
|
|
std::cerr << data;
|
|
|
|
if(addNewLine)
|
|
|
|
{
|
|
|
|
std::cerr << std::endl;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::cerr << std::flush;
|
|
|
|
}
|
2013-04-05 13:29:46 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-04-09 17:31:36 +03:00
|
|
|
std::cout << data;
|
|
|
|
if(addNewLine)
|
|
|
|
{
|
|
|
|
std::cout << std::endl;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::cout << std::flush;
|
|
|
|
}
|
2013-04-05 13:29:46 +03:00
|
|
|
}
|
2013-04-09 17:31:36 +03:00
|
|
|
|
2013-04-05 13:29:46 +03:00
|
|
|
if(color != EConsoleTextColor::DEFAULT) setColor(EConsoleTextColor::DEFAULT);
|
2014-08-26 13:19:04 +03:00
|
|
|
#ifndef VCMI_WINDOWS
|
2012-11-13 14:52:23 +03:00
|
|
|
funlockfile(stdout);
|
|
|
|
#endif
|
2009-04-16 14:14:13 +03:00
|
|
|
}
|
2023-01-15 02:18:35 +02:00
|
|
|
//function to be called when message is received - string: message, bool: whether call was made from in-game console
|
2023-01-16 01:37:18 +02:00
|
|
|
std::function<void(const std::string &, bool)> *cb;
|
2013-04-05 13:29:46 +03:00
|
|
|
|
|
|
|
private:
|
2023-03-13 23:26:44 +02:00
|
|
|
int run() const;
|
2013-04-05 13:29:46 +03:00
|
|
|
|
2023-03-13 23:26:44 +02:00
|
|
|
void end(); //kills listening thread
|
2013-04-05 13:29:46 +03:00
|
|
|
|
2023-03-13 23:26:44 +02:00
|
|
|
static void setColor(EConsoleTextColor::EConsoleTextColor color); //sets color of text appropriate for given logging level
|
2013-04-05 13:29:46 +03:00
|
|
|
|
2023-03-13 23:26:44 +02:00
|
|
|
/// FIXME: Implement CConsoleHandler as singleton, move some logic into CLogConsoleTarget, etc... needs to be disussed:)
|
|
|
|
/// Without static, application will crash complaining about mutex deleted. In short: CConsoleHandler gets deleted before
|
|
|
|
/// the logging system.
|
2023-04-11 16:11:14 +02:00
|
|
|
static std::mutex smx;
|
2013-04-05 13:29:46 +03:00
|
|
|
|
2023-03-13 23:26:44 +02:00
|
|
|
boost::thread * thread;
|
2009-04-16 14:14:13 +03:00
|
|
|
};
|
2013-04-11 18:58:01 +03:00
|
|
|
|
|
|
|
extern DLL_LINKAGE CConsoleHandler * console;
|
2022-07-26 15:07:42 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|