mirror of
https://github.com/fluencelabs/tendermint
synced 2025-06-11 20:31:20 +00:00
filelogger to write output to a file
This commit is contained in:
8
Makefile
8
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/...
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
|
6
cmd/filelogger/README.md
Normal file
6
cmd/filelogger/README.md
Normal file
@ -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
|
||||
```
|
63
cmd/filelogger/main.go
Normal file
63
cmd/filelogger/main.go
Normal file
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -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)
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user