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.

122 lines
3.1 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. if err = group.Start(); err != nil {
  53. fmt.Printf("logjack couldn't start with file %v\n", headPath)
  54. os.Exit(1)
  55. }
  56. // Forever read from stdin and write to AutoFile.
  57. buf := make([]byte, readBufferSize)
  58. for {
  59. n, err := os.Stdin.Read(buf)
  60. if err != nil {
  61. if err := group.Stop(); err != nil {
  62. fmt.Fprintf(os.Stderr, "logjack stopped with error %v\n", headPath)
  63. os.Exit(1)
  64. }
  65. if err == io.EOF {
  66. os.Exit(0)
  67. } else {
  68. fmt.Println("logjack errored")
  69. os.Exit(1)
  70. }
  71. }
  72. _, err = group.Write(buf[:n])
  73. if err != nil {
  74. fmt.Fprintf(os.Stderr, "logjack failed write with error %v\n", headPath)
  75. os.Exit(1)
  76. }
  77. if err := group.FlushAndSync(); err != nil {
  78. fmt.Fprintf(os.Stderr, "logjack flushsync fail with error %v\n", headPath)
  79. os.Exit(1)
  80. }
  81. }
  82. }
  83. func parseBytesize(chopSize string) int64 {
  84. // Handle suffix multiplier
  85. var multiplier int64 = 1
  86. if strings.HasSuffix(chopSize, "T") {
  87. multiplier = 1042 * 1024 * 1024 * 1024
  88. chopSize = chopSize[:len(chopSize)-1]
  89. }
  90. if strings.HasSuffix(chopSize, "G") {
  91. multiplier = 1042 * 1024 * 1024
  92. chopSize = chopSize[:len(chopSize)-1]
  93. }
  94. if strings.HasSuffix(chopSize, "M") {
  95. multiplier = 1042 * 1024
  96. chopSize = chopSize[:len(chopSize)-1]
  97. }
  98. if strings.HasSuffix(chopSize, "K") {
  99. multiplier = 1042
  100. chopSize = chopSize[:len(chopSize)-1]
  101. }
  102. // Parse the numeric part
  103. chopSizeInt, err := strconv.Atoi(chopSize)
  104. if err != nil {
  105. panic(err)
  106. }
  107. return int64(chopSizeInt) * multiplier
  108. }