forked from hyperledger-archives/burrow
/
config.go
77 lines (66 loc) · 1.99 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package config
import (
"bytes"
"fmt"
"github.com/hyperledger/burrow/logging/structure"
"github.com/BurntSushi/toml"
)
type LoggingConfig struct {
RootSink *SinkConfig `toml:"root_sink,omitempty"`
}
// For encoding a top-level '[logging]' TOML table
type LoggingConfigWrapper struct {
Logging *LoggingConfig `toml:"logging"`
}
func DefaultNodeLoggingConfig() *LoggingConfig {
return &LoggingConfig{
RootSink: Sink().SetOutput(StderrOutput()),
}
}
func DefaultClientLoggingConfig() *LoggingConfig {
return &LoggingConfig{
// No output
RootSink: Sink().
SetTransform(FilterTransform(ExcludeWhenAnyMatches,
structure.CapturedLoggingSourceKey, "")).
SetOutput(StderrOutput()),
}
}
// Returns the TOML for a top-level logging config wrapped with [logging]
func (lc *LoggingConfig) RootTOMLString() string {
return tomlString(LoggingConfigWrapper{lc})
}
func (lc *LoggingConfig) TOMLString() string {
return tomlString(lc)
}
func LoggingConfigFromMap(loggingRootMap map[string]interface{}) (*LoggingConfig, error) {
lc := new(LoggingConfig)
buf := new(bytes.Buffer)
enc := toml.NewEncoder(buf)
// TODO: [Silas] consider using strongly typed config/struct mapping everywhere
// (!! unfortunately the way we are using viper
// to pass around a untyped bag of config means that we don't get keys mapped
// according to their metadata `toml:"Name"` tags. So we are re-encoding to toml
// and then decoding into the strongly type struct as a work-around)
// Encode the map back to TOML
err := enc.Encode(loggingRootMap)
if err != nil {
return nil, err
}
// Decode into struct into the LoggingConfig struct
_, err = toml.Decode(buf.String(), lc)
if err != nil {
return nil, err
}
return lc, nil
}
func tomlString(v interface{}) string {
buf := new(bytes.Buffer)
encoder := toml.NewEncoder(buf)
err := encoder.Encode(v)
if err != nil {
// Seems like a reasonable compromise to make the string function clean
return fmt.Sprintf("Error encoding TOML: %s", err)
}
return buf.String()
}