logging.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * ECAL
  3. *
  4. * Copyright 2020 Matthias Ladkau. All rights reserved.
  5. *
  6. * This Source Code Form is subject to the terms of the MIT
  7. * License, If a copy of the MIT License was not distributed with this
  8. * file, You can obtain one at https://opensource.org/licenses/MIT.
  9. */
  10. package util
  11. import (
  12. "fmt"
  13. "log"
  14. "devt.de/krotik/common/datautil"
  15. )
  16. // Logging implementations
  17. // =======================
  18. /*
  19. MemoryLogger collects log messages in a RingBuffer in memory.
  20. */
  21. type MemoryLogger struct {
  22. *datautil.RingBuffer
  23. }
  24. /*
  25. NewMemoryLogger returns a new memory logger instance.
  26. */
  27. func NewMemoryLogger(size int) *MemoryLogger {
  28. return &MemoryLogger{datautil.NewRingBuffer(size)}
  29. }
  30. /*
  31. LogError adds a new error log message.
  32. */
  33. func (ml *MemoryLogger) LogError(m ...interface{}) {
  34. ml.RingBuffer.Add(fmt.Sprintf("error: %v", fmt.Sprint(m...)))
  35. }
  36. /*
  37. LogInfo adds a new info log message.
  38. */
  39. func (ml *MemoryLogger) LogInfo(m ...interface{}) {
  40. ml.RingBuffer.Add(fmt.Sprintf("%v", fmt.Sprint(m...)))
  41. }
  42. /*
  43. LogDebug adds a new debug log message.
  44. */
  45. func (ml *MemoryLogger) LogDebug(m ...interface{}) {
  46. ml.RingBuffer.Add(fmt.Sprintf("debug: %v", fmt.Sprint(m...)))
  47. }
  48. /*
  49. Slice returns the contents of the current log as a slice.
  50. */
  51. func (ml *MemoryLogger) Slice() []string {
  52. sl := ml.RingBuffer.Slice()
  53. ret := make([]string, len(sl))
  54. for i, lm := range sl {
  55. ret[i] = lm.(string)
  56. }
  57. return ret
  58. }
  59. /*
  60. Reset resets the current log.
  61. */
  62. func (ml *MemoryLogger) Reset() {
  63. ml.RingBuffer.Reset()
  64. }
  65. /*
  66. Size returns the current log size.
  67. */
  68. func (ml *MemoryLogger) Size() int {
  69. return ml.RingBuffer.Size()
  70. }
  71. /*
  72. String returns the current log as a string.
  73. */
  74. func (ml *MemoryLogger) String() string {
  75. return ml.RingBuffer.String()
  76. }
  77. /*
  78. StdOutLogger writes log messages to stdout.
  79. */
  80. type StdOutLogger struct {
  81. stdlog func(v ...interface{})
  82. }
  83. /*
  84. NewStdOutLogger returns a stdout logger instance.
  85. */
  86. func NewStdOutLogger() *StdOutLogger {
  87. return &StdOutLogger{log.Print}
  88. }
  89. /*
  90. LogError adds a new error log message.
  91. */
  92. func (sl *StdOutLogger) LogError(m ...interface{}) {
  93. sl.stdlog(fmt.Sprintf("error: %v", fmt.Sprint(m...)))
  94. }
  95. /*
  96. LogInfo adds a new info log message.
  97. */
  98. func (sl *StdOutLogger) LogInfo(m ...interface{}) {
  99. sl.stdlog(fmt.Sprintf("%v", fmt.Sprint(m...)))
  100. }
  101. /*
  102. LogDebug adds a new debug log message.
  103. */
  104. func (sl *StdOutLogger) LogDebug(m ...interface{}) {
  105. sl.stdlog(fmt.Sprintf("debug: %v", fmt.Sprint(m...)))
  106. }
  107. /*
  108. NullLogger discards log messages.
  109. */
  110. type NullLogger struct {
  111. }
  112. /*
  113. NewNullLogger returns a null logger instance.
  114. */
  115. func NewNullLogger() *NullLogger {
  116. return &NullLogger{}
  117. }
  118. /*
  119. LogError adds a new error log message.
  120. */
  121. func (nl *NullLogger) LogError(m ...interface{}) {
  122. }
  123. /*
  124. LogInfo adds a new info log message.
  125. */
  126. func (nl *NullLogger) LogInfo(m ...interface{}) {
  127. }
  128. /*
  129. LogDebug adds a new debug log message.
  130. */
  131. func (nl *NullLogger) LogDebug(m ...interface{}) {
  132. }