Skip to content

Commit b13cb68

Browse files
committed
refactor: faster log
refactor: remove dns check in init.d
1 parent 1ab23e4 commit b13cb68

22 files changed

+209
-219
lines changed

build_linux.ps1

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
$Env:GOOS="linux"
2+
go build

cmd/bounce.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package cmd
22

33
import (
44
"github.com/hdu-dn11/wg-quick-op/quick"
5-
"github.com/sirupsen/logrus"
5+
"github.com/rs/zerolog/log"
66
"github.com/spf13/cobra"
77
)
88

@@ -13,23 +13,23 @@ var bounceCmd = &cobra.Command{
1313
Long: `down and then up the interface,if the interface is not up, it will up the interface.`,
1414
Run: func(cmd *cobra.Command, args []string) {
1515
if len(args) != 1 {
16-
logrus.Errorln("bounce command requires exactly one interface name")
16+
log.Error().Msg("bounce command requires exactly one interface name")
1717
return
1818
}
1919
cfgs := quick.MatchConfig(args[0])
2020
for iface, cfg := range cfgs {
21-
err := quick.Down(cfg, iface, logrus.WithField("iface", iface))
21+
err := quick.Down(cfg, iface, log.With().Str("iface", iface).Logger())
2222
if err != nil {
23-
logrus.WithError(err).WithField("iface", iface).Errorln("failed to down interface")
23+
log.Err(err).Str("iface", iface).Msg("failed to down interface")
2424
}
2525
}
2626
for iface, cfg := range cfgs {
27-
err := quick.Up(cfg, iface, logrus.WithField("iface", iface))
27+
err := quick.Up(cfg, iface, log.With().Str("iface", iface).Logger())
2828
if err != nil {
29-
logrus.WithError(err).WithField("iface", iface).Errorln("failed to up interface")
29+
log.Err(err).Str("iface", iface).Msg("failed to up interface")
3030
}
3131
}
32-
logrus.Infoln("bounce done")
32+
log.Info().Msg("bounce done")
3333
},
3434
}
3535

cmd/down.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ package cmd
22

33
import (
44
"github.com/hdu-dn11/wg-quick-op/quick"
5-
"github.com/sirupsen/logrus"
6-
5+
"github.com/rs/zerolog/log"
76
"github.com/spf13/cobra"
87
)
98

@@ -13,14 +12,14 @@ var downCmd = &cobra.Command{
1312
Short: "down [interface name]",
1413
Run: func(cmd *cobra.Command, args []string) {
1514
if len(args) != 1 {
16-
logrus.Errorln("up command requires exactly one interface name")
15+
log.Error().Msg("up command requires exactly one interface name")
1716
return
1817
}
1918
cfgs := quick.MatchConfig(args[0])
2019
for iface, cfg := range cfgs {
21-
err := quick.Down(cfg, iface, logrus.WithField("iface", iface))
20+
err := quick.Down(cfg, iface, log.With().Str("iface", iface).Logger())
2221
if err != nil {
23-
logrus.WithError(err).Errorln("failed to up interface")
22+
log.Err(err).Msg("failed to up interface")
2423
}
2524
}
2625
},

cmd/root.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package cmd
33
import (
44
"github.com/hdu-dn11/wg-quick-op/conf"
55
"github.com/hdu-dn11/wg-quick-op/lib/dns"
6-
"github.com/sirupsen/logrus"
6+
"github.com/rs/zerolog"
77
"os"
88

99
"github.com/spf13/cobra"
@@ -31,7 +31,7 @@ func init() {
3131
rootCmd.PersistentPreRun = func(cmd *cobra.Command, args []string) {
3232
verbose, _ := cmd.Flags().GetBool("verbose")
3333
if verbose {
34-
logrus.SetLevel(logrus.DebugLevel)
34+
zerolog.SetGlobalLevel(zerolog.TraceLevel)
3535
}
3636
conf.Init(config)
3737
dns.Init()

cmd/sync.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ package cmd
22

33
import (
44
"github.com/hdu-dn11/wg-quick-op/quick"
5-
"github.com/sirupsen/logrus"
6-
5+
"github.com/rs/zerolog/log"
76
"github.com/spf13/cobra"
87
)
98

@@ -15,14 +14,14 @@ var syncCmd = &cobra.Command{
1514
it may result in address added by PostUp being deleted.'`,
1615
Run: func(cmd *cobra.Command, args []string) {
1716
if len(args) != 1 {
18-
logrus.Errorln("up command requires exactly one interface name")
17+
log.Error().Msg("up command requires exactly one interface name")
1918
return
2019
}
2120
cfgs := quick.MatchConfig(args[0])
2221
for iface, cfg := range cfgs {
23-
err := quick.Sync(cfg, iface, logrus.WithField("iface", iface))
22+
err := quick.Sync(cfg, iface, log.With().Str("iface", iface).Logger())
2423
if err != nil {
25-
logrus.WithError(err).Errorln("failed to sync interface")
24+
log.Err(err).Msg("failed to sync interface")
2625
}
2726
}
2827
},

cmd/uninstall.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"github.com/spf13/cobra"
77
)
88

9-
// uninstallCmd represents the uninstall command
9+
// uninstallCmd represents the uninstallation command
1010
var uninstallCmd = &cobra.Command{
1111
Use: "uninstall",
1212
Short: "uninstall wg-quick-op from /usr/sbin/wg-quick-op",

cmd/up.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package cmd
22

33
import (
44
"github.com/hdu-dn11/wg-quick-op/quick"
5-
"github.com/sirupsen/logrus"
5+
"github.com/rs/zerolog/log"
66
"github.com/spf13/cobra"
77
)
88

@@ -16,14 +16,14 @@ regexp in supported, match interface with ^<input>$ by default
1616
`,
1717
Run: func(cmd *cobra.Command, args []string) {
1818
if len(args) != 1 {
19-
logrus.Errorln("up command requires exactly one interface name")
19+
log.Error().Msg("up command requires exactly one interface name")
2020
return
2121
}
2222
cfgs := quick.MatchConfig(args[0])
2323
for iface, cfg := range cfgs {
24-
err := quick.Up(cfg, iface, logrus.WithField("iface", iface))
24+
err := quick.Up(cfg, iface, log.With().Str("iface", iface).Logger())
2525
if err != nil {
26-
logrus.WithError(err).Errorln("failed to up interface")
26+
log.Err(err).Msg("failed to up interface")
2727
}
2828
}
2929
},

conf/config.go

+10-9
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ package conf
33
import (
44
_ "embed"
55
"github.com/fsnotify/fsnotify"
6-
"github.com/sirupsen/logrus"
6+
"github.com/rs/zerolog"
7+
"github.com/rs/zerolog/log"
78
"github.com/spf13/viper"
89
"os"
910
"time"
@@ -33,21 +34,21 @@ var EnhancedDNS struct {
3334
}
3435

3536
var Log struct {
36-
Level logrus.Level
37+
Level zerolog.Level
3738
}
3839

3940
func Init(file string) {
4041
if _, err := os.Stat(file); err != nil {
4142
if !os.IsNotExist(err) {
42-
logrus.WithError(err).Fatalf("get stat of %s failed", file)
43+
log.Fatal().Err(err).Msgf("get stat of %s failed", file)
4344
}
44-
logrus.Infof("config not existed, creating at %s", file)
45+
log.Info().Msgf("config not existed, creating at %s", file)
4546
created, err := os.Create(file)
4647
if err != nil {
47-
logrus.WithError(err).Fatalf("create config at %s failed", file)
48+
log.Fatal().Err(err).Msgf("create config at %s failed", file)
4849
}
4950
if _, err := created.Write(configSample); err != nil {
50-
logrus.WithError(err).Fatalf("write config at %s failed", file)
51+
log.Fatal().Err(err).Msgf("write config at %s failed", file)
5152
}
5253
}
5354

@@ -59,7 +60,7 @@ func Init(file string) {
5960

6061
update()
6162
if err != nil {
62-
logrus.WithError(err).Fatalf("read config from %s failed", file)
63+
log.Fatal().Err(err).Msgf("read config from %s failed", file)
6364
}
6465

6566
viper.OnConfigChange(func(e fsnotify.Event) {
@@ -81,8 +82,8 @@ func update() {
8182
EnhancedDNS.DirectResolver.Enabled = viper.GetBool("enhanced_dns.direct_resolver.enabled")
8283
EnhancedDNS.DirectResolver.ROAFinder = viper.GetString("enhanced_dns.direct_resolver.roa_finder")
8384

84-
if level, err := logrus.ParseLevel(viper.GetString("log.level")); err == nil {
85+
if level, err := zerolog.ParseLevel(viper.GetString("log.level")); err == nil {
8586
Log.Level = level
86-
logrus.SetLevel(level)
87+
zerolog.SetGlobalLevel(level)
8788
}
8889
}

daemon/binary.go

+12-12
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package daemon
22

33
import (
44
"errors"
5-
"github.com/sirupsen/logrus"
5+
"github.com/rs/zerolog/log"
66
"io"
77
"os"
88
"os/exec"
@@ -15,60 +15,60 @@ const installed = "/usr/sbin/wg-quick-op"
1515
func Install() {
1616
file, err := exec.LookPath(os.Args[0])
1717
if err != nil && !errors.Is(err, exec.ErrDot) {
18-
logrus.WithError(err).Errorln("fetch current binary path failed")
18+
log.Err(err).Msg("fetch current binary path failed")
1919
return
2020
}
2121

2222
absFile, err := filepath.Abs(file)
2323
if err != nil {
24-
logrus.WithField("path", absFile).WithError(err).Errorln("The absPath failed")
24+
log.Err(err).Str("path", absFile).Msg("The absPath failed")
2525
return
2626
}
27-
logrus.Infof("current binary: %v", absFile)
27+
log.Info().Msgf("current binary: %v", absFile)
2828

2929
originFp, err := os.Open(absFile)
3030
if err != nil {
31-
logrus.WithError(err).Errorf("open current binary failed")
31+
log.Err(err).Msgf("open current binary failed")
3232
return
3333
}
3434
defer originFp.Close()
3535

3636
if _, err := os.Stat(installed); err != nil {
3737
if !os.IsNotExist(err) {
38-
logrus.WithError(err).Errorf("fetch binary stat failed")
38+
log.Err(err).Msgf("fetch binary stat failed")
3939
return
4040
}
4141
} else {
4242
if err := os.RemoveAll(installed); err != nil {
43-
logrus.WithError(err).Errorf("remove old binary failed")
43+
log.Err(err).Msgf("remove old binary failed")
4444
return
4545
}
4646
}
4747

4848
fp, err := os.OpenFile(installed, os.O_CREATE|os.O_RDWR, os.ModePerm)
4949
if err != nil {
50-
logrus.WithError(err).Errorf("cannot write to %v", installed)
50+
log.Err(err).Msgf("write to %v", installed)
5151
return
5252
}
5353
defer fp.Close()
5454
_, err = io.Copy(fp, originFp)
5555
if err != nil {
5656
_ = os.RemoveAll(installed)
57-
logrus.Errorf("copy binary to %v failed: %s", installed, err)
57+
log.Err(err).Msgf("copy binary to %s", installed)
5858
return
5959
}
60-
logrus.Infof("installed wg-quick-op")
60+
log.Info().Msg("installed wg-quick-op")
6161
}
6262

6363
func Uninstall() {
6464
file, err := exec.LookPath("wg-quick-op")
6565
if err != nil {
66-
logrus.WithError(err).Errorln("find wg-quick-op failed")
66+
log.Err(err).Msg("find wg-quick-op failed")
6767
return
6868
}
6969

7070
if err := os.RemoveAll(file); err != nil {
71-
logrus.WithField("path", file).WithError(err).Errorln("remove binary failed")
71+
log.Err(err).Str("path", file).Msg("remove binary failed")
7272
return
7373
}
7474
}

0 commit comments

Comments
 (0)