diff --git a/Makefile b/Makefile index a0bfd5f3..637ac93b 100644 --- a/Makefile +++ b/Makefile @@ -6,21 +6,19 @@ install: go install github.com/tendermint/tendermint/cmd/tendermint go install github.com/tendermint/tendermint/cmd/barak go install github.com/tendermint/tendermint/cmd/debora + go install github.com/tendermint/tendermint/cmd/filelogger build: go build -o build/tendermint github.com/tendermint/tendermint/cmd/tendermint go build -o build/barak github.com/tendermint/tendermint/cmd/barak go build -o build/debora github.com/tendermint/tendermint/cmd/debora - -no_get: - go build -o build/tendermint github.com/tendermint/tendermint/cmd/tendermint - go build -o build/barak github.com/tendermint/tendermint/cmd/barak - go build -o build/debora github.com/tendermint/tendermint/cmd/debora + go build -o build/filelogger github.com/tendermint/tendermint/cmd/filelogger build_race: go build -race -o build/tendermint github.com/tendermint/tendermint/cmd/tendermint go build -race -o build/barak github.com/tendermint/tendermint/cmd/barak go build -race -o build/debora github.com/tendermint/tendermint/cmd/debora + go build -race -o build/filelogger github.com/tendermint/tendermint/cmd/filelogger test: build go test github.com/tendermint/tendermint/... diff --git a/binary/byteslice.go b/binary/byteslice.go index 205a502c..c8b8cb2a 100644 --- a/binary/byteslice.go +++ b/binary/byteslice.go @@ -1,14 +1,9 @@ package binary import ( - . "github.com/tendermint/tendermint/common" "io" ) -const ( - ByteSliceChunk = 1024 -) - func WriteByteSlice(bz []byte, w io.Writer, n *int64, err *error) { WriteVarint(len(bz), w, n, err) WriteTo(bz, w, n, err) @@ -24,16 +19,8 @@ func ReadByteSlice(r io.Reader, n *int64, err *error) []byte { return nil } - var buf, tmpBuf []byte - // read one ByteSliceChunk at a time and append - for i := 0; i*ByteSliceChunk < length; i++ { - tmpBuf = make([]byte, MinInt(ByteSliceChunk, length-i*ByteSliceChunk)) - ReadFull(tmpBuf, r, n, err) - if *err != nil { - return nil - } - buf = append(buf, tmpBuf...) - } + buf := make([]byte, length) + ReadFull(buf, r, n, err) return buf } diff --git a/cmd/filelogger/README.md b/cmd/filelogger/README.md new file mode 100644 index 00000000..3e63b502 --- /dev/null +++ b/cmd/filelogger/README.md @@ -0,0 +1,6 @@ +filelogger reads from stdin and writes to the specified file, in a way compatible for logrotate to move around. +(see tendermint/common/os#AutoFile) + +```bash +some_command arg1 arg2 2>&1 | filelogger -o path_to_log.log +``` diff --git a/cmd/filelogger/main.go b/cmd/filelogger/main.go new file mode 100644 index 00000000..cb8c534a --- /dev/null +++ b/cmd/filelogger/main.go @@ -0,0 +1,63 @@ +package main + +import ( + "flag" + "fmt" + "io" + "os" + + . "github.com/tendermint/tendermint/common" +) + +const Version = "0.0.1" +const readBufferSize = 1024 + +// Parse command-line options +func parseFlags() (outpath string, version bool) { + flag.StringVar(&outpath, "outpath", "filelogger.out", "Output file name") + flag.BoolVar(&version, "version", false, "Version") + flag.Parse() + return +} + +func main() { + + // Read options + outpath, version := parseFlags() + if version { + fmt.Println(Fmt("filelogger version %v", Version)) + return + } + + outfile, err := OpenAutoFile(outpath) + if err != nil { + fmt.Println(Fmt("filelogger couldn't create outfile %v", outfile)) + os.Exit(1) + } + + go writeToOutfile(outfile) + + // Trap signal + TrapSignal(func() { + outfile.Close() + fmt.Println("filelogger shutting down") + }) +} + +func writeToOutfile(outfile *AutoFile) { + // Forever, read from stdin and write to AutoFile. + buf := make([]byte, readBufferSize) + for { + n, err := os.Stdin.Read(buf) + outfile.Write(buf[:n]) + if err != nil { + outfile.Close() + if err == io.EOF { + os.Exit(0) + } else { + fmt.Println("filelogger errored") + os.Exit(1) + } + } + } +} diff --git a/common/os.go b/common/os.go index 58222c99..8060ba5e 100644 --- a/common/os.go +++ b/common/os.go @@ -160,7 +160,6 @@ func (af *AutoFile) processTicks() { if !ok { return // Done. } - fmt.Println("closeFile()") af.mtx.Lock() af.closeFile() af.mtx.Unlock() @@ -184,7 +183,6 @@ func (af *AutoFile) Write(b []byte) (n int, err error) { return } } - fmt.Println("Write:", string(b)) return af.file.Write(b) }