diff options
author | Robin Jarry <robin@jarry.cc> | 2022-07-19 22:31:51 +0200 |
---|---|---|
committer | Robin Jarry <robin@jarry.cc> | 2022-07-23 22:52:15 +0200 |
commit | cd1999555714fb886493d2d04b6c472be55cebef (patch) | |
tree | 1df3bcf5f687752db671d8bc9c7eab8a5c0fde71 /aerc.go | |
parent | a1f779ccc9b16b22ad6cb2e0bf73c290fd0cc756 (diff) | |
download | aerc-cd1999555714fb886493d2d04b6c472be55cebef.tar.gz |
logging: use level-based logger functions
Do not pass logger objects around anymore. Shuffle some messages to make
them consistent with the new logging API. Avoid using %v when a more
specific verb exists for the argument types.
The loggers are completely disabled (i.e. Sprintf is not even called)
by default. They are only enabled when redirecting stdout to a file.
Signed-off-by: Robin Jarry <robin@jarry.cc>
Acked-by: Moritz Poldrack <moritz@poldrack.dev>
Diffstat (limited to 'aerc.go')
-rw-r--r-- | aerc.go | 37 |
1 files changed, 13 insertions, 24 deletions
@@ -3,9 +3,6 @@ package main import ( "bytes" "fmt" - "io" - "io/ioutil" - "log" "os" "sort" "time" @@ -91,8 +88,10 @@ func getCompletions(aerc *widgets.Aerc, cmd string) []string { // set at build time var Version string -func usage() { - log.Fatal("Usage: aerc [-v] [mailto:...]") +func usage(msg string) { + fmt.Fprintln(os.Stderr, msg) + fmt.Fprintln(os.Stderr, "usage: aerc [-v] [mailto:...]") + os.Exit(1) } func setWindowTitle() { @@ -116,8 +115,7 @@ func main() { defer logging.PanicHandler() opts, optind, err := getopt.Getopts(os.Args, "v") if err != nil { - log.Print(err) - usage() + usage("error: " + err.Error()) return } for _, opt := range opts { @@ -130,7 +128,7 @@ func main() { retryExec := false args := os.Args[optind:] if len(args) > 1 { - usage() + usage("error: invalid arguments") return } else if len(args) == 1 { arg := args[0] @@ -143,20 +141,12 @@ func main() { retryExec = true } - var ( - logOut io.Writer - logger *log.Logger - ) if !isatty.IsTerminal(os.Stdout.Fd()) { - logOut = os.Stdout - } else { - logOut = ioutil.Discard - os.Stdout, _ = os.Open(os.DevNull) + logging.Init() } - logger = log.New(logOut, "", log.LstdFlags) - logger.Println("Starting up aerc") + logging.Infof("Starting up") - conf, err := config.LoadConfigFromFile(nil, logger) + conf, err := config.LoadConfigFromFile(nil) if err != nil { fmt.Fprintf(os.Stderr, "Failed to load config: %v\n", err) os.Exit(1) @@ -170,10 +160,10 @@ func main() { deferLoop := make(chan struct{}) c := crypto.New(conf.General.PgpProvider) - c.Init(logger) + c.Init() defer c.Close() - aerc = widgets.NewAerc(conf, logger, c, func(cmd []string) error { + aerc = widgets.NewAerc(conf, c, func(cmd []string) error { return execCommand(aerc, ui, cmd) }, func(cmd string) []string { return getCompletions(aerc, cmd) @@ -193,10 +183,9 @@ func main() { ui.EnableMouse() } - logger.Println("Starting Unix server") - as, err := lib.StartServer(logger) + as, err := lib.StartServer() if err != nil { - logger.Printf("Failed to start Unix server: %v (non-fatal)", err) + logging.Warnf("Failed to start Unix server: %v", err) } else { defer as.Close() as.OnMailto = aerc.Mailto |