| 1 | /* |
| 2 | Copyright 2018 Google Inc. All Rights Reserved. |
| 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 RESONANCE_AUDIO_PLATFORM_LOGGING_H_ |
| 18 | #define RESONANCE_AUDIO_PLATFORM_LOGGING_H_ |
| 19 | |
| 20 | #include <cstdlib> |
| 21 | |
| 22 | #include <cassert> |
| 23 | #include <iostream> |
| 24 | #include <sstream> |
| 25 | |
| 26 | #undef DCHECK |
| 27 | #undef DCHECK_EQ |
| 28 | #undef DCHECK_NE |
| 29 | #undef DCHECK_LE |
| 30 | #undef DCHECK_LT |
| 31 | #undef DCHECK_GE |
| 32 | #undef DCHECK_GT |
| 33 | #undef CHECK |
| 34 | #undef CHECK_EQ |
| 35 | #undef CHECK_NE |
| 36 | #undef CHECK_LE |
| 37 | #undef CHECK_LT |
| 38 | #undef CHECK_GE |
| 39 | #undef CHECK_GT |
| 40 | #undef CHECK_NOTNULL |
| 41 | #undef LOG |
| 42 | |
| 43 | // This class is used to disable logging, while still allowing for log messages |
| 44 | // to contain '<<' expressions. |
| 45 | class NullLogger { |
| 46 | public: |
| 47 | std::ostream& GetStream() { |
| 48 | static std::ostream kNullStream(nullptr); |
| 49 | return kNullStream; |
| 50 | } |
| 51 | }; |
| 52 | |
| 53 | // If statement prevents unused variable warnings. |
| 54 | #define DCHECK(expr) \ |
| 55 | if (false && (expr)) \ |
| 56 | ; \ |
| 57 | else \ |
| 58 | NullLogger().GetStream() |
| 59 | #define DCHECK_OP(val1, val2, op) DCHECK((val1)op(val2)) |
| 60 | |
| 61 | #define DCHECK_EQ(val1, val2) DCHECK_OP((val1), (val2), ==) |
| 62 | #define DCHECK_NE(val1, val2) DCHECK_OP((val1), (val2), !=) |
| 63 | #define DCHECK_LE(val1, val2) DCHECK_OP((val1), (val2), <=) |
| 64 | #define DCHECK_LT(val1, val2) DCHECK_OP((val1), (val2), <) |
| 65 | #define DCHECK_GE(val1, val2) DCHECK_OP((val1), (val2), >=) |
| 66 | #define DCHECK_GT(val1, val2) DCHECK_OP((val1), (val2), >) |
| 67 | |
| 68 | // This class is used to log to std::cerr. |
| 69 | class FatalLogger { |
| 70 | public: |
| 71 | FatalLogger(const char* file, int line) { |
| 72 | error_string_ << file << ":" << line << ": " ; |
| 73 | } |
| 74 | ~FatalLogger() { |
| 75 | const std::string error_string = error_string_.str(); |
| 76 | std::cerr << error_string << std::endl; |
| 77 | abort(); |
| 78 | } |
| 79 | std::ostream& GetStream() { return error_string_; } |
| 80 | |
| 81 | private: |
| 82 | std::ostringstream error_string_; |
| 83 | }; |
| 84 | |
| 85 | #define CHECK(condition) \ |
| 86 | !(condition) ? FatalLogger(__FILE__, __LINE__).GetStream() \ |
| 87 | : NullLogger().GetStream() |
| 88 | |
| 89 | #define CHECK_OP(val1, val2, op) CHECK((val1)op(val2)) |
| 90 | |
| 91 | #define CHECK_EQ(val1, val2) CHECK_OP((val1), (val2), ==) |
| 92 | #define CHECK_NE(val1, val2) CHECK_OP((val1), (val2), !=) |
| 93 | #define CHECK_LE(val1, val2) CHECK_OP((val1), (val2), <=) |
| 94 | #define CHECK_LT(val1, val2) CHECK_OP((val1), (val2), <) |
| 95 | #define CHECK_GE(val1, val2) CHECK_OP((val1), (val2), >=) |
| 96 | #define CHECK_GT(val1, val2) CHECK_OP((val1), (val2), >) |
| 97 | |
| 98 | // Helper for CHECK_NOTNULL(), using C++11 perfect forwarding. |
| 99 | template <typename T> |
| 100 | T CheckNotNull(T&& t) { |
| 101 | assert(t != nullptr); |
| 102 | return std::forward<T>(t); |
| 103 | } |
| 104 | #define CHECK_NOTNULL(val) CheckNotNull(val) |
| 105 | |
| 106 | #define LOG(severity) NullLogger().GetStream() |
| 107 | |
| 108 | #endif // RESONANCE_AUDIO_PLATFORM_LOGGING_H_ |
| 109 | |