From 2c7c08c22f9a37c67af1e4886b8443d9d51fbdc8 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Mon, 9 Dec 2024 01:51:04 +0900 Subject: [PATCH] added the log-mask option in the configuration file --- client.go | 2 +- cmd/config.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ cmd/main.go | 14 +++++++++++--- hodu.go | 7 ++++--- server.go | 6 +++--- 5 files changed, 67 insertions(+), 10 deletions(-) diff --git a/client.go b/client.go index 6c20066..ac9df6e 100644 --- a/client.go +++ b/client.go @@ -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()) } diff --git a/cmd/config.go b/cmd/config.go index 0d5a008..caf6eb2 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -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) { diff --git a/cmd/main.go b/cmd/main.go index 8b69da7..2137999 100644 --- a/cmd/main.go +++ b/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) diff --git a/hodu.go b/hodu.go index 526e885..bb4836c 100644 --- a/hodu.go +++ b/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 { diff --git a/server.go b/server.go index 427e247..b16a63e 100644 --- a/server.go +++ b/server.go @@ -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()) }