• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2018 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 #include "tensorflow/lite/micro/micro_error_reporter.h"
17 
18 #include <cstdarg>
19 #include <cstdint>
20 #include <new>
21 
22 #if !defined(TF_LITE_STRIP_ERROR_STRINGS)
23 #include "tensorflow/lite/micro/debug_log.h"
24 #include "tensorflow/lite/micro/micro_string.h"
25 #endif
26 
27 namespace {
28 uint8_t micro_error_reporter_buffer[sizeof(tflite::MicroErrorReporter)];
29 tflite::MicroErrorReporter* error_reporter_ = nullptr;
30 
Log(const char * format,va_list args)31 void Log(const char* format, va_list args) {
32 #if !defined(TF_LITE_STRIP_ERROR_STRINGS)
33   // Only pulling in the implementation of this function for builds where we
34   // expect to make use of it to be extra cautious about not increasing the code
35   // size.
36   static constexpr int kMaxLogLen = 256;
37   char log_buffer[kMaxLogLen];
38   MicroVsnprintf(log_buffer, kMaxLogLen, format, args);
39   DebugLog(log_buffer);
40   DebugLog("\r\n");
41 #endif
42 }
43 
44 }  // namespace
45 
46 #if !defined(TF_LITE_STRIP_ERROR_STRINGS)
MicroPrintf(const char * format,...)47 void MicroPrintf(const char* format, ...) {
48   va_list args;
49   va_start(args, format);
50   Log(format, args);
51   va_end(args);
52 }
53 #endif
54 
55 namespace tflite {
GetMicroErrorReporter()56 ErrorReporter* GetMicroErrorReporter() {
57 #if !defined(RENODE)
58   if (error_reporter_ == nullptr) {
59     error_reporter_ = new (micro_error_reporter_buffer) MicroErrorReporter();
60   }
61 #else
62   // TODO(#46937): Until we resolve the global variable issue with Renode, we
63   // will be creating a new ErrorReporter object each time. While this is
64   // inefficient, it still allows us to make progress.
65   error_reporter_ = new (micro_error_reporter_buffer) MicroErrorReporter();
66 #endif
67 
68   return error_reporter_;
69 }
70 
Report(const char * format,va_list args)71 int MicroErrorReporter::Report(const char* format, va_list args) {
72   Log(format, args);
73   return 0;
74 }
75 
76 }  // namespace tflite
77