github.com/supragya/TendermintConnector@v0.0.0-20210619045051-113e32b84fb1/_deprecated_chains/irisnet/libs/autofile/cmd/logjack.go (about)

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