Refine log output

This commit is contained in:
世界
2022-07-04 16:45:32 +08:00
parent ca5b782106
commit 13f41f59d6
5 changed files with 159 additions and 55 deletions

View File

@@ -1,83 +1,45 @@
package main
import (
"context"
"os"
"os/signal"
"syscall"
"github.com/goccy/go-json"
"github.com/sagernet/sing-box"
"github.com/sagernet/sing-box/option"
"github.com/sagernet/sing-box/log"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
func init() {
logrus.StandardLogger().SetLevel(logrus.TraceLevel)
logrus.StandardLogger().Formatter.(*logrus.TextFormatter).ForceColors = true
logrus.StandardLogger().SetFormatter(&log.LogrusTextFormatter{})
}
var (
configPath string
workingDir string
formatConfig bool
disableColor bool
)
func main() {
command := &cobra.Command{
Use: "sing-box",
Run: run,
Use: "sing-box",
PersistentPreRun: preRun,
}
command.Flags().StringVarP(&configPath, "config", "c", "config.json", "set configuration file path")
command.Flags().StringVarP(&workingDir, "directory", "D", "", "set working directory")
command.Flags().BoolVarP(&formatConfig, "format", "f", false, "print formatted configuration file")
command.PersistentFlags().StringVarP(&configPath, "config", "c", "config.json", "set configuration file path")
command.PersistentFlags().StringVarP(&workingDir, "directory", "D", "", "set working directory")
command.PersistentFlags().BoolVarP(&disableColor, "disable-color", "", false, "disable color output")
command.AddCommand(commandRun)
if err := command.Execute(); err != nil {
logrus.Fatal(err)
}
}
func run(cmd *cobra.Command, args []string) {
func preRun(cmd *cobra.Command, args []string) {
if disableColor {
logrus.StandardLogger().SetFormatter(&log.LogrusTextFormatter{DisableColors: true})
}
if workingDir != "" {
if err := os.Chdir(workingDir); err != nil {
logrus.Fatal(err)
}
}
configContent, err := os.ReadFile(configPath)
if err != nil {
logrus.Fatal("read config: ", err)
}
var options option.Options
err = json.Unmarshal(configContent, &options)
if err != nil {
logrus.Fatal("decode config: ", err)
}
ctx, cancel := context.WithCancel(context.Background())
service, err := box.NewService(ctx, options)
if err != nil {
logrus.Fatal("create service: ", err)
}
if formatConfig {
cancel()
encoder := json.NewEncoder(os.Stdout)
encoder.SetIndent("", " ")
err = encoder.Encode(options)
if err != nil {
logrus.Fatal("encode config: ", err)
}
return
}
err = service.Start()
if err != nil {
logrus.Fatal("start service: ", err)
}
osSignals := make(chan os.Signal, 1)
signal.Notify(osSignals, os.Interrupt, syscall.SIGTERM)
<-osSignals
cancel()
service.Close()
}