diff options
-rw-r--r-- | internal/log/log.go | 46 | ||||
-rw-r--r-- | internal/parse/parse.go | 5 | ||||
-rw-r--r-- | internal/util/util.go | 17 | ||||
-rw-r--r-- | main.go | 10 |
4 files changed, 54 insertions, 24 deletions
diff --git a/internal/log/log.go b/internal/log/log.go new file mode 100644 index 0000000..d2c09d0 --- /dev/null +++ b/internal/log/log.go @@ -0,0 +1,46 @@ +package log + +import ( + "fmt" + "log" + "os" +) + +var errorLogger = log.New(os.Stderr, "ERROR ", log.LstdFlags|log.Lmsgprefix) +var warnLogger = log.New(os.Stdout, "WARN ", log.LstdFlags|log.Lmsgprefix) +var enableDebug = false + +func SetDebug(state bool) { + enableDebug = state +} + +func Print(v ...interface{}) { + if !enableDebug { + _ = log.Output(2, fmt.Sprint(v...)) + } +} + +func Printf(format string, v ...interface{}) { + if !enableDebug { + _ = log.Output(2, fmt.Sprintf(format, v...)) + } +} + +func Error(v ...interface{}) { + _ = errorLogger.Output(2, fmt.Sprint(v...)) +} + +//noinspection GoUnusedExportedFunction +func Errorf(format string, a ...interface{}) { + _ = errorLogger.Output(2, fmt.Sprintf(format, a...)) +} + +//noinspection GoUnusedExportedFunction +func Warn(v ...interface{}) { + _ = warnLogger.Output(2, fmt.Sprint(v...)) +} + +//noinspection GoUnusedExportedFunction +func Warnf(format string, a ...interface{}) { + _ = warnLogger.Output(2, fmt.Sprintf(format, a...)) +} diff --git a/internal/parse/parse.go b/internal/parse/parse.go index a4112d9..ddf1f0f 100644 --- a/internal/parse/parse.go +++ b/internal/parse/parse.go @@ -3,14 +3,13 @@ package parse import ( ctxt "context" "fmt" - "log" "sync" "time" "github.com/mmcdole/gofeed" "github.com/Necoro/feed2imap-go/internal/config" - "github.com/Necoro/feed2imap-go/internal/util" + "github.com/Necoro/feed2imap-go/internal/log" ) func context() (ctxt.Context, ctxt.CancelFunc) { @@ -33,7 +32,7 @@ func handleFeed(feed *config.Feed, wg *sync.WaitGroup) { log.Printf("Fetching %s from %s", feed.Name, feed.Url) if err := parseFeed(feed); err != nil { - util.Error(err) + log.Error(err) } } diff --git a/internal/util/util.go b/internal/util/util.go deleted file mode 100644 index 54a0805..0000000 --- a/internal/util/util.go +++ /dev/null @@ -1,17 +0,0 @@ -package util - -import ( - "log" - "os" -) - -var errorLogger = log.New(os.Stderr, "ERROR ", log.LstdFlags|log.Lmsgprefix) - -func Error(v ...interface{}) { - errorLogger.Print(v...) -} - -//noinspection GoUnusedExportedFunction -func Errorf(format string, a ...interface{}) { - errorLogger.Printf(format, a...) -} @@ -2,19 +2,21 @@ package main import ( "flag" - "log" "os" "github.com/Necoro/feed2imap-go/internal/config" + "github.com/Necoro/feed2imap-go/internal/log" "github.com/Necoro/feed2imap-go/internal/parse" - "github.com/Necoro/feed2imap-go/internal/util" ) var cfgFile = flag.String("f", "config.yml", "configuration file") +var verbose = flag.Bool("v", false, "enable verbose output") func run() error { - log.Print("Starting up...") flag.Parse() + log.SetDebug(*verbose) + + log.Print("Starting up...") log.Printf("Reading configuration file '%s'", *cfgFile) cfg, err := config.Load(*cfgFile) @@ -29,7 +31,7 @@ func run() error { func main() { if err := run(); err != nil { - util.Error(err) + log.Error(err) os.Exit(1) } } |