aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorn-peugnet <n.peugnet@free.fr>2021-10-06 11:25:10 +0200
committern-peugnet <n.peugnet@free.fr>2021-10-06 11:25:10 +0200
commit9b9710511b0dbe51ac030ef908f9468103b0bd0a (patch)
treea6c399d75a6dd266cc7765ba7e28a1e2fc6e4744
parent6897c0ae70892e40fd7603e4d00662038c01a82d (diff)
downloaddna-backup-9b9710511b0dbe51ac030ef908f9468103b0bd0a.tar.gz
dna-backup-9b9710511b0dbe51ac030ef908f9468103b0bd0a.zip
refactor main and logger
-rw-r--r--TODO.md5
-rw-r--r--logger/logger.go23
-rw-r--r--main.go32
3 files changed, 32 insertions, 28 deletions
diff --git a/TODO.md b/TODO.md
index 2ee7c6f..451d51b 100644
--- a/TODO.md
+++ b/TODO.md
@@ -15,8 +15,9 @@ priority 1
- [x] add chunk cache to uniquely store chunks in RAM
- [x] better tests for `(*Repo).Commit`
- [x] remove errored files from `fileList`
-- [ ] **TODO: Priority 3** add superblock logic.
-- [ ] **TODO: Priority 2** add version blocks or journal logic.
+- [ ] export in `dir` format
+ - [ ] add superblock.
+ - [ ] add version blocks.
- [x] command line with subcommands (like, hmm... git ? for instance).
- experiences:
- [ ] compare against UDF
diff --git a/logger/logger.go b/logger/logger.go
index c7a4756..417a0b9 100644
--- a/logger/logger.go
+++ b/logger/logger.go
@@ -103,6 +103,9 @@ func (l *Logger) output(s severity, v ...interface{}) {
if s < l.minSeverity {
return
}
+ if s == sError && l.minSeverity <= sInfo {
+ v = append(v, "\n", string(debug.Stack()))
+ }
str := fmt.Sprint(v...) + resetSeq
logLock.Lock()
defer logLock.Unlock()
@@ -113,6 +116,10 @@ func (l *Logger) outputf(s severity, format string, v ...interface{}) {
if s < l.minSeverity {
return
}
+ if s == sError && l.minSeverity <= sInfo {
+ v = append(v, string(debug.Stack()))
+ format += "\n%s"
+ }
str := fmt.Sprintf(format, v...) + resetSeq
logLock.Lock()
defer logLock.Unlock()
@@ -177,15 +184,13 @@ func (l *Logger) Warningf(format string, v ...interface{}) {
// Error logs with the Error severity.
// Arguments are handled in the manner of fmt.Print.
func (l *Logger) Error(v ...interface{}) {
- v = append(v, "\n"+string(debug.Stack()))
l.output(sError, v...)
}
// Errorf logs with the Error severity.
// Arguments are handled in the manner of fmt.Printf.
func (l *Logger) Errorf(format string, v ...interface{}) {
- v = append(v, "\n"+string(debug.Stack()))
- l.outputf(sError, format+"%s", v...)
+ l.outputf(sError, format, v...)
}
// Panic uses the default logger and logs with the Error severity, and ends up with panic().
@@ -207,7 +212,6 @@ func (l *Logger) Panicf(format string, v ...interface{}) {
// Fatal logs with the Fatal severity, and ends with os.Exit(1).
// Arguments are handled in the manner of fmt.Print.
func (l *Logger) Fatal(v ...interface{}) {
- v = append(v, "\n"+string(debug.Stack()))
l.output(sFatal, v...)
os.Exit(1)
}
@@ -215,8 +219,7 @@ func (l *Logger) Fatal(v ...interface{}) {
// Fatalf logs with the Fatal severity, and ends with os.Exit(1).
// Arguments are handled in the manner of fmt.Printf.
func (l *Logger) Fatalf(format string, v ...interface{}) {
- v = append(v, "\n"+string(debug.Stack()))
- l.outputf(sFatal, format+"%s", v...)
+ l.outputf(sFatal, format, v...)
os.Exit(1)
}
@@ -274,15 +277,13 @@ func Warningf(format string, v ...interface{}) {
// Error uses the default logger and logs with the Error severity.
// Arguments are handled in the manner of fmt.Print.
func Error(v ...interface{}) {
- v = append(v, "\n"+string(debug.Stack()))
defaultLogger.output(sError, v...)
}
// Errorf uses the default logger and logs with the Error severity.
// Arguments are handled in the manner of fmt.Printf.
func Errorf(format string, v ...interface{}) {
- v = append(v, "\n"+string(debug.Stack()))
- defaultLogger.outputf(sError, format+"%s", v...)
+ defaultLogger.outputf(sError, format, v...)
}
// Panic uses the default logger and logs with the Error severity, and ends up with panic().
@@ -305,7 +306,6 @@ func Panicf(format string, v ...interface{}) {
// and ends with os.Exit(1).
// Arguments are handled in the manner of fmt.Print.
func Fatal(v ...interface{}) {
- v = append(v, "\n"+string(debug.Stack()))
defaultLogger.output(sFatal, v...)
os.Exit(1)
}
@@ -314,8 +314,7 @@ func Fatal(v ...interface{}) {
// and ends with os.Exit(1).
// Arguments are handled in the manner of fmt.Printf.
func Fatalf(format string, v ...interface{}) {
- v = append(v, "\n"+string(debug.Stack()))
- defaultLogger.outputf(sFatal, format+"%s", v...)
+ defaultLogger.outputf(sFatal, format, v...)
debug.PrintStack()
os.Exit(1)
}
diff --git a/main.go b/main.go
index cf208c9..51cfac2 100644
--- a/main.go
+++ b/main.go
@@ -10,30 +10,34 @@ import (
type command struct {
Flag *flag.FlagSet
+ Run func([]string) error
Usage string
Help string
- Run func([]string) error
}
const (
- name = "dna-backup"
- baseUsage = "<command> [<options>] [--] <args>"
- commitUsage = "[<options>] [--] <source> <dest>"
- commitHelp = "Create a new version of folder <source> into repo <dest>"
- restoreUsage = "[<options>] [--] <source> <dest>"
- restoreHelp = "Restore the last version from repo <source> into folder <dest>"
+ name = "dna-backup"
+ baseUsage = "<command> [<options>] [--] <args>"
)
var (
- logLevel int
- commitCmd = flag.NewFlagSet("commit", flag.ExitOnError)
- restoreCmd = flag.NewFlagSet("restore", flag.ExitOnError)
- subcommands = map[string]command{
- commitCmd.Name(): {commitCmd, commitUsage, commitHelp, commitMain},
- restoreCmd.Name(): {restoreCmd, restoreUsage, restoreHelp, restoreMain},
- }
+ logLevel int
+ format string
)
+var commit = command{flag.NewFlagSet("commit", flag.ExitOnError), commitMain,
+ "[<options>] [--] <source> <dest>",
+ "Create a new version of folder <source> into repo <dest>",
+}
+var restore = command{flag.NewFlagSet("restore", flag.ExitOnError), restoreMain,
+ "[<options>] [--] <source> <dest>",
+ "Restore the last version from repo <source> into folder <dest>",
+}
+var subcommands = map[string]command{
+ commit.Flag.Name(): commit,
+ restore.Flag.Name(): restore,
+}
+
func init() {
// init default help message
flag.Usage = func() {