1 /* Copyright 2019 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 #ifndef TENSORFLOW_LITE_MINIMAL_LOGGING_H_ 16 #define TENSORFLOW_LITE_MINIMAL_LOGGING_H_ 17 18 #include <cstdarg> 19 20 namespace tflite { 21 22 enum LogSeverity { 23 TFLITE_LOG_INFO = 0, 24 TFLITE_LOG_WARNING = 1, 25 TFLITE_LOG_ERROR = 2, 26 }; 27 28 namespace logging_internal { 29 30 // Helper class for simple platform-specific console logging. Note that we 31 // explicitly avoid the convenience of ostream-style logging to minimize binary 32 // size impact. 33 class MinimalLogger { 34 public: 35 // Logging hook that takes variadic args. 36 static void Log(LogSeverity severity, const char* format, ...); 37 38 // Logging hook that takes a formatted va_list. 39 static void LogFormatted(LogSeverity severity, const char* format, 40 va_list args); 41 42 private: 43 static const char* GetSeverityName(LogSeverity severity); 44 }; 45 46 } // namespace logging_internal 47 } // namespace tflite 48 49 // Convenience macro for basic internal logging in production builds. 50 // Note: This should never be used for debug-type logs, as it will *not* be 51 // stripped in release optimized builds. In general, prefer the error reporting 52 // APIs for developer-facing errors, and only use this for diagnostic output 53 // that should always be logged in user builds. 54 #define TFLITE_LOG_PROD(severity, format, ...) \ 55 tflite::logging_internal::MinimalLogger::Log(severity, format, ##__VA_ARGS__); 56 57 // Convenience macro for logging a statement *once* for a given process lifetime 58 // in production builds. 59 #define TFLITE_LOG_PROD_ONCE(severity, format, ...) \ 60 do { \ 61 static const bool s_logged = [&] { \ 62 TFLITE_LOG_PROD(severity, format, ##__VA_ARGS__) \ 63 return true; \ 64 }(); \ 65 (void)s_logged; \ 66 } while (false); 67 68 #ifndef NDEBUG 69 // In debug builds, always log. 70 #define TFLITE_LOG TFLITE_LOG_PROD 71 #define TFLITE_LOG_ONCE TFLITE_LOG_PROD_ONCE 72 #else 73 // In prod builds, never log, but ensure the code is well-formed and compiles. 74 #define TFLITE_LOG(severity, format, ...) \ 75 while (false) { \ 76 TFLITE_LOG_PROD(severity, format, ##__VA_ARGS__); \ 77 } 78 #define TFLITE_LOG_ONCE TFLITE_LOG 79 #endif 80 81 #endif // TENSORFLOW_LITE_MINIMAL_LOGGING_H_ 82