Logger.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #ifndef ANKI_CORE_LOGGER_H
  2. #define ANKI_CORE_LOGGER_H
  3. #include "anki/Config.h"
  4. #include "anki/util/Observer.h"
  5. #include "anki/util/Singleton.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. /// To add a new signal:
  15. /// @code ANKI_CONNECT(&logger, messageRecieved, &instance, handle) @endcode
  16. class Logger
  17. {
  18. public:
  19. ANKI_HAS_SLOTS(Logger)
  20. /// Logger message type
  21. enum LoggerMessageType
  22. {
  23. LMT_NORMAL,
  24. LMT_ERROR,
  25. LMT_WARNING
  26. };
  27. /// Used as parammeter when emitting the signal
  28. struct Info
  29. {
  30. const char* file;
  31. int line;
  32. const char* func;
  33. LoggerMessageType type;
  34. const char* msg;
  35. };
  36. Logger();
  37. /// Send a message
  38. void write(const char* file, int line, const char* func,
  39. LoggerMessageType type, const char* msg);
  40. /// Send a formated message
  41. void writeFormated(const char* file, int line, const char* func,
  42. LoggerMessageType type, const char* fmt, ...);
  43. ANKI_SIGNAL(const Info&, messageRecieved)
  44. private:
  45. std::mutex mutex; ///< For thread safety
  46. /// Depending on the OS implement a different handler. This one is the
  47. /// default one
  48. void defaultSystemMessageHandler(const Info& info);
  49. ANKI_SLOT(defaultSystemMessageHandler, const Logger::Info&)
  50. };
  51. typedef Singleton<Logger> LoggerSingleton;
  52. /// @}
  53. } // end namespace
  54. //==============================================================================
  55. // Macros =
  56. //==============================================================================
  57. #define ANKI_LOGGER_MESSAGE(t, ...) \
  58. do \
  59. { \
  60. LoggerSingleton::get().writeFormated(ANKI_FILE, __LINE__, ANKI_FUNC, \
  61. t, __VA_ARGS__); \
  62. } while(false);
  63. #define ANKI_LOGI(...) ANKI_LOGGER_MESSAGE(Logger::LMT_NORMAL, __VA_ARGS__)
  64. #define ANKI_LOGW(...) ANKI_LOGGER_MESSAGE(Logger::LMT_WARNING, __VA_ARGS__)
  65. #define ANKI_LOGE(...) ANKI_LOGGER_MESSAGE(Logger::LMT_ERROR, __VA_ARGS__)
  66. #endif