123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- /*
- * ECAL
- *
- * Copyright 2020 Matthias Ladkau. All rights reserved.
- *
- * This Source Code Form is subject to the terms of the MIT
- * License, If a copy of the MIT License was not distributed with this
- * file, You can obtain one at https://opensource.org/licenses/MIT.
- */
- package util
- import (
- "fmt"
- "log"
- "devt.de/krotik/common/datautil"
- )
- // Logging implementations
- // =======================
- /*
- MemoryLogger collects log messages in a RingBuffer in memory.
- */
- type MemoryLogger struct {
- *datautil.RingBuffer
- }
- /*
- NewMemoryLogger returns a new memory logger instance.
- */
- func NewMemoryLogger(size int) *MemoryLogger {
- return &MemoryLogger{datautil.NewRingBuffer(size)}
- }
- /*
- LogError adds a new error log message.
- */
- func (ml *MemoryLogger) LogError(m ...interface{}) {
- ml.RingBuffer.Add(fmt.Sprintf("error: %v", fmt.Sprint(m...)))
- }
- /*
- LogInfo adds a new info log message.
- */
- func (ml *MemoryLogger) LogInfo(m ...interface{}) {
- ml.RingBuffer.Add(fmt.Sprintf("%v", fmt.Sprint(m...)))
- }
- /*
- LogDebug adds a new debug log message.
- */
- func (ml *MemoryLogger) LogDebug(m ...interface{}) {
- ml.RingBuffer.Add(fmt.Sprintf("debug: %v", fmt.Sprint(m...)))
- }
- /*
- Slice returns the contents of the current log as a slice.
- */
- func (ml *MemoryLogger) Slice() []string {
- sl := ml.RingBuffer.Slice()
- ret := make([]string, len(sl))
- for i, lm := range sl {
- ret[i] = lm.(string)
- }
- return ret
- }
- /*
- Reset resets the current log.
- */
- func (ml *MemoryLogger) Reset() {
- ml.RingBuffer.Reset()
- }
- /*
- Size returns the current log size.
- */
- func (ml *MemoryLogger) Size() int {
- return ml.RingBuffer.Size()
- }
- /*
- String returns the current log as a string.
- */
- func (ml *MemoryLogger) String() string {
- return ml.RingBuffer.String()
- }
- /*
- StdOutLogger writes log messages to stdout.
- */
- type StdOutLogger struct {
- stdlog func(v ...interface{})
- }
- /*
- NewStdOutLogger returns a stdout logger instance.
- */
- func NewStdOutLogger() *StdOutLogger {
- return &StdOutLogger{log.Print}
- }
- /*
- LogError adds a new error log message.
- */
- func (sl *StdOutLogger) LogError(m ...interface{}) {
- sl.stdlog(fmt.Sprintf("error: %v", fmt.Sprint(m...)))
- }
- /*
- LogInfo adds a new info log message.
- */
- func (sl *StdOutLogger) LogInfo(m ...interface{}) {
- sl.stdlog(fmt.Sprintf("%v", fmt.Sprint(m...)))
- }
- /*
- LogDebug adds a new debug log message.
- */
- func (sl *StdOutLogger) LogDebug(m ...interface{}) {
- sl.stdlog(fmt.Sprintf("debug: %v", fmt.Sprint(m...)))
- }
- /*
- NullLogger discards log messages.
- */
- type NullLogger struct {
- }
- /*
- NewNullLogger returns a null logger instance.
- */
- func NewNullLogger() *NullLogger {
- return &NullLogger{}
- }
- /*
- LogError adds a new error log message.
- */
- func (nl *NullLogger) LogError(m ...interface{}) {
- }
- /*
- LogInfo adds a new info log message.
- */
- func (nl *NullLogger) LogInfo(m ...interface{}) {
- }
- /*
- LogDebug adds a new debug log message.
- */
- func (nl *NullLogger) LogDebug(m ...interface{}) {
- }
|