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.

107 lines
2.5 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. cmn "github.com/tendermint/tendermint/libs/common"
  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. func main() {
  28. // Read options
  29. headPath, chopSize, limitSize, version := parseFlags()
  30. if version {
  31. fmt.Printf("logjack version %v\n", Version)
  32. return
  33. }
  34. // Open Group
  35. group, err := auto.OpenGroup(headPath)
  36. if err != nil {
  37. fmt.Printf("logjack couldn't create output file %v\n", headPath)
  38. os.Exit(1)
  39. }
  40. group.SetHeadSizeLimit(chopSize)
  41. group.SetTotalSizeLimit(limitSize)
  42. err = group.Start()
  43. if err != nil {
  44. fmt.Printf("logjack couldn't start with file %v\n", headPath)
  45. os.Exit(1)
  46. }
  47. go func() {
  48. // Forever, read from stdin and write to AutoFile.
  49. buf := make([]byte, readBufferSize)
  50. for {
  51. n, err := os.Stdin.Read(buf)
  52. group.Write(buf[:n])
  53. group.Flush()
  54. if err != nil {
  55. group.Stop()
  56. if err == io.EOF {
  57. os.Exit(0)
  58. } else {
  59. fmt.Println("logjack errored")
  60. os.Exit(1)
  61. }
  62. }
  63. }
  64. }()
  65. // Trap signal
  66. cmn.TrapSignal(func() {
  67. fmt.Println("logjack shutting down")
  68. })
  69. }
  70. func parseBytesize(chopSize string) int64 {
  71. // Handle suffix multiplier
  72. var multiplier int64 = 1
  73. if strings.HasSuffix(chopSize, "T") {
  74. multiplier = 1042 * 1024 * 1024 * 1024
  75. chopSize = chopSize[:len(chopSize)-1]
  76. }
  77. if strings.HasSuffix(chopSize, "G") {
  78. multiplier = 1042 * 1024 * 1024
  79. chopSize = chopSize[:len(chopSize)-1]
  80. }
  81. if strings.HasSuffix(chopSize, "M") {
  82. multiplier = 1042 * 1024
  83. chopSize = chopSize[:len(chopSize)-1]
  84. }
  85. if strings.HasSuffix(chopSize, "K") {
  86. multiplier = 1042
  87. chopSize = chopSize[:len(chopSize)-1]
  88. }
  89. // Parse the numeric part
  90. chopSizeInt, err := strconv.Atoi(chopSize)
  91. if err != nil {
  92. panic(err)
  93. }
  94. return int64(chopSizeInt) * multiplier
  95. }