Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 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 INCLUDE_PERFETTO_BASE_LOGGING_H_ |
| 18 | #define INCLUDE_PERFETTO_BASE_LOGGING_H_ |
| 19 | |
| 20 | #include <errno.h> |
Primiano Tucci | 5aab758 | 2017-12-07 12:22:03 +0000 | [diff] [blame] | 21 | #include <stdio.h> |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 22 | #include <stdlib.h> |
Hector Dearman | adc8c2b | 2018-03-12 13:19:02 +0000 | [diff] [blame] | 23 | #include <string.h> // For strerror. |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 24 | #include <unistd.h> |
| 25 | |
| 26 | #if defined(NDEBUG) |
| 27 | #define PERFETTO_DCHECK_IS_ON() 0 |
| 28 | #else |
| 29 | #define PERFETTO_DCHECK_IS_ON() 1 |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 30 | #endif |
| 31 | |
Primiano Tucci | 05889d5 | 2017-12-21 18:47:17 +0100 | [diff] [blame] | 32 | #include "perfetto/base/build_config.h" |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 33 | #include "perfetto/base/utils.h" |
| 34 | |
Oystein Eftevaag | ff72959 | 2018-02-12 14:24:06 -0800 | [diff] [blame] | 35 | #if PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID) |
Primiano Tucci | 05889d5 | 2017-12-21 18:47:17 +0100 | [diff] [blame] | 36 | #include <android/log.h> |
| 37 | #endif |
| 38 | |
Primiano Tucci | 5aab758 | 2017-12-07 12:22:03 +0000 | [diff] [blame] | 39 | namespace perfetto { |
| 40 | namespace base { |
| 41 | |
| 42 | // Constexpr functions to extract basename(__FILE__), e.g.: ../foo/f.c -> f.c . |
| 43 | constexpr const char* StrEnd(const char* s) { |
| 44 | return *s ? StrEnd(s + 1) : s; |
| 45 | } |
| 46 | |
| 47 | constexpr const char* BasenameRecursive(const char* s, |
| 48 | const char* begin, |
| 49 | const char* end) { |
| 50 | return (*s == '/' && s < end) |
| 51 | ? (s + 1) |
| 52 | : ((s > begin) ? BasenameRecursive(s - 1, begin, end) : s); |
| 53 | } |
| 54 | |
| 55 | constexpr const char* Basename(const char* str) { |
| 56 | return BasenameRecursive(StrEnd(str), str, StrEnd(str)); |
| 57 | } |
| 58 | |
| 59 | enum LogLev { kLogDebug = 0, kLogInfo, kLogImportant, kLogError }; |
| 60 | constexpr const char* kLogFmt[] = {"\x1b[2m", "\x1b[39m", "\x1b[32m\x1b[1m", |
| 61 | "\x1b[31m"}; |
| 62 | |
| 63 | #define PERFETTO_LOG_LINE__(x) #x |
| 64 | #define PERFETTO_LOG_LINE_(x) PERFETTO_LOG_LINE__(x) |
| 65 | #define PERFETTO_LOG_LINE PERFETTO_LOG_LINE_(__LINE__) |
| 66 | |
Primiano Tucci | 05889d5 | 2017-12-21 18:47:17 +0100 | [diff] [blame] | 67 | #define PERFETTO_XLOG_STDERR(level, fmt, ...) \ |
Primiano Tucci | 5aab758 | 2017-12-07 12:22:03 +0000 | [diff] [blame] | 68 | fprintf(stderr, "\x1b[90m%-24.24s\x1b[0m %s" fmt "\x1b[0m\n", \ |
| 69 | ::perfetto::base::Basename(__FILE__ ":" PERFETTO_LOG_LINE), \ |
| 70 | ::perfetto::base::kLogFmt[::perfetto::base::LogLev::level], \ |
| 71 | ##__VA_ARGS__) |
| 72 | |
Primiano Tucci | 05889d5 | 2017-12-21 18:47:17 +0100 | [diff] [blame] | 73 | // Let android log to both stderr and logcat. When part of the Android tree |
| 74 | // stderr points to /dev/null so logcat is the only way to get some logging. |
Oystein Eftevaag | ff72959 | 2018-02-12 14:24:06 -0800 | [diff] [blame] | 75 | #if PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID) |
Primiano Tucci | 05889d5 | 2017-12-21 18:47:17 +0100 | [diff] [blame] | 76 | #define PERFETTO_XLOG(level, fmt, ...) \ |
| 77 | do { \ |
| 78 | __android_log_print( \ |
| 79 | (ANDROID_LOG_DEBUG + ::perfetto::base::LogLev::level), "perfetto", \ |
| 80 | "%s " fmt, ::perfetto::base::Basename(__FILE__ ":" PERFETTO_LOG_LINE), \ |
| 81 | ##__VA_ARGS__); \ |
| 82 | PERFETTO_XLOG_STDERR(level, fmt, ##__VA_ARGS__); \ |
| 83 | } while (0) |
| 84 | #else |
| 85 | #define PERFETTO_XLOG PERFETTO_XLOG_STDERR |
| 86 | #endif |
| 87 | |
Florian Mayer | 2ab0d77 | 2018-01-17 15:53:17 +0000 | [diff] [blame] | 88 | #define PERFETTO_IMMEDIATE_CRASH() \ |
| 89 | do { \ |
| 90 | __builtin_trap(); \ |
| 91 | __builtin_unreachable(); \ |
| 92 | } while (0) |
| 93 | |
Primiano Tucci | 5aab758 | 2017-12-07 12:22:03 +0000 | [diff] [blame] | 94 | #define PERFETTO_LOG(fmt, ...) PERFETTO_XLOG(kLogInfo, fmt, ##__VA_ARGS__) |
| 95 | #define PERFETTO_ILOG(fmt, ...) PERFETTO_XLOG(kLogImportant, fmt, ##__VA_ARGS__) |
| 96 | #define PERFETTO_ELOG(fmt, ...) PERFETTO_XLOG(kLogError, fmt, ##__VA_ARGS__) |
Florian Mayer | 2ab0d77 | 2018-01-17 15:53:17 +0000 | [diff] [blame] | 97 | #define PERFETTO_FATAL(fmt, ...) \ |
| 98 | do { \ |
| 99 | PERFETTO_ELOG(fmt, ##__VA_ARGS__); \ |
| 100 | PERFETTO_IMMEDIATE_CRASH(); \ |
| 101 | } while (0) |
Primiano Tucci | 5aab758 | 2017-12-07 12:22:03 +0000 | [diff] [blame] | 102 | |
Florian Mayer | 8ede4ac | 2018-03-27 13:13:56 +0100 | [diff] [blame] | 103 | #define PERFETTO_PLOG(x, ...) \ |
| 104 | PERFETTO_ELOG(x " (errno: %d, %s)", ##__VA_ARGS__, errno, strerror(errno)) |
Sami Kyostila | 247110b | 2018-02-14 10:18:45 +0000 | [diff] [blame] | 105 | |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 106 | #if PERFETTO_DCHECK_IS_ON() |
Primiano Tucci | 5aab758 | 2017-12-07 12:22:03 +0000 | [diff] [blame] | 107 | |
| 108 | #define PERFETTO_DLOG(fmt, ...) PERFETTO_XLOG(kLogDebug, fmt, ##__VA_ARGS__) |
| 109 | |
Florian Mayer | 8ede4ac | 2018-03-27 13:13:56 +0100 | [diff] [blame] | 110 | #define PERFETTO_DPLOG(x, ...) \ |
| 111 | PERFETTO_DLOG(x " (errno: %d, %s)", ##__VA_ARGS__, errno, strerror(errno)) |
Primiano Tucci | 5aab758 | 2017-12-07 12:22:03 +0000 | [diff] [blame] | 112 | |
Florian Mayer | 8ede4ac | 2018-03-27 13:13:56 +0100 | [diff] [blame] | 113 | #define PERFETTO_DCHECK(x) \ |
| 114 | do { \ |
| 115 | if (PERFETTO_UNLIKELY(!(x))) { \ |
| 116 | PERFETTO_DPLOG("%s", "PERFETTO_CHECK(" #x ")"); \ |
| 117 | PERFETTO_IMMEDIATE_CRASH(); \ |
| 118 | } \ |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 119 | } while (0) |
Primiano Tucci | 5aab758 | 2017-12-07 12:22:03 +0000 | [diff] [blame] | 120 | |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 121 | #else |
Primiano Tucci | 5aab758 | 2017-12-07 12:22:03 +0000 | [diff] [blame] | 122 | |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 123 | #define PERFETTO_DLOG(...) ::perfetto::base::ignore_result(__VA_ARGS__) |
| 124 | #define PERFETTO_DPLOG(...) ::perfetto::base::ignore_result(__VA_ARGS__) |
| 125 | #define PERFETTO_DCHECK(x) ::perfetto::base::ignore_result(x) |
Primiano Tucci | 5aab758 | 2017-12-07 12:22:03 +0000 | [diff] [blame] | 126 | |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 127 | #endif // PERFETTO_DCHECK_IS_ON() |
| 128 | |
| 129 | #if PERFETTO_DCHECK_IS_ON() |
| 130 | #define PERFETTO_CHECK(x) PERFETTO_DCHECK(x) |
| 131 | #else |
Primiano Tucci | 5aab758 | 2017-12-07 12:22:03 +0000 | [diff] [blame] | 132 | #define PERFETTO_CHECK(x) \ |
| 133 | do { \ |
Primiano Tucci | 19318a7 | 2018-02-22 12:33:02 +0000 | [diff] [blame] | 134 | if (PERFETTO_UNLIKELY(!(x))) { \ |
Primiano Tucci | 5aab758 | 2017-12-07 12:22:03 +0000 | [diff] [blame] | 135 | PERFETTO_ELOG("%s", "PERFETTO_CHECK(" #x ")"); \ |
Florian Mayer | 2ab0d77 | 2018-01-17 15:53:17 +0000 | [diff] [blame] | 136 | PERFETTO_IMMEDIATE_CRASH(); \ |
Primiano Tucci | 5aab758 | 2017-12-07 12:22:03 +0000 | [diff] [blame] | 137 | } \ |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 138 | } while (0) |
Primiano Tucci | 5aab758 | 2017-12-07 12:22:03 +0000 | [diff] [blame] | 139 | |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 140 | #endif // PERFETTO_DCHECK_IS_ON() |
| 141 | |
Primiano Tucci | 5aab758 | 2017-12-07 12:22:03 +0000 | [diff] [blame] | 142 | } // namespace base |
| 143 | } // namespace perfetto |
| 144 | |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 145 | #endif // INCLUDE_PERFETTO_BASE_LOGGING_H_ |