github.com/GoneLikeAir/tail@v1.0.1/util/util.go (about)

     1  // Copyright (c) 2015 HPE Software Inc. All rights reserved.
     2  // Copyright (c) 2013 ActiveState Software Inc. All rights reserved.
     3  
     4  package util
     5  
     6  import (
     7  	"fmt"
     8  	"log"
     9  	"os"
    10  	"runtime/debug"
    11  )
    12  
    13  type Logger struct {
    14  	*log.Logger
    15  }
    16  
    17  var LOGGER = &Logger{log.New(os.Stderr, "", log.LstdFlags)}
    18  
    19  // fatal is like panic except it displays only the current goroutine's stack.
    20  func Fatal(format string, v ...interface{}) {
    21  	// https://github.com/hpcloud/log/blob/master/log.go#L45
    22  	LOGGER.Output(2, fmt.Sprintf("FATAL -- "+format, v...)+"\n"+string(debug.Stack()))
    23  	os.Exit(1)
    24  }
    25  
    26  // partitionString partitions the string into chunks of given size,
    27  // with the last chunk of variable size.
    28  func PartitionString(s string, chunkSize int) []string {
    29  	if chunkSize <= 0 {
    30  		panic("invalid chunkSize")
    31  	}
    32  	length := len(s)
    33  	chunks := 1 + length/chunkSize
    34  	start := 0
    35  	end := chunkSize
    36  	parts := make([]string, 0, chunks)
    37  	for {
    38  		if end > length {
    39  			end = length
    40  		}
    41  		parts = append(parts, s[start:end])
    42  		if end == length {
    43  			break
    44  		}
    45  		start, end = end, end+chunkSize
    46  	}
    47  	return parts
    48  }