3636using  namespace  executorch ::extension; 
3737using  namespace  torch ::executor; 
3838
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- 
10939namespace  executorch ::extension {
11040class  TensorHybrid  : public  facebook ::jni::HybridClass<TensorHybrid> {
11141 public: 
@@ -437,24 +367,26 @@ class ExecuTorchJni : public facebook::jni::HybridClass<ExecuTorchJni> {
437367  facebook::jni::local_ref<facebook::jni::JArrayClass<jstring>>
438368  readLogBuffer () {
439369#ifdef  __ANDROID__
440-     std::lock_guard<std::mutex> guard (log_buffer_mutex_);
441370
442371    const  auto  size = log_buffer_.size ();
443372    facebook::jni::local_ref<facebook::jni::JArrayClass<jstring>> ret =
444373        facebook::jni::JArrayClass<jstring>::newArray (size);
445374
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-     }
375+     access_log_buffer ([&](std::vector<log_entry>& buffer) {
376+       for  (auto  i = 0u ; i < size; i++) {
377+         const  auto & entry = buffer[i];
378+         //  Format the log entry as "[TIMESTAMP FUNCTION FILE:LINE] LEVEL
379+         //  MESSAGE".
380+         std::stringstream ss;
381+         ss << " ["   << entry.timestamp  << "  "   << entry.function  << "  " 
382+            << entry.filename  << " :"   << entry.line  << " ] " 
383+            << static_cast <char >(entry.level ) << "  "   << entry.message ;
384+ 
385+         facebook::jni::local_ref<facebook::jni::JString> jstr_message =
386+             facebook::jni::make_jstring (ss.str ().c_str ());
387+         (*ret)[i] = jstr_message;
388+       }
389+     });
458390
459391    return  ret;
460392#else 
@@ -468,10 +400,7 @@ class ExecuTorchJni : public facebook::jni::HybridClass<ExecuTorchJni> {
468400        makeNativeMethod (" forward"  , ExecuTorchJni::forward),
469401        makeNativeMethod (" execute"  , ExecuTorchJni::execute),
470402        makeNativeMethod (" loadMethod"  , ExecuTorchJni::load_method),
471- 
472- #ifdef  __ANDROID__
473403        makeNativeMethod (" readLogBuffer"  , ExecuTorchJni::readLogBuffer),
474- #endif 
475404    });
476405  }
477406};
0 commit comments