Skip to content

Commit

Permalink
Merge pull request #7 from tendermint/log
Browse files Browse the repository at this point in the history
Log
  • Loading branch information
ebuchman committed May 5, 2017
2 parents 3420b38 + 240215f commit d72136d
Show file tree
Hide file tree
Showing 19 changed files with 722 additions and 71 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
*.swp
*.swo
vendor
.glide
6 changes: 2 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
.PHONY: all test install get_vendor_deps ensure_tools
.PHONY: all test get_vendor_deps ensure_tools

GOTOOLS = \
github.com/Masterminds/glide
REPO:=github.com/tendermint/tmlibs

all: install test
all: test

test:
go test `glide novendor`
Expand All @@ -16,5 +16,3 @@ get_vendor_deps: ensure_tools

ensure_tools:
go get $(GOTOOLS)


56 changes: 27 additions & 29 deletions common/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package common
import (
"sync/atomic"

"github.com/tendermint/log15"
"github.com/tendermint/tmlibs/log"
)

type Service interface {
Expand All @@ -19,6 +19,8 @@ type Service interface {
IsRunning() bool

String() string

SetLogger(log.Logger)
}

/*
Expand Down Expand Up @@ -64,7 +66,7 @@ Typical usage:
}
*/
type BaseService struct {
log log15.Logger
Logger log.Logger
name string
started uint32 // atomic
stopped uint32 // atomic
Expand All @@ -74,27 +76,31 @@ type BaseService struct {
impl Service
}

func NewBaseService(log log15.Logger, name string, impl Service) *BaseService {
func NewBaseService(logger log.Logger, name string, impl Service) *BaseService {
if logger == nil {
logger = log.NewNopLogger()
}

return &BaseService{
log: log,
name: name,
Quit: make(chan struct{}),
impl: impl,
Logger: logger,
name: name,
Quit: make(chan struct{}),
impl: impl,
}
}

func (bs *BaseService) SetLogger(l log.Logger) {
bs.Logger = l
}

// Implements Servce
func (bs *BaseService) Start() (bool, error) {
if atomic.CompareAndSwapUint32(&bs.started, 0, 1) {
if atomic.LoadUint32(&bs.stopped) == 1 {
if bs.log != nil {
bs.log.Warn(Fmt("Not starting %v -- already stopped", bs.name), "impl", bs.impl)
}
bs.Logger.Error(Fmt("Not starting %v -- already stopped", bs.name), "impl", bs.impl)
return false, nil
} else {
if bs.log != nil {
bs.log.Info(Fmt("Starting %v", bs.name), "impl", bs.impl)
}
bs.Logger.Info(Fmt("Starting %v", bs.name), "impl", bs.impl)
}
err := bs.impl.OnStart()
if err != nil {
Expand All @@ -104,9 +110,7 @@ func (bs *BaseService) Start() (bool, error) {
}
return true, err
} else {
if bs.log != nil {
bs.log.Debug(Fmt("Not starting %v -- already started", bs.name), "impl", bs.impl)
}
bs.Logger.Debug(Fmt("Not starting %v -- already started", bs.name), "impl", bs.impl)
return false, nil
}
}
Expand All @@ -119,16 +123,12 @@ func (bs *BaseService) OnStart() error { return nil }
// Implements Service
func (bs *BaseService) Stop() bool {
if atomic.CompareAndSwapUint32(&bs.stopped, 0, 1) {
if bs.log != nil {
bs.log.Info(Fmt("Stopping %v", bs.name), "impl", bs.impl)
}
bs.Logger.Info(Fmt("Stopping %v", bs.name), "impl", bs.impl)
bs.impl.OnStop()
close(bs.Quit)
return true
} else {
if bs.log != nil {
bs.log.Debug(Fmt("Stopping %v (ignoring: already stopped)", bs.name), "impl", bs.impl)
}
bs.Logger.Debug(Fmt("Stopping %v (ignoring: already stopped)", bs.name), "impl", bs.impl)
return false
}
}
Expand All @@ -147,9 +147,7 @@ func (bs *BaseService) Reset() (bool, error) {
bs.Quit = make(chan struct{})
return true, bs.impl.OnReset()
} else {
if bs.log != nil {
bs.log.Debug(Fmt("Can't reset %v. Not stopped", bs.name), "impl", bs.impl)
}
bs.Logger.Debug(Fmt("Can't reset %v. Not stopped", bs.name), "impl", bs.impl)
return false, nil
}
// never happens
Expand Down Expand Up @@ -182,11 +180,11 @@ type QuitService struct {
BaseService
}

func NewQuitService(log log15.Logger, name string, impl Service) *QuitService {
if log != nil {
log.Warn("QuitService is deprecated, use BaseService instead")
func NewQuitService(logger log.Logger, name string, impl Service) *QuitService {
if logger != nil {
logger.Info("QuitService is deprecated, use BaseService instead")
}
return &QuitService{
BaseService: *NewBaseService(log, name, impl),
BaseService: *NewBaseService(logger, name, impl),
}
}
2 changes: 1 addition & 1 deletion events/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type eventSwitch struct {

func NewEventSwitch() EventSwitch {
evsw := &eventSwitch{}
evsw.BaseService = *NewBaseService(log, "EventSwitch", evsw)
evsw.BaseService = *NewBaseService(nil, "EventSwitch", evsw)
return evsw
}

Expand Down
7 changes: 0 additions & 7 deletions events/log.go

This file was deleted.

40 changes: 20 additions & 20 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 12 additions & 3 deletions glide.yaml
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
package: github.com/tendermint/tmlibs
import:
- package: github.com/go-kit/kit
subpackages:
- log
- log/level
- log/term
- package: github.com/go-logfmt/logfmt
- package: github.com/jmhodges/levigo
- package: github.com/pkg/errors
- package: github.com/spf13/cobra
- package: github.com/spf13/viper
- package: github.com/syndtr/goleveldb
subpackages:
- leveldb
- leveldb/errors
- leveldb/opt
- package: github.com/tendermint/go-wire
subpackages:
- data
- data/base58
- package: github.com/tendermint/log15
- package: golang.org/x/crypto
subpackages:
- ripemd160
- package: github.com/go-logfmt/logfmt
- package: github.com/spf13/cobra
- package: github.com/spf13/viper
testImport:
- package: github.com/stretchr/testify
version: ^1.1.4
Expand Down

0 comments on commit d72136d

Please sign in to comment.