fixed the broken code for enhanced logging mechanism

This commit is contained in:
hyung-hwan 2024-11-23 17:20:53 +09:00
parent 30f997d8e8
commit 37c600d0f6
2 changed files with 14 additions and 12 deletions

View File

@ -43,11 +43,11 @@ BAMCA0gAMEUCIEKzVMF3JqjQjuM2rX7Rx8hancI5KJhwfeKu1xbyR7XaAiEA2UT7
` `
// -------------------------------------------------------------------- // --------------------------------------------------------------------
type serverLogger struct { type AppLogger struct {
log *log.Logger log *log.Logger
} }
func (log* serverLogger) Write(id string, level hodu.LogLevel, fmt string, args ...interface{}) { func (log* AppLogger) Write(id string, level hodu.LogLevel, fmt string, args ...interface{}) {
log.log.Printf(fmt, args...) log.log.Printf(fmt, args...)
} }
@ -79,7 +79,7 @@ chan_loop:
//sh.svc.ReqReload() //sh.svc.ReqReload()
case sig = <-sigterm_chan: case sig = <-sigterm_chan:
sh.svc.StopServices() sh.svc.StopServices()
fmt.Printf("termination by signal %s\n", sig) sh.svc.WriteLog ("", hodu.LOG_INFO, "Received %s signal", sig)
break chan_loop break chan_loop
} }
} }
@ -121,7 +121,7 @@ func server_main(laddrs []string) error {
return fmt.Errorf("ERROR: failed to load key pair - %s\n", err) return fmt.Errorf("ERROR: failed to load key pair - %s\n", err)
} }
s, err = hodu.NewServer(laddrs, &serverLogger{log: log.Default}, &tls.Config{Certificates: []tls.Certificate{cert}}) s, err = hodu.NewServer(laddrs, &AppLogger{log: log.Default()}, &tls.Config{Certificates: []tls.Certificate{cert}})
if err != nil { if err != nil {
return fmt.Errorf("ERROR: failed to create new server - %s", err.Error()) return fmt.Errorf("ERROR: failed to create new server - %s", err.Error())
} }
@ -152,7 +152,7 @@ func client_main(listen_on string, server_addr string, peer_addrs []string) erro
InsecureSkipVerify: true, InsecureSkipVerify: true,
} }
c = hodu.NewClient(context.Background(), listen_on, &serverLogger{log: log.Default}, tlscfg) c = hodu.NewClient(context.Background(), listen_on, &AppLogger{log: log.Default()}, tlscfg)
cc.ServerAddr = server_addr cc.ServerAddr = server_addr
cc.PeerAddrs = peer_addrs cc.PeerAddrs = peer_addrs

View File

@ -170,26 +170,28 @@ func (r *ServerRoute) RunTask(wg *sync.WaitGroup) {
var err error var err error
var conn *net.TCPConn var conn *net.TCPConn
var pts *ServerPeerConn var pts *ServerPeerConn
var log_id string
defer wg.Done() defer wg.Done()
log_id = fmt.Sprintf("%s,%d", r.cts.caddr.String(), r.id)
for { for {
conn, err = r.l.AcceptTCP() conn, err = r.l.AcceptTCP()
if err != nil { if err != nil {
if errors.Is(err, net.ErrClosed) { if errors.Is(err, net.ErrClosed) {
r.cts.svr.log.Write("", LOG_INFO, "[%s,%d] Service-side peer listener closed\n", r.cts.caddr.String(), r.id) r.cts.svr.log.Write(log_id, LOG_INFO, "Rervice-side peer listener closed")
} else { } else {
fmt.Printf("[%s,%d] Server-side peer listener error - %s\n", r.cts.caddr.String(), r.id, err.Error()) r.cts.svr.log.Write(log_id, LOG_INFO, "Server-side peer listener error - %s", err.Error())
} }
break break
} }
pts, err = r.AddNewServerPeerConn(conn) pts, err = r.AddNewServerPeerConn(conn)
if err != nil { if err != nil {
r.cts.svr.log.Write("", LOG_ERROR, "[%s,%d] Failed to add new server-side peer %s - %s", r.cts.caddr.String(), r.id, conn.RemoteAddr().String(), err.Error()) r.cts.svr.log.Write(log_id, LOG_ERROR, "Failed to add new server-side peer %s - %s", conn.RemoteAddr().String(), err.Error())
conn.Close() conn.Close()
} else { } else {
r.cts.svr.log.Write("", LOG_DEBUG, "[%s,%d] Added new server-side peer %s", r.cts.caddr.String(), r.id, conn.RemoteAddr().String()) r.cts.svr.log.Write(log_id, LOG_DEBUG, "Added new server-side peer %s", conn.RemoteAddr().String())
r.pts_wg.Add(1) r.pts_wg.Add(1)
go pts.RunTask(&r.pts_wg) go pts.RunTask(&r.pts_wg)
} }
@ -197,7 +199,7 @@ func (r *ServerRoute) RunTask(wg *sync.WaitGroup) {
r.l.Close() // don't care about double close. it could have been closed in ReqStop r.l.Close() // don't care about double close. it could have been closed in ReqStop
r.pts_wg.Wait() r.pts_wg.Wait()
r.cts.svr.log.Write("", LOG_DEBUG, "[%s,%d] All service-side peer handlers completed", r.cts.caddr.String(), r.id) r.cts.svr.log.Write(log_id, LOG_DEBUG, "All service-side peer handlers completed")
} }
func (r *ServerRoute) ReqStop() { func (r *ServerRoute) ReqStop() {
@ -740,9 +742,9 @@ func (s *Server) RunTask(wg *sync.WaitGroup) {
} }
s.l_wg.Wait() s.l_wg.Wait()
s.log.Write("", LOG_DEBUG, "", "All GRPC listeners completed") s.log.Write("", LOG_DEBUG, "All GRPC listeners completed")
s.cts_wg.Wait() s.cts_wg.Wait()
s.log.Write("", LOG_DEBUG, "", "All CTS handlers completed") s.log.Write("", LOG_DEBUG, "All CTS handlers completed")
s.ReqStop() s.ReqStop()