Browse Source

filelogger to write output to a file

pull/102/head
Jae Kwon 10 years ago
parent
commit
28d7a21156
5 changed files with 74 additions and 22 deletions
  1. +3
    -5
      Makefile
  2. +2
    -15
      binary/byteslice.go
  3. +6
    -0
      cmd/filelogger/README.md
  4. +63
    -0
      cmd/filelogger/main.go
  5. +0
    -2
      common/os.go

+ 3
- 5
Makefile View File

@ -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/...


+ 2
- 15
binary/byteslice.go View File

@ -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
- 0
cmd/filelogger/README.md View 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
- 0
cmd/filelogger/main.go View 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)
}
}
}
}

+ 0
- 2
common/os.go View File

@ -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)
}


Loading…
Cancel
Save