Logger.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #ifndef ANKI_CORE_LOGGER_H
  2. #define ANKI_CORE_LOGGER_H
  3. #include "anki/util/Observer.h"
  4. #include "anki/util/Singleton.h"
  5. #include "anki/Config.h"
  6. #include <array>
  7. #include <mutex>
  8. #include <sstream> // For the macros
  9. namespace anki {
  10. /// @addtogroup Core
  11. /// @{
  12. /// The logger singleton class. The logger cannot print errors or throw
  13. /// exceptions, it has to recover somehow. Its thread safe
  14. class Logger
  15. {
  16. public:
  17. enum LoggerMessageType
  18. {
  19. LMT_NORMAL,
  20. LMT_ERROR,
  21. LMT_WARNING
  22. };
  23. /// XXX
  24. struct Info
  25. {
  26. const char* file;
  27. int line;
  28. const char* func;
  29. LoggerMessageType type;
  30. const char* msg;
  31. };
  32. /// Send a message
  33. void write(const char* file, int line, const char* func,
  34. LoggerMessageType type, const char* msg);
  35. ANKI_SIGNAL(const Info&, messageRecieved)
  36. private:
  37. std::mutex mutex; ///< For thread safety
  38. };
  39. typedef Singleton<Logger> LoggerSingleton;
  40. /// @}
  41. } // end namespace
  42. //==============================================================================
  43. // Macros =
  44. //==============================================================================
  45. #define ANKI_LOGGER_MESSAGE(t, msg) \
  46. do \
  47. { \
  48. std::stringstream ss; \
  49. ss << msg; \
  50. LoggerSingleton::get().write(ANKI_FILE, __LINE__, ANKI_FUNC, \
  51. t, ss.str().c_str()); \
  52. } while(false);
  53. #define ANKI_LOGI(x) ANKI_LOGGER_MESSAGE(Logger::LMT_NORMAL, x)
  54. #define ANKI_LOGW(x) ANKI_LOGGER_MESSAGE(Logger::LMT_WARNING, x)
  55. #define ANKI_LOGE(x) ANKI_LOGGER_MESSAGE(Logger::LMT_ERROR, x)
  56. #endif