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_EXAMPLES_LOGGING_H_
18 #define LIBGAV1_EXAMPLES_LOGGING_H_
19
20 #include <cstddef>
21 #include <cstdio>
22
23 namespace libgav1 {
24 namespace examples {
25
26 #if !defined(LIBGAV1_EXAMPLES_ENABLE_LOGGING)
27 #if defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION)
28 #define LIBGAV1_EXAMPLES_ENABLE_LOGGING 0
29 #else
30 #define LIBGAV1_EXAMPLES_ENABLE_LOGGING 1
31 #endif
32 #endif
33
34 #if LIBGAV1_EXAMPLES_ENABLE_LOGGING
35
36 // Compile-time function to get the 'base' file_name, that is, the part of
37 // a file_name after the last '/' or '\' path separator. The search starts at
38 // the end of the string; the second parameter is the length of the string.
Basename(const char * file_name,size_t offset)39 constexpr const char* Basename(const char* file_name, size_t offset) {
40 return (offset == 0 || file_name[offset - 1] == '/' ||
41 file_name[offset - 1] == '\\')
42 ? file_name + offset
43 : Basename(file_name, offset - 1);
44 }
45
46 #define LIBGAV1_EXAMPLES_LOG_ERROR(error_string) \
47 do { \
48 constexpr const char* libgav1_examples_basename = \
49 libgav1::examples::Basename(__FILE__, sizeof(__FILE__) - 1); \
50 fprintf(stderr, "%s:%d (%s): %s.\n", libgav1_examples_basename, __LINE__, \
51 __func__, error_string); \
52 } while (false)
53
54 #else // !LIBGAV1_EXAMPLES_ENABLE_LOGGING
55
56 #define LIBGAV1_EXAMPLES_LOG_ERROR(error_string) \
57 do { \
58 } while (false)
59
60 #endif // LIBGAV1_EXAMPLES_ENABLE_LOGGING
61
62 } // namespace examples
63 } // namespace libgav1
64
65 #endif // LIBGAV1_EXAMPLES_LOGGING_H_
66