util.go 1.1 KB

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