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.

49 lines
1.1 KiB

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