Primiano Tucci | d7d1be0 | 2017-10-30 17:41:34 +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 PERFETTO_BASE_LOGGING_H_ |
| 18 | #define PERFETTO_BASE_LOGGING_H_ |
| 19 | |
| 20 | #include <errno.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <unistd.h> |
| 23 | |
| 24 | #if defined(NDEBUG) |
| 25 | #define PERFETTO_DCHECK_IS_ON() 0 |
| 26 | #else |
| 27 | #define PERFETTO_DCHECK_IS_ON() 1 |
| 28 | #include <stdio.h> // For fprintf. |
| 29 | #include <string.h> // For strerror. |
| 30 | #endif |
| 31 | |
| 32 | #include "base/utils.h" |
| 33 | |
| 34 | #if PERFETTO_DCHECK_IS_ON() |
Primiano Tucci | 60fb878 | 2017-11-09 13:45:05 +0000 | [diff] [blame] | 35 | #define PERFETTO_DLOG(fmt, ...) \ |
| 36 | fprintf(stderr, "\n[%s:%d, errno: %d %s]\n" fmt "\n\n", __FILE__, __LINE__, \ |
| 37 | errno, errno ? strerror(errno) : "", ##__VA_ARGS__) |
| 38 | #define PERFETTO_DPLOG(...) PERFETTO_DLOG(__VA_ARGS__) |
Primiano Tucci | 60af59d | 2017-11-28 12:53:55 +0000 | [diff] [blame] | 39 | #define PERFETTO_DCHECK(x) \ |
| 40 | do { \ |
| 41 | if (!__builtin_expect(!!(x), true)) { \ |
| 42 | PERFETTO_DPLOG("%s", "PERFETTO_CHECK(" #x ")"); \ |
| 43 | *(reinterpret_cast<volatile int*>(0x10)) = 0x42; \ |
| 44 | __builtin_unreachable(); \ |
| 45 | } \ |
Primiano Tucci | d7d1be0 | 2017-10-30 17:41:34 +0000 | [diff] [blame] | 46 | } while (0) |
| 47 | #else |
| 48 | #define PERFETTO_DLOG(...) ::perfetto::base::ignore_result(__VA_ARGS__) |
| 49 | #define PERFETTO_DPLOG(...) ::perfetto::base::ignore_result(__VA_ARGS__) |
| 50 | #define PERFETTO_DCHECK(x) ::perfetto::base::ignore_result(x) |
| 51 | #endif // PERFETTO_DCHECK_IS_ON() |
| 52 | |
| 53 | #if PERFETTO_DCHECK_IS_ON() |
| 54 | #define PERFETTO_CHECK(x) PERFETTO_DCHECK(x) |
| 55 | #else |
| 56 | #define PERFETTO_CHECK(x) \ |
| 57 | do { \ |
| 58 | if (!__builtin_expect(!!(x), true)) \ |
| 59 | abort(); \ |
| 60 | } while (0) |
| 61 | #endif // PERFETTO_DCHECK_IS_ON() |
| 62 | |
| 63 | #endif // PERFETTO_BASE_LOGGING_H_ |