1 /*
2 * Copyright 2019 The libgav1 Authors
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #ifndef LIBGAV1_SRC_UTILS_LOGGING_H_
18 #define LIBGAV1_SRC_UTILS_LOGGING_H_
19
20 #include <cstddef>
21
22 #include "src/utils/compiler_attributes.h"
23
24 #if !defined(LIBGAV1_ENABLE_LOGGING)
25 #if defined(NDEBUG) || defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION)
26 #define LIBGAV1_ENABLE_LOGGING 0
27 #else
28 #define LIBGAV1_ENABLE_LOGGING 1
29 #endif
30 #endif
31
32 #if LIBGAV1_ENABLE_LOGGING
33 // LIBGAV1_DLOG(severity, printf-format-string)
34 // Debug logging that can optionally be enabled in release builds by explicitly
35 // setting LIBGAV1_ENABLE_LOGGING.
36 // Severity is given as an all-caps version of enum LogSeverity with the
37 // leading 'k' removed: LIBGAV1_DLOG(INFO, "...");
38 #define LIBGAV1_DLOG(severity, ...) \
39 do { \
40 constexpr const char* libgav1_logging_internal_basename = \
41 ::libgav1::internal::Basename(__FILE__, sizeof(__FILE__) - 1); \
42 ::libgav1::internal::Log(LIBGAV1_LOGGING_INTERNAL_##severity, \
43 libgav1_logging_internal_basename, __LINE__, \
44 __VA_ARGS__); \
45 } while (0)
46 #else
47 #define LIBGAV1_DLOG(severity, ...) \
48 do { \
49 } while (0)
50 #endif // LIBGAV1_ENABLE_LOGGING
51
52 #define LIBGAV1_LOGGING_INTERNAL_ERROR ::libgav1::internal::LogSeverity::kError
53 #define LIBGAV1_LOGGING_INTERNAL_WARNING \
54 ::libgav1::internal::LogSeverity::kWarning
55 #define LIBGAV1_LOGGING_INTERNAL_INFO ::libgav1::internal::LogSeverity::kInfo
56
57 namespace libgav1 {
58 namespace internal {
59
60 enum class LogSeverity : int {
61 kError,
62 kWarning,
63 kInfo,
64 };
65
66 // Helper function to implement LIBGAV1_DLOG
67 // Logs |format, ...| at |severity| level, reporting it as called from
68 // |file|:|line|.
69 void Log(libgav1::internal::LogSeverity severity, const char* file, int line,
70 const char* format, ...) LIBGAV1_PRINTF_ATTRIBUTE(4, 5);
71
72 // Compile-time function to get the 'base' file_name, that is, the part of
73 // a file_name after the last '/' or '\' path separator. The search starts at
74 // the end of the string; the second parameter is the length of the string.
Basename(const char * file_name,size_t offset)75 constexpr const char* Basename(const char* file_name, size_t offset) {
76 return (offset == 0 || file_name[offset - 1] == '/' ||
77 file_name[offset - 1] == '\\')
78 ? file_name + offset
79 : Basename(file_name, offset - 1);
80 }
81
82 } // namespace internal
83 } // namespace libgav1
84
85 #endif // LIBGAV1_SRC_UTILS_LOGGING_H_
86