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

feat: silent flag in .air.toml, suppressing all air prints #641

Merged
merged 2 commits into from
Sep 25, 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: 2 additions & 0 deletions air_example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ rerun_delay = 500
time = false
# Only show main log (silences watcher, build, runner)
main_only = false
# silence all logs produced by air
silent = false

[color]
# Customize each part's color. If no color found, use the raw app log.
Expand Down
15 changes: 10 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import (
"flag"
"fmt"
"log"

Check failure on line 6 in main.go

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest)

undefined: Println

Check failure on line 6 in main.go

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest)

undefined: Println

Check failure on line 6 in main.go

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest)

undefined: Println

Check failure on line 6 in main.go

View workflow job for this annotation

GitHub Actions / Build (macos-latest)

undefined: Println

Check failure on line 6 in main.go

View workflow job for this annotation

GitHub Actions / Build (macos-latest)

undefined: Println

Check failure on line 6 in main.go

View workflow job for this annotation

GitHub Actions / Build (macos-latest)

undefined: Println
"os"
"os/signal"
"runtime"
Expand Down Expand Up @@ -70,22 +70,21 @@
}
}

func main() {
func printSplash() {

Check warning on line 73 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L73

Added line #L73 was not covered by tests
versionInfo := GetVersionInfo()
fmt.Printf(`
__ _ ___
/ /\ | | | |_)
/_/--\ |_| |_| \_ %s, built with Go %s

`, versionInfo.airVersion, versionInfo.goVersion)
}

func main() {

Check warning on line 83 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L83

Added line #L83 was not covered by tests
if showVersion {
printSplash()

Check warning on line 85 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L85

Added line #L85 was not covered by tests
return
}

if debugMode {
fmt.Println("[debug] mode")
}
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)

Expand All @@ -96,6 +95,12 @@
return
}
cfg.WithArgs(cmdArgs)
if !cfg.Log.Silent {
printSplash()

Check warning on line 99 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L98-L99

Added lines #L98 - L99 were not covered by tests
}
if debugMode && !cfg.Log.Silent {
fmt.Println("[debug] mode")

Check warning on line 102 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L101-L102

Added lines #L101 - L102 were not covered by tests
}
r, err := runner.NewEngineWithConfig(cfg, debugMode)
if err != nil {
log.Fatal(err)
Expand Down
7 changes: 3 additions & 4 deletions runner/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ func (c *cfgBuild) RegexCompiled() ([]*regexp.Regexp, error) {
type cfgLog struct {
AddTime bool `toml:"time"`
MainOnly bool `toml:"main_only"`
Silent bool `toml:"silent"`
}

type cfgColor struct {
Expand Down Expand Up @@ -186,7 +187,7 @@ func defaultPathConfig() (*Config, error) {
for _, name := range []string{dftTOML, dftConf} {
cfg, err := readConfByName(name)
if err == nil {
if name == dftConf {
if name == dftConf && !cfg.Log.Silent {
fmt.Println("`.air.conf` will be deprecated soon, recommend using `.air.toml`.")
}
return cfg, nil
Expand Down Expand Up @@ -237,6 +238,7 @@ func defaultConfig() Config {
log := cfgLog{
AddTime: false,
MainOnly: false,
Silent: false,
}
color := cfgColor{
Main: "magenta",
Expand Down Expand Up @@ -305,9 +307,6 @@ func (c *Config) preprocess() error {
if c.TestDataDir == "" {
c.TestDataDir = "testdata"
}
if err != nil {
return err
}
ed := c.Build.ExcludeDir
for i := range ed {
ed[i] = cleanPath(ed[i])
Expand Down
3 changes: 3 additions & 0 deletions runner/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@
return func(msg string, v ...interface{}) {
// There are some escape sequences to format color in terminal, so cannot
// just trim new line from right.
if cfg.Silent {
return

Check warning on line 57 in runner/logger.go

View check run for this annotation

Codecov / codecov/patch

runner/logger.go#L57

Added line #L57 was not covered by tests
}
msg = strings.ReplaceAll(msg, "\n", "")
msg = strings.TrimSpace(msg)
if len(msg) == 0 {
Expand Down
18 changes: 18 additions & 0 deletions runner/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,27 @@
)

func (e *Engine) mainLog(format string, v ...interface{}) {
if e.config.Log.Silent {
return

Check warning on line 25 in runner/util.go

View check run for this annotation

Codecov / codecov/patch

runner/util.go#L25

Added line #L25 was not covered by tests
}
e.logWithLock(func() {
e.logger.main()(format, v...)
})
}

func (e *Engine) mainDebug(format string, v ...interface{}) {
if e.config.Log.Silent {
return

Check warning on line 34 in runner/util.go

View check run for this annotation

Codecov / codecov/patch

runner/util.go#L34

Added line #L34 was not covered by tests
}
if e.debugMode {
e.mainLog(format, v...)
}
}

func (e *Engine) buildLog(format string, v ...interface{}) {
if e.config.Log.Silent {
return

Check warning on line 43 in runner/util.go

View check run for this annotation

Codecov / codecov/patch

runner/util.go#L43

Added line #L43 was not covered by tests
}
if e.debugMode || !e.config.Log.MainOnly {
e.logWithLock(func() {
e.logger.build()(format, v...)
Expand All @@ -41,6 +50,9 @@
}

func (e *Engine) runnerLog(format string, v ...interface{}) {
if e.config.Log.Silent {
return

Check warning on line 54 in runner/util.go

View check run for this annotation

Codecov / codecov/patch

runner/util.go#L54

Added line #L54 was not covered by tests
}
if e.debugMode || !e.config.Log.MainOnly {
e.logWithLock(func() {
e.logger.runner()(format, v...)
Expand All @@ -49,6 +61,9 @@
}

func (e *Engine) watcherLog(format string, v ...interface{}) {
if e.config.Log.Silent {
return

Check warning on line 65 in runner/util.go

View check run for this annotation

Codecov / codecov/patch

runner/util.go#L65

Added line #L65 was not covered by tests
}
if e.debugMode || !e.config.Log.MainOnly {
e.logWithLock(func() {
e.logger.watcher()(format, v...)
Expand All @@ -57,6 +72,9 @@
}

func (e *Engine) watcherDebug(format string, v ...interface{}) {
if e.config.Log.Silent {
return

Check warning on line 76 in runner/util.go

View check run for this annotation

Codecov / codecov/patch

runner/util.go#L76

Added line #L76 was not covered by tests
}
if e.debugMode {
e.watcherLog(format, v...)
}
Expand Down
Loading