diff options
author | n-peugnet <n.peugnet@free.fr> | 2021-09-22 19:47:27 +0200 |
---|---|---|
committer | n-peugnet <n.peugnet@free.fr> | 2021-09-22 20:44:24 +0200 |
commit | a1b768438258bfd6c33573e156708f002c693ba2 (patch) | |
tree | 0ce9ef80263061e746fe10d93058b106cb93ee1f | |
parent | 368f89466f48e8621254b04c1bca996db5c7a66a (diff) | |
download | dna-backup-a1b768438258bfd6c33573e156708f002c693ba2.tar.gz dna-backup-a1b768438258bfd6c33573e156708f002c693ba2.zip |
add debug log level
-rw-r--r-- | logger/logger.go | 38 | ||||
-rw-r--r-- | logger/logger_test.go | 12 | ||||
-rw-r--r-- | main.go | 2 | ||||
-rw-r--r-- | repo.go | 19 |
4 files changed, 51 insertions, 20 deletions
diff --git a/logger/logger.go b/logger/logger.go index bd8b937..9613e17 100644 --- a/logger/logger.go +++ b/logger/logger.go @@ -20,15 +20,18 @@ type logger interface { // Severity levels. const ( - sInfo severity = iota + sDebug severity = iota + sInfo sWarning sError sFatal + sCount ) // Severity tags. const ( - tagInfo = "\033[0m[INFO] " + tagDebug = "\033[0m[DEBUG] " + tagInfo = "\033[97m[INFO] " tagWarning = "\033[33m[WARN] " tagError = "\033[31m[ERROR] " tagFatal = "\033[1;31m[FATAL] " @@ -44,8 +47,9 @@ var ( defaultLogger *Logger ) -func newLoggers() [4]logger { - return [4]logger{ +func newLoggers() [sCount]logger { + return [sCount]logger{ + log.New(os.Stderr, tagDebug, flags), log.New(os.Stderr, tagInfo, flags), log.New(os.Stderr, tagWarning, flags), log.New(os.Stderr, tagError, flags), @@ -90,7 +94,7 @@ func Init(level int) *Logger { // A Logger represents an active logging object. Multiple loggers can be used // simultaneously even if they are using the same writers. type Logger struct { - loggers [4]logger + loggers [sCount]logger minSeverity severity initialized bool } @@ -129,6 +133,18 @@ func (l *Logger) SetFlags(flag int) { } } +// Debug logs with the Info severity. +// Arguments are handled in the manner of fmt.Print. +func (l *Logger) Debug(v ...interface{}) { + l.output(sDebug, v...) +} + +// Debugf logs with the Info severity. +// Arguments are handled in the manner of fmt.Printf. +func (l *Logger) Debugf(format string, v ...interface{}) { + l.outputf(sDebug, format, v...) +} + // Info logs with the Info severity. // Arguments are handled in the manner of fmt.Print. func (l *Logger) Info(v ...interface{}) { @@ -209,6 +225,18 @@ func SetFlags(flag int) { defaultLogger.SetFlags(flag) } +// Debug uses the default logger and logs with the Info severity. +// Arguments are handled in the manner of fmt.Print. +func Debug(v ...interface{}) { + defaultLogger.output(sDebug, v...) +} + +// Debugf uses the default logger and logs with the Info severity. +// Arguments are handled in the manner of fmt.Printf. +func Debugf(format string, v ...interface{}) { + defaultLogger.outputf(sDebug, format, v...) +} + // Info uses the default logger and logs with the Info severity. // Arguments are handled in the manner of fmt.Print. func Info(v ...interface{}) { diff --git a/logger/logger_test.go b/logger/logger_test.go index 2352856..2e1ff25 100644 --- a/logger/logger_test.go +++ b/logger/logger_test.go @@ -116,8 +116,8 @@ func TestFlags(t *testing.T) { SetOutput(&buf) l.Infof("info %d", sInfo) s := buf.String() - if !strings.Contains(s, "info 0") { - t.Errorf("log output %q should contain: info 0", s) + if !strings.Contains(s, "info 1") { + t.Errorf("log output %q should contain: info 1", s) } path := "logger/logger_test.go:117" if !strings.Contains(s, path) { @@ -133,13 +133,13 @@ func TestFlags(t *testing.T) { if !strings.Contains(s, "warning") { t.Errorf("log output %q should contain: warning", s) } - if !strings.Contains(s, "warning 1") { - t.Errorf("log output %q should contain: warning 1", s) + if !strings.Contains(s, "warning 2") { + t.Errorf("log output %q should contain: warning 2", s) } if !strings.Contains(s, "error") { t.Errorf("log output %q should contain: error", s) } - if !strings.Contains(s, "error 2") { - t.Errorf("log output %q should contain: error 2", s) + if !strings.Contains(s, "error 3") { + t.Errorf("log output %q should contain: error 3", s) } } @@ -17,7 +17,7 @@ var ( ) func init() { - flag.IntVar(&logLevel, "v", 2, "log verbosity level (0-3)") + flag.IntVar(&logLevel, "v", 3, "log verbosity level (0-4)") } func main() { @@ -209,7 +209,7 @@ func listFiles(path string) []File { return nil }) if err != nil { - logger.Error(err) + // already logged in callback } return files } @@ -242,7 +242,7 @@ func concatFiles(files *[]File, stream io.WriteCloser) { } af := f if n, err := io.Copy(stream, file); err != nil { - logger.Error(n, err) + logger.Error("read ", n, " bytes, ", err) af.Size = n } actual = append(actual, af) @@ -349,7 +349,7 @@ func (r *Repo) storageWorker(version int, storeQueue <-chan chunkData, end chan< if err != nil { logger.Error(err) } - // logger.Info("stored", data.id) + logger.Debug("stored", data.id) } if err = file.Close(); err != nil { logger.Panic(err) @@ -472,6 +472,9 @@ func (r *Repo) hashChunk(id *ChunkId, reader io.Reader) (fp uint64, sk []uint64) go r.makeFingerprint(id, &buffFp, &wg, &fp) go r.makeSketch(id, &buffSk, &wg, &sk) wg.Wait() + if _, e := r.fingerprints[fp]; e { + logger.Error(fp, " already exists in fingerprints map") + } r.fingerprints[fp] = id r.sketches.Set(sk, id) return @@ -510,7 +513,7 @@ func (r *Repo) findSimilarChunk(chunk Chunk) (*ChunkId, bool) { for _, id := range chunkIds { count := similarChunks[*id] count += 1 - logger.Infof("found %d %d time(s)", id, count) + logger.Debugf("found %d %d time(s)", id, count) if count > max { max = count similarChunk = id @@ -544,7 +547,7 @@ func (r *Repo) tryDeltaEncodeChunk(temp BufferedChunk) (Chunk, bool) { func (r *Repo) encodeTempChunk(temp BufferedChunk, version int, last *uint64, storeQueue chan<- chunkData) (chunk Chunk, isDelta bool) { chunk, isDelta = r.tryDeltaEncodeChunk(temp) if isDelta { - logger.Info("add new delta chunk") + logger.Debug("add new delta chunk") return } if chunk.Len() == r.chunkSize { @@ -557,10 +560,10 @@ func (r *Repo) encodeTempChunk(temp BufferedChunk, version int, last *uint64, st id: id, } r.chunkCache.Set(id, temp.Bytes()) - logger.Info("add new chunk", id) + logger.Debug("add new chunk", id) return NewStoredChunk(r, id), false } - logger.Info("add new partial chunk of size:", chunk.Len()) + logger.Debug("add new partial chunk of size:", chunk.Len()) return } @@ -619,7 +622,7 @@ func (r *Repo) matchStream(stream io.Reader, version int) []Chunk { chunks = append(chunks, c) prev = nil } - logger.Infof("add existing chunk: %d", chunkId) + logger.Debugf("add existing chunk: %d", chunkId) chunks = append(chunks, NewStoredChunk(r, chunkId)) buff = make([]byte, 0, r.chunkSize*2) for i := 0; i < r.chunkSize && err == nil; i++ { |