From db44ee67e6f7b9156fe715a8db996a1e2b9af76f Mon Sep 17 00:00:00 2001 From: frank Date: Thu, 22 Jun 2023 07:45:55 +0800 Subject: [PATCH] init status-go logging once app start up (#16325) https://github.com/status-im/status-go/compare/4cc53630...7da1ed38 --- .../status/ethereum/module/StatusModule.java | 13 +++++++- .../ios/RCTStatus/RCTStatus.m | 31 +++++++++++++++++ modules/react-native-status/nodejs/status.cpp | 32 +++++++++++++++++- src/native_module/core.cljs | 4 +++ src/status_im/utils/test.cljs | 13 ++++++-- src/status_im2/common/log.cljs | 33 +++++++++++++------ status-go-version.json | 6 ++-- 7 files changed, 115 insertions(+), 17 deletions(-) diff --git a/modules/react-native-status/android/src/main/java/im/status/ethereum/module/StatusModule.java b/modules/react-native-status/android/src/main/java/im/status/ethereum/module/StatusModule.java index 17addc28ba..9a2f02305f 100644 --- a/modules/react-native-status/android/src/main/java/im/status/ethereum/module/StatusModule.java +++ b/modules/react-native-status/android/src/main/java/im/status/ethereum/module/StatusModule.java @@ -154,8 +154,8 @@ class StatusModule extends ReactContextBaseJavaModule implements LifecycleEventL // Environment.getExternalStoragePublicDirectory doesn't work as expected on Android Q // https://developer.android.com/reference/android/os/Environment#getExternalStoragePublicDirectory(java.lang.String) return context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS); - } + private File getLogsFile() { final File pubDirectory = this.getPublicStorageDirectory(); final File logFile = new File(pubDirectory, gethLogFileName); @@ -1374,5 +1374,16 @@ class StatusModule extends ReactContextBaseJavaModule implements LifecycleEventL }, callback); } + @ReactMethod + public void initLogging(final boolean enabled, final boolean mobileSystem, final String logLevel, final Callback callback) throws JSONException { + final JSONObject jsonConfig = new JSONObject(); + jsonConfig.put("Enabled", enabled); + jsonConfig.put("MobileSystem", mobileSystem); + jsonConfig.put("Level", logLevel); + jsonConfig.put("File", getLogsFile().getAbsolutePath()); + final String config = jsonConfig.toString(); + executeRunnableStatusGoMethod(() -> Statusgo.initLogging(config), callback); + } + } diff --git a/modules/react-native-status/ios/RCTStatus/RCTStatus.m b/modules/react-native-status/ios/RCTStatus/RCTStatus.m index fa09b3b092..9ec053c35b 100644 --- a/modules/react-native-status/ios/RCTStatus/RCTStatus.m +++ b/modules/react-native-status/ios/RCTStatus/RCTStatus.m @@ -906,6 +906,37 @@ RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(logFileDirectory) { return rootUrl.path; } +RCT_EXPORT_METHOD(initLogging:(BOOL)enabled + mobileSystem:(BOOL)mobileSystem + logLevel:(NSString *)logLevel + callback:(RCTResponseSenderBlock)callback) +{ + NSString *logDirectory = [self logFileDirectory]; + NSString *logFilePath = [logDirectory stringByAppendingPathComponent:@"geth.log"]; + + NSMutableDictionary *jsonConfig = [NSMutableDictionary dictionary]; + jsonConfig[@"Enabled"] = @(enabled); + jsonConfig[@"MobileSystem"] = @(mobileSystem); + jsonConfig[@"Level"] = logLevel; + jsonConfig[@"File"] = logFilePath; + + NSError *error = nil; + NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonConfig options:0 error:&error]; + + if (error) { + // Handle JSON serialization error + callback(@[error.localizedDescription]); + return; + } + + NSString *config = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; + + // Call your native logging initialization method here + NSString *initResult = StatusgoInitLogging(config); + + callback(@[initResult]); +} + RCT_EXPORT_METHOD(generateAliasAsync:(NSString *)publicKey callback:(RCTResponseSenderBlock)callback) { #if DEBUG diff --git a/modules/react-native-status/nodejs/status.cpp b/modules/react-native-status/nodejs/status.cpp index 86c5c94269..0a71ed1f96 100644 --- a/modules/react-native-status/nodejs/status.cpp +++ b/modules/react-native-status/nodejs/status.cpp @@ -1873,6 +1873,35 @@ void _ConnectionChange(const FunctionCallbackInfo& args) { ConnectionChange(arg0, arg1); } +void _InitLogging(const FunctionCallbackInfo& args) { + Isolate* isolate = args.GetIsolate(); + Local context = isolate->GetCurrentContext(); + + if (args.Length() != 1) { + // Throw an Error that is passed back to JavaScript + isolate->ThrowException(Exception::TypeError( + String::NewFromUtf8Literal(isolate, "Wrong number of arguments for InitLogging"))); + return; + } + + // Check the argument types + if (!args[0]->IsString()) { + isolate->ThrowException(Exception::TypeError( + String::NewFromUtf8Literal(isolate, "Wrong argument type for 'logSettingsJSON'"))); + return; + } + + String::Utf8Value arg0Obj(isolate, args[0]->ToString(context).ToLocalChecked()); + char *arg0 = *arg0Obj; + + // Call exported Go function, which returns a C string + char *c = InitLogging(arg0); + + Local ret = String::NewFromUtf8(isolate, c).ToLocalChecked(); + args.GetReturnValue().Set(ret); + delete c; +} + void init(Local exports) { NODE_SET_METHOD(exports, "multiAccountGenerateAndDeriveAddresses", _MultiAccountGenerateAndDeriveAddresses); @@ -1922,13 +1951,14 @@ void init(Local exports) { NODE_SET_METHOD(exports, "signTypedData", _SignTypedData); NODE_SET_METHOD(exports, "sendTransaction", _SendTransaction); NODE_SET_METHOD(exports, "appStateChange", _AppStateChange); - NODE_SET_METHOD(exports, "setSignalEventCallback", _SetSignalEventCallback); + NODE_SET_METHOD(exports, "setSignalEventCallback", _SetSignalEventCallback); NODE_SET_METHOD(exports, "validateNodeConfig", _ValidateNodeConfig); NODE_SET_METHOD(exports, "hashTypedData", _HashTypedData); NODE_SET_METHOD(exports, "recover", _Recover); NODE_SET_METHOD(exports, "hashTransaction", _HashTransaction); NODE_SET_METHOD(exports, "connectionChange", _ConnectionChange); NODE_SET_METHOD(exports, "pollSignal", _PollSignal); + NODE_SET_METHOD(exports, "initLogging", _InitLogging); } NODE_MODULE(NODE_GYP_MODULE_NAME, init) diff --git a/src/native_module/core.cljs b/src/native_module/core.cljs index 729306892b..839df4cae6 100644 --- a/src/native_module/core.cljs +++ b/src/native_module/core.cljs @@ -545,3 +545,7 @@ (defn log-file-directory [] (.logFileDirectory ^js (status))) + +(defn init-status-go-logging + [{:keys [enable? mobile-system? log-level callback]}] + (.initLogging ^js (status) enable? mobile-system? log-level callback)) diff --git a/src/status_im/utils/test.cljs b/src/status_im/utils/test.cljs index 3161afe806..1e2fec692d 100644 --- a/src/status_im/utils/test.cljs +++ b/src/status_im/utils/test.cljs @@ -1,5 +1,6 @@ (ns status-im.utils.test - (:require [re-frame.core :as re-frame])) + (:require [re-frame.core :as re-frame] + [status-im.utils.types :as types])) (def native-status (js/require "../../modules/react-native-status/nodejs/bindings")) @@ -125,4 +126,12 @@ :validateMnemonic (fn [json callback] (callback (.validateMnemonic native-status json))) - :startLocalNotifications identity})) + :startLocalNotifications identity + + :initLogging + (fn [enabled mobile-system log-level callback] + (callback (.initLogging native-status + (types/clj->json {:Enabled enabled + :MobileSystem mobile-system + :Level log-level + :File (str test-dir "/geth.log")}))))})) diff --git a/src/status_im2/common/log.cljs b/src/status_im2/common/log.cljs index 1ed62c150b..9dc17bb1d5 100644 --- a/src/status_im2/common/log.cljs +++ b/src/status_im2/common/log.cljs @@ -3,7 +3,9 @@ [re-frame.core :as re-frame] [status-im2.config :as config] [taoensso.timbre :as log] - [utils.re-frame :as rf])) + [utils.re-frame :as rf] + [native-module.core :as native-module] + [status-im.utils.types :as types])) (def logs-queue (atom #queue [])) (def max-log-entries 1000) @@ -18,15 +20,26 @@ (defn setup [level] - (when-not (string/blank? level) - (log/set-level! (-> level - string/lower-case - keyword)) - (log/merge-config! - {:output-fn (fn [& data] - (let [res (apply log/default-output-fn data)] - (add-log-entry res) - res))}))) + (let [handle-error (fn [res] + (let [{:keys [error]} (types/json->clj res)] + (when-not (string/blank? error) + (log/error "init statusgo logging failed" error)))) + logging-params {:enable? true + :mobile-system? false + :log-level level + :callback handle-error}] + (if (string/blank? level) + (native-module/init-status-go-logging (merge logging-params {:log-level "WARN"})) + (do + (log/set-level! (-> level + string/lower-case + keyword)) + (log/merge-config! + {:output-fn (fn [& data] + (let [res (apply log/default-output-fn data)] + (add-log-entry res) + res))}) + (native-module/init-status-go-logging logging-params))))) (re-frame/reg-fx :logs/set-level diff --git a/status-go-version.json b/status-go-version.json index 8402242d6f..33491f41fa 100644 --- a/status-go-version.json +++ b/status-go-version.json @@ -3,7 +3,7 @@ "_comment": "Instead use: scripts/update-status-go.sh ", "owner": "status-im", "repo": "status-go", - "version": "v0.159.2", - "commit-sha1": "fee033fadbaf7654ffde52c224e21603afbd122e", - "src-sha256": "0m9r3wknmc96r90pgv918im09fnia21hsdcxcd2gcn18jrjsy13b" + "version": "v0.159.4", + "commit-sha1": "7da1ed38d4da3c6f9f5bd6fb32657b4183e8fa74", + "src-sha256": "10j8p8ng0i6jkv8bplfwwq847yfvyvnz1r2xf5csiy0b86gdwn8n" }