[feature #5527] allow user to change status-go loglevel in settings

This commit is contained in:
Eric Dvorsak 2018-08-28 05:45:35 +02:00 committed by Eric Dvorsak
parent aa949c64e3
commit 8d053b4389
No known key found for this signature in database
GPG Key ID: 32D08503358DB921
17 changed files with 236 additions and 64 deletions

View File

@ -53,7 +53,7 @@ public class MainApplication extends MultiDexApplication implements ReactApplica
webViewDebugEnabled = true;
}
StatusPackage statusPackage = new StatusPackage(BuildConfig.DEBUG, devCluster, BuildConfig.LOG_LEVEL_STATUS_GO);
StatusPackage statusPackage = new StatusPackage(BuildConfig.DEBUG, devCluster);
Function<String, String> callRPC = statusPackage.getCallRPC();
List<ReactPackage> packages = new ArrayList<ReactPackage>(Arrays.asList(
new MainReactPackage(),

View File

@ -47,17 +47,15 @@ class StatusModule extends ReactContextBaseJavaModule implements LifecycleEventL
private ExecutorService executor = null;
private boolean debug;
private boolean devCluster;
private String logLevel;
private ReactApplicationContext reactContext;
StatusModule(ReactApplicationContext reactContext, boolean debug, boolean devCluster, String logLevel) {
StatusModule(ReactApplicationContext reactContext, boolean debug, boolean devCluster) {
super(reactContext);
if (executor == null) {
executor = Executors.newCachedThreadPool();
}
this.debug = debug;
this.devCluster = devCluster;
this.logLevel = logLevel;
this.reactContext = reactContext;
reactContext.addLifecycleEventListener(this);
}
@ -153,63 +151,59 @@ class StatusModule extends ReactContextBaseJavaModule implements LifecycleEventL
return null;
}
private String generateConfig(final String dataDir, final int networkId, final String keystoreDir, final String fleet, final Object upstreamConfig) throws JSONException {
private String generateConfig(final JSONObject defaultConfig, final String root, final String keystoreDir, final String fleet) throws JSONException {
// retrieve parameters from app config, that will be applied onto the Go-side config later on
final String dataDir = root + defaultConfig.get("DataDir");
final int networkId = defaultConfig.getInt("NetworkId");
final Object upstreamConfig = defaultConfig.opt("UpstreamConfig");
final Boolean logEnabled = defaultConfig.getBoolean("LogEnabled");
final String logLevel = defaultConfig.optString("LogLevel", "ERROR");
JSONObject jsonConfig = new JSONObject(
Statusgo.GenerateConfig(dataDir, fleet, networkId));
// retrieve config from Go side, in order to use as the basis of the config
JSONObject jsonConfig = new JSONObject(
Statusgo.GenerateConfig(dataDir, fleet, networkId));
jsonConfig.put("NetworkId", networkId);
jsonConfig.put("DataDir", dataDir);
jsonConfig.put("KeyStoreDir", keystoreDir);
jsonConfig.put("NetworkId", networkId);
jsonConfig.put("DataDir", dataDir);
jsonConfig.put("KeyStoreDir", keystoreDir);
if (upstreamConfig != null) {
Log.d(TAG, "UpstreamConfig is not null");
jsonConfig.put("UpstreamConfig", upstreamConfig);
}
if (upstreamConfig != null) {
Log.d(TAG, "UpstreamConfig is not null");
jsonConfig.put("UpstreamConfig", upstreamConfig);
}
final String gethLogFilePath = TextUtils.isEmpty(this.logLevel) ? null : prepareLogsFile();
final boolean logsEnabled = (gethLogFilePath != null);
final String gethLogFilePath = logEnabled ? prepareLogsFile() : null;
jsonConfig.put("LogEnabled", logEnabled);
jsonConfig.put("LogFile", gethLogFilePath);
jsonConfig.put("LogLevel", TextUtils.isEmpty(logLevel) ? "ERROR" : logLevel);
jsonConfig.put("LogEnabled", logsEnabled);
jsonConfig.put("LogFile", gethLogFilePath);
jsonConfig.put("LogLevel", TextUtils.isEmpty(this.logLevel) ? "ERROR" : this.logLevel.toUpperCase());
// Setting up whisper config
JSONObject whisperConfig = jsonConfig.optJSONObject("WhisperConfig");
if (whisperConfig == null) {
whisperConfig = new JSONObject();
}
whisperConfig.put("LightClient", true);
jsonConfig.put("WhisperConfig", whisperConfig);
// Setting up cluster config
JSONObject clusterConfig = jsonConfig.optJSONObject("ClusterConfig");
if (clusterConfig != null) {
Log.d(TAG, "ClusterConfig is not null");
clusterConfig.put("Fleet", fleet);
jsonConfig.put("ClusterConfig", clusterConfig);
} else {
Log.w(TAG, "ClusterConfig: Cannot find ClusterConfig: doesn't exist or not a JSON object");
Log.w(TAG, "ClusterConfig: Fleet will be set to defaults");
}
// Setting up whisper config
JSONObject whisperConfig = jsonConfig.optJSONObject("WhisperConfig");
if (whisperConfig == null) {
whisperConfig = new JSONObject();
}
whisperConfig.put("LightClient", true);
jsonConfig.put("WhisperConfig", whisperConfig);
// Setting up cluster config
JSONObject clusterConfig = jsonConfig.optJSONObject("ClusterConfig");
if (clusterConfig != null) {
Log.d(TAG, "ClusterConfig is not null");
clusterConfig.put("Fleet", fleet);
jsonConfig.put("ClusterConfig", clusterConfig);
} else {
Log.w(TAG, "ClusterConfig: Cannot find ClusterConfig: doesn't exist or not a JSON object");
Log.w(TAG, "ClusterConfig: Fleet will be set to defaults");
}
return jsonConfig.toString();
return jsonConfig.toString();
}
private String generateConfigFromDefaultConfig(final String root, final String keystoreDir, final String fleet, final String defaultConfig) {
try {
JSONObject customConfig = new JSONObject(defaultConfig);
// parameters from config
final String dataDir = root + customConfig.get("DataDir");
final int networkId = customConfig.getInt("NetworkId");
final Object upstreamConfig = customConfig.opt("UpstreamConfig");
return generateConfig(dataDir, networkId, keystoreDir, fleet, upstreamConfig);
return generateConfig(customConfig, root, keystoreDir, fleet);
} catch (JSONException e) {
Log.d(TAG, "Something went wrong " + e.getMessage());
Log.d(TAG, "Default configuration will be used: ropsten, beta fleet");

View File

@ -16,12 +16,10 @@ public class StatusPackage implements ReactPackage {
private boolean debug;
private boolean devCluster;
private String logLevel;
public StatusPackage (boolean debug, boolean devCluster, String logLevel) {
public StatusPackage (boolean debug, boolean devCluster) {
this.debug = debug;
this.devCluster = devCluster;
this.logLevel = logLevel;
}
@Override
@ -29,7 +27,7 @@ public class StatusPackage implements ReactPackage {
List<NativeModule> modules = new ArrayList<>();
System.loadLibrary("statusgoraw");
System.loadLibrary("statusgo");
modules.add(new StatusModule(reactContext, this.debug, this.devCluster, this.logLevel));
modules.add(new StatusModule(reactContext, this.debug, this.devCluster));
return modules;
}

View File

@ -108,10 +108,8 @@ void RCTStatus::startNode(QString configString, QString fleet) {
qDebug() << " RCTStatus::startNode GenerateConfig configString: " << jsonDoc.toVariant().toMap();
QVariantMap generatedConfig = jsonDoc.toVariant().toMap();
generatedConfig["KeyStoreDir"] = keyStoreDir;
generatedConfig["LogEnabled"] = true;
generatedConfig["LogFile"] = networkDir + "/geth.log";
generatedConfig["ClusterConfig.Fleet"] = fleet;
//generatedConfig["LogLevel"] = "DEBUG";
const char* result = StartNode(QString(QJsonDocument::fromVariant(generatedConfig).toJson(QJsonDocument::Compact)).toUtf8().data());
qDebug() << "RCTStatus::startNode StartNode result: " << result;

View File

@ -116,7 +116,8 @@ RCT_EXPORT_METHOD(startNode:(NSString *)configString
NSArray *bootnodes = [configJSON valueForKeyPath:@"ClusterConfig.BootNodes"];
NSString *networkDir = [rootUrl.path stringByAppendingString:dataDir];
NSString *devCluster = [ReactNativeConfig envFor:@"ETHEREUM_DEV_CLUSTER"];
NSString *logLevel = [[ReactNativeConfig envFor:@"LOG_LEVEL_STATUS_GO"] uppercaseString];
NSString *logEnabled = [configJSON objectForKey:@"LogEnabled"];
NSString *logLevel = [configJSON objectForKey:@"LogLevel"];
char *configChars = GenerateConfig((char *)[networkDir UTF8String], (char *)[fleet UTF8String], networkId);
NSString *config = [NSString stringWithUTF8String: configChars];
configData = [config dataUsingEncoding:NSUTF8StringEncoding];
@ -124,8 +125,8 @@ RCT_EXPORT_METHOD(startNode:(NSString *)configString
NSURL *networkDirUrl = [NSURL fileURLWithPath:networkDir];
NSURL *logUrl = [networkDirUrl URLByAppendingPathComponent:@"geth.log"];
[resultingConfigJson setValue:newKeystoreUrl.path forKey:@"KeyStoreDir"];
[resultingConfigJson setValue:[NSNumber numberWithBool:[logLevel length] != 0] forKey:@"LogEnabled"];
[resultingConfigJson setValue:([logLevel length] == 0 ? [NSNull null] : logUrl.path) forKey:@"LogFile"];
[resultingConfigJson setValue:logEnabled forKey:@"LogEnabled"];
[resultingConfigJson setValue:logUrl.path forKey:@"LogFile"];
[resultingConfigJson setValue:([logLevel length] == 0 ? [NSString stringWithUTF8String: "ERROR"] : logLevel) forKey:@"LogLevel"];
[resultingConfigJson setValue:[NSNumber numberWithBool:YES] forKeyPath:@"WhisperConfig.LightClient"];

View File

@ -1,6 +1,8 @@
(ns status-im.node.models
(:require [status-im.utils.config :as config]
[status-im.utils.types :as types]))
[status-im.utils.types :as types]
[clojure.string :as str]
[taoensso.timbre :as log]))
(defn- add-custom-bootnodes [config network all-bootnodes]
(let [bootnodes (as-> all-bootnodes $
@ -12,6 +14,14 @@
:BootNodes bootnodes})
config)))
(defn- add-log-level [config log-level]
(if (empty? log-level)
(assoc config
:LogEnabled false)
(assoc config
:LogLevel log-level
:LogEnabled true)))
(defn get-account-network [db address]
(get-in db [:accounts/accounts address :network]))
@ -21,12 +31,21 @@
settings
bootnodes
networks]} (get accounts address)
use-custom-bootnodes (get-in settings [:bootnodes network])]
use-custom-bootnodes (get-in settings [:bootnodes network])
log-level (or (:log-level settings)
config/log-level-status-go)]
(cond-> (get-in networks [network :config])
(and
config/bootnodes-settings-enabled?
use-custom-bootnodes)
(add-custom-bootnodes network bootnodes))))
(add-custom-bootnodes network bootnodes)
:always
(add-log-level log-level))))
(defn get-node-config [db network]
(-> (get-in (:networks/networks db) [network :config])
(add-log-level config/log-level-status-go)))
(defn start
([cofx]
@ -37,10 +56,10 @@
(:network db))
node-config (if address
(get-account-node-config db address)
(get-in (:networks/networks db) [network :config]))
(get-node-config db network))
node-config-json (types/clj->json node-config)]
{:db (assoc db
:network network)
(log/info "Node config: " node-config-json)
{:db (assoc db :network network)
:node/start node-config-json})))
(defn restart

View File

@ -679,6 +679,9 @@
:network-details "Network details"
:remove-network "Remove network"
:network-settings "Network settings"
:log-level "Log level"
:log-level-settings "Log level settings"
:change-log-level "Change log level to {{log-level}}"
:offline-messaging "Mailserver"
:offline-messaging-settings "Mailserver settings"
:edit-network-warning "Be careful! Editing the network data may disable this network for you"

View File

@ -83,6 +83,7 @@
:icons/open (js/require "./resources/icons/open.svg")
:icons/network (js/require "./resources/icons/network.svg")
:icons/wnode (js/require "./resources/icons/wnode.svg")
:icons/log-level (js/require "./resources/icons/wnode.svg")
:icons/refresh (js/require "./resources/icons/refresh.svg")
:icons/newchat (js/require "./resources/icons/newchat.svg")
:icons/logo (js/require "./resources/icons/logo.svg")
@ -145,6 +146,7 @@
:icons/open (components.svg/slurp-svg "./resources/icons/open.svg")
:icons/network (components.svg/slurp-svg "./resources/icons/network.svg")
:icons/wnode (components.svg/slurp-svg "./resources/icons/wnode.svg")
:icons/log-level (components.svg/slurp-svg "./resources/icons/wnode.svg")
:icons/refresh (components.svg/slurp-svg "./resources/icons/refresh.svg")
:icons/newchat (components.svg/slurp-svg "./resources/icons/newchat.svg")
:icons/logo (components.svg/slurp-svg "./resources/icons/logo.svg")
@ -202,4 +204,4 @@
([name] (icon name nil))
([name options]
(let [icon-fn (if platform/desktop? desktop-icon mobile-icon)]
[icon-fn name options])))
[icon-fn name options])))

View File

@ -37,6 +37,7 @@
status-im.ui.screens.wallet.collectibles.superrare.events
status-im.ui.screens.browser.events
status-im.ui.screens.offline-messaging-settings.events
status-im.ui.screens.log-level-settings.events
status-im.ui.screens.privacy-policy.events
status-im.ui.screens.bootnodes-settings.events
status-im.ui.screens.currency-settings.events

View File

@ -0,0 +1,27 @@
(ns status-im.ui.screens.log-level-settings.events
(:require [re-frame.core :as re-frame]
[status-im.i18n :as i18n]
[status-im.ui.screens.accounts.models :as accounts.models]
[status-im.utils.handlers :as handlers]
[status-im.utils.handlers-macro :as handlers-macro]))
(handlers/register-handler-fx
::save-log-level
(fn [{:keys [db now] :as cofx} [_ log-level]]
(let [settings (get-in db [:account/account :settings])]
(handlers-macro/merge-fx cofx
(accounts.models/update-settings
(if log-level
(assoc settings :log-level log-level)
(dissoc settings :log-level))
[:logout])))))
(handlers/register-handler-fx
:change-log-level
(fn [{:keys [db]} [_ {:keys [name value] :as log-level}]]
{:show-confirmation {:title (i18n/label :t/close-app-title)
:content (i18n/label :t/change-log-level
{:log-level name})
:confirm-button-text (i18n/label :t/close-app-button)
:on-accept #(re-frame/dispatch [::save-log-level value])
:on-cancel nil}}))

View File

@ -0,0 +1,48 @@
(ns status-im.ui.screens.log-level-settings.styles
(:require [status-im.ui.components.colors :as colors]
[status-im.utils.platform :as platform])
(:require-macros [status-im.utils.styles :refer [defstyle]]))
(def wrapper
{:flex 1
:background-color :white})
(def log-level-item-inner
{:padding-horizontal 16})
(defstyle log-level-item
{:flex-direction :row
:background-color :white
:align-items :center
:padding-horizontal 16
:ios {:height 64}
:android {:height 56}})
(defstyle log-level-item-name-text
{:color colors/black
:ios {:font-size 17
:letter-spacing -0.2
:line-height 20}
:desktop {:font-size 16}
:android {:font-size 16}})
(defstyle log-level-item-connected-text
{:color colors/gray
:ios {:font-size 14
:margin-top 6
:letter-spacing -0.2}
:android {:font-size 12
:margin-top 2}})
(defn log-level-icon-container [current?]
{:width 40
:height 40
:border-radius 20
:background-color (if current?
colors/blue
colors/gray-light)
:align-items :center
:justify-content :center})
(defn log-level-icon [current?]
(hash-map (if platform/desktop? :tint-color :color) (if current? :white :gray)))

View File

@ -0,0 +1,9 @@
(ns status-im.ui.screens.log-level-settings.subs
(:require [re-frame.core :as re-frame]
[status-im.utils.config :as config]))
(re-frame/reg-sub
:settings/current-log-level
(fn [db _]
(or (get-in db [:account/account :settings :log-level])
config/log-level-status-go)))

View File

@ -0,0 +1,61 @@
(ns status-im.ui.screens.log-level-settings.views
(:require [re-frame.core :as re-frame]
[status-im.i18n :as i18n]
[status-im.ui.components.icons.vector-icons :as vector-icons]
[status-im.ui.components.list.views :as list]
[status-im.ui.components.react :as react]
[status-im.ui.components.status-bar.view :as status-bar]
[status-im.ui.components.toolbar.view :as toolbar]
[status-im.ui.screens.log-level-settings.styles :as styles]
[status-im.utils.platform :as platform])
(:require-macros [status-im.utils.views :as views]))
(defn- log-level-icon [current?]
[react/view (if platform/desktop?
{:style (styles/log-level-icon-container current?)}
(styles/log-level-icon-container current?))
[vector-icons/icon :icons/log-level
(if platform/desktop? {:style (styles/log-level-icon current?)}
(styles/log-level-icon current?))]])
(defn change-log-level [log-level]
(re-frame/dispatch [:change-log-level log-level]))
(defn render-row [current-log-level]
(fn [{:keys [name value] :as log-level}]
(let [current? (= value current-log-level)]
[react/touchable-highlight
{:on-press #(change-log-level log-level)
:accessibility-label :log-level-item}
[react/view styles/log-level-item
[log-level-icon current?]
[react/view styles/log-level-item-inner
[react/text {:style styles/log-level-item-name-text}
name]]]])))
(def log-levels
[{:name "DISABLED"
:value ""}
{:name "ERROR"
:value "ERROR"}
{:name "WARN"
:value "WARN"}
{:name "INFO"
:value "INFO"}
{:name "DEBUG"
:value "DEBUG"}
{:name "TRACE"
:value "TRACE"}])
(views/defview log-level-settings []
(views/letsubs [current-log-level [:settings/current-log-level]]
[react/view {:flex 1}
[status-bar/status-bar]
[toolbar/toolbar {}
toolbar/default-nav-back
[toolbar/content-title (i18n/label :t/log-level-settings)]]
[react/view styles/wrapper
[list/flat-list {:data log-levels
:default-separator? false
:key-fn :name
:render-fn (render-row current-log-level)}]]]))

View File

@ -161,6 +161,11 @@
{:label-kw :t/offline-messaging
:action-fn #(re-frame/dispatch [:navigate-to :offline-messaging-settings])
:accessibility-label :offline-messages-settings-button}]
[profile.components/settings-item-separator]
[profile.components/settings-item
{:label-kw :t/log-level
:action-fn #(re-frame/dispatch [:navigate-to :log-level-settings])
:accessibility-label :log-level-settings-button}]
(when config/bootnodes-settings-enabled?
[profile.components/settings-item-separator])
(when config/bootnodes-settings-enabled?

View File

@ -13,6 +13,7 @@
status-im.ui.screens.wallet.send.subs
status-im.ui.screens.wallet.transactions.subs
status-im.ui.screens.network-settings.subs
status-im.ui.screens.log-level-settings.subs
status-im.ui.screens.offline-messaging-settings.subs
status-im.ui.screens.bootnodes-settings.subs
status-im.ui.screens.currency-settings.subs

View File

@ -43,6 +43,7 @@
[status-im.ui.screens.network-settings.network-details.views :refer [network-details]]
[status-im.ui.screens.network-settings.edit-network.views :refer [edit-network]]
[status-im.ui.screens.extensions.views :refer [extensions-settings]]
[status-im.ui.screens.log-level-settings.views :refer [log-level-settings]]
[status-im.ui.screens.offline-messaging-settings.views :refer [offline-messaging-settings]]
[status-im.ui.screens.offline-messaging-settings.edit-mailserver.views :refer [edit-mailserver]]
[status-im.ui.screens.extensions.add.views :refer [add-extension show-extension]]
@ -95,6 +96,7 @@
:extensions-settings extensions-settings
:network-details network-details
:edit-network edit-network
:log-level-settings log-level-settings
:offline-messaging-settings offline-messaging-settings
:edit-mailserver edit-mailserver
:add-extension add-extension

View File

@ -33,6 +33,9 @@
(-> (get-config :LOG_LEVEL "error")
string/lower-case
keyword))
(def log-level-status-go
(-> (get-config :LOG_LEVEL_STATUS_GO "")
string/upper-case))
(def fleet (get-config :FLEET "eth.beta"))
(def default-network (get-config :DEFAULT_NETWORK))
;; the default value should be a string for `enabled?` to work correctly.