aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--env.go5
-rw-r--r--logging/log.go56
-rw-r--r--main.go27
-rw-r--r--restore_dir.go2
-rw-r--r--snapshot.go12
-rw-r--r--write_dir.go2
6 files changed, 90 insertions, 14 deletions
diff --git a/env.go b/env.go
index ad8cf9a..9ec74e8 100644
--- a/env.go
+++ b/env.go
@@ -3,6 +3,7 @@ package main
import (
"code.laria.me/petrific/cache"
"code.laria.me/petrific/config"
+ "code.laria.me/petrific/logging"
"code.laria.me/petrific/storage"
"code.laria.me/petrific/storage/registry"
"fmt"
@@ -13,6 +14,7 @@ type Env struct {
Conf config.Config
Store storage.Storage
IdCache cache.Cache
+ Log *logging.Log
}
func (e *Env) Close() {
@@ -40,8 +42,9 @@ func (env *Env) loadCache() error {
return nil
}
-func NewEnv(confPath, storageName string) (*Env, error) {
+func NewEnv(log *logging.Log, confPath, storageName string) (*Env, error) {
env := new(Env)
+ env.Log = log
var err error
diff --git a/logging/log.go b/logging/log.go
new file mode 100644
index 0000000..9bef591
--- /dev/null
+++ b/logging/log.go
@@ -0,0 +1,56 @@
+package logging
+
+import (
+ "io"
+ "log"
+)
+
+type Logger interface {
+ Print(v ...interface{})
+ Printf(format string, v ...interface{})
+ Println(v ...interface{})
+}
+
+type NopLogger struct{}
+
+func (NopLogger) Print(v ...interface{}) {}
+func (NopLogger) Printf(format string, v ...interface{}) {}
+func (NopLogger) Println(v ...interface{}) {}
+
+type Level int
+
+const (
+ LQuiet Level = iota
+ LError
+ LWarn
+ LInfo
+ LDebug
+)
+
+type Log struct {
+ err, warn, info, debug Logger
+}
+
+func (l Log) Error() Logger { return l.err }
+func (l Log) Warn() Logger { return l.warn }
+func (l Log) Info() Logger { return l.info }
+func (l Log) Debug() Logger { return l.debug }
+
+func buildLogger(out io.Writer, prefix string, lWant, lHave Level) Logger {
+ if lWant <= lHave {
+ return log.New(out, prefix, log.LstdFlags)
+ } else {
+ return NopLogger{}
+ }
+}
+
+func NewLog(out io.Writer, level Level) *Log {
+ l := new(Log)
+
+ l.err = buildLogger(out, "[Err]", LError, level)
+ l.warn = buildLogger(out, "[Warn]", LWarn, level)
+ l.info = buildLogger(out, "[Info]", LInfo, level)
+ l.debug = buildLogger(out, "[Debug]", LDebug, level)
+
+ return l
+}
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
+}
diff --git a/restore_dir.go b/restore_dir.go
index 4e45fae..1ed8a11 100644
--- a/restore_dir.go
+++ b/restore_dir.go
@@ -9,7 +9,7 @@ import (
func RestoreDir(env *Env, args []string) int {
usage := subcmdUsage("restore-dir", "directory object-id", nil)
- errout := subcmdErrout("restore-dir")
+ errout := subcmdErrout(env.Log, "restore-dir")
if len(args) != 2 {
usage()
diff --git a/snapshot.go b/snapshot.go
index ff4c0be..06244e2 100644
--- a/snapshot.go
+++ b/snapshot.go
@@ -48,7 +48,7 @@ func CreateSnapshot(env *Env, args []string) int {
comment := flags.String("comment", "", "comment for the snapshot")
flags.Usage = subcmdUsage("create-snapshot", "[flags] archive tree-object", flags)
- errout := subcmdErrout("create-snapshot")
+ errout := subcmdErrout(env.Log, "create-snapshot")
err := flags.Parse(args)
if err != nil {
@@ -84,7 +84,7 @@ func TakeSnapshot(env *Env, args []string) int {
comment := flags.String("comment", "", "comment for the snapshot")
flags.Usage = subcmdUsage("take-snapshot", "[flags] archive dir", flags)
- errout := subcmdErrout("take-snapshot")
+ errout := subcmdErrout(env.Log, "take-snapshot")
if err := flags.Parse(args); err != nil {
errout(err)
@@ -123,7 +123,7 @@ func TakeSnapshot(env *Env, args []string) int {
snapshot_id, err := createSnapshot(env, args[0], *comment, tree_id, *nosign)
if err != nil {
errout(err)
- fmt.Fprintf(os.Stderr, "You can try again by running `%s create-snapshot -c '%s' '%s' '%s'\n`", os.Args[0], *comment, args[0], tree_id)
+ env.Log.Error().Printf("You can try again by running `%s create-snapshot -c '%s' '%s' '%s'\n`", os.Args[0], *comment, args[0], tree_id)
return 1
}
@@ -144,7 +144,7 @@ func (s sortableSnapshots) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func ListSnapshots(env *Env, args []string) int {
// usage := subcmdUsage("list-snapshots", "[archive]", nil)
- errout := subcmdErrout("list-snapshots")
+ errout := subcmdErrout(env.Log, "list-snapshots")
filter := func(s objects.Snapshot) bool { return true }
if len(args) > 0 {
@@ -166,7 +166,7 @@ func ListSnapshots(env *Env, args []string) int {
for _, objid := range objids {
_snapshot, err := storage.GetObjectOfType(env.Store, objid, objects.OTSnapshot)
if err != nil {
- fmt.Fprintf(os.Stderr, "warning: list-snapshots: could not get snapshot %s: %s\n", objid, err)
+ env.Log.Warn().Printf("list-snapshots: could not get snapshot %s: %s\n", objid, err)
failed = true
continue
}
@@ -199,7 +199,7 @@ func RestoreSnapshot(env *Env, args []string) int {
archive := flags.String("archive", "", "Get latest snapshot for this archive")
flags.Usage = subcmdUsage("restore-snapshot", "[flags] directory", flags)
- errout := subcmdErrout("restore-snapshot")
+ errout := subcmdErrout(env.Log, "restore-snapshot")
err := flags.Parse(args)
if err != nil {
diff --git a/write_dir.go b/write_dir.go
index d7c78e6..e3348c5 100644
--- a/write_dir.go
+++ b/write_dir.go
@@ -21,7 +21,7 @@ func abspath(p string) (string, error) {
func WriteDir(env *Env, args []string) int {
usage := subcmdUsage("write-dir", "directory", nil)
- errout := subcmdErrout("write-dir")
+ errout := subcmdErrout(env.Log, "write-dir")
if len(args) != 1 {
usage()