Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #298 - Configuration of app's logger #317

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
f03d126
[ui] refs #298 - Add Radio Buttons to select log output destination a…
AntiD2ta Jan 4, 2020
0364ad1
[config] refs #298 - Add default configuration for log level
AntiD2ta Jan 4, 2020
0b39a01
[ui] refs #298 - Add config option for log level
AntiD2ta Jan 4, 2020
b956f08
[main] [config] refs #298 - Add log level settings
AntiD2ta Jan 5, 2020
7fbc7a7
[ui] refs #298 - Add TextField for log Level
AntiD2ta Jan 5, 2020
2346a76
Merge branch 'develop' into stdevAntiD2ta_t298_Log_events_to_file
AntiD2ta Jan 18, 2020
9cf37c9
[util] refs #298 - Keep references of packages loggers
AntiD2ta Jan 18, 2020
9cad3e2
[settings] Add log level as an independent option
mauricio1802 Jan 18, 2020
a693890
[ui] refs #298 - Change path to log level configuration
AntiD2ta Jan 18, 2020
9b97156
[util] refs #298 - Add function to set log's output to a file
AntiD2ta Jan 18, 2020
85f0c7e
[util] refs #298 - Add some changes to logging package
AntiD2ta Jan 19, 2020
74e7cdd
[config] refs #298 - Add configuration option for log's output
AntiD2ta Jan 19, 2020
fca2d43
[main] refs #298 - Set log's output correctly given option in settings
AntiD2ta Jan 19, 2020
9ea9df3
[ui] refs #298 - Add ui to set log's output in Settings
AntiD2ta Jan 19, 2020
8f7bc72
[ui] - Remove unnecessary qml log
AntiD2ta Jan 19, 2020
4ded230
[logging] refs #298 - Create Writer for none option
AntiD2ta Jan 20, 2020
8bb88cb
[logging] refs #298 - Add stderr and none options to SetOutput
AntiD2ta Jan 20, 2020
f4215c5
[config] refs #298 - Change default value for log's output to none
AntiD2ta Jan 20, 2020
f313996
[models] refs #298 - Apply changed settings related to logs when they…
AntiD2ta Jan 20, 2020
c053490
[gitignore] - Ignore .log extensions
AntiD2ta Jan 20, 2020
a2b05a3
[logging] refs #298 - Refactorize code
AntiD2ta Jan 21, 2020
512ec7a
[skycoin] [models] refs #298 - Change output of SkycoinProject's logger
AntiD2ta Jan 21, 2020
40b6449
[main] - Handle unmarshaling node settings error
AntiD2ta Jan 21, 2020
3bf565f
[makefile] - Improve test-core rule to have a coverage profile
AntiD2ta Jan 21, 2020
2cfd783
[logging] refs #298 - Change permissions of file for logs to 0600
AntiD2ta Jan 21, 2020
99a2875
Merge remote-tracking branch 'simelo_alt/stdevRulo_t279_improve_cover…
olemis Jan 22, 2020
6803f10
[config] - Add logs to package config
AntiD2ta Jan 22, 2020
6e4c5c1
[models] - Add logs to config view model
AntiD2ta Jan 22, 2020
fac268e
[logging] - Fix bug
AntiD2ta Jan 22, 2020
10e5cb0
[logging] refs #298 - Add initial tests for logging package
AntiD2ta Jan 22, 2020
9ca97e0
Merge remote-tracking branch 'simelo/stdevAntiD2ta_t298_Log_events_to…
AntiD2ta Jan 22, 2020
04159e1
Merge branch 'stdevRulo_t279_improve_coverage' into stdevAntiD2ta_t29…
AntiD2ta Jan 22, 2020
9e13d3f
[test] - Solve ineffectual assignments to `val`
AntiD2ta Jan 22, 2020
ee944a6
[test] - Solve assignment to val variable problem
AntiD2ta Jan 22, 2020
03ba11f
[test] - Amend incorrect path to a directory
AntiD2ta Jan 23, 2020
7334734
[test] - Solve ineffectual assignment to `val`
AntiD2ta Jan 23, 2020
4ef8650
Merge remote-tracking branch 'fc/develop' into stdevAntiD2ta_t298_Log…
olemis Jan 23, 2020
1651c5f
[view] refs #298 - Remove initial implementation of log level Combo Box
AntiD2ta Jan 23, 2020
81d34da
[test] refs #298 - Fix linter errors
AntiD2ta Jan 24, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,5 @@ darwin

# Auto-generates go files.

*.pb.go
*.pb.go
*.log
24 changes: 22 additions & 2 deletions src/coin/skycoin/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,27 @@ import (
"strings"

local "github.com/fibercrypto/fibercryptowallet/src/main"
"github.com/fibercrypto/fibercryptowallet/src/util/logging"
)

const (
LocalWallet = "local"
RemoteWallet = "remote"
SectionName = "skycoin"
SettingPathToLog = "log"
SettingPathToNode = "node"
SettingPathToWalletSource = "walletSource"
)

var (
sectionManager *local.SectionManager
log = logging.MustGetLogger("Skycoin Config")
)

func getMultiPlatformUserDirectory() string {
usr, err := user.Current()
if err != nil {
//TODO: Log error
log.WithError(err).Error()
return ""
}
return filepath.Join(usr.HomeDir, string(os.PathSeparator), ".skycoin", string(os.PathSeparator), "wallets")
Expand All @@ -53,7 +56,21 @@ func RegisterConfig() error {

wltOpt := local.NewOption(string(wltSrc.id), []string{SettingPathToWalletSource}, false, string(wltSrcBytes))

sectionManager = cm.RegisterSection(SectionName, []*local.Option{nodeOpt, wltOpt})
level := map[string]string{"level": "warn"}
levelBytes, err := json.Marshal(level)
if err != nil {
return err
}
logLevelOpt := local.NewOption(SettingPathToLog, []string{}, false, string(levelBytes))

output := map[string]string{"output": "none"}
outputBytes, err := json.Marshal(output)
if err != nil {
return err
}
logOutputOpt := local.NewOption(SettingPathToLog, []string{}, false, string(outputBytes))

sectionManager = cm.RegisterSection(SectionName, []*local.Option{nodeOpt, wltOpt, logLevelOpt, logOutputOpt})
return nil
}

Expand All @@ -71,12 +88,14 @@ func GetDataRefreshTimeout() uint64 {
sm := cm.GetSectionManager("global")
value, err := sm.GetValue("cache", nil)
if err != nil {
log.WithError(err).Warn("Couldn't get cache value option for saved settings")
return 0
}

keyValue := make(map[string]string)
err = json.Unmarshal([]byte(value), &keyValue)
if err != nil {
log.WithError(err).Warn("Couldn't unmarshal from options")
return 0
}
strVal, ok := keyValue["lifeTime"]
Expand All @@ -85,6 +104,7 @@ func GetDataRefreshTimeout() uint64 {
}
val, err := strconv.ParseUint(strVal, 10, 64)
if err != nil {
log.WithError(err).Warn("Couldn't parse %s to int", strVal)
return 0
}
return val
Expand Down
35 changes: 31 additions & 4 deletions src/coin/skycoin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package skycoin //nolint goimports
import (
"encoding/json"

skylog "github.com/SkycoinProject/skycoin/src/util/logging"
"github.com/fibercrypto/fibercryptowallet/src/coin/skycoin/config"
sky "github.com/fibercrypto/fibercryptowallet/src/coin/skycoin/models"
"github.com/fibercrypto/fibercryptowallet/src/core"
Expand All @@ -20,15 +21,41 @@ func init() {
if err != nil {
logSkycoin.Warn("Couldn't register Skycoin configuration")
}
nodeStr, err := config.GetOption(config.SettingPathToNode)

logSettingStr, err := config.GetOption(config.SettingPathToLog)
if err != nil {
logSkycoin.Warn("Couldn't get log options")
}
logSetting := make(map[string]string)
err = json.Unmarshal([]byte(logSettingStr), &logSetting)
if err != nil {
logSkycoin.Warn("Couldn't get node options")
logSkycoin.WithError(err).Warn("Couldn't unmarshal from options")
}
level, err := logging.LevelFromString(logSetting["level"])
if err != nil {
logSkycoin.Warn("Couldn't get level from logging")
logSkycoin.WithError(err).WithField("string", logSetting["level"]).Error()
} else {
logging.SetLevel(level)
}
writer, err := logging.GetOutputWriter(logSetting["output"])
if err != nil {
logSkycoin.WithError(err).Error("Error opening file: ", logSetting["output"])
} else {
logging.SetOutputTo(writer)
skylog.SetOutputTo(writer)
}

nodeSettingStr, err := config.GetOption(config.SettingPathToNode)
if err != nil {
logSkycoin.Warn("Couldn't get node settings")
}
node := make(map[string]string)
err = json.Unmarshal([]byte(nodeStr), &node)
err = json.Unmarshal([]byte(nodeSettingStr), &node)
if err != nil {
logSkycoin.Warn("Couldn't unmarshal from options")
logSkycoin.WithError(err).Warn("Couldn't unmarshal from options")
}

err = core.GetMultiPool().CreateSection(sky.PoolSection, sky.NewSkycoinConnectionFactory(node["address"]))
if err != nil {
logSkycoin.Warn("Couldn't create section for Skycoin")
Expand Down
6 changes: 5 additions & 1 deletion src/main/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,18 @@ func (cm *ConfigManager) RegisterSection(name string, options []*Option) *Sectio
defer cm.setting.Sync()

for _, opt := range options {
depthLevel := 0
for _, sect := range opt.sectionPath {
cm.setting.BeginGroup(sect)
defer cm.setting.EndGroup()
depthLevel++
}
if !opt.optional && !cm.setting.Contains(opt.name) {
cm.setting.SetValue(opt.name, qtcore.NewQVariant1(opt._default))

}
for i := 0; i < depthLevel; i++ {
cm.setting.EndGroup()
}
}

return cm.sections[name]
Expand Down
41 changes: 33 additions & 8 deletions src/models/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ import (

local "github.com/fibercrypto/fibercryptowallet/src/main"

"github.com/therecipe/qt/qml"

skylog "github.com/SkycoinProject/skycoin/src/util/logging"
"github.com/fibercrypto/fibercryptowallet/src/util/logging"
qtcore "github.com/therecipe/qt/core"
"github.com/therecipe/qt/qml"
)

var log = logging.MustGetLogger("Config Model")

type ConfigManager struct {
qtcore.QObject
configManager *local.ConfigManager
Expand Down Expand Up @@ -75,6 +78,8 @@ func (cm *ConfigManager) getDefaultValue(path string) string {
return cm.GetSection(section).getDefaultValue(optName, optPath, name)
}

//TODO: Exported type ConfigSection should have comment or be unexported (linter suggestion)
// ConfigSection <description>
type ConfigSection struct {
qtcore.QObject
sm *local.SectionManager
Expand All @@ -96,7 +101,7 @@ func (cs *ConfigSection) getPaths() [][]string {
func (cs *ConfigSection) getOptions(path []string) []*KeyValueStorage {
opts, err := cs.sm.GetValues(path)
if err != nil {
//log error
log.WithError(err).Warn("Couldn't get values from options")
return nil
}

Expand All @@ -115,7 +120,7 @@ func (cs *ConfigSection) getOptions(path []string) []*KeyValueStorage {
func (cs *ConfigSection) getOption(name string, path []string) *KeyValueStorage {
opt, err := cs.sm.GetValue(name, path)
if err != nil {
//log error
log.WithError(err).Warn("Couldn't get value %s from saved settings", name)
return nil
}

Expand All @@ -129,24 +134,42 @@ func (cs *ConfigSection) getOption(name string, path []string) *KeyValueStorage
func (cs *ConfigSection) saveOptionValue(opt string, path []string, name string, value string) {
optV := cs.getOption(opt, path)
if optV == nil {
//log error
log.Warn("Couldn't get option %s from saved settings when saving its new value", opt)
return
}

optV.setValue(name, value)
data, err := json.Marshal(optV.keyValues)
if err != nil {
//log error
log.WithError(err).Error("Couldn't marshal values")
return
}
cs.sm.Save(opt, path, string(data))

if opt == "log" {
if name == "level" {
level, err := logging.LevelFromString(value)
if err != nil {
log.WithError(err).Warn("%s is not a correct log level", value)
} else {
logging.SetLevel(level)
}
} else if name == "output" {
writer, err := logging.GetOutputWriter(value)
if err != nil {
log.WithError(err).Warn("%s is not a correct output entry or path", value)
} else {
logging.SetOutputTo(writer)
skylog.SetOutputTo(writer)
}
}
}
}

func (cs *ConfigSection) getValue(opt string, path []string, name string) string {
optV := cs.getOption(opt, path)
if optV == nil {
//log error
log.Warn("Couldn't get value %s from saved settings", opt)
return ""
}
return optV.getValue(name)
Expand All @@ -161,6 +184,8 @@ func (cs *ConfigSection) getDefaultValue(opt string, path []string, name string)
return val
}

//TODO: Exported type KeyValueStorage should have comment or be unexported (linter suggestion)
// KeyValueStorage <description>
type KeyValueStorage struct {
qtcore.QObject
_ func() []string `slot:"getKeys"`
Expand Down Expand Up @@ -192,7 +217,7 @@ func (kv *KeyValueStorage) getKeys() []string {
func (kv *KeyValueStorage) getValue(key string) string {
val, ok := kv.keyValues[key]
if !ok {
//log error
log.Warn("Incorrect key %s", key)
return ""
}
return val
Expand Down
65 changes: 61 additions & 4 deletions src/ui/Settings.qml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ Page {
readonly property string defaultWalletPath: configManager.getDefaultValue("skycoin/walletSource/1/Source")
readonly property bool defaultIsLocalWalletEnv: configManager.getDefaultValue("skycoin/walletSource/1/SourceType") === "local"
readonly property string defaultNodeUrl: configManager.getDefaultValue("skycoin/node/address")
readonly property string defaultLogLevel: configManager.getDefaultValue("skycoin/log/level")
readonly property string defaultLogOutput: configManager.getDefaultValue("skycoin/log/output")
readonly property var defaultCacheLifeTime: configManager.getDefaultValue("global/cache/lifeTime")

// These are the saved settings, must be applied when the settings are opened or when
Expand All @@ -30,14 +32,24 @@ Page {
property string savedWalletPath: configManager.getValue("skycoin/walletSource/1/Source")
property bool savedIsLocalWalletEnv: configManager.getValue("skycoin/walletSource/1/SourceType") === "local"
property url savedNodeUrl: configManager.getValue("skycoin/node/address")
property string savedLogLevel: configManager.getValue("skycoin/log/level")
property string savedLogOutput: configManager.getValue("skycoin/log/output")
property var savedLifeTime: configManager.getValue("global/cache/lifeTime")

// QtObject{
// id: logLevel
// property string modifier
// property string old
// }

// These are the properties that are actually set, so they are aliases of the respective
// control's properties
property alias walletPath: textFieldWalletPath.text
property alias isLocalWalletEnv: switchLocalWalletEnv.checked
property alias nodeUrl: textFieldNodeUrl.text
property alias cacheLifeTime: textFieldCacheLifeTime.text
property alias logLevel: textFieldLogLevel.text
property alias logOutput: textFieldLogOutput.text

Component.onCompleted: {
loadSavedSettings()
Expand All @@ -47,7 +59,8 @@ Page {
configManager.setValue("skycoin/walletSource/1/Source", walletPath)
configManager.setValue("skycoin/walletSource/1/SourceType", isLocalWalletEnv ? "local" : "remote")
configManager.setValue("skycoin/node/address", nodeUrl)
configManager.setValue("global/cache/lifeTime", cacheLifeTime)
configManager.setValue("skycoin/log/level", logLevel)
configManager.setValue("skycoin/log/output", logOutput)
loadSavedSettings()
}

Expand All @@ -56,6 +69,8 @@ Page {
isLocalWalletEnv = savedIsLocalWalletEnv = configManager.getValue("skycoin/walletSource/1/SourceType") === "local"
nodeUrl = savedNodeUrl = configManager.getValue("skycoin/node/address")
cacheLifeTime = savedLifeTime = configManager.getValue("global/cache/lifeTime")
logLevel = savedLogLevel = configManager.getValue("skycoin/log/level")
logOutput = savedLogOutput = configManager.getValue("skycoin/log/output")

updateFooterButtonsStatus()
}
Expand All @@ -65,13 +80,15 @@ Page {
isLocalWalletEnv = defaultIsLocalWalletEnv
nodeUrl = defaultNodeUrl
cacheLifeTime = defaultCacheLifeTime
logLevel = defaultLogLevel
logOutput = defaultLogOutput

saveCurrentSettings()
}

function updateFooterButtonsStatus() {
var configChanged = (walletPath !== savedWalletPath || isLocalWalletEnv !== savedIsLocalWalletEnv || nodeUrl != savedNodeUrl || cacheLifeTime != savedLifeTime)
var noDefaultConfig = (walletPath !== defaultWalletPath || isLocalWalletEnv !== defaultIsLocalWalletEnv || nodeUrl !== defaultNodeUrl || cacheLifeTime || defaultCacheLifeTime)
var configChanged = (walletPath !== savedWalletPath || isLocalWalletEnv !== savedIsLocalWalletEnv || nodeUrl != savedNodeUrl || cacheLifeTime != savedLifeTime || logLevel != savedLogLevel || logOutput != savedLogOutput)
var noDefaultConfig = (walletPath !== defaultWalletPath || isLocalWalletEnv !== defaultIsLocalWalletEnv || nodeUrl !== defaultNodeUrl || cacheLifeTime || defaultCacheLifeTime || logLevel !== defaultLogLevel || logOutput != defaultLogOutput)
footer.standardButton(Dialog.Apply).enabled = configChanged
footer.standardButton(Dialog.Discard).enabled = configChanged
footer.standardButton(Dialog.RestoreDefaults).enabled = noDefaultConfig
Expand Down Expand Up @@ -288,7 +305,47 @@ Page {
}

} // GroupBox (addressBook setting)
} // ColumnLayout

GroupBox{
Layout.fillWidth: true
title: qsTr("Log level")

RowLayout{

anchors.fill: parent
TextField {
id: textFieldLogLevel

selectByMouse: true

placeholderText: qsTr("Log level")
onTextChanged: {
updateFooterButtonsStatus();
}
}
}
}

GroupBox{
Layout.fillWidth: true
title: qsTr("Log output")

RowLayout{

anchors.fill: parent
TextField {
id: textFieldLogOutput

selectByMouse: true

placeholderText: qsTr("Log output")
onTextChanged: {
updateFooterButtonsStatus();
}
}
}
}
}

ButtonGroup {
property int select:checkedButton.pos
Expand Down
1 change: 0 additions & 1 deletion src/ui/TransactionDetails.qml
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,6 @@ Item {
Layout.alignment: Qt.AlignTop
Layout.fillWidth: true
Layout.fillHeight: true
Component.onCompleted: console.log("LALALALA", width, height, contentWidth, contentHeight)

ListView {
id: listViewOutputs
Expand Down
Loading