| #ifndef LIBGAV1_SRC_UTILS_LOGGING_H_ |
| #define LIBGAV1_SRC_UTILS_LOGGING_H_ |
| |
| #include <cstddef> |
| |
| #include "src/utils/compiler_attributes.h" |
| |
| #if !defined(LIBGAV1_ENABLE_LOGGING) |
| #if defined(NDEBUG) |
| #define LIBGAV1_ENABLE_LOGGING 0 |
| #else |
| #define LIBGAV1_ENABLE_LOGGING 1 |
| #endif |
| #endif |
| |
| #if LIBGAV1_ENABLE_LOGGING |
| // LIBGAV1_DLOG(severity, printf-format-string) |
| // Debug logging that can optionally be enabled in release builds by explicitly |
| // setting LIBGAV1_ENABLE_LOGGING. |
| // Severity is given as an all-caps version of enum LogSeverity with the |
| // leading 'k' removed: LIBGAV1_DLOG(INFO, "..."); |
| #define LIBGAV1_DLOG(severity, ...) \ |
| do { \ |
| constexpr const char* libgav1_logging_internal_basename = \ |
| ::libgav1::internal::Basename(__FILE__, sizeof(__FILE__) - 1); \ |
| ::libgav1::internal::Log(LIBGAV1_LOGGING_INTERNAL_##severity, \ |
| libgav1_logging_internal_basename, __LINE__, \ |
| __VA_ARGS__); \ |
| } while (0) |
| #else |
| #define LIBGAV1_DLOG(severity, ...) \ |
| do { \ |
| } while (0) |
| #endif // LIBGAV1_ENABLE_LOGGING |
| |
| #define LIBGAV1_LOGGING_INTERNAL_ERROR ::libgav1::internal::LogSeverity::kError |
| #define LIBGAV1_LOGGING_INTERNAL_WARNING \ |
| ::libgav1::internal::LogSeverity::kWarning |
| #define LIBGAV1_LOGGING_INTERNAL_INFO ::libgav1::internal::LogSeverity::kInfo |
| |
| namespace libgav1 { |
| namespace internal { |
| |
| enum class LogSeverity : int { |
| kError, |
| kWarning, |
| kInfo, |
| }; |
| |
| // Helper function to implement LIBGAV1_DLOG |
| // Logs |format, ...| at |severity| level, reporting it as called from |
| // |file|:|line|. |
| void Log(libgav1::internal::LogSeverity severity, const char* file, int line, |
| const char* format, ...) LIBGAV1_PRINTF_ATTRIBUTE(4, 5); |
| |
| // Compile-time function to get the 'base' file_name, that is, the part of |
| // a file_name after the last '/' or '\' path separator. The search starts at |
| // the end of the string; the second parameter is the length of the string. |
| constexpr const char* Basename(const char* file_name, size_t offset) { |
| return (offset == 0 || file_name[offset - 1] == '/' || |
| file_name[offset - 1] == '\\') |
| ? file_name + offset |
| : Basename(file_name, offset - 1); |
| } |
| |
| } // namespace internal |
| } // namespace libgav1 |
| |
| #endif // LIBGAV1_SRC_UTILS_LOGGING_H_ |