added the log-mask option in the configuration file
This commit is contained in:
parent
be0d31b06c
commit
2c7c08c22f
@ -1274,7 +1274,7 @@ func (c *Client) RunCtlTask(wg *sync.WaitGroup) {
|
||||
l.Close()
|
||||
}
|
||||
if errors.Is(err, http.ErrServerClosed) {
|
||||
c.log.Write("", LOG_DEBUG, "Control channel[%d] ended", i)
|
||||
c.log.Write("", LOG_INFO, "Control channel[%d] ended", i)
|
||||
} else {
|
||||
c.log.Write("", LOG_ERROR, "Control channel[%d] error - %s", i, err.Error())
|
||||
}
|
||||
|
@ -4,6 +4,7 @@ import "crypto/tls"
|
||||
import "crypto/x509"
|
||||
import "errors"
|
||||
import "fmt"
|
||||
import "hodu"
|
||||
import "io"
|
||||
import "io/ioutil"
|
||||
import "os"
|
||||
@ -56,7 +57,14 @@ type RPCEndpointConfig struct { // rpc client-side configuration
|
||||
SeedTimeout int `yaml:"seed-timeout"`
|
||||
}
|
||||
|
||||
type AppConfig struct {
|
||||
LogMask []string `yaml:"log-mask"`
|
||||
LogFile string `yaml:"log-file"`
|
||||
}
|
||||
|
||||
type ServerConfig struct {
|
||||
APP AppConfig `yaml:"app"`
|
||||
|
||||
CTL struct {
|
||||
Service CTLServiceConfig `yaml:"service"`
|
||||
TLS ServerTLSConfig `yaml:"tls"`
|
||||
@ -69,6 +77,8 @@ type ServerConfig struct {
|
||||
}
|
||||
|
||||
type ClientConfig struct {
|
||||
APP AppConfig `yaml:"app"`
|
||||
|
||||
CTL struct {
|
||||
Service CTLServiceConfig `yaml:"service"`
|
||||
TLS ServerTLSConfig `yaml:"tls"`
|
||||
@ -139,6 +149,44 @@ func tls_string_to_client_auth_type(str string) tls.ClientAuthType {
|
||||
}
|
||||
}
|
||||
|
||||
func log_strings_to_mask(str []string) hodu.LogMask {
|
||||
|
||||
var mask hodu.LogMask
|
||||
var all hodu.LogMask
|
||||
|
||||
all = hodu.LogMask(hodu.LOG_DEBUG | hodu.LOG_INFO | hodu.LOG_WARN | hodu.LOG_ERROR)
|
||||
|
||||
if len(str) > 0 {
|
||||
var name string
|
||||
|
||||
mask = hodu.LogMask(0)
|
||||
for _, name = range str {
|
||||
|
||||
switch name {
|
||||
case "all":
|
||||
mask = all
|
||||
|
||||
case "none":
|
||||
mask = hodu.LogMask(0)
|
||||
|
||||
case "debug":
|
||||
mask |= hodu.LogMask(hodu.LOG_DEBUG)
|
||||
case "info":
|
||||
mask |= hodu.LogMask(hodu.LOG_INFO)
|
||||
case "warn":
|
||||
mask |= hodu.LogMask(hodu.LOG_WARN)
|
||||
case "error":
|
||||
mask |= hodu.LogMask(hodu.LOG_ERROR)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// if not specified, log messages of all levels
|
||||
mask = all
|
||||
}
|
||||
|
||||
return mask
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
func make_tls_server_config(cfg *ServerTLSConfig) (*tls.Config, error) {
|
||||
|
14
cmd/main.go
14
cmd/main.go
@ -32,6 +32,7 @@ type AppLogger struct {
|
||||
id string
|
||||
out io.Writer
|
||||
mtx sync.Mutex
|
||||
mask hodu.LogMask
|
||||
}
|
||||
|
||||
func (l* AppLogger) Write(id string, level hodu.LogLevel, fmtstr string, args ...interface{}) {
|
||||
@ -46,7 +47,8 @@ func (l* AppLogger) Write(id string, level hodu.LogLevel, fmtstr string, args ..
|
||||
var caller_line int
|
||||
var caller_ok bool
|
||||
|
||||
// TODO: do something with level
|
||||
if l.mask & hodu.LogMask(level) == 0 { return }
|
||||
|
||||
now = time.Now()
|
||||
|
||||
_, off_s = now.Zone()
|
||||
@ -146,6 +148,7 @@ func server_main(ctl_addrs []string, rpc_addrs []string, cfg *ServerConfig) erro
|
||||
var ctltlscfg *tls.Config
|
||||
var rpctlscfg *tls.Config
|
||||
var ctl_prefix string
|
||||
var logger *AppLogger
|
||||
var err error
|
||||
|
||||
if cfg != nil {
|
||||
@ -173,11 +176,13 @@ func server_main(ctl_addrs []string, rpc_addrs []string, cfg *ServerConfig) erro
|
||||
return fmt.Errorf("no rpc service addresses specified")
|
||||
}
|
||||
|
||||
// TODO: Change out field depending on cfg.APP.LogFile
|
||||
logger = &AppLogger{id: "server", out: os.Stderr, mask: log_strings_to_mask(cfg.APP.LogMask)}
|
||||
s, err = hodu.NewServer(
|
||||
context.Background(),
|
||||
ctl_addrs,
|
||||
rpc_addrs,
|
||||
&AppLogger{id: "server", out: os.Stderr},
|
||||
logger,
|
||||
ctl_prefix,
|
||||
ctltlscfg,
|
||||
rpctlscfg)
|
||||
@ -201,6 +206,7 @@ func client_main(ctl_addrs []string, rpc_addrs []string, peer_addrs []string, cf
|
||||
var rpctlscfg *tls.Config
|
||||
var ctl_prefix string
|
||||
var cc hodu.ClientConfig
|
||||
var logger *AppLogger
|
||||
var err error
|
||||
|
||||
if cfg != nil {
|
||||
@ -228,10 +234,12 @@ func client_main(ctl_addrs []string, rpc_addrs []string, peer_addrs []string, cf
|
||||
cc.ServerAddrs = rpc_addrs
|
||||
cc.PeerAddrs = peer_addrs
|
||||
|
||||
// TODO: Change out field depending on cfg.APP.LogFile
|
||||
logger = &AppLogger{id: "client", out: os.Stderr, mask: log_strings_to_mask(cfg.APP.LogMask)}
|
||||
c = hodu.NewClient(
|
||||
context.Background(),
|
||||
ctl_addrs,
|
||||
&AppLogger{id: "client", out: os.Stderr},
|
||||
logger,
|
||||
ctl_prefix,
|
||||
ctltlscfg,
|
||||
rpctlscfg)
|
||||
|
7
hodu.go
7
hodu.go
@ -8,12 +8,13 @@ import "sync"
|
||||
const HODU_RPC_VERSION uint32 = 0x010000
|
||||
|
||||
type LogLevel int
|
||||
type LogMask int
|
||||
|
||||
const (
|
||||
LOG_DEBUG LogLevel = iota + 1
|
||||
LOG_ERROR
|
||||
LOG_WARN
|
||||
LOG_DEBUG LogLevel = 1 << iota
|
||||
LOG_INFO
|
||||
LOG_WARN
|
||||
LOG_ERROR
|
||||
)
|
||||
|
||||
type Logger interface {
|
||||
|
@ -958,11 +958,11 @@ func (s *Server) run_grpc_server(idx int, wg *sync.WaitGroup) error {
|
||||
|
||||
l = s.rpc[idx]
|
||||
// it seems to be safe to call a single grpc server on differnt listening sockets multiple times
|
||||
s.log.Write("", LOG_ERROR, "Starting RPC server on %s", l.Addr().String())
|
||||
s.log.Write("", LOG_INFO, "Starting RPC server on %s", l.Addr().String())
|
||||
err = s.rpc_svr.Serve(l)
|
||||
if err != nil {
|
||||
if errors.Is(err, net.ErrClosed) {
|
||||
s.log.Write("", LOG_ERROR, "RPC server on %s closed", l.Addr().String())
|
||||
s.log.Write("", LOG_INFO, "RPC server on %s closed", l.Addr().String())
|
||||
} else {
|
||||
s.log.Write("", LOG_ERROR, "Error from RPC server on %s - %s", l.Addr().String(), err.Error())
|
||||
}
|
||||
@ -1033,7 +1033,7 @@ func (s *Server) RunCtlTask(wg *sync.WaitGroup) {
|
||||
l.Close()
|
||||
}
|
||||
if errors.Is(err, http.ErrServerClosed) {
|
||||
s.log.Write("", LOG_DEBUG, "Control channel[%d] ended", i)
|
||||
s.log.Write("", LOG_INFO, "Control channel[%d] ended", i)
|
||||
} else {
|
||||
s.log.Write("", LOG_ERROR, "Control channel[%d] error - %s", i, err.Error())
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user