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

Bug/better progress logger #3540

Merged
merged 3 commits into from
Mar 14, 2024
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
2 changes: 1 addition & 1 deletion src/apps/chifra/internal/abis/handle_find.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (opts *AbisOptions) HandleAbiFind() error {
str, _ := hex.DecodeString(arg[2:])
if bytes.Equal(sigBytes[:len(str)], str) {
scanBar.Found++
logger.Progress(!testMode || len(opts.Find) < 2, "Found", scanBar.Found, "of", scanBar.Wanted, arg, testSig)
logger.Progress(len(opts.Find) < 2, "Found", scanBar.Found, "of", scanBar.Wanted, arg, testSig)
found := types.SimpleFunction{Encoding: arg, Signature: testSig.(string)}
if testMode {
mutex.Lock()
Expand Down
5 changes: 3 additions & 2 deletions src/apps/chifra/internal/abis/handle_many.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ func (opts *AbisOptions) HandleMany() (err error) {
for _, addr := range opts.Addrs {
abiCache := articulate.NewAbiCache(opts.Conn, opts.Known)
address := base.HexToAddress(addr)
if len(opts.ProxyFor) > 0 {
address = base.HexToAddress(opts.ProxyFor)
proxy := base.HexToAddress(opts.ProxyFor)
if !proxy.IsZero() {
address = proxy
}
err = abi.LoadAbi(opts.Conn, address, &abiCache.AbiMap)
if err != nil {
Expand Down
5 changes: 3 additions & 2 deletions src/apps/chifra/internal/abis/handle_show.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ func (opts *AbisOptions) HandleShow() (err error) {
// Note here, that known ABIs are not downloaded. They are only loaded from the local cache.
for _, addr := range opts.Addrs {
address := base.HexToAddress(addr)
if len(opts.ProxyFor) > 0 {
address = base.HexToAddress(opts.ProxyFor)
proxy := base.HexToAddress(opts.ProxyFor)
if !proxy.IsZero() {
address = proxy
}
err = abi.LoadAbi(opts.Conn, address, &abiCache.AbiMap)
if err != nil {
Expand Down
6 changes: 4 additions & 2 deletions src/apps/chifra/internal/abis/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
package abisPkg

import (
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/base"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/config"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/validate"
)

func (opts *AbisOptions) validateAbis() error {
chain := opts.Globals.Chain
proxy := base.HexToAddress(opts.ProxyFor)

opts.testLog()

Expand All @@ -35,7 +37,7 @@ func (opts *AbisOptions) validateAbis() error {
if opts.Known {
return validate.Usage("Please choose only one of {0}.", "--decache or --known")
}
if len(opts.ProxyFor) > 0 {
if !proxy.IsZero() {
return validate.Usage("Please choose only one of {0}.", "--decache or --proxy_for")
}
}
Expand All @@ -52,7 +54,7 @@ func (opts *AbisOptions) validateAbis() error {
return validate.Usage("Please choose only one of {0}.", "--find or --encode")
}

if len(opts.Addrs) != 1 && len(opts.ProxyFor) > 0 {
if len(opts.Addrs) != 1 && !proxy.IsZero() {
return validate.Usage("The {0} option requires exactly one address.", "--proxy_for")
}

Expand Down
4 changes: 2 additions & 2 deletions src/apps/chifra/internal/globals/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
package globals

import (
"fmt"
"strconv"

"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/file"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/logger"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/validate"
)

Expand Down Expand Up @@ -47,7 +47,7 @@ func (opts *GlobalOptions) Validate() error {

// TODO: This hack is here to make test cases pass. It can be removed at some point
if opts.Format == "json" && len(opts.OutputFn) > 0 && opts.TestMode {
fmt.Println("{ \"outputFilename\": \"--output_filename--\" }")
logger.Info("{ \"outputFilename\": \"--output_filename--\" }")
}

if opts.Cache && opts.Decache {
Expand Down
2 changes: 1 addition & 1 deletion src/apps/chifra/internal/slurp/handle_appearances.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (opts *SlurpOptions) HandleAppearances() error {
sleep := opts.Sleep
if sleep > 0 {
ms := time.Duration(sleep*1000) * time.Millisecond
logger.Progress(!opts.Globals.TestMode, fmt.Sprintf("Sleeping for %g seconds", sleep))
logger.Progress(true, fmt.Sprintf("Sleeping for %g seconds", sleep))
time.Sleep(ms)
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/apps/chifra/internal/slurp/handle_appearances_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (opts *SlurpOptions) HandleAppearancesKey() error {
sleep := opts.Sleep
if sleep > 0 {
ms := time.Duration(sleep*1000) * time.Millisecond
logger.Progress(!opts.Globals.TestMode, fmt.Sprintf("Sleeping for %g seconds", sleep))
logger.Progress(true, fmt.Sprintf("Sleeping for %g seconds", sleep))
time.Sleep(ms)
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/apps/chifra/internal/slurp/handle_count.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (opts *SlurpOptions) HandleCount() error {
sleep := opts.Sleep
if sleep > 0 {
ms := time.Duration(sleep*1000) * time.Millisecond
logger.Progress(!opts.Globals.TestMode, fmt.Sprintf("Sleeping for %g seconds", sleep))
logger.Progress(true, fmt.Sprintf("Sleeping for %g seconds", sleep))
time.Sleep(ms)
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/apps/chifra/internal/slurp/handle_show.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (opts *SlurpOptions) HandleShow() error {
sleep := opts.Sleep
if sleep > 0 {
ms := time.Duration(sleep*1000) * time.Millisecond
logger.Progress(!opts.Globals.TestMode, fmt.Sprintf("Sleeping for %g seconds", sleep))
logger.Progress(true, fmt.Sprintf("Sleeping for %g seconds", sleep))
time.Sleep(ms)
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/apps/chifra/internal/state/handle_call.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ func (opts *StateOptions) HandleCall() error {
}

callAddress := base.HexToAddress(opts.Addrs[0])
if opts.ProxyFor != "" {
callAddress = base.HexToAddress(opts.ProxyFor)
proxy := base.HexToAddress(opts.ProxyFor)
if !proxy.IsZero() {
callAddress = proxy
}

ctx, cancel := context.WithCancel(context.Background())
Expand Down
5 changes: 3 additions & 2 deletions src/apps/chifra/internal/state/handle_decache.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ func (opts *StateOptions) HandleDecache() error {
address := base.HexToAddress(addressStr)
if len(opts.Call) > 0 {
callAddress := base.HexToAddress(opts.Addrs[0])
if opts.ProxyFor != "" {
callAddress = base.HexToAddress(opts.ProxyFor)
proxy := base.HexToAddress(opts.ProxyFor)
if !proxy.IsZero() {
callAddress = proxy
}
if contractCall, _, err := call.NewContractCall(opts.Conn, callAddress, opts.Call); err != nil {
wrapped := fmt.Errorf("the --call value provided (%s) was not found: %s", opts.Call, err)
Expand Down
11 changes: 6 additions & 5 deletions src/apps/chifra/internal/state/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (

func (opts *StateOptions) validateState() error {
chain := opts.Globals.Chain
proxy := base.HexToAddress(opts.ProxyFor)

opts.testLog()

Expand Down Expand Up @@ -60,8 +61,8 @@ func (opts *StateOptions) validateState() error {
}

contract := opts.Addrs[0]
if len(opts.ProxyFor) > 0 {
contract = opts.ProxyFor
if !proxy.IsZero() {
contract = proxy.Hex()
}

err := opts.Conn.IsContractAt(base.HexToAddress(contract), nil)
Expand All @@ -74,8 +75,8 @@ func (opts *StateOptions) validateState() error {

// Before we do anythinng, let's just make sure we have a valid four-byte
callAddress := base.HexToAddress(opts.Addrs[0])
if opts.ProxyFor != "" {
callAddress = base.HexToAddress(opts.ProxyFor)
if !proxy.IsZero() {
callAddress = proxy
}
// TODO: Can't we preserve the results of this so we don't have to do it later?
if _, suggestions, err := call.NewContractCall(opts.Conn, callAddress, opts.Call); err != nil {
Expand All @@ -99,7 +100,7 @@ func (opts *StateOptions) validateState() error {
return validate.Usage("The {0} option is only available with the {1} option.", "--articulate", "--call")
}

if len(opts.ProxyFor) > 0 {
if !proxy.IsZero() {
return validate.Usage("The {0} option is only available with the {1} option.", "--proxy_for", "--call")
}

Expand Down
48 changes: 31 additions & 17 deletions src/apps/chifra/pkg/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package logger

import (
"fmt"
"io"
"log"
"math"
"os"
Expand Down Expand Up @@ -41,6 +42,11 @@ var (
testMode = false
)

func SetTestMode(onOff bool) {
testMode = onOff
testModeSet = true
}

// TestLog is used to print log lines during testing only
func TestLog(notDefault bool, a ...interface{}) {
if !testModeSet {
Expand All @@ -58,6 +64,7 @@ func TestLog(notDefault bool, a ...interface{}) {
}

var (
isTestMode = false
timingModeSet = false
timingMode = true
decorationOff = false
Expand All @@ -71,13 +78,20 @@ func ToggleDecoration() {
decorationOff = !decorationOff
}

var loggerWriter io.Writer = nil

func init() {
if !timingModeSet {
on := os.Getenv("TB_LOGTIMER_OFF") == ""
testing := os.Getenv("TEST_MODE") == "true"
timingMode = on && !testing
isTestMode = os.Getenv("TEST_MODE") == "true"
timingMode = on && !isTestMode
timingModeSet = true
}
loggerWriter = os.Stderr
}

func SetLoggerWriter(w io.Writer) {
loggerWriter = w
}

// toLog prints `a` to stderr with a label corresponding to the severity level
Expand All @@ -90,36 +104,36 @@ func toLog(sev severity, a ...interface{}) {
}

if !decorationOff {
fmt.Fprintf(os.Stderr, "%s[%s] ", severityToLabel[sev], timeDatePart)
fmt.Fprintf(loggerWriter, "%s[%s] ", severityToLabel[sev], timeDatePart)
}
if sev == progress {
for index, aa := range a {
if index > 0 {
fmt.Fprint(os.Stderr, " ")
fmt.Fprint(loggerWriter, " ")
}
fmt.Fprint(os.Stderr, aa)
fmt.Fprint(loggerWriter, aa)
}
fmt.Fprint(os.Stderr, "\r")
fmt.Fprint(loggerWriter, "\r")

} else if sev == infoC {
fmt.Fprintf(os.Stderr, "%s%s%s ", colors.Green, a[0], colors.Off)
fmt.Fprintf(loggerWriter, "%s%s%s ", colors.Green, a[0], colors.Off)
for _, aa := range a[1:] {
fmt.Fprintf(os.Stderr, "%s", aa)
fmt.Fprintf(loggerWriter, "%s", aa)
}
fmt.Fprintln(os.Stderr, "")
fmt.Fprintln(loggerWriter, "")

} else if sev == warning {
defer fmt.Fprint(os.Stderr, colors.Off)
fmt.Fprint(os.Stderr, colors.Yellow)
fmt.Fprintln(os.Stderr, a...)
defer fmt.Fprint(loggerWriter, colors.Off)
fmt.Fprint(loggerWriter, colors.Yellow)
fmt.Fprintln(loggerWriter, a...)

} else if sev == err {
defer fmt.Fprint(os.Stderr, colors.Off)
fmt.Fprint(os.Stderr, colors.Red)
fmt.Fprintln(os.Stderr, a...)
defer fmt.Fprint(loggerWriter, colors.Off)
fmt.Fprint(loggerWriter, colors.Red)
fmt.Fprintln(loggerWriter, a...)

} else {
fmt.Fprintln(os.Stderr, a...)
fmt.Fprintln(loggerWriter, a...)
}
}

Expand Down Expand Up @@ -148,7 +162,7 @@ func Panic(v ...any) {
}

func Progress(tick bool, v ...any) {
if !utils.IsTerminal() || !tick {
if isTestMode || !utils.IsTerminal() || !tick {
return
}
toLog(progress, v...)
Expand Down
2 changes: 1 addition & 1 deletion src/apps/chifra/pkg/rpc/get_slurp_etherscan.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func getEtherscanUrl(chain, value string, requestType string, paginator *Paginat
}

if actions[requestType] == "" {
logger.Fatal("should not happen ==> in getEtherscanUrl", requestType)
return "", fmt.Errorf("should not happen (%s) ==> in getEtherscanUrl", requestType)
}

key := config.GetKey("etherscan").ApiKey
Expand Down
6 changes: 3 additions & 3 deletions src/dev_tools/testRunner/testCases/apps/acctExport.csv
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ on ,both ,fast ,list ,apps/acctExport ,caps_allowed_l ,y
on ,both ,fast ,list ,apps/acctExport ,caps_disallowed_1_l ,y ,addrs = trueblocks.eth & cache
on ,both ,fast ,list ,apps/acctExport ,caps_disallowed_2_l ,y ,addrs = trueblocks.eth & raw
on ,both ,fast ,list ,apps/acctExport ,caps_disallowed_3_l ,y ,addrs = trueblocks.eth & ether
on ,both ,fast ,list ,apps/acctExport ,caps_disallowed_4_l ,y ,addrs = trueblocks.eth & wei
on ,both ,fast ,list ,apps/acctExport ,caps_disallowed_4_l ,y ,addrs = trueblocks.eth & wei & max_records = 10
on ,both ,fast ,list ,apps/acctExport ,caps_disallowed_5_l ,y ,addrs = trueblocks.eth & decache

# Not tested for chifra list
Expand Down Expand Up @@ -320,5 +320,5 @@ statbad ,both ,fast ,export ,apps/acctExport ,statement_forward ,y
# Capabilities
# chain & fmt & help & nocolor & noop & version & verbose & no_header & file & output & append & cache & decache & raw & ether
on ,both ,fast ,export ,apps/acctExport ,caps_allowed_e ,y ,addrs = trueblocks.eth & chain & fmt & nocolor & noop & version & verbose & no_header & file & output & append & cache & decache & ether & fail_on_purpose
on ,both ,fast ,export ,apps/acctExport ,caps_disallowed_1_e ,y ,addrs = trueblocks.eth & raw
on ,both ,fast ,export ,apps/acctExport ,caps_disallowed_2_e ,y ,addrs = trueblocks.eth & wei
on ,both ,fast ,export ,apps/acctExport ,caps_disallowed_1_e ,y ,addrs = trueblocks.eth & raw & max_records = 3
on ,both ,fast ,export ,apps/acctExport ,caps_disallowed_2_e ,y ,addrs = trueblocks.eth & wei & max_records = 3
2 changes: 1 addition & 1 deletion src/dev_tools/utillib/options_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -806,7 +806,7 @@ void COptionsBase::closeRedirect(void) {
// only report if the user isn't in API mode
break;
default:
cout << "{ \"outputFilename\": \"" << outFn << "\" }";
cerr << "{ \"outputFilename\": \"" << outFn << "\" }";
break;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
chifra export trueblocks.eth --raw
chifra export trueblocks.eth --raw --max_records 3
Error:
unknown flag: --raw

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
chifra export trueblocks.eth --wei
chifra export trueblocks.eth --wei --max_records 3
Error:
unknown flag: --wei

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
chifra list trueblocks.eth --wei
chifra list trueblocks.eth --wei --max_records 10
Error:
unknown flag: --wei

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export?addrs=trueblocks.eth&raw
export?addrs=trueblocks.eth&raw&maxRecords=3
{
"errors": [
"Invalid key (raw) in export route."
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export?addrs=trueblocks.eth&wei
export?addrs=trueblocks.eth&wei&maxRecords=3
{
"errors": [
"Invalid key (wei) in export route."
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
list?addrs=trueblocks.eth&wei
list?addrs=trueblocks.eth&wei&maxRecords=10
{
"errors": [
"Invalid key (wei) in list route."
Expand Down
2 changes: 1 addition & 1 deletion test/gold/apps/cacheStatus/cacheStatus_redir_output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ TEST[DATE|TIME] Modes: [names]
TEST[DATE|TIME] MaxRecords: 2
TEST[DATE|TIME] OutputFn: output_test_file
TEST[DATE|TIME] Format: json
{ "outputFilename": "--output_filename--" }
INFO[DATE|TIME] { "outputFilename": "--output_filename--" }
----
Results in ./output_test_file
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ TEST[DATE|TIME] NoHeader: true
TEST[DATE|TIME] OutputFn: output_test_file
TEST[DATE|TIME] Append: true
TEST[DATE|TIME] Format: json
{ "outputFilename": "--output_filename--" }
INFO[DATE|TIME] { "outputFilename": "--output_filename--" }
----
Results in ./output_test_file
{
Expand Down
2 changes: 1 addition & 1 deletion test/gold/tools/ethNames/ethNames_redir_output_json.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ chifra names 0x1234 --output output_file.json
TEST[DATE|TIME] Terms: [0x1234]
TEST[DATE|TIME] OutputFn: output_file.json
TEST[DATE|TIME] Format: json
{ "outputFilename": "--output_filename--" }
INFO[DATE|TIME] { "outputFilename": "--output_filename--" }
----
Results in ./output_file.json
{
Expand Down
Loading
Loading