From 09232ffdf175d3981749605bb0f6cb473b72edad Mon Sep 17 00:00:00 2001 From: codegold79 Date: Sun, 11 Apr 2021 15:11:17 -0700 Subject: [PATCH 01/25] Add logging policy Signed-off-by: codegold79 --- back/cmd/openbsrv/frontsrv.go | 8 +++++- back/cmd/openbsrv/grpcsrv.go | 13 ++++++--- back/cmd/openbsrv/httpsrv.go | 10 +++++-- back/cmd/openbsrv/main.go | 45 +++++++++++++++++++++++++------- back/cmd/openbsrv/srvmgmt.go | 16 +++++++----- back/internal/authsvc/authsvc.go | 11 +++++--- back/internal/logsvc/logging.go | 34 ++++++++++++++++++++++++ back/internal/postsvc/postsvc.go | 13 ++++++--- back/internal/usersvc/usersvc.go | 11 +++++--- 9 files changed, 126 insertions(+), 35 deletions(-) create mode 100644 back/internal/logsvc/logging.go diff --git a/back/cmd/openbsrv/frontsrv.go b/back/cmd/openbsrv/frontsrv.go index 6994fac..a084b4d 100644 --- a/back/cmd/openbsrv/frontsrv.go +++ b/back/cmd/openbsrv/frontsrv.go @@ -7,13 +7,17 @@ import ( "github.com/codemodus/alfred" "github.com/codemodus/chain/v2" "github.com/codemodus/hedrs" + + "github.com/OpenEugene/openboard/back/internal/logsvc" ) type frontSrv struct { s *http.Server + + log logsvc.LineLogger } -func newFrontSrv(port, dir string, origins []string) (*frontSrv, error) { +func newFrontSrv(log logsvc.LineLogger, port, dir string, origins []string) (*frontSrv, error) { origins = append(hedrs.DefaultOrigins, origins...) corsOrigins := hedrs.CORSOrigins(hedrs.NewAllowed(origins...)) corsMethods := hedrs.CORSMethods(hedrs.NewValues(hedrs.AllMethods...)) @@ -30,12 +34,14 @@ func newFrontSrv(port, dir string, origins []string) (*frontSrv, error) { Addr: port, Handler: cmn.End(alfred.New(dir)), }, + log: log, } return &s, nil } func (s *frontSrv) Serve() error { + s.log.Info("starting frontend server on port %s", s.s.Addr) if err := s.s.ListenAndServe(); err != nil && err != http.ErrServerClosed { return err } diff --git a/back/cmd/openbsrv/grpcsrv.go b/back/cmd/openbsrv/grpcsrv.go index b149cba..929ec11 100644 --- a/back/cmd/openbsrv/grpcsrv.go +++ b/back/cmd/openbsrv/grpcsrv.go @@ -5,6 +5,7 @@ import ( "github.com/OpenEugene/openboard/back/internal/authsvc" "github.com/OpenEugene/openboard/back/internal/grpcsrv" + "github.com/OpenEugene/openboard/back/internal/logsvc" "github.com/OpenEugene/openboard/back/internal/postsvc" "github.com/OpenEugene/openboard/back/internal/usersvc" ) @@ -14,20 +15,22 @@ type grpcSrv struct { port string svcs []interface{} + + log logsvc.LineLogger } -func newGRPCSrv(port string, db *sql.DB, drvr string) (*grpcSrv, error) { - auth, err := authsvc.New() +func newGRPCSrv(log logsvc.LineLogger, port string, db *sql.DB, drvr string) (*grpcSrv, error) { + auth, err := authsvc.New(log) if err != nil { return nil, err } - user, err := usersvc.New(db, drvr, 123456) + user, err := usersvc.New(log, db, drvr, 123456) if err != nil { return nil, err } - post, err := postsvc.New(db, drvr, 123456) + post, err := postsvc.New(log, db, drvr, 123456) if err != nil { return nil, err } @@ -49,6 +52,7 @@ func newGRPCSrv(port string, db *sql.DB, drvr string) (*grpcSrv, error) { s: gs, port: port, svcs: svcs, + log: log, } return &s, nil @@ -59,6 +63,7 @@ func (s *grpcSrv) services() []interface{} { } func (s *grpcSrv) Serve() error { + s.log.Info("starting gRPC server on port %s", s.port) return s.s.Serve(s.port) } diff --git a/back/cmd/openbsrv/httpsrv.go b/back/cmd/openbsrv/httpsrv.go index 0ebad9a..c18a261 100644 --- a/back/cmd/openbsrv/httpsrv.go +++ b/back/cmd/openbsrv/httpsrv.go @@ -1,8 +1,10 @@ package main import ( - "github.com/OpenEugene/openboard/back/internal/httpsrv" "github.com/codemodus/hedrs" + + "github.com/OpenEugene/openboard/back/internal/httpsrv" + "github.com/OpenEugene/openboard/back/internal/logsvc" ) type httpSrv struct { @@ -10,9 +12,11 @@ type httpSrv struct { rpcPort string httpPort string + + log logsvc.LineLogger } -func newHTTPSrv(rpcPort, httpPort string, origins []string) (*httpSrv, error) { +func newHTTPSrv(log logsvc.LineLogger, rpcPort, httpPort string, origins []string) (*httpSrv, error) { hs, err := httpsrv.New(hedrs.DefaultOrigins) if err != nil { return nil, err @@ -22,6 +26,7 @@ func newHTTPSrv(rpcPort, httpPort string, origins []string) (*httpSrv, error) { s: hs, rpcPort: rpcPort, httpPort: httpPort, + log: log, } return &s, nil @@ -29,6 +34,7 @@ func newHTTPSrv(rpcPort, httpPort string, origins []string) (*httpSrv, error) { // Serve ... func (s *httpSrv) Serve() error { + s.log.Info("starting HTTP server on port %s", s.httpPort) return s.s.Serve(s.rpcPort, s.httpPort) } diff --git a/back/cmd/openbsrv/main.go b/back/cmd/openbsrv/main.go index 0c623a3..b69f3f4 100644 --- a/back/cmd/openbsrv/main.go +++ b/back/cmd/openbsrv/main.go @@ -3,21 +3,32 @@ package main import ( "flag" "fmt" + "log" "os" "path" "github.com/codemodus/sigmon/v2" + + "github.com/OpenEugene/openboard/back/internal/logsvc" ) +var dbgLog func(string, ...interface{}) + func main() { - if err := run(); err != nil { + handle := logsvc.Handle{ + Err: os.Stderr, + Inf: os.Stdout, + } + srvLog := logsvc.NewServerLog(handle) + + if err := run(srvLog); err != nil { cmd := path.Base(os.Args[0]) - fmt.Fprintf(os.Stderr, "%s: %s\n", cmd, err) + srvLog.Error("%s: %s", cmd, err) os.Exit(1) } } -func run() error { +func run(srvLog logsvc.LineLogger) error { var ( dbdrvr = "mysql" dbname = "openeug_openb_dev" @@ -28,6 +39,7 @@ func run() error { migrate bool rollback bool skipsrv bool + debug bool frontDir = "../../../front/public" migTblPfx = "mig_" ) @@ -40,6 +52,7 @@ func run() error { flag.BoolVar(&migrate, "migrate", migrate, "migrate up") flag.BoolVar(&rollback, "rollback", rollback, "migrate dn") flag.BoolVar(&skipsrv, "skipsrv", skipsrv, "skip server run") + flag.BoolVar(&debug, "debug", debug, "debug true or false") flag.StringVar(&frontDir, "frontdir", frontDir, "front public assets directory") flag.Parse() @@ -47,6 +60,18 @@ func run() error { sm.Start() defer sm.Stop() + if debug { + dLog := log.New(os.Stdout, "[debug]", log.Ldate|log.Ltime|log.Lshortfile) + + // dbgLog is a package-level variable. + dbgLog = func(format string, as ...interface{}) { + dLog.Printf(format+"\n", as...) + } + } + + if dbgLog != nil { + dbgLog("set up SQL database at %s:%s.", dbaddr, dbport) + } db, err := newSQLDB(dbdrvr, dbCreds(dbname, dbuser, dbpass, dbaddr, dbport)) if err != nil { return err @@ -57,7 +82,7 @@ func run() error { return err } - gsrv, err := newGRPCSrv(":4242", db, dbdrvr) + gsrv, err := newGRPCSrv(srvLog, ":4242", db, dbdrvr) if err != nil { return err } @@ -67,7 +92,7 @@ func run() error { if mres.HasError() { return mres.ErrsErr() } - fmt.Println(migType+":", mres) + srvLog.Info("%s: %s", migType, mres) } if skipsrv { @@ -75,25 +100,25 @@ func run() error { return nil } - hsrv, err := newHTTPSrv(":4242", ":4243", nil) + hsrv, err := newHTTPSrv(srvLog, ":4242", ":4243", nil) if err != nil { return err } - fsrv, err := newFrontSrv(":4244", frontDir, nil) + fsrv, err := newFrontSrv(srvLog, ":4244", frontDir, nil) if err != nil { return err } - m := newServerMgmt(gsrv, hsrv, fsrv) + m := newServerMgmt(srvLog, gsrv, hsrv, fsrv) sm.Set(func(s *sigmon.State) { if err := m.stop(); err != nil { - fmt.Fprintln(os.Stderr, err) + srvLog.Error(err.Error()) } }) - fmt.Println("to gracefully stop the application, send signal like TERM (CTRL-C) or HUP") + srvLog.Info("to gracefully stop the application, send signal like TERM (CTRL-C) or HUP") return m.serve() } diff --git a/back/cmd/openbsrv/srvmgmt.go b/back/cmd/openbsrv/srvmgmt.go index b1f7b59..c73c70c 100644 --- a/back/cmd/openbsrv/srvmgmt.go +++ b/back/cmd/openbsrv/srvmgmt.go @@ -1,10 +1,10 @@ package main import ( - "fmt" - "os" "sync" "time" + + "github.com/OpenEugene/openboard/back/internal/logsvc" ) type server interface { @@ -13,12 +13,14 @@ type server interface { } type serverMgmt struct { - ss []server + ss []server + log logsvc.LineLogger } -func newServerMgmt(ss ...server) *serverMgmt { +func newServerMgmt(log logsvc.LineLogger, ss ...server) *serverMgmt { return &serverMgmt{ - ss: ss, + ss: ss, + log: log, } } @@ -32,7 +34,7 @@ func (m *serverMgmt) serve() error { // TODO: gather returned errors if err := s.Serve(); err != nil { - fmt.Fprintln(os.Stderr, "server error:", err) + m.log.Error("server error: %v", err) } }(s) @@ -49,7 +51,7 @@ func (m *serverMgmt) stop() error { go func(s server) { // TODO: gather returned errors if err := s.Stop(); err != nil { - fmt.Fprintln(os.Stderr, "stop error:", err) + m.log.Error("stop error: %v", err) } }(s) } diff --git a/back/internal/authsvc/authsvc.go b/back/internal/authsvc/authsvc.go index 67fd119..1d9ab10 100644 --- a/back/internal/authsvc/authsvc.go +++ b/back/internal/authsvc/authsvc.go @@ -3,8 +3,10 @@ package authsvc import ( "context" - "github.com/OpenEugene/openboard/back/internal/pb" "google.golang.org/grpc" + + "github.com/OpenEugene/openboard/back/internal/logsvc" + "github.com/OpenEugene/openboard/back/internal/pb" ) var _ pb.AuthServer = &AuthSvc{} @@ -12,12 +14,13 @@ var _ pb.AuthServer = &AuthSvc{} // AuthSvc ecapsulates dependencies and data required to implement the // pb.AuthServer interface. type AuthSvc struct { + log logsvc.LineLogger // TODO: implement AuthSvc } // New returns a pointer to an AuthSvc instance or an error. -func New() (*AuthSvc, error) { - return &AuthSvc{}, nil +func New(log logsvc.LineLogger) (*AuthSvc, error) { + return &AuthSvc{log: log}, nil } // RegisterWithGRPCServer implements the grpcsrv.Registerable interface. @@ -30,7 +33,7 @@ func (s *AuthSvc) RegisterWithGRPCServer(g *grpc.Server) error { // AddAuth implements part of the pb.AuthServer interface. func (s *AuthSvc) AddAuth(ctx context.Context, req *pb.AddAuthReq) (*pb.AuthResp, error) { // TODO: implement AddAuth - + s.log.Info("Adding authorization") return nil, nil } diff --git a/back/internal/logsvc/logging.go b/back/internal/logsvc/logging.go new file mode 100644 index 0000000..e46878f --- /dev/null +++ b/back/internal/logsvc/logging.go @@ -0,0 +1,34 @@ +package logsvc + +import ( + "io" + "log" +) + +type LineLogger interface { + Info(format string, as ...interface{}) + Error(format string, as ...interface{}) +} + +type ServerLog struct { + err, inf *log.Logger +} + +func (log *ServerLog) Info(format string, as ...interface{}) { + log.inf.Printf(format+"\n", as...) +} + +func (log *ServerLog) Error(format string, as ...interface{}) { + log.err.Printf(format+"\n", as...) +} + +type Handle struct { + Err, Inf io.Writer +} + +func NewServerLog(h Handle) *ServerLog { + return &ServerLog{ + inf: log.New(h.Inf, "[info] ", log.Ldate|log.Ltime|log.Lshortfile), + err: log.New(h.Err, "[error] ", log.Ldate|log.Ltime|log.Lshortfile), + } +} diff --git a/back/internal/postsvc/postsvc.go b/back/internal/postsvc/postsvc.go index be1a297..dc8508a 100644 --- a/back/internal/postsvc/postsvc.go +++ b/back/internal/postsvc/postsvc.go @@ -4,10 +4,12 @@ import ( "context" "database/sql" + "google.golang.org/grpc" + + "github.com/OpenEugene/openboard/back/internal/logsvc" "github.com/OpenEugene/openboard/back/internal/pb" "github.com/OpenEugene/openboard/back/internal/postsvc/internal/postdb" "github.com/OpenEugene/openboard/back/internal/postsvc/internal/postdb/mysqlmig" - "google.golang.org/grpc" ) var _ pb.PostServer = &PostSvc{} @@ -23,18 +25,20 @@ type relDB interface { // PostSvc encapsulates dependencies and data required to implement the // pb.PostServer interface. type PostSvc struct { - db relDB + db relDB + log logsvc.LineLogger } // New returns a pointer to a PostSvc instance or an error. -func New(relDb *sql.DB, driver string, offset uint64) (*PostSvc, error) { +func New(log logsvc.LineLogger, relDb *sql.DB, driver string, offset uint64) (*PostSvc, error) { db, err := postdb.New(relDb, driver, offset) if err != nil { return nil, err } s := PostSvc{ - db: db, + db: db, + log: log, } return &s, nil @@ -42,6 +46,7 @@ func New(relDb *sql.DB, driver string, offset uint64) (*PostSvc, error) { // AddType implements part of the pb.PostServer interface. func (s *PostSvc) AddType(ctx context.Context, req *pb.AddTypeReq) (*pb.TypeResp, error) { + s.log.Info("Adding post service type, %s", req.Name) return s.db.AddType(ctx, req) } diff --git a/back/internal/usersvc/usersvc.go b/back/internal/usersvc/usersvc.go index 1edfe99..bb97296 100644 --- a/back/internal/usersvc/usersvc.go +++ b/back/internal/usersvc/usersvc.go @@ -8,6 +8,8 @@ import ( "github.com/OpenEugene/openboard/back/internal/usersvc/internal/userdb" "github.com/OpenEugene/openboard/back/internal/usersvc/internal/userdb/mysqlmig" "google.golang.org/grpc" + + "github.com/OpenEugene/openboard/back/internal/logsvc" ) var _ pb.UserSvcServer = &UserSvc{} @@ -23,18 +25,20 @@ type relDb interface { // UserSvc encapsulates dependencies and data required to implement the // pb.UserServer interface. type UserSvc struct { - db relDb + db relDb + log logsvc.LineLogger } // New returns a pointer to a UserSvc instance or an error. -func New(relDb *sql.DB, driver string, offset uint64) (*UserSvc, error) { +func New(log logsvc.LineLogger, relDb *sql.DB, driver string, offset uint64) (*UserSvc, error) { db, err := userdb.New(relDb, driver, offset) if err != nil { return nil, err } s := UserSvc{ - db: db, + db: db, + log: log, } return &s, nil @@ -59,6 +63,7 @@ func (s *UserSvc) FndRoles(ctx context.Context, req *pb.FndRolesReq) (*pb.RolesR // AddUser implements part of the pb.UserServer interface. func (s *UserSvc) AddUser(ctx context.Context, req *pb.AddUserReq) (*pb.UserResp, error) { + s.log.Info("Adding user, %s.", req.Username) return s.db.AddUser(ctx, req) } From f0b016761508fb0ff41b5c3389651344b58b2e3e Mon Sep 17 00:00:00 2001 From: codegold79 Date: Mon, 31 May 2021 12:02:43 -0700 Subject: [PATCH 02/25] Add debug and logging libraries Signed-off-by: codegold79 --- back/cmd/openbsrv/frontsrv.go | 8 +----- back/cmd/openbsrv/grpcsrv.go | 13 +++------ back/cmd/openbsrv/httpsrv.go | 7 +---- back/cmd/openbsrv/main.go | 45 ++++++++++++++------------------ back/cmd/openbsrv/srvmgmt.go | 11 +++++--- back/internal/authsvc/authsvc.go | 7 ++--- back/internal/dbg/dbg.go | 28 ++++++++++++++++++++ back/internal/log/log.go | 29 ++++++++++++++++++++ back/internal/logsvc/logging.go | 34 ------------------------ back/internal/postsvc/postsvc.go | 10 +++---- back/internal/usersvc/usersvc.go | 11 +++----- 11 files changed, 98 insertions(+), 105 deletions(-) create mode 100644 back/internal/dbg/dbg.go create mode 100644 back/internal/log/log.go delete mode 100644 back/internal/logsvc/logging.go diff --git a/back/cmd/openbsrv/frontsrv.go b/back/cmd/openbsrv/frontsrv.go index a084b4d..6994fac 100644 --- a/back/cmd/openbsrv/frontsrv.go +++ b/back/cmd/openbsrv/frontsrv.go @@ -7,17 +7,13 @@ import ( "github.com/codemodus/alfred" "github.com/codemodus/chain/v2" "github.com/codemodus/hedrs" - - "github.com/OpenEugene/openboard/back/internal/logsvc" ) type frontSrv struct { s *http.Server - - log logsvc.LineLogger } -func newFrontSrv(log logsvc.LineLogger, port, dir string, origins []string) (*frontSrv, error) { +func newFrontSrv(port, dir string, origins []string) (*frontSrv, error) { origins = append(hedrs.DefaultOrigins, origins...) corsOrigins := hedrs.CORSOrigins(hedrs.NewAllowed(origins...)) corsMethods := hedrs.CORSMethods(hedrs.NewValues(hedrs.AllMethods...)) @@ -34,14 +30,12 @@ func newFrontSrv(log logsvc.LineLogger, port, dir string, origins []string) (*fr Addr: port, Handler: cmn.End(alfred.New(dir)), }, - log: log, } return &s, nil } func (s *frontSrv) Serve() error { - s.log.Info("starting frontend server on port %s", s.s.Addr) if err := s.s.ListenAndServe(); err != nil && err != http.ErrServerClosed { return err } diff --git a/back/cmd/openbsrv/grpcsrv.go b/back/cmd/openbsrv/grpcsrv.go index 929ec11..b149cba 100644 --- a/back/cmd/openbsrv/grpcsrv.go +++ b/back/cmd/openbsrv/grpcsrv.go @@ -5,7 +5,6 @@ import ( "github.com/OpenEugene/openboard/back/internal/authsvc" "github.com/OpenEugene/openboard/back/internal/grpcsrv" - "github.com/OpenEugene/openboard/back/internal/logsvc" "github.com/OpenEugene/openboard/back/internal/postsvc" "github.com/OpenEugene/openboard/back/internal/usersvc" ) @@ -15,22 +14,20 @@ type grpcSrv struct { port string svcs []interface{} - - log logsvc.LineLogger } -func newGRPCSrv(log logsvc.LineLogger, port string, db *sql.DB, drvr string) (*grpcSrv, error) { - auth, err := authsvc.New(log) +func newGRPCSrv(port string, db *sql.DB, drvr string) (*grpcSrv, error) { + auth, err := authsvc.New() if err != nil { return nil, err } - user, err := usersvc.New(log, db, drvr, 123456) + user, err := usersvc.New(db, drvr, 123456) if err != nil { return nil, err } - post, err := postsvc.New(log, db, drvr, 123456) + post, err := postsvc.New(db, drvr, 123456) if err != nil { return nil, err } @@ -52,7 +49,6 @@ func newGRPCSrv(log logsvc.LineLogger, port string, db *sql.DB, drvr string) (*g s: gs, port: port, svcs: svcs, - log: log, } return &s, nil @@ -63,7 +59,6 @@ func (s *grpcSrv) services() []interface{} { } func (s *grpcSrv) Serve() error { - s.log.Info("starting gRPC server on port %s", s.port) return s.s.Serve(s.port) } diff --git a/back/cmd/openbsrv/httpsrv.go b/back/cmd/openbsrv/httpsrv.go index c18a261..d2e1f00 100644 --- a/back/cmd/openbsrv/httpsrv.go +++ b/back/cmd/openbsrv/httpsrv.go @@ -4,7 +4,6 @@ import ( "github.com/codemodus/hedrs" "github.com/OpenEugene/openboard/back/internal/httpsrv" - "github.com/OpenEugene/openboard/back/internal/logsvc" ) type httpSrv struct { @@ -12,11 +11,9 @@ type httpSrv struct { rpcPort string httpPort string - - log logsvc.LineLogger } -func newHTTPSrv(log logsvc.LineLogger, rpcPort, httpPort string, origins []string) (*httpSrv, error) { +func newHTTPSrv(rpcPort, httpPort string, origins []string) (*httpSrv, error) { hs, err := httpsrv.New(hedrs.DefaultOrigins) if err != nil { return nil, err @@ -26,7 +23,6 @@ func newHTTPSrv(log logsvc.LineLogger, rpcPort, httpPort string, origins []strin s: hs, rpcPort: rpcPort, httpPort: httpPort, - log: log, } return &s, nil @@ -34,7 +30,6 @@ func newHTTPSrv(log logsvc.LineLogger, rpcPort, httpPort string, origins []strin // Serve ... func (s *httpSrv) Serve() error { - s.log.Info("starting HTTP server on port %s", s.httpPort) return s.s.Serve(s.rpcPort, s.httpPort) } diff --git a/back/cmd/openbsrv/main.go b/back/cmd/openbsrv/main.go index b69f3f4..9b4426a 100644 --- a/back/cmd/openbsrv/main.go +++ b/back/cmd/openbsrv/main.go @@ -3,32 +3,31 @@ package main import ( "flag" "fmt" - "log" + "io" "os" "path" "github.com/codemodus/sigmon/v2" - "github.com/OpenEugene/openboard/back/internal/logsvc" + "github.com/OpenEugene/openboard/back/internal/dbg" + "github.com/OpenEugene/openboard/back/internal/log" ) -var dbgLog func(string, ...interface{}) - func main() { - handle := logsvc.Handle{ + outs := log.Outputs{ Err: os.Stderr, Inf: os.Stdout, } - srvLog := logsvc.NewServerLog(handle) + log := log.New(outs) - if err := run(srvLog); err != nil { + if err := run(log); err != nil { cmd := path.Base(os.Args[0]) - srvLog.Error("%s: %s", cmd, err) + log.Error("%s: %s", cmd, err) os.Exit(1) } } -func run(srvLog logsvc.LineLogger) error { +func run(log *log.Log) error { var ( dbdrvr = "mysql" dbname = "openeug_openb_dev" @@ -60,18 +59,14 @@ func run(srvLog logsvc.LineLogger) error { sm.Start() defer sm.Stop() + var w io.Writer if debug { - dLog := log.New(os.Stdout, "[debug]", log.Ldate|log.Ltime|log.Lshortfile) - - // dbgLog is a package-level variable. - dbgLog = func(format string, as ...interface{}) { - dLog.Printf(format+"\n", as...) - } + w = os.Stdout } + dbg := dbg.New(w).Log + + dbg("set up SQL database at %s:%s.", dbaddr, dbport) - if dbgLog != nil { - dbgLog("set up SQL database at %s:%s.", dbaddr, dbport) - } db, err := newSQLDB(dbdrvr, dbCreds(dbname, dbuser, dbpass, dbaddr, dbport)) if err != nil { return err @@ -82,7 +77,7 @@ func run(srvLog logsvc.LineLogger) error { return err } - gsrv, err := newGRPCSrv(srvLog, ":4242", db, dbdrvr) + gsrv, err := newGRPCSrv(":4242", db, dbdrvr) if err != nil { return err } @@ -92,7 +87,7 @@ func run(srvLog logsvc.LineLogger) error { if mres.HasError() { return mres.ErrsErr() } - srvLog.Info("%s: %s", migType, mres) + log.Info("%s: %s", migType, mres) } if skipsrv { @@ -100,25 +95,25 @@ func run(srvLog logsvc.LineLogger) error { return nil } - hsrv, err := newHTTPSrv(srvLog, ":4242", ":4243", nil) + hsrv, err := newHTTPSrv(":4242", ":4243", nil) if err != nil { return err } - fsrv, err := newFrontSrv(srvLog, ":4244", frontDir, nil) + fsrv, err := newFrontSrv(":4244", frontDir, nil) if err != nil { return err } - m := newServerMgmt(srvLog, gsrv, hsrv, fsrv) + m := newServerMgmt(log, gsrv, hsrv, fsrv) sm.Set(func(s *sigmon.State) { if err := m.stop(); err != nil { - srvLog.Error(err.Error()) + log.Error(err.Error()) } }) - srvLog.Info("to gracefully stop the application, send signal like TERM (CTRL-C) or HUP") + log.Info("to gracefully stop the application, send signal like TERM (CTRL-C) or HUP") return m.serve() } diff --git a/back/cmd/openbsrv/srvmgmt.go b/back/cmd/openbsrv/srvmgmt.go index c73c70c..a2a77b2 100644 --- a/back/cmd/openbsrv/srvmgmt.go +++ b/back/cmd/openbsrv/srvmgmt.go @@ -3,8 +3,6 @@ package main import ( "sync" "time" - - "github.com/OpenEugene/openboard/back/internal/logsvc" ) type server interface { @@ -12,12 +10,17 @@ type server interface { Stop() error } +type lineLogger interface { + Info(format string, as ...interface{}) + Error(format string, as ...interface{}) +} + type serverMgmt struct { ss []server - log logsvc.LineLogger + log lineLogger } -func newServerMgmt(log logsvc.LineLogger, ss ...server) *serverMgmt { +func newServerMgmt(log lineLogger, ss ...server) *serverMgmt { return &serverMgmt{ ss: ss, log: log, diff --git a/back/internal/authsvc/authsvc.go b/back/internal/authsvc/authsvc.go index 1d9ab10..df77355 100644 --- a/back/internal/authsvc/authsvc.go +++ b/back/internal/authsvc/authsvc.go @@ -5,7 +5,6 @@ import ( "google.golang.org/grpc" - "github.com/OpenEugene/openboard/back/internal/logsvc" "github.com/OpenEugene/openboard/back/internal/pb" ) @@ -14,13 +13,12 @@ var _ pb.AuthServer = &AuthSvc{} // AuthSvc ecapsulates dependencies and data required to implement the // pb.AuthServer interface. type AuthSvc struct { - log logsvc.LineLogger // TODO: implement AuthSvc } // New returns a pointer to an AuthSvc instance or an error. -func New(log logsvc.LineLogger) (*AuthSvc, error) { - return &AuthSvc{log: log}, nil +func New() (*AuthSvc, error) { + return &AuthSvc{}, nil } // RegisterWithGRPCServer implements the grpcsrv.Registerable interface. @@ -33,7 +31,6 @@ func (s *AuthSvc) RegisterWithGRPCServer(g *grpc.Server) error { // AddAuth implements part of the pb.AuthServer interface. func (s *AuthSvc) AddAuth(ctx context.Context, req *pb.AddAuthReq) (*pb.AuthResp, error) { // TODO: implement AddAuth - s.log.Info("Adding authorization") return nil, nil } diff --git a/back/internal/dbg/dbg.go b/back/internal/dbg/dbg.go new file mode 100644 index 0000000..250de15 --- /dev/null +++ b/back/internal/dbg/dbg.go @@ -0,0 +1,28 @@ +package dbg + +import ( + "io" + "log" +) + +type DbgLog struct { + log *log.Logger +} + +func (l *DbgLog) Log(format string, as ...interface{}) { + if l.log == nil { + return + } + + l.log.Printf(format+"\n", as...) +} + +func New(out io.Writer) *DbgLog { + if out == nil { + return &DbgLog{} + } + + return &DbgLog{ + log: log.New(out, "[debug] ", log.Ldate|log.Ltime), + } +} diff --git a/back/internal/log/log.go b/back/internal/log/log.go new file mode 100644 index 0000000..affd3a4 --- /dev/null +++ b/back/internal/log/log.go @@ -0,0 +1,29 @@ +package log + +import ( + "io" + "log" +) + +type Log struct { + err, inf *log.Logger +} + +func (log *Log) Info(format string, as ...interface{}) { + log.inf.Printf(format+"\n", as...) +} + +func (log *Log) Error(format string, as ...interface{}) { + log.err.Printf(format+"\n", as...) +} + +type Outputs struct { + Err, Inf io.Writer +} + +func New(h Outputs) *Log { + return &Log{ + inf: log.New(h.Inf, "[info] ", log.Ldate|log.Ltime), + err: log.New(h.Err, "[error] ", log.Ldate|log.Ltime), + } +} diff --git a/back/internal/logsvc/logging.go b/back/internal/logsvc/logging.go deleted file mode 100644 index e46878f..0000000 --- a/back/internal/logsvc/logging.go +++ /dev/null @@ -1,34 +0,0 @@ -package logsvc - -import ( - "io" - "log" -) - -type LineLogger interface { - Info(format string, as ...interface{}) - Error(format string, as ...interface{}) -} - -type ServerLog struct { - err, inf *log.Logger -} - -func (log *ServerLog) Info(format string, as ...interface{}) { - log.inf.Printf(format+"\n", as...) -} - -func (log *ServerLog) Error(format string, as ...interface{}) { - log.err.Printf(format+"\n", as...) -} - -type Handle struct { - Err, Inf io.Writer -} - -func NewServerLog(h Handle) *ServerLog { - return &ServerLog{ - inf: log.New(h.Inf, "[info] ", log.Ldate|log.Ltime|log.Lshortfile), - err: log.New(h.Err, "[error] ", log.Ldate|log.Ltime|log.Lshortfile), - } -} diff --git a/back/internal/postsvc/postsvc.go b/back/internal/postsvc/postsvc.go index dc8508a..67f4fbc 100644 --- a/back/internal/postsvc/postsvc.go +++ b/back/internal/postsvc/postsvc.go @@ -6,7 +6,6 @@ import ( "google.golang.org/grpc" - "github.com/OpenEugene/openboard/back/internal/logsvc" "github.com/OpenEugene/openboard/back/internal/pb" "github.com/OpenEugene/openboard/back/internal/postsvc/internal/postdb" "github.com/OpenEugene/openboard/back/internal/postsvc/internal/postdb/mysqlmig" @@ -25,20 +24,18 @@ type relDB interface { // PostSvc encapsulates dependencies and data required to implement the // pb.PostServer interface. type PostSvc struct { - db relDB - log logsvc.LineLogger + db relDB } // New returns a pointer to a PostSvc instance or an error. -func New(log logsvc.LineLogger, relDb *sql.DB, driver string, offset uint64) (*PostSvc, error) { +func New(relDb *sql.DB, driver string, offset uint64) (*PostSvc, error) { db, err := postdb.New(relDb, driver, offset) if err != nil { return nil, err } s := PostSvc{ - db: db, - log: log, + db: db, } return &s, nil @@ -46,7 +43,6 @@ func New(log logsvc.LineLogger, relDb *sql.DB, driver string, offset uint64) (*P // AddType implements part of the pb.PostServer interface. func (s *PostSvc) AddType(ctx context.Context, req *pb.AddTypeReq) (*pb.TypeResp, error) { - s.log.Info("Adding post service type, %s", req.Name) return s.db.AddType(ctx, req) } diff --git a/back/internal/usersvc/usersvc.go b/back/internal/usersvc/usersvc.go index bb97296..1edfe99 100644 --- a/back/internal/usersvc/usersvc.go +++ b/back/internal/usersvc/usersvc.go @@ -8,8 +8,6 @@ import ( "github.com/OpenEugene/openboard/back/internal/usersvc/internal/userdb" "github.com/OpenEugene/openboard/back/internal/usersvc/internal/userdb/mysqlmig" "google.golang.org/grpc" - - "github.com/OpenEugene/openboard/back/internal/logsvc" ) var _ pb.UserSvcServer = &UserSvc{} @@ -25,20 +23,18 @@ type relDb interface { // UserSvc encapsulates dependencies and data required to implement the // pb.UserServer interface. type UserSvc struct { - db relDb - log logsvc.LineLogger + db relDb } // New returns a pointer to a UserSvc instance or an error. -func New(log logsvc.LineLogger, relDb *sql.DB, driver string, offset uint64) (*UserSvc, error) { +func New(relDb *sql.DB, driver string, offset uint64) (*UserSvc, error) { db, err := userdb.New(relDb, driver, offset) if err != nil { return nil, err } s := UserSvc{ - db: db, - log: log, + db: db, } return &s, nil @@ -63,7 +59,6 @@ func (s *UserSvc) FndRoles(ctx context.Context, req *pb.FndRolesReq) (*pb.RolesR // AddUser implements part of the pb.UserServer interface. func (s *UserSvc) AddUser(ctx context.Context, req *pb.AddUserReq) (*pb.UserResp, error) { - s.log.Info("Adding user, %s.", req.Username) return s.db.AddUser(ctx, req) } From 2f45ea595b42d3cde93ebde494d2c02a2e7521ac Mon Sep 17 00:00:00 2001 From: codegold79 Date: Mon, 31 May 2021 19:24:54 -0700 Subject: [PATCH 03/25] Sync/atomic version of debug log Signed-off-by: codegold79 --- back/cmd/openbsrv/main.go | 5 ++--- back/internal/dbg/dbg.go | 39 ++++++++++++++++++++++++++++++--------- 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/back/cmd/openbsrv/main.go b/back/cmd/openbsrv/main.go index 9b4426a..ada1a38 100644 --- a/back/cmd/openbsrv/main.go +++ b/back/cmd/openbsrv/main.go @@ -63,10 +63,9 @@ func run(log *log.Log) error { if debug { w = os.Stdout } - dbg := dbg.New(w).Log - - dbg("set up SQL database at %s:%s.", dbaddr, dbport) + dbg := dbg.New(w) + dbg.Log("set up SQL database at %s:%s.", dbaddr, dbport) db, err := newSQLDB(dbdrvr, dbCreds(dbname, dbuser, dbpass, dbaddr, dbport)) if err != nil { return err diff --git a/back/internal/dbg/dbg.go b/back/internal/dbg/dbg.go index 250de15..d21a9d8 100644 --- a/back/internal/dbg/dbg.go +++ b/back/internal/dbg/dbg.go @@ -3,26 +3,47 @@ package dbg import ( "io" "log" + "sync/atomic" ) +var atomicV atomic.Value + +// DbgLog is thread-safe. type DbgLog struct { log *log.Logger } +func New(out io.Writer) *DbgLog { + var dl DbgLog + + if out == nil { + atomicV.Store(&dl) + return &dl + } + + dl = DbgLog{ + log: log.New(out, "[debug] ", log.Ldate|log.Ltime), + } + atomicV.Store(&dl) + return &dl +} + func (l *DbgLog) Log(format string, as ...interface{}) { - if l.log == nil { + load := atomicV.Load().(*DbgLog) + + if load.log == nil { return } - l.log.Printf(format+"\n", as...) + load.log.Printf(format+"\n", as...) } -func New(out io.Writer) *DbgLog { - if out == nil { - return &DbgLog{} - } +func (l *DbgLog) Off() { + l.log = nil + atomicV.Store(l) +} - return &DbgLog{ - log: log.New(out, "[debug] ", log.Ldate|log.Ltime), - } +func (l *DbgLog) On(out io.Writer) { + l.log = log.New(out, "[debug] ", log.Ldate|log.Ltime) + atomicV.Store(l) } From cdd91d88e28e190dabc389b41f4fe8447ed0192a Mon Sep 17 00:00:00 2001 From: codegold79 Date: Mon, 31 May 2021 20:10:38 -0700 Subject: [PATCH 04/25] Remote logging interface from main package Signed-off-by: codegold79 --- back/cmd/openbsrv/srvmgmt.go | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/back/cmd/openbsrv/srvmgmt.go b/back/cmd/openbsrv/srvmgmt.go index a2a77b2..1d42d2d 100644 --- a/back/cmd/openbsrv/srvmgmt.go +++ b/back/cmd/openbsrv/srvmgmt.go @@ -3,6 +3,8 @@ package main import ( "sync" "time" + + "github.com/OpenEugene/openboard/back/internal/log" ) type server interface { @@ -10,17 +12,12 @@ type server interface { Stop() error } -type lineLogger interface { - Info(format string, as ...interface{}) - Error(format string, as ...interface{}) -} - type serverMgmt struct { ss []server - log lineLogger + log *log.Log } -func newServerMgmt(log lineLogger, ss ...server) *serverMgmt { +func newServerMgmt(log *log.Log, ss ...server) *serverMgmt { return &serverMgmt{ ss: ss, log: log, From 98664ee6d9c8d187efee55d902290175314d96c9 Mon Sep 17 00:00:00 2001 From: codegold79 Date: Sun, 6 Jun 2021 20:57:13 -0700 Subject: [PATCH 05/25] Make dbg package a singleton Signed-off-by: codegold79 --- back/internal/dbg/dbg.go | 60 +++++++++++++++++++++++----------------- 1 file changed, 35 insertions(+), 25 deletions(-) diff --git a/back/internal/dbg/dbg.go b/back/internal/dbg/dbg.go index d21a9d8..ee600c9 100644 --- a/back/internal/dbg/dbg.go +++ b/back/internal/dbg/dbg.go @@ -6,44 +6,54 @@ import ( "sync/atomic" ) -var atomicV atomic.Value - -// DbgLog is thread-safe. -type DbgLog struct { +// dbgLog is thread-safe. +type dbgLog struct { log *log.Logger + out io.Writer } -func New(out io.Writer) *DbgLog { - var dl DbgLog - - if out == nil { - atomicV.Store(&dl) - return &dl - } +var atomicV atomic.Value +var dbg = new() - dl = DbgLog{ - log: log.New(out, "[debug] ", log.Ldate|log.Ltime), - } +func new() *dbgLog { + var dl dbgLog atomicV.Store(&dl) return &dl } -func (l *DbgLog) Log(format string, as ...interface{}) { - load := atomicV.Load().(*DbgLog) +func (dl *dbgLog) println(text string) { + dbg.log.Println(text) +} - if load.log == nil { +func (dl *dbgLog) printf(format string, as ...interface{}) { + dbg.log.Printf(format+"\n", as...) +} + +func Log(text string) { + dbgLoad := atomicV.Load().(*dbgLog) + if dbgLoad.out == nil { return } - - load.log.Printf(format+"\n", as...) + dbgLoad.println(text) } -func (l *DbgLog) Off() { - l.log = nil - atomicV.Store(l) +func Logf(format string, as ...interface{}) { + dbgLoad := atomicV.Load().(*dbgLog) + if dbgLoad.out == nil { + return + } + dbgLoad.printf(format, as...) } -func (l *DbgLog) On(out io.Writer) { - l.log = log.New(out, "[debug] ", log.Ldate|log.Ltime) - atomicV.Store(l) +func SetDebugOut(out io.Writer) { + dbgLoad := atomicV.Load().(*dbgLog) + dbgLoad.out = out + + if out != nil { + dbgLoad.log = log.New(out, "", 0) + } else { + dbgLoad.log = log.Default() + } + + atomicV.Store(dbgLoad) } From 265fb75b6bb66ae811ceba224d3a914712d6c0c9 Mon Sep 17 00:00:00 2001 From: codegold79 Date: Sun, 6 Jun 2021 20:57:31 -0700 Subject: [PATCH 06/25] Make log package more configurable Signed-off-by: codegold79 --- back/internal/log/log.go | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/back/internal/log/log.go b/back/internal/log/log.go index affd3a4..8e3ebf3 100644 --- a/back/internal/log/log.go +++ b/back/internal/log/log.go @@ -5,8 +5,27 @@ import ( "log" ) +type Output struct { + Out io.Writer + Prefix string + Flag int +} + +type Config struct { + Err Output + Inf Output +} + type Log struct { - err, inf *log.Logger + err *log.Logger + inf *log.Logger +} + +func New(c Config) *Log { + return &Log{ + inf: log.New(c.Inf.Out, c.Inf.Prefix, c.Inf.Flag), + err: log.New(c.Err.Out, c.Err.Prefix, c.Inf.Flag), + } } func (log *Log) Info(format string, as ...interface{}) { @@ -16,14 +35,3 @@ func (log *Log) Info(format string, as ...interface{}) { func (log *Log) Error(format string, as ...interface{}) { log.err.Printf(format+"\n", as...) } - -type Outputs struct { - Err, Inf io.Writer -} - -func New(h Outputs) *Log { - return &Log{ - inf: log.New(h.Inf, "[info] ", log.Ldate|log.Ltime), - err: log.New(h.Err, "[error] ", log.Ldate|log.Ltime), - } -} From 37ecd01a840384fdd59a661c83fe9447838c6f3a Mon Sep 17 00:00:00 2001 From: codegold79 Date: Sun, 6 Jun 2021 20:57:59 -0700 Subject: [PATCH 07/25] Update main.go with log and dbg package changes Signed-off-by: codegold79 --- back/cmd/openbsrv/main.go | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/back/cmd/openbsrv/main.go b/back/cmd/openbsrv/main.go index ada1a38..3996ddb 100644 --- a/back/cmd/openbsrv/main.go +++ b/back/cmd/openbsrv/main.go @@ -3,7 +3,6 @@ package main import ( "flag" "fmt" - "io" "os" "path" @@ -14,11 +13,17 @@ import ( ) func main() { - outs := log.Outputs{ - Err: os.Stderr, - Inf: os.Stdout, + config := log.Config{ + Err: log.Output{ + Out: os.Stderr, + Prefix: "[ERROR] ", + }, + Inf: log.Output{ + Out: os.Stdout, + Prefix: "[INFO] ", + }, } - log := log.New(outs) + log := log.New(config) if err := run(log); err != nil { cmd := path.Base(os.Args[0]) @@ -59,13 +64,11 @@ func run(log *log.Log) error { sm.Start() defer sm.Stop() - var w io.Writer if debug { - w = os.Stdout + dbg.SetDebugOut(os.Stdout) } - dbg := dbg.New(w) - dbg.Log("set up SQL database at %s:%s.", dbaddr, dbport) + dbg.Logf("set up SQL database at %s:%s.", dbaddr, dbport) db, err := newSQLDB(dbdrvr, dbCreds(dbname, dbuser, dbpass, dbaddr, dbport)) if err != nil { return err From 6bbaba26f1b9626e588276cfcf882a7221ab8aea Mon Sep 17 00:00:00 2001 From: codegold79 Date: Fri, 18 Jun 2021 20:54:40 -0700 Subject: [PATCH 08/25] Remove Flag output option for logs Signed-off-by: codegold79 --- back/internal/log/log.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/back/internal/log/log.go b/back/internal/log/log.go index 8e3ebf3..0bcab4a 100644 --- a/back/internal/log/log.go +++ b/back/internal/log/log.go @@ -8,7 +8,6 @@ import ( type Output struct { Out io.Writer Prefix string - Flag int } type Config struct { @@ -23,8 +22,8 @@ type Log struct { func New(c Config) *Log { return &Log{ - inf: log.New(c.Inf.Out, c.Inf.Prefix, c.Inf.Flag), - err: log.New(c.Err.Out, c.Err.Prefix, c.Inf.Flag), + inf: log.New(c.Inf.Out, c.Inf.Prefix, 0), + err: log.New(c.Err.Out, c.Err.Prefix, 0), } } From 914cc23eecc138c7fc4211d5bda20c6582cd35a5 Mon Sep 17 00:00:00 2001 From: codegold79 Date: Fri, 18 Jun 2021 21:18:28 -0700 Subject: [PATCH 09/25] Add documentation to log and dbg packages Signed-off-by: codegold79 --- back/internal/dbg/dbg.go | 6 ++++++ back/internal/log/log.go | 9 +++++++++ 2 files changed, 15 insertions(+) diff --git a/back/internal/dbg/dbg.go b/back/internal/dbg/dbg.go index ee600c9..78fc55d 100644 --- a/back/internal/dbg/dbg.go +++ b/back/internal/dbg/dbg.go @@ -1,3 +1,6 @@ + +// Package dbg allows for outputting information that can help with debugging +// the application. package dbg import ( @@ -29,6 +32,7 @@ func (dl *dbgLog) printf(format string, as ...interface{}) { dbg.log.Printf(format+"\n", as...) } +// Log outputs information to help with application debugging. func Log(text string) { dbgLoad := atomicV.Load().(*dbgLog) if dbgLoad.out == nil { @@ -37,6 +41,7 @@ func Log(text string) { dbgLoad.println(text) } +// Logf outputs debugging information and is able to interpret formatting verbs. func Logf(format string, as ...interface{}) { dbgLoad := atomicV.Load().(*dbgLog) if dbgLoad.out == nil { @@ -45,6 +50,7 @@ func Logf(format string, as ...interface{}) { dbgLoad.printf(format, as...) } +// SetDebugOut allows for choosing where debug information will be written to. func SetDebugOut(out io.Writer) { dbgLoad := atomicV.Load().(*dbgLog) dbgLoad.out = out diff --git a/back/internal/log/log.go b/back/internal/log/log.go index 0bcab4a..64503ff 100644 --- a/back/internal/log/log.go +++ b/back/internal/log/log.go @@ -1,3 +1,5 @@ +// Package log allows for outputting information regarding application errors +// and events important to know for proper operation. package log import ( @@ -5,21 +7,26 @@ import ( "log" ) +// Output is where logger will write to. Prefix is what will precede log text. type Output struct { Out io.Writer Prefix string } +// Config allows for configuring the logger outputs for errors and information +// type log messages. type Config struct { Err Output Inf Output } +// Log is able to write information to a chosen output. type Log struct { err *log.Logger inf *log.Logger } +// New provides a pointer to a new instance of the Log object. func New(c Config) *Log { return &Log{ inf: log.New(c.Inf.Out, c.Inf.Prefix, 0), @@ -27,10 +34,12 @@ func New(c Config) *Log { } } +// Info outputs information from the application. func (log *Log) Info(format string, as ...interface{}) { log.inf.Printf(format+"\n", as...) } +// Error outputs error information from the application. func (log *Log) Error(format string, as ...interface{}) { log.err.Printf(format+"\n", as...) } From 94c5a78647362b05c47563848e9ac2ef9b267325 Mon Sep 17 00:00:00 2001 From: codegold79 Date: Tue, 22 Jun 2021 18:26:59 -0700 Subject: [PATCH 10/25] instantiate log in run, not main; fmt.print errors and not log Signed-off-by: codegold79 --- back/cmd/openbsrv/main.go | 32 ++++++++++++++++---------------- back/cmd/openbsrv/srvmgmt.go | 4 +++- back/internal/dbg/dbg.go | 4 ++-- 3 files changed, 21 insertions(+), 19 deletions(-) diff --git a/back/cmd/openbsrv/main.go b/back/cmd/openbsrv/main.go index 3996ddb..e1c7f45 100644 --- a/back/cmd/openbsrv/main.go +++ b/back/cmd/openbsrv/main.go @@ -13,26 +13,14 @@ import ( ) func main() { - config := log.Config{ - Err: log.Output{ - Out: os.Stderr, - Prefix: "[ERROR] ", - }, - Inf: log.Output{ - Out: os.Stdout, - Prefix: "[INFO] ", - }, - } - log := log.New(config) - - if err := run(log); err != nil { + if err := run(); err != nil { cmd := path.Base(os.Args[0]) - log.Error("%s: %s", cmd, err) + fmt.Fprintf(os.Stderr, "%s: %s\n", cmd, err) os.Exit(1) } } -func run(log *log.Log) error { +func run() error { var ( dbdrvr = "mysql" dbname = "openeug_openb_dev" @@ -64,6 +52,18 @@ func run(log *log.Log) error { sm.Start() defer sm.Stop() + config := log.Config{ + Err: log.Output{ + Out: os.Stderr, + Prefix: "[ERROR] ", + }, + Inf: log.Output{ + Out: os.Stdout, + Prefix: "[INFO] ", + }, + } + log := log.New(config) + if debug { dbg.SetDebugOut(os.Stdout) } @@ -111,7 +111,7 @@ func run(log *log.Log) error { sm.Set(func(s *sigmon.State) { if err := m.stop(); err != nil { - log.Error(err.Error()) + fmt.Fprintln(os.Stderr, err) } }) diff --git a/back/cmd/openbsrv/srvmgmt.go b/back/cmd/openbsrv/srvmgmt.go index 1d42d2d..a369e84 100644 --- a/back/cmd/openbsrv/srvmgmt.go +++ b/back/cmd/openbsrv/srvmgmt.go @@ -1,6 +1,8 @@ package main import ( + "fmt" + "os" "sync" "time" @@ -34,7 +36,7 @@ func (m *serverMgmt) serve() error { // TODO: gather returned errors if err := s.Serve(); err != nil { - m.log.Error("server error: %v", err) + fmt.Fprintln(os.Stderr, "server error:", err) } }(s) diff --git a/back/internal/dbg/dbg.go b/back/internal/dbg/dbg.go index 78fc55d..e58f6f0 100644 --- a/back/internal/dbg/dbg.go +++ b/back/internal/dbg/dbg.go @@ -24,8 +24,8 @@ func new() *dbgLog { return &dl } -func (dl *dbgLog) println(text string) { - dbg.log.Println(text) +func (dl *dbgLog) println(as ...interface{}) { + dbg.log.Println(as...) } func (dl *dbgLog) printf(format string, as ...interface{}) { From e7b82d85bae185cd59561d1df65affdd78062522 Mon Sep 17 00:00:00 2001 From: codegold79 Date: Mon, 28 Jun 2021 18:42:26 -0700 Subject: [PATCH 11/25] rename print{f,ln} methods to log{f,ln}; use interface, not string args Signed-off-by: codegold79 --- back/internal/dbg/dbg.go | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/back/internal/dbg/dbg.go b/back/internal/dbg/dbg.go index e58f6f0..6222366 100644 --- a/back/internal/dbg/dbg.go +++ b/back/internal/dbg/dbg.go @@ -1,4 +1,3 @@ - // Package dbg allows for outputting information that can help with debugging // the application. package dbg @@ -24,21 +23,21 @@ func new() *dbgLog { return &dl } -func (dl *dbgLog) println(as ...interface{}) { +func (dl *dbgLog) logln(as ...interface{}) { dbg.log.Println(as...) } -func (dl *dbgLog) printf(format string, as ...interface{}) { +func (dl *dbgLog) logf(format string, as ...interface{}) { dbg.log.Printf(format+"\n", as...) } // Log outputs information to help with application debugging. -func Log(text string) { +func Log(as ...interface{}) { dbgLoad := atomicV.Load().(*dbgLog) if dbgLoad.out == nil { return } - dbgLoad.println(text) + dbgLoad.logln(as...) } // Logf outputs debugging information and is able to interpret formatting verbs. @@ -47,7 +46,7 @@ func Logf(format string, as ...interface{}) { if dbgLoad.out == nil { return } - dbgLoad.printf(format, as...) + dbgLoad.logf(format, as...) } // SetDebugOut allows for choosing where debug information will be written to. From ef29245fc275b483fe129b8227d5464e858793cc Mon Sep 17 00:00:00 2001 From: codegold79 Date: Mon, 28 Jun 2021 19:34:44 -0700 Subject: [PATCH 12/25] Make atomic.Value not a global Signed-off-by: codegold79 --- back/internal/dbg/dbg.go | 34 +++++++++++++++------------------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/back/internal/dbg/dbg.go b/back/internal/dbg/dbg.go index 6222366..0bea36c 100644 --- a/back/internal/dbg/dbg.go +++ b/back/internal/dbg/dbg.go @@ -10,16 +10,17 @@ import ( // dbgLog is thread-safe. type dbgLog struct { - log *log.Logger - out io.Writer + log *log.Logger + atomVal atomic.Value + toggle bool } -var atomicV atomic.Value var dbg = new() func new() *dbgLog { var dl dbgLog - atomicV.Store(&dl) + + dl.atomVal.Store(dl.toggle) return &dl } @@ -33,32 +34,27 @@ func (dl *dbgLog) logf(format string, as ...interface{}) { // Log outputs information to help with application debugging. func Log(as ...interface{}) { - dbgLoad := atomicV.Load().(*dbgLog) - if dbgLoad.out == nil { - return + toggle := dbg.atomVal.Load().(bool) + if toggle { + dbg.logln(as...) } - dbgLoad.logln(as...) } // Logf outputs debugging information and is able to interpret formatting verbs. func Logf(format string, as ...interface{}) { - dbgLoad := atomicV.Load().(*dbgLog) - if dbgLoad.out == nil { - return + toggle := dbg.atomVal.Load().(bool) + if toggle { + dbg.logf(format, as...) } - dbgLoad.logf(format, as...) } // SetDebugOut allows for choosing where debug information will be written to. func SetDebugOut(out io.Writer) { - dbgLoad := atomicV.Load().(*dbgLog) - dbgLoad.out = out - if out != nil { - dbgLoad.log = log.New(out, "", 0) - } else { - dbgLoad.log = log.Default() + dbg.atomVal.Store(true) + dbg.log = log.New(out, "", 0) + return } - atomicV.Store(dbgLoad) + dbg.atomVal.Store(false) } From 316b47be6e6131284674320a6f0bc2eb624f6819 Mon Sep 17 00:00:00 2001 From: codegold79 Date: Mon, 28 Jun 2021 19:51:50 -0700 Subject: [PATCH 13/25] undo using log and return to printing directly to stderr Signed-off-by: codegold79 --- back/cmd/openbsrv/srvmgmt.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/back/cmd/openbsrv/srvmgmt.go b/back/cmd/openbsrv/srvmgmt.go index a369e84..f469706 100644 --- a/back/cmd/openbsrv/srvmgmt.go +++ b/back/cmd/openbsrv/srvmgmt.go @@ -53,7 +53,7 @@ func (m *serverMgmt) stop() error { go func(s server) { // TODO: gather returned errors if err := s.Stop(); err != nil { - m.log.Error("stop error: %v", err) + fmt.Fprintln(os.Stderr, err) } }(s) } From 001cd5ca9633786280397a1d0de7bf0bd63aba45 Mon Sep 17 00:00:00 2001 From: codegold79 Date: Mon, 28 Jun 2021 19:56:07 -0700 Subject: [PATCH 14/25] Rename dbgLog struct to dbg Signed-off-by: codegold79 --- back/internal/dbg/dbg.go | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/back/internal/dbg/dbg.go b/back/internal/dbg/dbg.go index 0bea36c..37214ea 100644 --- a/back/internal/dbg/dbg.go +++ b/back/internal/dbg/dbg.go @@ -8,53 +8,53 @@ import ( "sync/atomic" ) -// dbgLog is thread-safe. -type dbgLog struct { +// dbg is thread-safe. +type dbg struct { log *log.Logger atomVal atomic.Value toggle bool } -var dbg = new() +var debug = new() -func new() *dbgLog { - var dl dbgLog +func new() *dbg { + var d dbg - dl.atomVal.Store(dl.toggle) - return &dl + d.atomVal.Store(d.toggle) + return &d } -func (dl *dbgLog) logln(as ...interface{}) { - dbg.log.Println(as...) +func (d *dbg) logln(as ...interface{}) { + debug.log.Println(as...) } -func (dl *dbgLog) logf(format string, as ...interface{}) { - dbg.log.Printf(format+"\n", as...) +func (d *dbg) logf(format string, as ...interface{}) { + debug.log.Printf(format+"\n", as...) } // Log outputs information to help with application debugging. func Log(as ...interface{}) { - toggle := dbg.atomVal.Load().(bool) + toggle := debug.atomVal.Load().(bool) if toggle { - dbg.logln(as...) + debug.logln(as...) } } // Logf outputs debugging information and is able to interpret formatting verbs. func Logf(format string, as ...interface{}) { - toggle := dbg.atomVal.Load().(bool) + toggle := debug.atomVal.Load().(bool) if toggle { - dbg.logf(format, as...) + debug.logf(format, as...) } } // SetDebugOut allows for choosing where debug information will be written to. func SetDebugOut(out io.Writer) { if out != nil { - dbg.atomVal.Store(true) - dbg.log = log.New(out, "", 0) + debug.atomVal.Store(true) + debug.log = log.New(out, "", 0) return } - dbg.atomVal.Store(false) + debug.atomVal.Store(false) } From 38f2a655abe7969f017cf0cc6c8be98edde5b049 Mon Sep 17 00:00:00 2001 From: codegold79 Date: Mon, 28 Jun 2021 19:59:12 -0700 Subject: [PATCH 15/25] Undo change to error server stop message Signed-off-by: codegold79 --- back/cmd/openbsrv/srvmgmt.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/back/cmd/openbsrv/srvmgmt.go b/back/cmd/openbsrv/srvmgmt.go index f469706..a02bd1a 100644 --- a/back/cmd/openbsrv/srvmgmt.go +++ b/back/cmd/openbsrv/srvmgmt.go @@ -53,7 +53,7 @@ func (m *serverMgmt) stop() error { go func(s server) { // TODO: gather returned errors if err := s.Stop(); err != nil { - fmt.Fprintln(os.Stderr, err) + fmt.Fprintln(os.Stderr, "stop error:", err) } }(s) } From 644e17e48419b8bbcfe09e86e67a6cfd66365909 Mon Sep 17 00:00:00 2001 From: codegold79 Date: Mon, 5 Jul 2021 14:32:21 -0700 Subject: [PATCH 16/25] Changes per PR: Rename, remove param, change output - Raname config->logcfg in main.go - Remove log from srvmgmt.go - Use stdOut for errors being logged in main.go Signed-off-by: codegold79 --- back/cmd/openbsrv/main.go | 8 ++++---- back/cmd/openbsrv/srvmgmt.go | 10 +++------- 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/back/cmd/openbsrv/main.go b/back/cmd/openbsrv/main.go index e1c7f45..4d47f13 100644 --- a/back/cmd/openbsrv/main.go +++ b/back/cmd/openbsrv/main.go @@ -52,9 +52,9 @@ func run() error { sm.Start() defer sm.Stop() - config := log.Config{ + logCfg := log.Config{ Err: log.Output{ - Out: os.Stderr, + Out: os.Stdout, Prefix: "[ERROR] ", }, Inf: log.Output{ @@ -62,7 +62,7 @@ func run() error { Prefix: "[INFO] ", }, } - log := log.New(config) + log := log.New(logCfg) if debug { dbg.SetDebugOut(os.Stdout) @@ -107,7 +107,7 @@ func run() error { return err } - m := newServerMgmt(log, gsrv, hsrv, fsrv) + m := newServerMgmt(gsrv, hsrv, fsrv) sm.Set(func(s *sigmon.State) { if err := m.stop(); err != nil { diff --git a/back/cmd/openbsrv/srvmgmt.go b/back/cmd/openbsrv/srvmgmt.go index a02bd1a..b1f7b59 100644 --- a/back/cmd/openbsrv/srvmgmt.go +++ b/back/cmd/openbsrv/srvmgmt.go @@ -5,8 +5,6 @@ import ( "os" "sync" "time" - - "github.com/OpenEugene/openboard/back/internal/log" ) type server interface { @@ -15,14 +13,12 @@ type server interface { } type serverMgmt struct { - ss []server - log *log.Log + ss []server } -func newServerMgmt(log *log.Log, ss ...server) *serverMgmt { +func newServerMgmt(ss ...server) *serverMgmt { return &serverMgmt{ - ss: ss, - log: log, + ss: ss, } } From 27dce310e1657951721b369eb480250168aa30f2 Mon Sep 17 00:00:00 2001 From: codegold79 Date: Mon, 5 Jul 2021 15:02:36 -0700 Subject: [PATCH 17/25] Guard dbg methods scope concurrent processes Signed-off-by: codegold79 --- back/internal/dbg/dbg.go | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/back/internal/dbg/dbg.go b/back/internal/dbg/dbg.go index 37214ea..395cb16 100644 --- a/back/internal/dbg/dbg.go +++ b/back/internal/dbg/dbg.go @@ -25,27 +25,27 @@ func new() *dbg { } func (d *dbg) logln(as ...interface{}) { - debug.log.Println(as...) + toggle := debug.atomVal.Load().(bool) + if toggle { + debug.log.Println(as...) + } } func (d *dbg) logf(format string, as ...interface{}) { - debug.log.Printf(format+"\n", as...) + toggle := debug.atomVal.Load().(bool) + if toggle { + debug.log.Printf(format+"\n", as...) + } } // Log outputs information to help with application debugging. func Log(as ...interface{}) { - toggle := debug.atomVal.Load().(bool) - if toggle { - debug.logln(as...) - } + debug.logln(as...) } // Logf outputs debugging information and is able to interpret formatting verbs. func Logf(format string, as ...interface{}) { - toggle := debug.atomVal.Load().(bool) - if toggle { - debug.logf(format, as...) - } + debug.logf(format, as...) } // SetDebugOut allows for choosing where debug information will be written to. From b563c5de068b5a1e789a91732584416bf9c87b44 Mon Sep 17 00:00:00 2001 From: codegold79 Date: Sat, 10 Jul 2021 10:21:41 -0700 Subject: [PATCH 18/25] use dbg object's methods over global's; move var closer to use Signed-off-by: codegold79 --- back/internal/dbg/dbg.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/back/internal/dbg/dbg.go b/back/internal/dbg/dbg.go index 395cb16..6658bf0 100644 --- a/back/internal/dbg/dbg.go +++ b/back/internal/dbg/dbg.go @@ -15,8 +15,6 @@ type dbg struct { toggle bool } -var debug = new() - func new() *dbg { var d dbg @@ -25,19 +23,21 @@ func new() *dbg { } func (d *dbg) logln(as ...interface{}) { - toggle := debug.atomVal.Load().(bool) + toggle := d.atomVal.Load().(bool) if toggle { - debug.log.Println(as...) + d.log.Println(as...) } } func (d *dbg) logf(format string, as ...interface{}) { - toggle := debug.atomVal.Load().(bool) + toggle := d.atomVal.Load().(bool) if toggle { - debug.log.Printf(format+"\n", as...) + d.log.Printf(format+"\n", as...) } } +var debug = new() + // Log outputs information to help with application debugging. func Log(as ...interface{}) { debug.logln(as...) From 85d5bbe8acfa327d8460ba6a115c659438a23578 Mon Sep 17 00:00:00 2001 From: codegold79 Date: Sat, 10 Jul 2021 10:45:54 -0700 Subject: [PATCH 19/25] Add benchmark test Signed-off-by: codegold79 --- back/internal/dbg/dbg_test.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 back/internal/dbg/dbg_test.go diff --git a/back/internal/dbg/dbg_test.go b/back/internal/dbg/dbg_test.go new file mode 100644 index 0000000..2433e8f --- /dev/null +++ b/back/internal/dbg/dbg_test.go @@ -0,0 +1,22 @@ +package dbg + +import ( + "os" + "testing" +) + +func BenchmarkDbgUse(b *testing.B) { + for n := 0; n < b.N; n++ { + useDbgLogExample() + } +} + +func useDbgLogExample() { + Log("\ndebug is writing to nothing") + + SetDebugOut(os.Stdout) + + Log("debug has been set to write to stdout") + + Logf("writing various formats: %t, %s, %d", true, "word", 79) +} From 9303f1b6d345f98ed49962373bd362d341775df4 Mon Sep 17 00:00:00 2001 From: codegold79 Date: Sat, 10 Jul 2021 11:24:36 -0700 Subject: [PATCH 20/25] improve benchmark test Signed-off-by: codegold79 --- back/internal/dbg/dbg_test.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/back/internal/dbg/dbg_test.go b/back/internal/dbg/dbg_test.go index 2433e8f..b02360c 100644 --- a/back/internal/dbg/dbg_test.go +++ b/back/internal/dbg/dbg_test.go @@ -19,4 +19,8 @@ func useDbgLogExample() { Log("debug has been set to write to stdout") Logf("writing various formats: %t, %s, %d", true, "word", 79) + + SetDebugOut(nil) + + Log("write to nothing again") } From 1007345f24508a1dcc7a8477d7994c96d61aea73 Mon Sep 17 00:00:00 2001 From: codegold79 Date: Mon, 19 Jul 2021 19:43:39 -0700 Subject: [PATCH 21/25] Use b.RunParallel in benchmark tests Signed-off-by: codegold79 --- back/internal/dbg/dbg_test.go | 46 ++++++++++++++++++++++------------- 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/back/internal/dbg/dbg_test.go b/back/internal/dbg/dbg_test.go index b02360c..df7f2fe 100644 --- a/back/internal/dbg/dbg_test.go +++ b/back/internal/dbg/dbg_test.go @@ -1,26 +1,38 @@ package dbg import ( - "os" + "io/ioutil" "testing" ) func BenchmarkDbgUse(b *testing.B) { - for n := 0; n < b.N; n++ { - useDbgLogExample() - } + SetDebugOut(ioutil.Discard) + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + Log("") + } + }) } - -func useDbgLogExample() { - Log("\ndebug is writing to nothing") - - SetDebugOut(os.Stdout) - - Log("debug has been set to write to stdout") - - Logf("writing various formats: %t, %s, %d", true, "word", 79) - - SetDebugOut(nil) - - Log("write to nothing again") +func BenchmarkDbgUseNil(b *testing.B) { + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + Log("") + } + }) +} +func BenchmarkDbgSetAndUse(b *testing.B) { + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + SetDebugOut(ioutil.Discard) + Log("") + } + }) +} +func BenchmarkDbgSetAndUseNil(b *testing.B) { + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + SetDebugOut(nil) + Log("") + } + }) } From 08aab25ce19588f619a39d68a6b7192e8ff73de0 Mon Sep 17 00:00:00 2001 From: codegold79 Date: Mon, 26 Jul 2021 19:24:43 -0700 Subject: [PATCH 22/25] store and load log.Logger in sync/atomic value Signed-off-by: codegold79 --- back/internal/dbg/dbg.go | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/back/internal/dbg/dbg.go b/back/internal/dbg/dbg.go index 6658bf0..3eb3f71 100644 --- a/back/internal/dbg/dbg.go +++ b/back/internal/dbg/dbg.go @@ -10,29 +10,27 @@ import ( // dbg is thread-safe. type dbg struct { - log *log.Logger atomVal atomic.Value - toggle bool } func new() *dbg { var d dbg - - d.atomVal.Store(d.toggle) + var logr *log.Logger + d.atomVal.Store(logr) return &d } func (d *dbg) logln(as ...interface{}) { - toggle := d.atomVal.Load().(bool) - if toggle { - d.log.Println(as...) + logr := d.atomVal.Load().(*log.Logger) + if logr != nil { + logr.Println(as...) } } func (d *dbg) logf(format string, as ...interface{}) { - toggle := d.atomVal.Load().(bool) - if toggle { - d.log.Printf(format+"\n", as...) + logr := d.atomVal.Load().(*log.Logger) + if logr != nil { + logr.Printf(format+"\n", as...) } } @@ -50,11 +48,13 @@ func Logf(format string, as ...interface{}) { // SetDebugOut allows for choosing where debug information will be written to. func SetDebugOut(out io.Writer) { + var logr *log.Logger + if out != nil { - debug.atomVal.Store(true) - debug.log = log.New(out, "", 0) + logr = log.New(out, "", 0) + debug.atomVal.Store(logr) return } - debug.atomVal.Store(false) + debug.atomVal.Store(logr) } From 050f097beefc49afe8e67e89ddfef3d8df68b8dc Mon Sep 17 00:00:00 2001 From: codegold79 Date: Wed, 4 Aug 2021 17:37:33 -0700 Subject: [PATCH 23/25] Add tests ensuring debug setting works as intended Signed-off-by: codegold79 --- back/internal/dbg/dbg.go | 24 +++++++++++--------- back/internal/dbg/dbg_test.go | 41 ++++++++++++++++++++++++++++++++--- 2 files changed, 52 insertions(+), 13 deletions(-) diff --git a/back/internal/dbg/dbg.go b/back/internal/dbg/dbg.go index 3eb3f71..61092fc 100644 --- a/back/internal/dbg/dbg.go +++ b/back/internal/dbg/dbg.go @@ -34,6 +34,18 @@ func (d *dbg) logf(format string, as ...interface{}) { } } +func (d *dbg) setOut(out io.Writer) { + var logr *log.Logger + + if out != nil { + logr = log.New(out, "", 0) + d.atomVal.Store(logr) + return + } + + d.atomVal.Store(logr) +} + var debug = new() // Log outputs information to help with application debugging. @@ -47,14 +59,6 @@ func Logf(format string, as ...interface{}) { } // SetDebugOut allows for choosing where debug information will be written to. -func SetDebugOut(out io.Writer) { - var logr *log.Logger - - if out != nil { - logr = log.New(out, "", 0) - debug.atomVal.Store(logr) - return - } - - debug.atomVal.Store(logr) +func SetOut(out io.Writer) { + debug.setOut(out) } diff --git a/back/internal/dbg/dbg_test.go b/back/internal/dbg/dbg_test.go index df7f2fe..58e5bf3 100644 --- a/back/internal/dbg/dbg_test.go +++ b/back/internal/dbg/dbg_test.go @@ -1,12 +1,13 @@ package dbg import ( + "bytes" "io/ioutil" "testing" ) func BenchmarkDbgUse(b *testing.B) { - SetDebugOut(ioutil.Discard) + SetOut(ioutil.Discard) b.RunParallel(func(pb *testing.PB) { for pb.Next() { Log("") @@ -23,7 +24,7 @@ func BenchmarkDbgUseNil(b *testing.B) { func BenchmarkDbgSetAndUse(b *testing.B) { b.RunParallel(func(pb *testing.PB) { for pb.Next() { - SetDebugOut(ioutil.Discard) + SetOut(ioutil.Discard) Log("") } }) @@ -31,8 +32,42 @@ func BenchmarkDbgSetAndUse(b *testing.B) { func BenchmarkDbgSetAndUseNil(b *testing.B) { b.RunParallel(func(pb *testing.PB) { for pb.Next() { - SetDebugOut(nil) + SetOut(nil) Log("") } }) } + +func TestSetOut(t *testing.T) { + buff := bytes.NewBuffer([]byte{}) + Log("debug not set") + + SetOut(buff) + msg := "debug set to bytes buffer" + Log(msg) + got := buff.String() + + want := msg + "\n" + if got != want { + t.Errorf("want: %s, got: %s", want, got) + } + + SetOut(nil) + buff.Reset() + want = "" + Log("debug set to nil") + got = buff.String() + if got != want { + t.Errorf("want: nothing, got: %s", got) + } + + SetOut(buff) + buff.Reset() + msg = "debug set again to bytes buffer" + Log(msg) + got = buff.String() + want = msg + "\n" + if got != want { + t.Errorf("want: %s, got: %s", want, got) + } +} From 2b92b151c0747bc18b5a685e72d6b57c1ecfe646 Mon Sep 17 00:00:00 2001 From: codegold79 Date: Wed, 4 Aug 2021 20:17:19 -0700 Subject: [PATCH 24/25] Test Logf after second SetOut Signed-off-by: codegold79 --- back/internal/dbg/dbg_test.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/back/internal/dbg/dbg_test.go b/back/internal/dbg/dbg_test.go index 58e5bf3..da2abe1 100644 --- a/back/internal/dbg/dbg_test.go +++ b/back/internal/dbg/dbg_test.go @@ -2,6 +2,7 @@ package dbg import ( "bytes" + "fmt" "io/ioutil" "testing" ) @@ -39,9 +40,9 @@ func BenchmarkDbgSetAndUseNil(b *testing.B) { } func TestSetOut(t *testing.T) { - buff := bytes.NewBuffer([]byte{}) Log("debug not set") + buff := bytes.NewBuffer([]byte{}) SetOut(buff) msg := "debug set to bytes buffer" Log(msg) @@ -63,10 +64,10 @@ func TestSetOut(t *testing.T) { SetOut(buff) buff.Reset() - msg = "debug set again to bytes buffer" - Log(msg) + msg = "debug set to bytes buffer, %s time" + Logf(msg, "second") got = buff.String() - want = msg + "\n" + want = fmt.Sprintf(msg+"\n", "second") if got != want { t.Errorf("want: %s, got: %s", want, got) } From 3c42fd628105772e8dd9641cf873ca4158e1915d Mon Sep 17 00:00:00 2001 From: codegold79 Date: Wed, 4 Aug 2021 20:19:23 -0700 Subject: [PATCH 25/25] Update main.go to use dbg.SetOut Signed-off-by: codegold79 --- back/cmd/openbsrv/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/back/cmd/openbsrv/main.go b/back/cmd/openbsrv/main.go index 4d47f13..e782370 100644 --- a/back/cmd/openbsrv/main.go +++ b/back/cmd/openbsrv/main.go @@ -65,7 +65,7 @@ func run() error { log := log.New(logCfg) if debug { - dbg.SetDebugOut(os.Stdout) + dbg.SetOut(os.Stdout) } dbg.Logf("set up SQL database at %s:%s.", dbaddr, dbport)