interaction_env_logger.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Copyright 2019 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package rafttest
  15. import (
  16. "fmt"
  17. "strings"
  18. "go.etcd.io/etcd/raft"
  19. )
  20. type logLevels [6]string
  21. func (l logLevels) strToLev(s string) int {
  22. for i, lvl := range l {
  23. if strings.ToUpper(s) == lvl {
  24. return i
  25. }
  26. }
  27. panic(fmt.Sprintf("unknown level %q", s))
  28. }
  29. var lvlNames logLevels = [...]string{"DEBUG", "INFO", "WARN", "ERROR", "FATAL", "NONE"}
  30. type RedirectLogger struct {
  31. *strings.Builder
  32. Lvl int // 0 = DEBUG, 1 = INFO, 2 = WARNING, 3 = ERROR, 4 = FATAL, 5 = NONE
  33. }
  34. var _ raft.Logger = (*RedirectLogger)(nil)
  35. func (l *RedirectLogger) printf(lvl int, format string, args ...interface{}) {
  36. if l.Lvl <= lvl {
  37. fmt.Fprint(l, lvlNames[lvl], " ")
  38. fmt.Fprintf(l, format, args...)
  39. if n := len(format); n > 0 && format[n-1] != '\n' {
  40. l.WriteByte('\n')
  41. }
  42. }
  43. }
  44. func (l *RedirectLogger) print(lvl int, args ...interface{}) {
  45. if l.Lvl <= lvl {
  46. fmt.Fprint(l, lvlNames[lvl], " ")
  47. fmt.Fprintln(l, args...)
  48. }
  49. }
  50. func (l *RedirectLogger) Debug(v ...interface{}) {
  51. l.print(0, v...)
  52. }
  53. func (l *RedirectLogger) Debugf(format string, v ...interface{}) {
  54. l.printf(0, format, v...)
  55. }
  56. func (l *RedirectLogger) Info(v ...interface{}) {
  57. l.print(1, v...)
  58. }
  59. func (l *RedirectLogger) Infof(format string, v ...interface{}) {
  60. l.printf(1, format, v...)
  61. }
  62. func (l *RedirectLogger) Warning(v ...interface{}) {
  63. l.print(2, v...)
  64. }
  65. func (l *RedirectLogger) Warningf(format string, v ...interface{}) {
  66. l.printf(2, format, v...)
  67. }
  68. func (l *RedirectLogger) Error(v ...interface{}) {
  69. l.print(3, v...)
  70. }
  71. func (l *RedirectLogger) Errorf(format string, v ...interface{}) {
  72. l.printf(3, format, v...)
  73. }
  74. func (l *RedirectLogger) Fatal(v ...interface{}) {
  75. l.print(4, v...)
  76. }
  77. func (l *RedirectLogger) Fatalf(format string, v ...interface{}) {
  78. l.printf(4, format, v...)
  79. }
  80. func (l *RedirectLogger) Panic(v ...interface{}) {
  81. l.print(4, v...)
  82. }
  83. func (l *RedirectLogger) Panicf(format string, v ...interface{}) {
  84. l.printf(4, format, v...)
  85. }