Skip to content

Commit 3815668

Browse files
authored
Adding logging support on droid and logging failure message (#31)
1 parent e3fe96c commit 3815668

File tree

5 files changed

+27
-19
lines changed

5 files changed

+27
-19
lines changed

ReactAndroid/src/main/java/com/facebook/react/v8executor/OnLoad.cpp

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,6 @@
1212
#include <react/jni/JSLogging.h>
1313
#include <react/jni/ReadableNativeMap.h>
1414

15-
namespace facebook {
16-
namespace v8runtime {
17-
std::unique_ptr<jsi::Runtime> makeV8Runtime();
18-
} // namespace v8runtime
19-
} // namespace facebook
20-
2115
namespace facebook {
2216
namespace react {
2317

@@ -31,13 +25,16 @@ class V8ExecutorFactory : public JSExecutorFactory {
3125

3226
std::unique_ptr<JSExecutor> createJSExecutor(
3327
std::shared_ptr<ExecutorDelegate> delegate,
34-
std::shared_ptr<MessageQueueThread> jsQueue) override {
28+
std::shared_ptr<MessageQueueThread> jsQueue) override {
29+
30+
auto logger = std::make_shared<JSIExecutor::Logger>([](const std::string& message, unsigned int logLevel) {
31+
reactAndroidLoggingHook(message, logLevel);
32+
});
33+
3534
return folly::make_unique<JSIExecutor>(
36-
facebook::v8runtime::makeV8Runtime(m_v8Config),
35+
facebook::v8runtime::makeV8Runtime(m_v8Config, logger),
3736
delegate,
38-
[](const std::string& message, unsigned int logLevel) {
39-
reactAndroidLoggingHook(message, logLevel);
40-
},
37+
*logger,
4138
JSIExecutor::defaultTimeoutInvoker,
4239
nullptr);
4340
}

ReactCommon/jsi/V8Runtime.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ namespace v8runtime {
6060
std::unique_ptr<const jsi::Buffer> custom_snapshot = nullptr); /*Optional*/
6161

6262
std::unique_ptr<jsi::Runtime> makeV8Runtime();
63-
std::unique_ptr<jsi::Runtime> makeV8Runtime(const folly::dynamic& v8Config);
63+
std::unique_ptr<jsi::Runtime> makeV8Runtime(const folly::dynamic& v8Config, const std::shared_ptr<Logger>& logger);
6464

6565
} // namespace v8runtime
6666
} // namespace facebook

ReactCommon/jsi/V8Runtime_droid.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ namespace facebook { namespace v8runtime {
5050
}
5151
}
5252

53-
V8Runtime::V8Runtime(const folly::dynamic& v8Config) : V8Runtime() {
53+
V8Runtime::V8Runtime(const folly::dynamic& v8Config, const std::shared_ptr<Logger>& logger) : V8Runtime() {
54+
logger_ = logger;
5455
isCacheEnabled_ = IsCacheEnabled(v8Config);
5556
shouldProduceFullCache_ = ShouldProduceFullCache(v8Config);
5657
shouldSetNoLazyFlag_ = ShouldSetNoLazyFlag(v8Config);

ReactCommon/jsi/V8Runtime_impl.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ namespace facebook { namespace v8runtime {
4343
class V8Runtime : public jsi::Runtime {
4444
public:
4545
V8Runtime();
46-
V8Runtime(const folly::dynamic& v8Config);
46+
V8Runtime(const folly::dynamic& v8Config, const std::shared_ptr<Logger>& logger);
4747

4848
V8Runtime(const v8::Platform* platform, std::shared_ptr<Logger>&& logger,
4949
std::shared_ptr<facebook::react::MessageQueueThread>&& jsQueue, std::shared_ptr<CacheProvider>&& cacheProvider,
@@ -374,6 +374,12 @@ namespace facebook { namespace v8runtime {
374374
bool ExecuteString(v8::Local<v8::String> source, const jsi::Buffer* cache, v8::Local<v8::Value> name, bool report_exceptions);
375375
bool ExecuteString(const v8::Local<v8::String>& source, const std::string& sourceURL);
376376

377+
void Log(const std::string& message, const unsigned int logLevel) {
378+
if (logger_) {
379+
(*logger_)("V8Runtime:: " + message, logLevel);
380+
}
381+
}
382+
377383
void ReportException(v8::TryCatch* try_catch);
378384

379385
v8::Isolate* GetIsolate() const { return isolate_; }

ReactCommon/jsi/V8Runtime_shared.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,9 @@ namespace facebook { namespace v8runtime {
247247
if (message.IsEmpty()) {
248248
// V8 didn't provide any extra information about this error; just
249249
// throw the exception.
250-
throw jsi::JSError(*this, "<Unknown exception>");
250+
std::string errorMessage{ "<Unknown exception>" };
251+
Log(errorMessage, 3 /*LogLevel error*/);
252+
throw jsi::JSError(*this, errorMessage);
251253
}
252254
else {
253255
// Print (filename):(line number): (message).
@@ -283,7 +285,9 @@ namespace facebook { namespace v8runtime {
283285
sstr << stack_trace_string2 << std::endl;
284286
}
285287

286-
throw jsi::JSError(*this, sstr.str());
288+
std::string errorMessage{ sstr.str() };
289+
Log(errorMessage, 3 /*LogLevel error*/);
290+
throw jsi::JSError(*this, errorMessage);
287291
}
288292
}
289293

@@ -740,7 +744,7 @@ namespace facebook { namespace v8runtime {
740744
return std::make_unique<V8Runtime>();
741745
}
742746

743-
std::unique_ptr<jsi::Runtime> makeV8Runtime(const folly::dynamic& v8Config) {
744-
return std::make_unique<V8Runtime>(v8Config);
747+
std::unique_ptr<jsi::Runtime> makeV8Runtime(const folly::dynamic& v8Config, const std::shared_ptr<Logger>& logger) {
748+
return std::make_unique<V8Runtime>(v8Config, logger);
745749
}
746-
}} // namespace facebook::v8runtime
750+
}} // namespace facebook::v8runtime

0 commit comments

Comments
 (0)