CConsoleHandler.h 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #pragma once
  2. /*
  3. * CConsoleHandler.h, part of VCMI engine
  4. *
  5. * Authors: listed in file AUTHORS in main folder
  6. *
  7. * License: GNU General Public License v2.0 or later
  8. * Full text of license available in license.txt file, in main folder
  9. *
  10. */
  11. /// Class which wraps the native console. It can print text based on
  12. /// the chosen color
  13. class DLL_LINKAGE CConsoleHandler
  14. {
  15. public:
  16. boost::function<void(const std::string &)> *cb; //function to be called when message is received
  17. int curLvl; //logging level
  18. boost::thread *thread;
  19. int run();
  20. void setColor(int level); //sets color of text appropriate for given logging level
  21. CConsoleHandler(); //c-tor
  22. ~CConsoleHandler(); //d-tor
  23. void start(); //starts listening thread
  24. void end(); //kills listening thread
  25. template<typename T> void print(const T &data, int lvl)
  26. {
  27. #ifndef _WIN32
  28. // with love from ffmpeg - library is trying to print some warnings from separate thread
  29. // this results in broken console on Linux. Lock stdout to print all our data at once
  30. flockfile(stdout);
  31. #endif
  32. setColor(lvl);
  33. std::cout << data << std::flush;
  34. setColor(-1);
  35. #ifndef _WIN32
  36. funlockfile(stdout);
  37. #endif
  38. }
  39. };