init status-go logging once app start up (#16325)

4cc53630...7da1ed38
This commit is contained in:
frank 2023-06-22 07:45:55 +08:00 committed by GitHub
parent 83a298f831
commit db44ee67e6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 115 additions and 17 deletions

View File

@ -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);
}
}

View File

@ -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

View File

@ -1873,6 +1873,35 @@ void _ConnectionChange(const FunctionCallbackInfo<Value>& args) {
ConnectionChange(arg0, arg1);
}
void _InitLogging(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
Local<Context> 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<String> ret = String::NewFromUtf8(isolate, c).ToLocalChecked();
args.GetReturnValue().Set(ret);
delete c;
}
void init(Local<Object> exports) {
NODE_SET_METHOD(exports, "multiAccountGenerateAndDeriveAddresses", _MultiAccountGenerateAndDeriveAddresses);
@ -1922,13 +1951,14 @@ void init(Local<Object> 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)

View File

@ -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))

View File

@ -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")}))))}))

View File

@ -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

View File

@ -3,7 +3,7 @@
"_comment": "Instead use: scripts/update-status-go.sh <rev>",
"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"
}