util.go

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