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
|
|
|
|
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
|
|
|
}
|
2013-04-05 13:29:46 +03:00
|
|
|
|
2013-06-26 14:18:27 +03:00
|
|
|
std::function<void(const std::string &)> *cb; //function to be called when message is received
|
2013-04-05 13:29:46 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
int run();
|
|
|
|
|
|
|
|
void end(); //kills listening thread
|
|
|
|
|
|
|
|
void setColor(EConsoleTextColor::EConsoleTextColor color); //sets color of text appropriate for given logging level
|
|
|
|
|
2013-04-09 17:31:36 +03: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.
|
|
|
|
static boost::mutex smx;
|
2013-04-05 13:29:46 +03: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;
|