1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-11-28 08:48:48 +02:00
vcmi/lib/CConsoleHandler.h
Johannes Schauer Marin Rodrigues a1a5bc28c2
convert line endings from CRLF (Windows) to LF (Linux/Unix)
Mixed line endings cause problems when exporting patches with
git-format-patch and then trying to "git am" a patch with mixed and
non-matching line endings. In such a situation git will fail to apply
the patch.

This commit runs the dos2unix tools on the remaining files with CRLF
(\r\n) line endings to convert them to line-feeds (\n) only.

Files that are Windows specific like *.vcxproj and *.props files were
not converted.

Closes: #3073
2023-10-19 16:23:21 +02:00

99 lines
2.6 KiB
C++

/*
* 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
*
*/
#pragma once
VCMI_LIB_NAMESPACE_BEGIN
namespace EConsoleTextColor
{
/** The color enum is used for colored text console output. */
enum EConsoleTextColor
{
DEFAULT = -1,
GREEN,
RED,
MAGENTA,
YELLOW,
WHITE,
GRAY,
TEAL = -2
};
}
/// Class which wraps the native console. It can print text based on
/// the chosen color
class DLL_LINKAGE CConsoleHandler
{
public:
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)
{
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(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;
void end(); //kills listening thread
static 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 disussed:)
/// Without static, application will crash complaining about mutex deleted. In short: CConsoleHandler gets deleted before
/// the logging system.
static std::mutex smx;
boost::thread * thread;
};
extern DLL_LINKAGE CConsoleHandler * console;
VCMI_LIB_NAMESPACE_END