aboutsummaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
authorLaria Carolin Chabowski <laria@laria.me>2017-12-18 22:34:56 +0100
committerLaria Carolin Chabowski <laria@laria.me>2017-12-18 22:35:09 +0100
commitb01cef071c5e95f73e756c3d039e2ddf1a6ed3df (patch)
tree12573257a20a055e86526602535018756c9e9527 /main.go
parent2cfd9f146d042a1c30176ad91fea730df439704d (diff)
downloadpetrific-b01cef071c5e95f73e756c3d039e2ddf1a6ed3df.tar.gz
petrific-b01cef071c5e95f73e756c3d039e2ddf1a6ed3df.tar.bz2
petrific-b01cef071c5e95f73e756c3d039e2ddf1a6ed3df.zip
Add logging
Diffstat (limited to 'main.go')
-rw-r--r--main.go27
1 files changed, 22 insertions, 5 deletions
diff --git a/main.go b/main.go
index b6b541b..c567403 100644
--- a/main.go
+++ b/main.go
@@ -1,6 +1,7 @@
package main
import (
+ "code.laria.me/petrific/logging"
"flag"
"fmt"
"os"
@@ -27,16 +28,17 @@ func subcmdUsage(name string, usage string, flags *flag.FlagSet) func() {
}
}
-func subcmdErrout(name string) func(error) {
+func subcmdErrout(log *logging.Log, name string) func(error) {
return func(err error) {
- fmt.Fprintf(os.Stderr, "%s: %s\n", name, err)
+ log.Error().Printf("%s: %s\n", name, err)
}
}
// Global flags
var (
- flagConfPath = flag.String("config", "", "Use this config file instead of the default")
- flagStorage = flag.String("storage", "", "Operate on this storage instead of the default one")
+ flagConfPath = flag.String("config", "", "Use this config file instead of the default")
+ flagStorage = flag.String("storage", "", "Operate on this storage instead of the default one")
+ flagVerbosity = flag.Int("verbosity", int(logging.LWarn), "Verbosity level (0: quiet, 4: everything)")
)
func main() {
@@ -54,11 +56,17 @@ func Main() int {
}
flag.Parse()
- env, err := NewEnv(*flagConfPath, *flagStorage)
+ log, err := createLogger(logging.Level(*flagVerbosity))
if err != nil {
fmt.Fprintln(os.Stderr, err)
return 1
}
+
+ env, err := NewEnv(log, *flagConfPath, *flagStorage)
+ if err != nil {
+ log.Error().Println(err)
+ return 1
+ }
defer env.Close()
remaining := make([]string, 0)
@@ -80,3 +88,12 @@ func Main() int {
return cmd(env, remaining[1:])
}
+
+func createLogger(level logging.Level) (log *logging.Log, err error) {
+ if level < logging.LQuiet || level > logging.LDebug {
+ level = logging.LDebug
+ return nil, fmt.Errorf("verbosity %d is out of range", level)
+ }
+
+ return logging.NewLog(os.Stderr, level), nil
+}