new logging

This commit is contained in:
Anton Kaliaev
2017-04-28 00:37:18 +04:00
parent 8d8e35ae53
commit 986bdd00a5
20 changed files with 141 additions and 102 deletions

View File

@@ -2,11 +2,13 @@ package main
import (
"flag"
"log"
stdlog "log"
"os"
"github.com/tendermint/abci/server"
"github.com/tendermint/abci/types"
cmn "github.com/tendermint/tmlibs/common"
"github.com/tendermint/tmlibs/log"
)
func main() {
@@ -18,8 +20,10 @@ func main() {
// Start the listener
srv, err := server.NewServer(*addrPtr, *abciPtr, NewChainAwareApplication())
if err != nil {
log.Fatal(err.Error())
stdlog.Fatal(err.Error())
}
logger := log.NewTmLogger(os.Stdout)
srv.SetLogger(log.With(logger, "module", "abci-server"))
// Wait forever
cmn.TrapSignal(func() {

View File

@@ -1,33 +1,35 @@
package main
import (
"fmt"
"log"
"os"
"strconv"
"strings"
"testing"
"github.com/tendermint/abci/client"
abcicli "github.com/tendermint/abci/client"
"github.com/tendermint/abci/server"
"github.com/tendermint/abci/types"
"github.com/tendermint/tmlibs/log"
)
func TestChainAware(t *testing.T) {
app := NewChainAwareApplication()
logger := log.NewTmLogger(os.Stdout)
// Start the listener
srv, err := server.NewServer("unix://test.sock", "socket", app)
if err != nil {
t.Fatal(err)
}
srv.SetLogger(log.With(logger, "module", "abci-server"))
defer srv.Stop()
// Connect to the socket
client, err := abcicli.NewSocketClient("unix://test.sock", false)
if err != nil {
log.Fatal(fmt.Sprintf("Error starting socket client: %v", err.Error()))
t.Fatalf("Error starting socket client: %v", err.Error())
}
client.SetLogger(log.With(logger, "module", "abci-client"))
client.Start()
defer client.Stop()

View File

@@ -3,6 +3,7 @@ package dummy
import (
"bytes"
"io/ioutil"
"os"
"sort"
"testing"
@@ -10,9 +11,10 @@ import (
abcicli "github.com/tendermint/abci/client"
"github.com/tendermint/abci/server"
"github.com/tendermint/abci/types"
"github.com/tendermint/go-crypto"
crypto "github.com/tendermint/go-crypto"
"github.com/tendermint/merkleeyes/iavl"
cmn "github.com/tendermint/tmlibs/common"
"github.com/tendermint/tmlibs/log"
)
func testDummy(t *testing.T, app types.Application, tx []byte, key, value string) {
@@ -211,10 +213,13 @@ func valsEqual(t *testing.T, vals1, vals2 []*types.Validator) {
func makeSocketClientServer(app types.Application, name string) (abcicli.Client, cmn.Service, error) {
// Start the listener
socket := cmn.Fmt("unix://%s.sock", name)
logger := log.NewTmLogger(os.Stdout)
server, err := server.NewSocketServer(socket, app)
if err != nil {
return nil, nil, err
}
server.SetLogger(log.With(logger, "module", "abci-server"))
// Connect to the socket
client, err := abcicli.NewSocketClient(socket, false)
@@ -222,6 +227,7 @@ func makeSocketClientServer(app types.Application, name string) (abcicli.Client,
server.Stop()
return nil, nil, err
}
client.SetLogger(log.With(logger, "module", "abci-client"))
client.Start()
return client, server, err
@@ -230,18 +236,21 @@ func makeSocketClientServer(app types.Application, name string) (abcicli.Client,
func makeGRPCClientServer(app types.Application, name string) (abcicli.Client, cmn.Service, error) {
// Start the listener
socket := cmn.Fmt("unix://%s.sock", name)
logger := log.NewTmLogger(os.Stdout)
gapp := types.NewGRPCApplication(app)
server, err := server.NewGRPCServer(socket, gapp)
if err != nil {
return nil, nil, err
}
server.SetLogger(log.With(logger, "module", "abci-server"))
client, err := abcicli.NewGRPCClient(socket, true)
if err != nil {
server.Stop()
return nil, nil, err
}
client.SetLogger(log.With(logger, "module", "abci-client"))
return client, server, err
}

View File

@@ -1,7 +0,0 @@
package dummy
import (
"github.com/tendermint/tmlibs/logger"
)
var log = logger.New("module", "dummy")

View File

@@ -6,11 +6,13 @@ import (
"strconv"
"strings"
"github.com/pkg/errors"
"github.com/tendermint/abci/types"
"github.com/tendermint/go-wire"
wire "github.com/tendermint/go-wire"
"github.com/tendermint/merkleeyes/iavl"
cmn "github.com/tendermint/tmlibs/common"
dbm "github.com/tendermint/tmlibs/db"
"github.com/tendermint/tmlibs/log"
)
const (
@@ -29,6 +31,8 @@ type PersistentDummyApplication struct {
// validator set
changes []*types.Validator
logger log.Logger
}
func NewPersistentDummyApplication(dbDir string) *PersistentDummyApplication {
@@ -38,14 +42,19 @@ func NewPersistentDummyApplication(dbDir string) *PersistentDummyApplication {
stateTree := iavl.NewIAVLTree(0, db)
stateTree.Load(lastBlock.AppHash)
log.Notice("Loaded state", "block", lastBlock.Height, "root", stateTree.Hash())
// log.Notice("Loaded state", "block", lastBlock.Height, "root", stateTree.Hash())
return &PersistentDummyApplication{
app: &DummyApplication{state: stateTree},
db: db,
app: &DummyApplication{state: stateTree},
db: db,
logger: log.NewNopLogger(),
}
}
func (app *PersistentDummyApplication) SetLogger(l log.Logger) {
app.logger = l
}
func (app *PersistentDummyApplication) Info() (resInfo types.ResponseInfo) {
resInfo = app.app.Info()
lastBlock := LoadLastBlock(app.db)
@@ -79,13 +88,16 @@ func (app *PersistentDummyApplication) CheckTx(tx []byte) types.Result {
func (app *PersistentDummyApplication) Commit() types.Result {
// Save
appHash := app.app.state.Save()
log.Info("Saved state", "root", appHash)
app.logger.Info("Saved state", "root", appHash)
lastBlock := LastBlockInfo{
Height: app.blockHeader.Height,
AppHash: appHash, // this hash will be in the next block header
}
app.logger.Info("Saving block", "height", lastBlock.Height, "root", lastBlock.AppHash)
SaveLastBlock(app.db, lastBlock)
return types.NewResultOK(appHash, "")
}
@@ -98,7 +110,7 @@ func (app *PersistentDummyApplication) InitChain(validators []*types.Validator)
for _, v := range validators {
r := app.updateValidator(v)
if r.IsErr() {
log.Error("Error updating validators", "r", r)
app.logger.Error("Error updating validators", "r", r)
}
}
}
@@ -134,8 +146,7 @@ func LoadLastBlock(db dbm.DB) (lastBlock LastBlockInfo) {
r, n, err := bytes.NewReader(buf), new(int), new(error)
wire.ReadBinaryPtr(&lastBlock, r, 0, n, err)
if *err != nil {
// DATA HAS BEEN CORRUPTED OR THE SPEC HAS CHANGED
log.Crit(cmn.Fmt("Data has been corrupted or its spec has changed: %v\n", *err))
cmn.PanicCrisis(errors.Wrap(*err, "cannot load last block (data has been corrupted or its spec has changed)"))
}
// TODO: ensure that buf is completely read.
}
@@ -144,12 +155,11 @@ func LoadLastBlock(db dbm.DB) (lastBlock LastBlockInfo) {
}
func SaveLastBlock(db dbm.DB, lastBlock LastBlockInfo) {
log.Notice("Saving block", "height", lastBlock.Height, "root", lastBlock.AppHash)
buf, n, err := new(bytes.Buffer), new(int), new(error)
wire.WriteBinary(lastBlock, buf, n, err)
if *err != nil {
// TODO
cmn.PanicCrisis(*err)
cmn.PanicCrisis(errors.Wrap(*err, "cannot save last block"))
}
db.Set(lastBlockKey, buf.Bytes())
}

View File

@@ -2,8 +2,9 @@ package example
import (
"fmt"
"log"
stdlog "log"
"net"
"os"
"reflect"
"testing"
"time"
@@ -12,11 +13,12 @@ import (
"golang.org/x/net/context"
"github.com/tendermint/abci/client"
abcicli "github.com/tendermint/abci/client"
"github.com/tendermint/abci/example/dummy"
"github.com/tendermint/abci/server"
"github.com/tendermint/abci/types"
cmn "github.com/tendermint/tmlibs/common"
"github.com/tendermint/tmlibs/log"
)
func TestDummy(t *testing.T) {
@@ -37,19 +39,22 @@ func TestGRPC(t *testing.T) {
func testStream(t *testing.T, app types.Application) {
numDeliverTxs := 200000
logger := log.NewTmLogger(os.Stdout)
// Start the listener
server, err := server.NewSocketServer("unix://test.sock", app)
if err != nil {
log.Fatal(cmn.Fmt("Error starting socket server: %v", err.Error()))
stdlog.Fatal(cmn.Fmt("Error starting socket server: %v", err.Error()))
}
server.SetLogger(log.With(logger, "module", "abci-server"))
defer server.Stop()
// Connect to the socket
client, err := abcicli.NewSocketClient("unix://test.sock", false)
if err != nil {
log.Fatal(cmn.Fmt("Error starting socket client: %v", err.Error()))
stdlog.Fatal(cmn.Fmt("Error starting socket client: %v", err.Error()))
}
client.SetLogger(log.With(logger, "module", "abci-client"))
client.Start()
defer client.Stop()
@@ -110,18 +115,20 @@ func dialerFunc(addr string, timeout time.Duration) (net.Conn, error) {
func testGRPCSync(t *testing.T, app *types.GRPCApplication) {
numDeliverTxs := 2000
logger := log.NewTmLogger(os.Stdout)
// Start the listener
server, err := server.NewGRPCServer("unix://test.sock", app)
if err != nil {
log.Fatal(cmn.Fmt("Error starting GRPC server: %v", err.Error()))
stdlog.Fatal(cmn.Fmt("Error starting GRPC server: %v", err.Error()))
}
server.SetLogger(log.With(logger, "module", "abci-server"))
defer server.Stop()
// Connect to the socket
conn, err := grpc.Dial("unix://test.sock", grpc.WithInsecure(), grpc.WithDialer(dialerFunc))
if err != nil {
log.Fatal(cmn.Fmt("Error dialing GRPC server: %v", err.Error()))
stdlog.Fatal(cmn.Fmt("Error dialing GRPC server: %v", err.Error()))
}
defer conn.Close()