Skip to content

Commit 8001f63

Browse files
GregoryComerfacebook-github-bot
authored andcommitted
Add Android standalone log target (#6590)
Summary: Add a standalone Android log target to enable ExecuTorch logging to logcat when not using JNI bindings. Differential Revision: D65268257
1 parent c045c35 commit 8001f63

File tree

4 files changed

+55
-29
lines changed

4 files changed

+55
-29
lines changed

extension/android/BUCK

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
load("@fbsource//tools/build_defs/android:fb_android_library.bzl", "fb_android_library")
2+
load("@fbsource//tools/build_defs:fb_xplat_cxx_library.bzl", "fb_xplat_cxx_library")
23

34
oncall("executorch")
45

@@ -39,3 +40,17 @@ fb_android_library(
3940
"//fbandroid/libraries/soloader/java/com/facebook/soloader/nativeloader:nativeloader",
4041
],
4142
)
43+
44+
fb_xplat_cxx_library(
45+
name = "log_provider",
46+
srcs = ["jni/log.cpp"],
47+
compiler_flags = [
48+
"-frtti",
49+
"-fexceptions",
50+
"-Wno-unused-variable",
51+
],
52+
deps = [
53+
"//xplat/executorch/runtime/platform:platform",
54+
],
55+
visibility = ["PUBLIC"],
56+
)

extension/android/jni/BUCK

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ executorch_generated_lib(
2525

2626
fb_android_cxx_library(
2727
name = "executorch_jni",
28-
srcs = ["jni_layer.cpp"],
28+
srcs = ["jni_layer.cpp", "log.cpp"],
2929
headers = ["jni_layer_constants.h"],
3030
allow_jni_merging = False,
3131
compiler_flags = [
@@ -49,7 +49,7 @@ fb_android_cxx_library(
4949

5050
fb_android_cxx_library(
5151
name = "executorch_jni_full",
52-
srcs = ["jni_layer.cpp"],
52+
srcs = ["jni_layer.cpp", "log.cpp"],
5353
headers = ["jni_layer_constants.h"],
5454
allow_jni_merging = False,
5555
compiler_flags = [

extension/android/jni/jni_layer.cpp

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -33,33 +33,6 @@
3333
#include <fbjni/ByteBuffer.h>
3434
#include <fbjni/fbjni.h>
3535

36-
#ifdef __ANDROID__
37-
#include <android/log.h>
38-
39-
// For Android, write to logcat
40-
void et_pal_emit_log_message(
41-
et_timestamp_t timestamp,
42-
et_pal_log_level_t level,
43-
const char* filename,
44-
const char* function,
45-
size_t line,
46-
const char* message,
47-
size_t length) {
48-
int android_log_level = ANDROID_LOG_UNKNOWN;
49-
if (level == 'D') {
50-
android_log_level = ANDROID_LOG_DEBUG;
51-
} else if (level == 'I') {
52-
android_log_level = ANDROID_LOG_INFO;
53-
} else if (level == 'E') {
54-
android_log_level = ANDROID_LOG_ERROR;
55-
} else if (level == 'F') {
56-
android_log_level = ANDROID_LOG_FATAL;
57-
}
58-
59-
__android_log_print(android_log_level, "ExecuTorch", "%s", message);
60-
}
61-
#endif
62-
6336
using namespace executorch::extension;
6437
using namespace torch::executor;
6538

extension/android/jni/log.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
#include <executorch/runtime/platform/log.h>
10+
#include <executorch/runtime/platform/platform.h>
11+
#include <executorch/runtime/platform/runtime.h>
12+
13+
#ifdef __ANDROID__
14+
#include <android/log.h>
15+
16+
// For Android, write to logcat
17+
void et_pal_emit_log_message(
18+
et_timestamp_t timestamp,
19+
et_pal_log_level_t level,
20+
const char* filename,
21+
const char* function,
22+
size_t line,
23+
const char* message,
24+
size_t length) {
25+
int android_log_level = ANDROID_LOG_UNKNOWN;
26+
if (level == 'D') {
27+
android_log_level = ANDROID_LOG_DEBUG;
28+
} else if (level == 'I') {
29+
android_log_level = ANDROID_LOG_INFO;
30+
} else if (level == 'E') {
31+
android_log_level = ANDROID_LOG_ERROR;
32+
} else if (level == 'F') {
33+
android_log_level = ANDROID_LOG_FATAL;
34+
}
35+
36+
__android_log_print(android_log_level, "ExecuTorch", "%s", message);
37+
}
38+
#endif

0 commit comments

Comments
 (0)