1717
1818#include " jni_layer_constants.h"
1919
20+ #include < executorch/extension/android/jni/log.h>
2021#include < executorch/extension/module/module.h>
2122#include < executorch/extension/runner_util/inputs.h>
2223#include < executorch/extension/tensor/tensor.h>
3637using namespace executorch ::extension;
3738using namespace torch ::executor;
3839
39- #ifdef __ANDROID__
40- #include < android/log.h>
41- #include < mutex>
42- #include < sstream>
43-
44- // Number of entries to store in the in-memory log buffer.
45- const size_t log_buffer_length = 16 ;
46-
47- struct log_entry {
48- et_timestamp_t timestamp;
49- et_pal_log_level_t level;
50- std::string filename;
51- std::string function;
52- size_t line;
53- std::string message;
54-
55- log_entry (
56- et_timestamp_t timestamp,
57- et_pal_log_level_t level,
58- const char * filename,
59- const char * function,
60- size_t line,
61- const char * message,
62- size_t length)
63- : timestamp(timestamp),
64- level (level),
65- filename(filename),
66- function(function),
67- line(line),
68- message(message, length) {}
69- };
70-
71- namespace {
72- std::vector<log_entry> log_buffer_;
73- std::mutex log_buffer_mutex_;
74- } // namespace
75-
76- // For Android, write to logcat
77- void et_pal_emit_log_message (
78- et_timestamp_t timestamp,
79- et_pal_log_level_t level,
80- const char * filename,
81- const char * function,
82- size_t line,
83- const char * message,
84- size_t length) {
85- std::lock_guard<std::mutex> guard (log_buffer_mutex_);
86-
87- while (log_buffer_.size () >= log_buffer_length) {
88- log_buffer_.erase (log_buffer_.begin ());
89- }
90-
91- log_buffer_.emplace_back (
92- timestamp, level, filename, function, line, message, length);
93-
94- int android_log_level = ANDROID_LOG_UNKNOWN;
95- if (level == ' D' ) {
96- android_log_level = ANDROID_LOG_DEBUG;
97- } else if (level == ' I' ) {
98- android_log_level = ANDROID_LOG_INFO;
99- } else if (level == ' E' ) {
100- android_log_level = ANDROID_LOG_ERROR;
101- } else if (level == ' F' ) {
102- android_log_level = ANDROID_LOG_FATAL;
103- }
104-
105- __android_log_print (android_log_level, " ExecuTorch" , " %s" , message);
106- }
107- #endif
108-
10940namespace executorch ::extension {
11041class TensorHybrid : public facebook ::jni::HybridClass<TensorHybrid> {
11142 public:
@@ -437,24 +368,26 @@ class ExecuTorchJni : public facebook::jni::HybridClass<ExecuTorchJni> {
437368 facebook::jni::local_ref<facebook::jni::JArrayClass<jstring>>
438369 readLogBuffer () {
439370#ifdef __ANDROID__
440- std::lock_guard<std::mutex> guard (log_buffer_mutex_);
441-
442- const auto size = log_buffer_.size ();
443- facebook::jni::local_ref<facebook::jni::JArrayClass<jstring>> ret =
444- facebook::jni::JArrayClass<jstring>::newArray (size);
445-
446- for (auto i = 0u ; i < size; i++) {
447- const auto & entry = log_buffer_[i];
448- // Format the log entry as "[TIMESTAMP FUNCTION FILE:LINE] LEVEL MESSAGE".
449- std::stringstream ss;
450- ss << " [" << entry.timestamp << " " << entry.function << " "
451- << entry.filename << " :" << entry.line << " ] "
452- << static_cast <char >(entry.level ) << " " << entry.message ;
453-
454- facebook::jni::local_ref<facebook::jni::JString> jstr_message =
455- facebook::jni::make_jstring (ss.str ().c_str ());
456- (*ret)[i] = jstr_message;
457- }
371+
372+ facebook::jni::local_ref<facebook::jni::JArrayClass<jstring>> ret;
373+
374+ access_log_buffer ([&](std::vector<log_entry>& buffer) {
375+ const auto size = buffer.size ();
376+ ret = facebook::jni::JArrayClass<jstring>::newArray (size);
377+ for (auto i = 0u ; i < size; i++) {
378+ const auto & entry = buffer[i];
379+ // Format the log entry as "[TIMESTAMP FUNCTION FILE:LINE] LEVEL
380+ // MESSAGE".
381+ std::stringstream ss;
382+ ss << " [" << entry.timestamp << " " << entry.function << " "
383+ << entry.filename << " :" << entry.line << " ] "
384+ << static_cast <char >(entry.level ) << " " << entry.message ;
385+
386+ facebook::jni::local_ref<facebook::jni::JString> jstr_message =
387+ facebook::jni::make_jstring (ss.str ().c_str ());
388+ (*ret)[i] = jstr_message;
389+ }
390+ });
458391
459392 return ret;
460393#else
@@ -468,10 +401,7 @@ class ExecuTorchJni : public facebook::jni::HybridClass<ExecuTorchJni> {
468401 makeNativeMethod (" forward" , ExecuTorchJni::forward),
469402 makeNativeMethod (" execute" , ExecuTorchJni::execute),
470403 makeNativeMethod (" loadMethod" , ExecuTorchJni::load_method),
471-
472- #ifdef __ANDROID__
473404 makeNativeMethod (" readLogBuffer" , ExecuTorchJni::readLogBuffer),
474- #endif
475405 });
476406 }
477407};
0 commit comments