From 21f6b3ba911eefd1820bc118c98ed60b42e58f48 Mon Sep 17 00:00:00 2001 From: Tom Date: Wed, 22 Apr 2020 21:44:24 -0700 Subject: [PATCH] logging, added count to tarp create --- dpipes/common.go | 7 ++++--- tarp/create.go | 11 +++++++++-- tarp/main.go | 4 ++-- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/dpipes/common.go b/dpipes/common.go index 4658653..6c9b789 100644 --- a/dpipes/common.go +++ b/dpipes/common.go @@ -101,7 +101,8 @@ func GetFirst(sample Sample, spec string) (Bytes, error) { // OpenLogger opens a logger on a given file, // with abbreviations for stdout/stderr -func OpenLogger(where string, prefix string) *log.Logger { +func OpenLogger(where string, ident string) *log.Logger { + prefix := "[" + ident + "] " if where == "null" || where == "" { stream, _ := os.Open("/dev/null") return log.New(stream, prefix, 0) @@ -126,6 +127,6 @@ func MyInfo() string { } func init() { - Debug = OpenLogger(GetEnv("debug", ""), "[debug]") - Progress = OpenLogger(GetEnv("progress", "stderr"), "") + Debug = OpenLogger(GetEnv("debug", ""), "debug") + Progress = OpenLogger(GetEnv("progress", "stderr"), "progress") } diff --git a/tarp/create.go b/tarp/create.go index edc4c72..a63b933 100644 --- a/tarp/create.go +++ b/tarp/create.go @@ -10,6 +10,7 @@ import ( var createopts struct { Output string `short:"o" long:"output" description:"output file" default:""` + Count int `long:"count" description:"maximum number of files to write (for testing)"` Positional struct { Input string `required:"yes"` } `positional-args:"yes"` @@ -30,19 +31,25 @@ func createcmd() { defer stream.Close() dpipes.TarRawSink(stream)(outch) }) + count := 0 for { + if count >= createopts.Count { + break + } line, _, err := reader.ReadLine() if err == io.EOF { break } + Handle(err) lineno += 1 fields := whitespace.Split(string(line), 2) - infolog.Println(fields) - Validate(len(fields) == 2, "bad input line at", lineno) + Validate(len(fields) == 2, "bad input line at", lineno, ":", line) + infolog.Println(count, fields[0], "<-", fields[1]) output, source := fields[0], fields[1] contents, err := dpipes.ReadBinary(source) Handle(err) outch <- dpipes.Raw{output, contents} + count++ } close(outch) <-done diff --git a/tarp/main.go b/tarp/main.go index bccca63..73c5746 100644 --- a/tarp/main.go +++ b/tarp/main.go @@ -47,8 +47,8 @@ func Validate(ok bool, args ...interface{}) { } func main() { - infolog = dpipes.OpenLogger(opts.Infolog, "[info]") - errlog = dpipes.OpenLogger(opts.Errlog, "[error]") + infolog = dpipes.OpenLogger("stderr", "info") + errlog = dpipes.OpenLogger("stderr", "error") if len(os.Args) == 1 { Parser.WriteHelp(os.Stderr) os.Exit(1)