xref: /aosp_15_r20/external/tensorflow/tensorflow/lite/minimal_logging_ios.cc (revision b6fb3261f9314811a0f4371741dbb8839866f948)
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 
16 #include <syslog.h>
17 
18 #include <cstdarg>
19 #include <cstdio>
20 
21 #include "tensorflow/lite/minimal_logging.h"
22 
23 namespace tflite {
24 namespace logging_internal {
25 namespace {
26 
GetPlatformSeverity(LogSeverity severity)27 int GetPlatformSeverity(LogSeverity severity) {
28   switch (severity) {
29     case TFLITE_LOG_VERBOSE:
30     case TFLITE_LOG_INFO:
31       return LOG_INFO;
32     case TFLITE_LOG_WARNING:
33       return LOG_WARNING;
34     case TFLITE_LOG_ERROR:
35       return LOG_ERR;
36     default:
37       return LOG_DEBUG;
38   }
39 }
40 
41 }  // namespace
42 
43 #ifndef NDEBUG
44 // In debug builds, default is VERBOSE.
45 LogSeverity MinimalLogger::minimum_log_severity_ = TFLITE_LOG_VERBOSE;
46 #else
47 // In prod builds, default is INFO.
48 LogSeverity MinimalLogger::minimum_log_severity_ = TFLITE_LOG_INFO;
49 #endif
50 
LogFormatted(LogSeverity severity,const char * format,va_list args)51 void MinimalLogger::LogFormatted(LogSeverity severity, const char* format,
52                                  va_list args) {
53   // First log to iOS system logging API.
54   va_list args_copy;
55   va_copy(args_copy, args);
56   // TODO(b/123704468): Use os_log when available.
57   vsyslog(GetPlatformSeverity(severity), format, args_copy);
58   va_end(args_copy);
59 
60   // Also print to stderr for standard console applications.
61   fprintf(stderr, "%s: ", GetSeverityName(severity));
62   va_copy(args_copy, args);
63   vfprintf(stderr, format, args_copy);
64   va_end(args_copy);
65   fputc('\n', stderr);
66 }
67 
68 }  // namespace logging_internal
69 }  // namespace tflite
70