log.h 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <algorithm>
  6. #include <string_view>
  7. #include <fmt/format.h>
  8. #include "common/logging/formatter.h"
  9. #include "common/logging/types.h"
  10. namespace Common::Log {
  11. // trims up to and including the last of ../, ..\, src/, src\ in a string
  12. constexpr const char* TrimSourcePath(std::string_view source) {
  13. const auto rfind = [source](const std::string_view match) {
  14. return source.rfind(match) == source.npos ? 0 : (source.rfind(match) + match.size());
  15. };
  16. auto idx = std::max({rfind("src/"), rfind("src\\"), rfind("../"), rfind("..\\")});
  17. return source.data() + idx;
  18. }
  19. /// Logs a message to the global logger, using fmt
  20. void FmtLogMessageImpl(Class log_class, Level log_level, const char* filename,
  21. unsigned int line_num, const char* function, const char* format,
  22. const fmt::format_args& args);
  23. template <typename... Args>
  24. void FmtLogMessage(Class log_class, Level log_level, const char* filename, unsigned int line_num,
  25. const char* function, const char* format, const Args&... args) {
  26. FmtLogMessageImpl(log_class, log_level, filename, line_num, function, format,
  27. fmt::make_format_args(args...));
  28. }
  29. } // namespace Common::Log
  30. #ifdef _DEBUG
  31. #define LOG_TRACE(log_class, ...) \
  32. Common::Log::FmtLogMessage(Common::Log::Class::log_class, Common::Log::Level::Trace, \
  33. Common::Log::TrimSourcePath(__FILE__), __LINE__, __func__, \
  34. __VA_ARGS__)
  35. #else
  36. #define LOG_TRACE(log_class, fmt, ...) (void(0))
  37. #endif
  38. #define LOG_DEBUG(log_class, ...) \
  39. Common::Log::FmtLogMessage(Common::Log::Class::log_class, Common::Log::Level::Debug, \
  40. Common::Log::TrimSourcePath(__FILE__), __LINE__, __func__, \
  41. __VA_ARGS__)
  42. #define LOG_INFO(log_class, ...) \
  43. Common::Log::FmtLogMessage(Common::Log::Class::log_class, Common::Log::Level::Info, \
  44. Common::Log::TrimSourcePath(__FILE__), __LINE__, __func__, \
  45. __VA_ARGS__)
  46. #define LOG_WARNING(log_class, ...) \
  47. Common::Log::FmtLogMessage(Common::Log::Class::log_class, Common::Log::Level::Warning, \
  48. Common::Log::TrimSourcePath(__FILE__), __LINE__, __func__, \
  49. __VA_ARGS__)
  50. #define LOG_ERROR(log_class, ...) \
  51. Common::Log::FmtLogMessage(Common::Log::Class::log_class, Common::Log::Level::Error, \
  52. Common::Log::TrimSourcePath(__FILE__), __LINE__, __func__, \
  53. __VA_ARGS__)
  54. #define LOG_CRITICAL(log_class, ...) \
  55. Common::Log::FmtLogMessage(Common::Log::Class::log_class, Common::Log::Level::Critical, \
  56. Common::Log::TrimSourcePath(__FILE__), __LINE__, __func__, \
  57. __VA_ARGS__)