Skip to content

Commit

Permalink
Merge pull request #114 from Build-Squad/logging-improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
turbolent authored Jun 6, 2023
2 parents cd0fb41 + 32fca2c commit cda82f7
Show file tree
Hide file tree
Showing 4 changed files with 603 additions and 111 deletions.
33 changes: 21 additions & 12 deletions test/emulator_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ type EmulatorBackend struct {
configuration *stdlib.Configuration

stdlibHandler stdlib.StandardLibraryHandler

// logCollection is a hook attached in the server logger, in order
// to aggregate and expose log messages from the blockchain.
logCollection *LogCollectionHook
}

type keyInfo struct {
Expand Down Expand Up @@ -112,14 +116,16 @@ func NewEmulatorBackend(
stdlibHandler stdlib.StandardLibraryHandler,
coverageReport *runtime.CoverageReport,
) *EmulatorBackend {
logCollectionHook := NewLogCollectionHook()
var blockchain *emulator.Blockchain
if coverageReport != nil {
excludeCommonLocations(coverageReport)
blockchain = newBlockchain(
logCollectionHook,
emulator.WithCoverageReport(coverageReport),
)
} else {
blockchain = newBlockchain()
blockchain = newBlockchain(logCollectionHook)
}

return &EmulatorBackend{
Expand All @@ -129,6 +135,7 @@ func NewEmulatorBackend(
fileResolver: fileResolver,
configuration: baseConfiguration(),
stdlibHandler: stdlibHandler,
logCollection: logCollectionHook,
}
}

Expand Down Expand Up @@ -430,17 +437,24 @@ func (e *EmulatorBackend) ReadFile(path string) (string, error) {
return e.fileResolver(path)
}

// Logs returns all the log messages from the blockchain.
func (e *EmulatorBackend) Logs() []string {
return e.logCollection.Logs
}

// newBlockchain returns an emulator blockchain for testing.
func newBlockchain(opts ...emulator.Option) *emulator.Blockchain {
func newBlockchain(
hook *LogCollectionHook,
opts ...emulator.Option,
) *emulator.Blockchain {
output := zerolog.ConsoleWriter{Out: os.Stdout}
logger := zerolog.New(output).With().Timestamp().Logger().Hook(hook)

b, err := emulator.New(
append(
[]emulator.Option{
emulator.WithStorageLimitEnabled(false),
emulator.WithServerLogger(
zerolog.New(
zerolog.ConsoleWriter{Out: os.Stdout},
).With().Timestamp().Logger(),
),
emulator.WithServerLogger(logger),
emulator.Contracts(emulator.CommonContracts),
},
opts...,
Expand Down Expand Up @@ -520,11 +534,6 @@ func (e *EmulatorBackend) Reset() {
e.blockOffset = 0
}

func (e *EmulatorBackend) Logs() []string {
// TODO
return nil
}

func (e *EmulatorBackend) ServiceAccount() (*stdlib.Account, error) {
// TODO
return nil, errors.New("TODO")
Expand Down
2 changes: 1 addition & 1 deletion test/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/onflow/cadence-tools/test
go 1.18

require (
github.com/logrusorgru/aurora v2.0.3+incompatible
github.com/onflow/cadence v0.39.1
github.com/onflow/flow-emulator v0.50.1
github.com/onflow/flow-go v0.30.1-0.20230602200347-2a0e824db380
Expand Down Expand Up @@ -69,7 +70,6 @@ require (
github.com/libp2p/go-buffer-pool v0.1.0 // indirect
github.com/libp2p/go-libp2p v0.24.2 // indirect
github.com/libp2p/go-openssl v0.1.0 // indirect
github.com/logrusorgru/aurora v2.0.3+incompatible // indirect
github.com/logrusorgru/aurora/v4 v4.0.0 // indirect
github.com/m4ksio/wal v1.0.1-0.20221209164835-154a17396e4c // indirect
github.com/magiconair/properties v1.8.6 // indirect
Expand Down
Loading

0 comments on commit cda82f7

Please sign in to comment.