Skip to content

Commit

Permalink
Log new ErrorReporter and old nnapi delegate errors to logcat.
Browse files Browse the repository at this point in the history
On Android stderr is not captured for applications, so developers do not see
the errors from tflite. This adds logcat output.

Output to stderr is kept, as it is convenient for unit tests run through the
shell.

PiperOrigin-RevId: 208463170
  • Loading branch information
tensorflower-gardener committed Aug 13, 2018
1 parent 9b44e8b commit 2c623ea
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
13 changes: 13 additions & 0 deletions tensorflow/contrib/lite/error_reporter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ limitations under the License.
#include <cstdarg>
#include <cstdio>

#ifdef __ANDROID__
#include <android/log.h>
#endif

namespace tflite {

ErrorReporter::~ErrorReporter() {}
Expand All @@ -39,6 +43,15 @@ int ErrorReporter::ReportError(void*, const char* format, ...) {
}

int StderrReporter::Report(const char* format, va_list args) {
#ifdef __ANDROID__
// On Android stderr is not captured for applications, only for code run from
// the shell. Rather than assume all users will set up a custom error
// reporter, let's output to logcat here
va_list args_for_log;
va_copy(args_for_log, args);
__android_log_vprint(ANDROID_LOG_ERROR, "tflite", format, args_for_log);
va_end(args_for_log);
#endif
const int result = vfprintf(stderr, format, args);
fputc('\n', stderr);
return result;
Expand Down
19 changes: 13 additions & 6 deletions tensorflow/contrib/lite/nnapi_delegate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,27 @@ limitations under the License.
#include "tensorflow/contrib/lite/nnapi/NeuralNetworksShim.h"

#ifdef __ANDROID__
#include <android/log.h>
#include <sys/system_properties.h>
#endif

namespace tflite {

void logError(const char* format, ...) {
// TODO(mikie): use android logging, stderr is not captured for Java
// applications
va_list args;
va_start(args, format);
vfprintf(stderr, format, args);
va_end(args);
// stderr is convenient for native tests, but is not captured for apps
va_list args_for_stderr;
va_start(args_for_stderr, format);
vfprintf(stderr, format, args_for_stderr);
va_end(args_for_stderr);
fprintf(stderr, "\n");
fflush(stderr);
#ifdef __ANDROID__
// produce logcat output for general consumption
va_list args_for_log;
va_start(args_for_log, format);
__android_log_vprint(ANDROID_LOG_ERROR, "tflite", format, args_for_log);
va_end(args_for_log);
#endif
}

#define FATAL(...) \
Expand Down

0 comments on commit 2c623ea

Please sign in to comment.