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

Support NoColor and --no-color #222

Merged
merged 5 commits into from
Sep 25, 2023
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
12 changes: 12 additions & 0 deletions cmd/fuzz_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ func addFuzzFlags() error {
// Trace all
fuzzCmd.Flags().Bool("trace-all", false,
fmt.Sprintf("print the execution trace for every element in a shrunken call sequence instead of only the last element (unless a config file is provided, default is %t)", defaultConfig.Fuzzing.Testing.TraceAll))

// Logging color
fuzzCmd.Flags().Bool("no-color", false, "disabled colored terminal output")

return nil
}

Expand Down Expand Up @@ -176,5 +180,13 @@ func updateProjectConfigWithFuzzFlags(cmd *cobra.Command, projectConfig *config.
return err
}
}

// Update logging color mode
if cmd.Flags().Changed("no-color") {
projectConfig.Logging.NoColor, err = cmd.Flags().GetBool("no-color")
if err != nil {
return err
}
}
return nil
}
6 changes: 5 additions & 1 deletion fuzzing/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package config
import (
"encoding/json"
"errors"
"os"

"github.com/crytic/medusa/chain/config"
"github.com/rs/zerolog"
"os"

"github.com/crytic/medusa/compilation"
"github.com/crytic/medusa/utils"
Expand Down Expand Up @@ -189,6 +190,9 @@ type LoggingConfig struct {
// LogDirectory describes what directory log files should be outputted in/ LogDirectory being a non-empty string is
// equivalent to enabling file logging.
LogDirectory string `json:"logDirectory"`

// NoColor indicates whether or not log messages should be displayed with colored formatting.
NoColor bool `json:"noColor"`
}

// ConsoleLoggingConfig describes the configuration options for logging to console. Note that this not being used right now
Expand Down
1 change: 1 addition & 0 deletions fuzzing/config/config_defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ func GetDefaultProjectConfig(platform string) (*ProjectConfig, error) {
Logging: LoggingConfig{
Level: zerolog.InfoLevel,
LogDirectory: "",
NoColor: false,
},
}

Expand Down
17 changes: 11 additions & 6 deletions fuzzing/fuzzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ package fuzzing
import (
"context"
"fmt"
"github.com/crytic/medusa/fuzzing/coverage"
"github.com/crytic/medusa/logging"
"github.com/crytic/medusa/logging/colors"
"github.com/rs/zerolog"
"math/big"
"math/rand"
"os"
Expand All @@ -18,6 +14,11 @@ import (
"sync"
"time"

"github.com/crytic/medusa/fuzzing/coverage"
"github.com/crytic/medusa/logging"
"github.com/crytic/medusa/logging/colors"
"github.com/rs/zerolog"

"github.com/crytic/medusa/fuzzing/calls"
"github.com/crytic/medusa/utils/randomutils"
"github.com/ethereum/go-ethereum/core/types"
Expand Down Expand Up @@ -88,9 +89,13 @@ type Fuzzer struct {
// NewFuzzer returns an instance of a new Fuzzer provided a project configuration, or an error if one is encountered
// while initializing the code.
func NewFuzzer(config config.ProjectConfig) (*Fuzzer, error) {
// Create the global logger and add stdout as an unstructured, colored output stream
// Disable colors if requested
if config.Logging.NoColor {
colors.DisableColor()
}
// Create the global logger and add stdout as an unstructured output stream
logging.GlobalLogger = logging.NewLogger(config.Logging.Level)
logging.GlobalLogger.AddWriter(os.Stdout, logging.UNSTRUCTURED, true)
logging.GlobalLogger.AddWriter(os.Stdout, logging.UNSTRUCTURED, !config.Logging.NoColor)

// If the log directory is a non-empty string, create a file for unstructured, un-colorized file logging
if config.Logging.LogDirectory != "" {
Expand Down
14 changes: 12 additions & 2 deletions logging/colors/colorize_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,21 @@ package colors

import "fmt"

// EnableColor is a no-op function for non-windows systems because we know that they support ANSI escape codes
func EnableColor() {}
var enabled = true

// EnableColor enables the use of colors for non-windows systems.
func EnableColor() { enabled = true }

// DisableColor disables the use of colors for non-windows systems.
func DisableColor() { enabled = false }

// Colorize returns the string s wrapped in ANSI code c for non-windows systems
// Source: https://github.com/rs/zerolog/blob/4fff5db29c3403bc26dee9895e12a108aacc0203/console.go
func Colorize(s any, c Color) string {
// Return original string if explicitly disabled
if !enabled {
return fmt.Sprintf("%v", s)
}

return fmt.Sprintf("\x1b[%dm%v\x1b[0m", c, s)
}
6 changes: 5 additions & 1 deletion logging/colors/colorize_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ package colors

import (
"fmt"
"golang.org/x/sys/windows"
"os"

"golang.org/x/sys/windows"
)

var enabled bool
Expand Down Expand Up @@ -53,6 +54,9 @@ func EnableColor() {
}
}

// DisableColor will disable colors
func DisableColor() { enabled = false }

// Colorize returns the string s wrapped in ANSI code c assuming that ANSI is supported on the Windows version
// Source: https://github.com/rs/zerolog/blob/4fff5db29c3403bc26dee9895e12a108aacc0203/console.go
func Colorize(s any, c Color) string {
Expand Down
32 changes: 30 additions & 2 deletions logging/logger_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package logging

import (
"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
"bytes"
"fmt"
"os"
"strings"
"testing"

"github.com/crytic/medusa/logging/colors"
"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
)

// TestAddAndRemoveWriter will test to Logger.AddWriter and Logger.RemoveWriter functions to ensure that they work as expected.
Expand Down Expand Up @@ -45,3 +50,26 @@ func TestAddAndRemoveWriter(t *testing.T) {
assert.Equal(t, len(logger.unstructuredColorWriters), 0)
assert.Equal(t, len(logger.structuredWriters), 0)
}

// TestDisabledColors verifies the behavior of the unstructured colored logger when colors are disabled,
// ensuring that it does not output colors when the color feature is turned off.
func TestDisabledColors(t *testing.T) {
Exca-DK marked this conversation as resolved.
Show resolved Hide resolved
// Create a base logger
logger := NewLogger(zerolog.InfoLevel)

// Add colorized logger
var buf bytes.Buffer
logger.AddWriter(&buf, UNSTRUCTURED, true)

// We should expect the underlying data structures are correctly updated
assert.Equal(t, len(logger.unstructuredColorWriters), 1)

// Disable colors and log msg
colors.DisableColor()
logger.Info("foo")

// Ensure that msg doesn't include colors afterwards
prefix := fmt.Sprintf("%s %s", colors.LEFT_ARROW, "foo")
_, ok := strings.CutPrefix(buf.String(), prefix)
anishnaik marked this conversation as resolved.
Show resolved Hide resolved
assert.True(t, ok)
}