• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 namespace tflite {
30 namespace logging {
31 // A wrapper that logs to stderr.
32 //
33 // Used for TFLITE_LOG and TFLITE_BENCHMARK_CHECK macros.
34 class LoggingWrapper {
35  public:
36   enum class LogSeverity : int {
37     INFO = 0,
38     WARN = 1,
39     ERROR = 2,
40     FATAL = 3,
41   };
LoggingWrapper(LogSeverity severity)42   LoggingWrapper(LogSeverity severity)
43       : severity_(severity), should_log_(true) {}
LoggingWrapper(LogSeverity severity,bool log)44   LoggingWrapper(LogSeverity severity, bool log)
45       : severity_(severity), should_log_(log) {}
Stream()46   std::stringstream& Stream() { return stream_; }
~LoggingWrapper()47   ~LoggingWrapper() {
48     if (should_log_) {
49       switch (severity_) {
50         case LogSeverity::INFO:
51         case LogSeverity::WARN:
52           std::cout << stream_.str() << std::endl;
53           break;
54         case LogSeverity::ERROR:
55           std::cerr << stream_.str() << std::endl;
56           break;
57         case LogSeverity::FATAL:
58           std::cerr << stream_.str() << std::endl;
59           std::flush(std::cerr);
60           std::abort();
61           break;
62       }
63     }
64   }
65 
66  private:
67   std::stringstream stream_;
68   LogSeverity severity_;
69   bool should_log_;
70 };
71 }  // namespace logging
72 }  // namespace tflite
73 
74 #define TFLITE_LOG(severity)                                  \
75   tflite::logging::LoggingWrapper(                            \
76       tflite::logging::LoggingWrapper::LogSeverity::severity) \
77       .Stream()
78 
79 #define TFLITE_MAY_LOG(severity, should_log)                                \
80   tflite::logging::LoggingWrapper(                                          \
81       tflite::logging::LoggingWrapper::LogSeverity::severity, (should_log)) \
82       .Stream()
83 
84 #define TFLITE_TOOLS_CHECK(condition) TFLITE_MAY_LOG(FATAL, !(condition))
85 
86 #define TFLITE_TOOLS_CHECK_EQ(a, b) TFLITE_TOOLS_CHECK((a) == (b))
87 
88 #endif  // TENSORFLOW_LITE_TOOLS_LOGGING_H_
89