added the log-mask option in the configuration file

This commit is contained in:
hyung-hwan 2024-12-09 01:51:04 +09:00
parent be0d31b06c
commit 2c7c08c22f
5 changed files with 67 additions and 10 deletions

View File

@ -1274,7 +1274,7 @@ func (c *Client) RunCtlTask(wg *sync.WaitGroup) {
l.Close() l.Close()
} }
if errors.Is(err, http.ErrServerClosed) { 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 { } else {
c.log.Write("", LOG_ERROR, "Control channel[%d] error - %s", i, err.Error()) c.log.Write("", LOG_ERROR, "Control channel[%d] error - %s", i, err.Error())
} }

View File

@ -4,6 +4,7 @@ import "crypto/tls"
import "crypto/x509" import "crypto/x509"
import "errors" import "errors"
import "fmt" import "fmt"
import "hodu"
import "io" import "io"
import "io/ioutil" import "io/ioutil"
import "os" import "os"
@ -56,7 +57,14 @@ type RPCEndpointConfig struct { // rpc client-side configuration
SeedTimeout int `yaml:"seed-timeout"` SeedTimeout int `yaml:"seed-timeout"`
} }
type AppConfig struct {
LogMask []string `yaml:"log-mask"`
LogFile string `yaml:"log-file"`
}
type ServerConfig struct { type ServerConfig struct {
APP AppConfig `yaml:"app"`
CTL struct { CTL struct {
Service CTLServiceConfig `yaml:"service"` Service CTLServiceConfig `yaml:"service"`
TLS ServerTLSConfig `yaml:"tls"` TLS ServerTLSConfig `yaml:"tls"`
@ -69,6 +77,8 @@ type ServerConfig struct {
} }
type ClientConfig struct { type ClientConfig struct {
APP AppConfig `yaml:"app"`
CTL struct { CTL struct {
Service CTLServiceConfig `yaml:"service"` Service CTLServiceConfig `yaml:"service"`
TLS ServerTLSConfig `yaml:"tls"` 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) { func make_tls_server_config(cfg *ServerTLSConfig) (*tls.Config, error) {

View File

@ -32,6 +32,7 @@ type AppLogger struct {
id string id string
out io.Writer out io.Writer
mtx sync.Mutex mtx sync.Mutex
mask hodu.LogMask
} }
func (l* AppLogger) Write(id string, level hodu.LogLevel, fmtstr string, args ...interface{}) { 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_line int
var caller_ok bool var caller_ok bool
// TODO: do something with level if l.mask & hodu.LogMask(level) == 0 { return }
now = time.Now() now = time.Now()
_, off_s = now.Zone() _, 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 ctltlscfg *tls.Config
var rpctlscfg *tls.Config var rpctlscfg *tls.Config
var ctl_prefix string var ctl_prefix string
var logger *AppLogger
var err error var err error
if cfg != nil { 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") 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( s, err = hodu.NewServer(
context.Background(), context.Background(),
ctl_addrs, ctl_addrs,
rpc_addrs, rpc_addrs,
&AppLogger{id: "server", out: os.Stderr}, logger,
ctl_prefix, ctl_prefix,
ctltlscfg, ctltlscfg,
rpctlscfg) rpctlscfg)
@ -201,6 +206,7 @@ func client_main(ctl_addrs []string, rpc_addrs []string, peer_addrs []string, cf
var rpctlscfg *tls.Config var rpctlscfg *tls.Config
var ctl_prefix string var ctl_prefix string
var cc hodu.ClientConfig var cc hodu.ClientConfig
var logger *AppLogger
var err error var err error
if cfg != nil { 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.ServerAddrs = rpc_addrs
cc.PeerAddrs = peer_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( c = hodu.NewClient(
context.Background(), context.Background(),
ctl_addrs, ctl_addrs,
&AppLogger{id: "client", out: os.Stderr}, logger,
ctl_prefix, ctl_prefix,
ctltlscfg, ctltlscfg,
rpctlscfg) rpctlscfg)

View File

@ -8,12 +8,13 @@ import "sync"
const HODU_RPC_VERSION uint32 = 0x010000 const HODU_RPC_VERSION uint32 = 0x010000
type LogLevel int type LogLevel int
type LogMask int
const ( const (
LOG_DEBUG LogLevel = iota + 1 LOG_DEBUG LogLevel = 1 << iota
LOG_ERROR
LOG_WARN
LOG_INFO LOG_INFO
LOG_WARN
LOG_ERROR
) )
type Logger interface { type Logger interface {

View File

@ -958,11 +958,11 @@ func (s *Server) run_grpc_server(idx int, wg *sync.WaitGroup) error {
l = s.rpc[idx] l = s.rpc[idx]
// it seems to be safe to call a single grpc server on differnt listening sockets multiple times // 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) err = s.rpc_svr.Serve(l)
if err != nil { if err != nil {
if errors.Is(err, net.ErrClosed) { 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 { } else {
s.log.Write("", LOG_ERROR, "Error from RPC server on %s - %s", l.Addr().String(), err.Error()) 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() l.Close()
} }
if errors.Is(err, http.ErrServerClosed) { 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 { } else {
s.log.Write("", LOG_ERROR, "Control channel[%d] error - %s", i, err.Error()) s.log.Write("", LOG_ERROR, "Control channel[%d] error - %s", i, err.Error())
} }