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.9 KiB

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