Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow setting log level via cli parameters #229

Merged
merged 4 commits into from
Sep 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions cmd/wavelet/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"encoding/hex"
"errors"
"fmt"
"github.com/perlin-network/wavelet/conf"
"io"
"io/ioutil"
"net"
Expand All @@ -33,6 +32,8 @@ import (
"strconv"
"time"

"github.com/perlin-network/wavelet/conf"

"github.com/perlin-network/noise"
"github.com/perlin-network/noise/cipher"
"github.com/perlin-network/noise/edwards25519"
Expand All @@ -58,6 +59,7 @@ type Config struct {
Port uint
Wallet string
Genesis *string
LogLevel string
APIPort uint
APIHost *string
APICertsCache *string
Expand Down Expand Up @@ -85,8 +87,6 @@ func Run(args []string, stdin io.ReadCloser, stdout io.Writer, withoutGC bool) {
)),
)

logger := log.Node()

app := cli.NewApp()

app.Name = "wavelet"
Expand Down Expand Up @@ -150,6 +150,12 @@ func Run(args []string, stdin io.ReadCloser, stdout io.Writer, withoutGC bool) {
Usage: "Directory path to the database. If empty, a temporary in-memory database will be used instead.",
EnvVar: "WAVELET_DB_PATH",
}),
altsrc.NewStringFlag(cli.StringFlag{
Name: "loglevel",
Value: "debug",
Usage: "Minimum log level to output. Possible values: debug, info, warn, error, fatal, panic.",
EnvVar: "WAVELET_LOGLEVEL",
}),
altsrc.NewIntFlag(cli.IntFlag{
Name: "memory.max",
Value: 0,
Expand Down Expand Up @@ -238,6 +244,7 @@ func Run(args []string, stdin io.ReadCloser, stdout io.Writer, withoutGC bool) {
Database: c.String("db"),
MaxMemoryMB: c.Uint64("memory.max"),
WithoutGC: withoutGC,
LogLevel: c.String("loglevel"),
}

if genesis := c.String("genesis"); len(genesis) > 0 {
Expand Down Expand Up @@ -278,12 +285,14 @@ func Run(args []string, stdin io.ReadCloser, stdout io.Writer, withoutGC bool) {
sort.Sort(cli.CommandsByName(app.Commands))

if err := app.Run(args); err != nil {
logger := log.Node()
logger.Fatal().Err(err).
Msg("Failed to parse configuration/command-line arguments.")
}
}

func start(cfg *Config, stdin io.ReadCloser, stdout io.Writer) {
log.SetLevel(cfg.LogLevel)
logger := log.Node()

listener, err := net.Listen("tcp", fmt.Sprintf(":%d", cfg.Port))
Expand Down Expand Up @@ -365,7 +374,10 @@ func start(cfg *Config, stdin io.ReadCloser, stdout io.Writer) {
logger.Fatal().Err(err).Msgf("Failed to create/open database located at %q.", cfg.Database)
}

opts := []wavelet.Option{wavelet.WithGenesis(cfg.Genesis)}
opts := []wavelet.Option{
wavelet.WithGenesis(cfg.Genesis),
}

if cfg.WithoutGC {
opts = append(opts, wavelet.WithoutGC())
}
Expand Down
70 changes: 54 additions & 16 deletions cmd/wavelet/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,17 @@ import (
var wallet1 = "87a6813c3b4cf534b6ae82db9b1409fa7dbd5c13dba5858970b56084c4a930eb400056ee68a7cc2695222df05ea76875bc27ec6e61e8e62317c336157019c405"
var wallet2 = "85e7450f7cf0d9cd1d1d7bf4169c2f364eea4ba833a7280e0f931a1d92fd92c2696937c2c8df35dba0169de72990b80761e51dd9e2411fa1fce147f68ade830a"

func TestMainMain(t *testing.T) {
func TestMain_WithInvalidLogLevel(t *testing.T) {
// Invalid loglevel will cause the ledger to use the default log level,
// which is debug
w := NewTestWavelet(t, &TestWaveletConfig{LogLevel: "foobar"})
defer w.Cleanup()

w.Stdin <- "status"
w.Stdout.Search(t, "Here is the current status of your node")
}

func TestMain(t *testing.T) {
w := NewTestWavelet(t, nil)
defer w.Cleanup()

Expand All @@ -34,9 +44,31 @@ func TestMainMain(t *testing.T) {
assert.NotEqual(t, wallet1[64:], ledger.PublicKey) // A random wallet should be generated
}

func TestMain_WithLogLevel(t *testing.T) {
w := NewTestWavelet(t, &TestWaveletConfig{LogLevel: "warn"})
defer w.Cleanup()

w.Stdin <- "status"
// Info message should not be logged to stdout
search := "Here is the current status of your node"

timeout := time.NewTimer(time.Millisecond * 200)
for {
select {
case line := <-w.Stdout.Lines:
if strings.Contains(line, search) {
t.Fatal("info should not be logged to stdout")
}

case <-timeout.C:
return
}
}
}

func TestMain_WithWalletString(t *testing.T) {
wallet := "b27b880e6e44e3b127186a08bc5698316e8dd99157cec56211560b62141f0851c72096021609681eb8cab244752945b2008e1b51d8bc2208b2b562f35485d5cc"
w := NewTestWavelet(t, &TestWaveletConfig{wallet})
w := NewTestWavelet(t, &TestWaveletConfig{Wallet: wallet})
defer w.Cleanup()

ledger := w.GetLedgerStatus(t)
Expand All @@ -58,7 +90,7 @@ func TestMain_WithWalletFile(t *testing.T) {
t.Fatal(err)
}

w := NewTestWavelet(t, &TestWaveletConfig{walletPath})
w := NewTestWavelet(t, &TestWaveletConfig{Wallet: walletPath})
defer w.Cleanup()

ledger := w.GetLedgerStatus(t)
Expand All @@ -67,7 +99,7 @@ func TestMain_WithWalletFile(t *testing.T) {

func TestMain_WithInvalidWallet(t *testing.T) {
wallet := "foobar"
w := NewTestWavelet(t, &TestWaveletConfig{wallet})
w := NewTestWavelet(t, &TestWaveletConfig{Wallet: wallet})
defer w.Cleanup()

ledger := w.GetLedgerStatus(t)
Expand All @@ -83,7 +115,7 @@ func TestMain_Status(t *testing.T) {
}

func TestMain_Pay(t *testing.T) {
alice := NewTestWavelet(t, &TestWaveletConfig{wallet2})
alice := NewTestWavelet(t, &TestWaveletConfig{Wallet: wallet2})
defer alice.Cleanup()

bob := alice.Testnet.AddNode(t)
Expand All @@ -105,7 +137,7 @@ func TestMain_Pay(t *testing.T) {
}

func TestMain_Spawn(t *testing.T) {
w := NewTestWavelet(t, &TestWaveletConfig{wallet2})
w := NewTestWavelet(t, &TestWaveletConfig{Wallet: wallet2})
defer w.Cleanup()

for i := 0; i < 3; i++ {
Expand All @@ -125,7 +157,7 @@ func TestMain_Spawn(t *testing.T) {
}

func TestMain_Call(t *testing.T) {
w := NewTestWavelet(t, &TestWaveletConfig{wallet2})
w := NewTestWavelet(t, &TestWaveletConfig{Wallet: wallet2})
defer w.Cleanup()

for i := 0; i < 3; i++ {
Expand All @@ -146,7 +178,7 @@ func TestMain_Call(t *testing.T) {
}

func TestMain_CallWithParams(t *testing.T) {
w := NewTestWavelet(t, &TestWaveletConfig{wallet2})
w := NewTestWavelet(t, &TestWaveletConfig{Wallet: wallet2})
defer w.Cleanup()

for i := 0; i < 3; i++ {
Expand Down Expand Up @@ -217,7 +249,7 @@ func TestMain_CallWithParams(t *testing.T) {
}

func TestMain_DepositGas(t *testing.T) {
w := NewTestWavelet(t, &TestWaveletConfig{wallet2})
w := NewTestWavelet(t, &TestWaveletConfig{Wallet: wallet2})
defer w.Cleanup()

for i := 0; i < 3; i++ {
Expand All @@ -238,7 +270,7 @@ func TestMain_DepositGas(t *testing.T) {
}

func TestMain_Find(t *testing.T) {
alice := NewTestWavelet(t, &TestWaveletConfig{wallet2})
alice := NewTestWavelet(t, &TestWaveletConfig{Wallet: wallet2})
defer alice.Cleanup()

bob := alice.Testnet.AddNode(t)
Expand All @@ -256,7 +288,7 @@ func TestMain_Find(t *testing.T) {
}

func TestMain_PlaceStake(t *testing.T) {
alice := NewTestWavelet(t, &TestWaveletConfig{wallet2})
alice := NewTestWavelet(t, &TestWaveletConfig{Wallet: wallet2})
defer alice.Cleanup()

bob := alice.Testnet.AddNode(t)
Expand All @@ -277,7 +309,7 @@ func TestMain_PlaceStake(t *testing.T) {
}

func TestMain_WithdrawStake(t *testing.T) {
alice := NewTestWavelet(t, &TestWaveletConfig{wallet2})
alice := NewTestWavelet(t, &TestWaveletConfig{Wallet: wallet2})
defer alice.Cleanup()

bob := alice.Testnet.AddNode(t)
Expand Down Expand Up @@ -305,7 +337,7 @@ func TestMain_WithdrawStake(t *testing.T) {
}

func TestMain_WithdrawReward(t *testing.T) {
w := NewTestWavelet(t, &TestWaveletConfig{wallet2})
w := NewTestWavelet(t, &TestWaveletConfig{Wallet: wallet2})
defer w.Cleanup()

for i := 0; i < 3; i++ {
Expand Down Expand Up @@ -490,7 +522,8 @@ func (w *TestWavelet) Cleanup() {
}

type TestWaveletConfig struct {
Wallet string
Wallet string
LogLevel string
}

func NewTestWavelet(t *testing.T, cfg *TestWaveletConfig) *TestWavelet {
Expand All @@ -500,8 +533,13 @@ func NewTestWavelet(t *testing.T, cfg *TestWaveletConfig) *TestWavelet {
apiPort := nextPort(t)

args := []string{"wavelet", "--port", port, "--api.port", apiPort}
if cfg != nil && cfg.Wallet != "" {
args = append(args, []string{"--wallet", cfg.Wallet}...)
if cfg != nil {
if cfg.Wallet != "" {
args = append(args, []string{"--wallet", cfg.Wallet}...)
}
if cfg.LogLevel != "" {
args = append(args, []string{"--loglevel", cfg.LogLevel}...)
}
}

// Bootstrap with the faucet
Expand Down
5 changes: 3 additions & 2 deletions collapse.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ package wavelet
import (
"encoding/binary"
"encoding/hex"
"fmt"

"github.com/perlin-network/wavelet/avl"
"github.com/perlin-network/wavelet/conf"
"github.com/perlin-network/wavelet/log"
Expand Down Expand Up @@ -250,7 +250,8 @@ func collapseTransactions(g *Graph, accounts *Accounts, round uint64, current *R
res.rejectedErrors = append(res.rejectedErrors, err)
res.rejectedCount += popped.LogicalUnits()

fmt.Println("error applying transaction", err)
logger := log.Node()
logger.Error().Err(err).Msg("error applying transaction")

continue
}
Expand Down
Loading