You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

logger.hpp 1.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #pragma once
  2. #include <common.hpp>
  3. /** Example usage:
  4. DEBUG("error: %d", errno);
  5. will print something like
  6. [0.123 debug myfile.cpp:45] error: 67
  7. */
  8. #define DEBUG(format, ...) rack::logger::log(rack::logger::DEBUG_LEVEL, __FILE__, __LINE__, __FUNCTION__, format, ##__VA_ARGS__)
  9. #define INFO(format, ...) rack::logger::log(rack::logger::INFO_LEVEL, __FILE__, __LINE__, __FUNCTION__, format, ##__VA_ARGS__)
  10. #define WARN(format, ...) rack::logger::log(rack::logger::WARN_LEVEL, __FILE__, __LINE__, __FUNCTION__, format, ##__VA_ARGS__)
  11. #define FATAL(format, ...) rack::logger::log(rack::logger::FATAL_LEVEL, __FILE__, __LINE__, __FUNCTION__, format, ##__VA_ARGS__)
  12. namespace rack {
  13. /** Logs messages to a file or the terminal */
  14. namespace logger {
  15. extern std::string logPath;
  16. enum Level {
  17. DEBUG_LEVEL,
  18. INFO_LEVEL,
  19. WARN_LEVEL,
  20. FATAL_LEVEL
  21. };
  22. /** Returns whether logger was successfully initialized. */
  23. PRIVATE bool init();
  24. PRIVATE void destroy();
  25. /** Do not use this function directly. Use the macros above.
  26. Thread-safe, meaning messages cannot overlap each other in the log.
  27. */
  28. __attribute__((format(printf, 5, 6)))
  29. void log(Level level, const char* filename, int line, const char* func, const char* format, ...);
  30. /** Returns whether the last log file failed to end properly, due to a possible crash.
  31. */
  32. PRIVATE bool wasTruncated();
  33. } // namespace logger
  34. } // namespace rack