Skip to content

Commit

Permalink
logging, added count to tarp create
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom committed Apr 23, 2020
1 parent 992a98e commit 21f6b3b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
7 changes: 4 additions & 3 deletions dpipes/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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")
}
11 changes: 9 additions & 2 deletions tarp/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions tarp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 21f6b3b

Please sign in to comment.