2011-12-14 00:35:28 +03:00
|
|
|
#pragma once
|
2009-04-16 14:14:13 +03:00
|
|
|
|
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
|
|
|
*/
|
|
|
|
|
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:
|
2009-05-07 20:20:41 +03:00
|
|
|
boost::function<void(const std::string &)> *cb; //function to be called when message is received
|
|
|
|
int curLvl; //logging level
|
2009-06-23 11:14:49 +03:00
|
|
|
boost::thread *thread;
|
|
|
|
|
2009-04-16 14:14:13 +03:00
|
|
|
int run();
|
2009-05-07 20:20:41 +03:00
|
|
|
void setColor(int level); //sets color of text appropriate for given logging level
|
2009-06-23 11:14:49 +03:00
|
|
|
|
2009-05-07 20:20:41 +03:00
|
|
|
CConsoleHandler(); //c-tor
|
|
|
|
~CConsoleHandler(); //d-tor
|
2009-06-23 11:14:49 +03:00
|
|
|
void start(); //starts listening thread
|
|
|
|
void end(); //kills listening thread
|
|
|
|
|
2011-12-14 00:35:28 +03:00
|
|
|
template<typename T> void print(const T &data, int lvl)
|
2009-04-16 14:14:13 +03:00
|
|
|
{
|
2012-11-13 14:52:23 +03:00
|
|
|
#ifndef _WIN32
|
|
|
|
// 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
|
2011-12-14 00:35:28 +03:00
|
|
|
setColor(lvl);
|
2009-04-16 14:14:13 +03:00
|
|
|
std::cout << data << std::flush;
|
|
|
|
setColor(-1);
|
2012-11-13 14:52:23 +03:00
|
|
|
#ifndef _WIN32
|
|
|
|
funlockfile(stdout);
|
|
|
|
#endif
|
2009-04-16 14:14:13 +03:00
|
|
|
}
|
|
|
|
};
|