1 /* Copyright 2020 The TensorFlow Authors. All Rights Reserved. 2 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9 Unless required by applicable law or agreed to in writing, software 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14 ==============================================================================*/ 15 16 #ifndef TENSORFLOW_LITE_TOOLS_LOGGING_H_ 17 #define TENSORFLOW_LITE_TOOLS_LOGGING_H_ 18 19 // LOG and CHECK macros for tflite tooling. 20 21 #include <cstdlib> 22 #include <iostream> 23 #include <sstream> 24 25 #ifdef _WIN32 26 #undef ERROR 27 #endif 28 29 #ifdef __ANDROID__ 30 #include <android/log.h> 31 #endif 32 33 namespace tflite { 34 namespace logging { 35 // A wrapper that logs to stderr. 36 // 37 // Used for TFLITE_LOG and TFLITE_BENCHMARK_CHECK macros. 38 class LoggingWrapper { 39 public: 40 enum class LogSeverity : int { 41 INFO = 0, 42 WARN = 1, 43 ERROR = 2, 44 FATAL = 3, 45 }; LoggingWrapper(LogSeverity severity)46 explicit LoggingWrapper(LogSeverity severity) 47 : severity_(severity), should_log_(true) {} LoggingWrapper(LogSeverity severity,bool log)48 LoggingWrapper(LogSeverity severity, bool log) 49 : severity_(severity), should_log_(log) {} Stream()50 std::stringstream& Stream() { return stream_; } ~LoggingWrapper()51 ~LoggingWrapper() { 52 if (should_log_) { 53 // Also print log to logcat for android, as stderr will be hidden 54 // in the app use case. 55 #ifdef __ANDROID__ 56 switch (severity_) { 57 case LogSeverity::INFO: 58 __android_log_print(ANDROID_LOG_INFO, "tflite", "%s", 59 stream_.str().c_str()); 60 break; 61 case LogSeverity::WARN: 62 __android_log_print(ANDROID_LOG_WARN, "tflite", "%s", 63 stream_.str().c_str()); 64 break; 65 case LogSeverity::ERROR: 66 __android_log_print(ANDROID_LOG_ERROR, "tflite", "%s", 67 stream_.str().c_str()); 68 break; 69 case LogSeverity::FATAL: 70 __android_log_print(ANDROID_LOG_ERROR, "tflite", "%s", 71 stream_.str().c_str()); 72 } 73 #endif 74 switch (severity_) { 75 case LogSeverity::INFO: 76 case LogSeverity::WARN: 77 std::cout << stream_.str() << std::endl; 78 break; 79 case LogSeverity::ERROR: 80 std::cerr << stream_.str() << std::endl; 81 break; 82 case LogSeverity::FATAL: 83 std::cerr << stream_.str() << std::endl; 84 std::flush(std::cerr); 85 std::abort(); 86 break; 87 } 88 } 89 } 90 91 private: 92 std::stringstream stream_; 93 LogSeverity severity_; 94 bool should_log_; 95 }; 96 } // namespace logging 97 } // namespace tflite 98 99 #define TFLITE_LOG(severity) \ 100 tflite::logging::LoggingWrapper( \ 101 tflite::logging::LoggingWrapper::LogSeverity::severity) \ 102 .Stream() 103 104 #define TFLITE_MAY_LOG(severity, should_log) \ 105 tflite::logging::LoggingWrapper( \ 106 tflite::logging::LoggingWrapper::LogSeverity::severity, (should_log)) \ 107 .Stream() 108 109 #define TFLITE_TOOLS_CHECK(condition) TFLITE_MAY_LOG(FATAL, !(condition)) 110 111 #define TFLITE_TOOLS_CHECK_EQ(a, b) TFLITE_TOOLS_CHECK((a) == (b)) 112 113 #endif // TENSORFLOW_LITE_TOOLS_LOGGING_H_ 114