switched to use own function for logging
This commit is contained in:
parent
37c600d0f6
commit
493309a4e9
@ -903,6 +903,6 @@ func (c *Client) WaitForTermination() {
|
|||||||
c.wg.Wait()
|
c.wg.Wait()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) WriteLog (id string, level LogLevel, fmt string, args ...interface{}) {
|
func (c *Client) WriteLog (id string, level LogLevel, fmtstr string, args ...interface{}) {
|
||||||
c.log.Write(id, level, fmt, args...)
|
c.log.Write(id, level, fmtstr, args...)
|
||||||
}
|
}
|
||||||
|
49
cmd/main.go
49
cmd/main.go
@ -7,12 +7,12 @@ import "flag"
|
|||||||
import "fmt"
|
import "fmt"
|
||||||
import "hodu"
|
import "hodu"
|
||||||
import "io"
|
import "io"
|
||||||
import "log"
|
|
||||||
import "os"
|
import "os"
|
||||||
import "os/signal"
|
import "os/signal"
|
||||||
import "strings"
|
import "strings"
|
||||||
import "sync"
|
import "sync"
|
||||||
import "syscall"
|
import "syscall"
|
||||||
|
import "time"
|
||||||
|
|
||||||
|
|
||||||
// --------------------------------------------------------------------
|
// --------------------------------------------------------------------
|
||||||
@ -44,11 +44,46 @@ BAMCA0gAMEUCIEKzVMF3JqjQjuM2rX7Rx8hancI5KJhwfeKu1xbyR7XaAiEA2UT7
|
|||||||
// --------------------------------------------------------------------
|
// --------------------------------------------------------------------
|
||||||
|
|
||||||
type AppLogger struct {
|
type AppLogger struct {
|
||||||
log *log.Logger
|
id string
|
||||||
|
out io.Writer
|
||||||
|
mtx sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
func (log* AppLogger) Write(id string, level hodu.LogLevel, fmt string, args ...interface{}) {
|
func (l* AppLogger) Write(id string, level hodu.LogLevel, fmtstr string, args ...interface{}) {
|
||||||
log.log.Printf(fmt, args...)
|
var now time.Time
|
||||||
|
var off_m int
|
||||||
|
var off_h int
|
||||||
|
var off_s int
|
||||||
|
var hdr string
|
||||||
|
var msg string
|
||||||
|
var lid string
|
||||||
|
|
||||||
|
// TODO: do something with level
|
||||||
|
now = time.Now()
|
||||||
|
|
||||||
|
_, off_s = now.Zone()
|
||||||
|
off_m = off_s / 60;
|
||||||
|
off_h = off_m / 60;
|
||||||
|
off_m = off_m % 60;
|
||||||
|
if (off_m < 0) { off_m = -off_m; }
|
||||||
|
|
||||||
|
hdr = fmt.Sprintf("%04d-%02d-%02d %02d:%02d:%02d %+03d%02d ",
|
||||||
|
now.Year(), now.Month(), now.Day(), now.Hour(), now.Minute(), now.Second(), off_h, off_m)
|
||||||
|
|
||||||
|
// TODO: add pid?
|
||||||
|
msg = fmt.Sprintf(fmtstr, args...)
|
||||||
|
if id == "" {
|
||||||
|
lid = fmt.Sprintf("%s: ", l.id)
|
||||||
|
} else {
|
||||||
|
lid = fmt.Sprintf("%s(%s): ", l.id, id)
|
||||||
|
}
|
||||||
|
|
||||||
|
l.mtx.Lock()
|
||||||
|
l.out.Write([]byte(hdr))
|
||||||
|
if lid != "" { l.out.Write([]byte(lid)) }
|
||||||
|
l.out.Write([]byte(msg))
|
||||||
|
if msg[len(msg) - 1] != '\n' { l.out.Write([]byte("\n")) }
|
||||||
|
l.mtx.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
// --------------------------------------------------------------------
|
// --------------------------------------------------------------------
|
||||||
@ -121,7 +156,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, &AppLogger{log: log.Default()}, &tls.Config{Certificates: []tls.Certificate{cert}})
|
s, err = hodu.NewServer(laddrs, &AppLogger{id: "server", out: os.Stderr}, &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())
|
||||||
}
|
}
|
||||||
@ -144,7 +179,7 @@ func client_main(listen_on string, server_addr string, peer_addrs []string) erro
|
|||||||
cert_pool = x509.NewCertPool()
|
cert_pool = x509.NewCertPool()
|
||||||
ok := cert_pool.AppendCertsFromPEM([]byte(rootCert))
|
ok := cert_pool.AppendCertsFromPEM([]byte(rootCert))
|
||||||
if !ok {
|
if !ok {
|
||||||
log.Fatal("failed to parse root certificate")
|
fmt.Printf("failed to parse root certificate")
|
||||||
}
|
}
|
||||||
tlscfg = &tls.Config{
|
tlscfg = &tls.Config{
|
||||||
RootCAs: cert_pool,
|
RootCAs: cert_pool,
|
||||||
@ -152,7 +187,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, &AppLogger{log: log.Default()}, tlscfg)
|
c = hodu.NewClient(context.Background(), listen_on, &AppLogger{id: "client", out: os.Stderr}, tlscfg)
|
||||||
|
|
||||||
cc.ServerAddr = server_addr
|
cc.ServerAddr = server_addr
|
||||||
cc.PeerAddrs = peer_addrs
|
cc.PeerAddrs = peer_addrs
|
||||||
|
4
hodu.go
4
hodu.go
@ -14,7 +14,7 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Logger interface {
|
type Logger interface {
|
||||||
Write (id string, level LogLevel, fmt string, args ...interface{})
|
Write (id string, level LogLevel, fmtstr string, args ...interface{})
|
||||||
}
|
}
|
||||||
|
|
||||||
type Service interface {
|
type Service interface {
|
||||||
@ -22,5 +22,5 @@ type Service interface {
|
|||||||
StartService(data interface{}) // non-blocking. spin up a service. it may be invokded multiple times for multiple instances
|
StartService(data interface{}) // non-blocking. spin up a service. it may be invokded multiple times for multiple instances
|
||||||
StopServices() // non-blocking. send stop request to all services spun up
|
StopServices() // non-blocking. send stop request to all services spun up
|
||||||
WaitForTermination() // blocking. must wait until all services are stopped
|
WaitForTermination() // blocking. must wait until all services are stopped
|
||||||
WriteLog(id string, level LogLevel, fmt string, args ...interface{})
|
WriteLog(id string, level LogLevel, fmtstr string, args ...interface{})
|
||||||
}
|
}
|
||||||
|
@ -867,6 +867,6 @@ func (s *Server) WaitForTermination() {
|
|||||||
s.wg.Wait()
|
s.wg.Wait()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) WriteLog (id string, level LogLevel, fmt string, args ...interface{}) {
|
func (s *Server) WriteLog (id string, level LogLevel, fmtstr string, args ...interface{}) {
|
||||||
s.log.Write(id, level, fmt, args...)
|
s.log.Write(id, level, fmtstr, args...)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user