From 31341857abc73334852d3b888f4fb558d8ed3c61 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Sun, 25 Dec 2022 14:03:02 -0700 Subject: Factor logger code from main.go in to textui --- cmd/btrfs-rec/main.go | 20 ++------------------ lib/textui/log.go | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 18 deletions(-) create mode 100644 lib/textui/log.go diff --git a/cmd/btrfs-rec/main.go b/cmd/btrfs-rec/main.go index a221cb7..d14675d 100644 --- a/cmd/btrfs-rec/main.go +++ b/cmd/btrfs-rec/main.go @@ -14,7 +14,6 @@ import ( "github.com/datawire/ocibuild/pkg/cliutil" "github.com/sirupsen/logrus" "github.com/spf13/cobra" - "github.com/spf13/pflag" "git.lukeshu.com/btrfs-progs-ng/lib/btrfs" "git.lukeshu.com/btrfs-progs-ng/lib/btrfs/btrfsvol" @@ -22,19 +21,6 @@ import ( "git.lukeshu.com/btrfs-progs-ng/lib/textui" ) -type logLevelFlag struct { - logrus.Level -} - -func (lvl *logLevelFlag) Type() string { return "loglevel" } -func (lvl *logLevelFlag) Set(str string) error { - var err error - lvl.Level, err = logrus.ParseLevel(str) - return err -} - -var _ pflag.Value = (*logLevelFlag)(nil) - type subcommand struct { cobra.Command RunE func(*btrfs.FS, *cobra.Command, []string) error @@ -43,7 +29,7 @@ type subcommand struct { var inspectors, repairers []subcommand func main() { - logLevelFlag := logLevelFlag{ + logLevelFlag := textui.LogLevelFlag{ Level: logrus.InfoLevel, } var pvsFlag []string @@ -115,9 +101,7 @@ func main() { runE := child.RunE cmd.RunE = func(cmd *cobra.Command, args []string) error { ctx := cmd.Context() - logger := logrus.New() - logger.SetLevel(logLevelFlag.Level) - ctx = dlog.WithLogger(ctx, dlog.WrapLogrus(logger)) + ctx = dlog.WithLogger(ctx, textui.NewLogger(logLevelFlag.Level)) grp := dgroup.NewGroup(ctx, dgroup.GroupConfig{ EnableSignalHandling: true, diff --git a/lib/textui/log.go b/lib/textui/log.go new file mode 100644 index 0000000..cee51db --- /dev/null +++ b/lib/textui/log.go @@ -0,0 +1,33 @@ +// Copyright (C) 2022 Luke Shumaker +// +// SPDX-License-Identifier: GPL-2.0-or-later + +package textui + +import ( + "github.com/datawire/dlib/dlog" + "github.com/sirupsen/logrus" + "github.com/spf13/pflag" +) + +type LogLevelFlag struct { + logrus.Level +} + +var _ pflag.Value = (*LogLevelFlag)(nil) + +// Type implements pflag.Value. +func (lvl *LogLevelFlag) Type() string { return "loglevel" } + +// Type implements pflag.Value. +func (lvl *LogLevelFlag) Set(str string) error { + var err error + lvl.Level, err = logrus.ParseLevel(str) + return err +} + +func NewLogger(lvl logrus.Level) dlog.Logger { + logger := logrus.New() + logger.SetLevel(lvl) + return dlog.WrapLogrus(logger) +} -- cgit v1.1-4-g5e80