You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
2.8 KiB

  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "io"
  6. "os"
  7. "strconv"
  8. "strings"
  9. auto "github.com/tendermint/tendermint/libs/autofile"
  10. tmos "github.com/tendermint/tendermint/libs/os"
  11. )
  12. const Version = "0.0.1"
  13. const readBufferSize = 1024 // 1KB at a time
  14. // Parse command-line options
  15. func parseFlags() (headPath string, chopSize int64, limitSize int64, version bool) {
  16. var flagSet = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
  17. var chopSizeStr, limitSizeStr string
  18. flagSet.StringVar(&headPath, "head", "logjack.out", "Destination (head) file.")
  19. flagSet.StringVar(&chopSizeStr, "chop", "100M", "Move file if greater than this")
  20. flagSet.StringVar(&limitSizeStr, "limit", "10G", "Only keep this much (for each specified file). Remove old files.")
  21. flagSet.BoolVar(&version, "version", false, "Version")
  22. flagSet.Parse(os.Args[1:])
  23. chopSize = parseBytesize(chopSizeStr)
  24. limitSize = parseBytesize(limitSizeStr)
  25. return
  26. }
  27. type fmtLogger struct{}
  28. func (fmtLogger) Info(msg string, keyvals ...interface{}) {
  29. strs := make([]string, len(keyvals))
  30. for i, kv := range keyvals {
  31. strs[i] = fmt.Sprintf("%v", kv)
  32. }
  33. fmt.Printf("%s %s\n", msg, strings.Join(strs, ","))
  34. }
  35. func main() {
  36. // Stop upon receiving SIGTERM or CTRL-C.
  37. tmos.TrapSignal(fmtLogger{}, func() {
  38. fmt.Println("logjack shutting down")
  39. })
  40. // Read options
  41. headPath, chopSize, limitSize, version := parseFlags()
  42. if version {
  43. fmt.Printf("logjack version %v\n", Version)
  44. return
  45. }
  46. // Open Group
  47. group, err := auto.OpenGroup(headPath, auto.GroupHeadSizeLimit(chopSize), auto.GroupTotalSizeLimit(limitSize))
  48. if err != nil {
  49. fmt.Printf("logjack couldn't create output file %v\n", headPath)
  50. os.Exit(1)
  51. }
  52. err = group.Start()
  53. if err != nil {
  54. fmt.Printf("logjack couldn't start with file %v\n", headPath)
  55. os.Exit(1)
  56. }
  57. // Forever read from stdin and write to AutoFile.
  58. buf := make([]byte, readBufferSize)
  59. for {
  60. n, err := os.Stdin.Read(buf)
  61. group.Write(buf[:n])
  62. group.FlushAndSync()
  63. if err != nil {
  64. group.Stop()
  65. if err == io.EOF {
  66. os.Exit(0)
  67. } else {
  68. fmt.Println("logjack errored")
  69. os.Exit(1)
  70. }
  71. }
  72. }
  73. }
  74. func parseBytesize(chopSize string) int64 {
  75. // Handle suffix multiplier
  76. var multiplier int64 = 1
  77. if strings.HasSuffix(chopSize, "T") {
  78. multiplier = 1042 * 1024 * 1024 * 1024
  79. chopSize = chopSize[:len(chopSize)-1]
  80. }
  81. if strings.HasSuffix(chopSize, "G") {
  82. multiplier = 1042 * 1024 * 1024
  83. chopSize = chopSize[:len(chopSize)-1]
  84. }
  85. if strings.HasSuffix(chopSize, "M") {
  86. multiplier = 1042 * 1024
  87. chopSize = chopSize[:len(chopSize)-1]
  88. }
  89. if strings.HasSuffix(chopSize, "K") {
  90. multiplier = 1042
  91. chopSize = chopSize[:len(chopSize)-1]
  92. }
  93. // Parse the numeric part
  94. chopSizeInt, err := strconv.Atoi(chopSize)
  95. if err != nil {
  96. panic(err)
  97. }
  98. return int64(chopSizeInt) * multiplier
  99. }