|
|
@@ -0,0 +1,179 @@
|
|
|
+/*
|
|
|
+ * EliasDB
|
|
|
+ *
|
|
|
+ * Copyright 2016 Matthias Ladkau. All rights reserved.
|
|
|
+ *
|
|
|
+ * This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
+ */
|
|
|
+
|
|
|
+/*
|
|
|
+Package ecal contains the main API for the event condition language ECAL.
|
|
|
+*/
|
|
|
+package ecal
|
|
|
+
|
|
|
+import (
|
|
|
+ "fmt"
|
|
|
+ "log"
|
|
|
+
|
|
|
+ "devt.de/krotik/common/datautil"
|
|
|
+)
|
|
|
+
|
|
|
+// Logging
|
|
|
+// =======
|
|
|
+
|
|
|
+/*
|
|
|
+Logger is an interface for log message consumers.
|
|
|
+*/
|
|
|
+type Logger interface {
|
|
|
+
|
|
|
+ /*
|
|
|
+ LogError adds a new error log message.
|
|
|
+ */
|
|
|
+ LogError(p *Processor, v ...interface{})
|
|
|
+
|
|
|
+ /*
|
|
|
+ LogInfo adds a new info log message.
|
|
|
+ */
|
|
|
+ LogInfo(p *Processor, v ...interface{})
|
|
|
+
|
|
|
+ /*
|
|
|
+ LogDebug adds a new debug log message.
|
|
|
+ */
|
|
|
+ LogDebug(p *Processor, v ...interface{})
|
|
|
+}
|
|
|
+
|
|
|
+/*
|
|
|
+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(p *Processor, m ...interface{}) {
|
|
|
+ ml.RingBuffer.Add(fmt.Sprintf("error: %v", fmt.Sprint(m...)))
|
|
|
+}
|
|
|
+
|
|
|
+/*
|
|
|
+LogInfo adds a new info log message.
|
|
|
+*/
|
|
|
+func (ml *MemoryLogger) LogInfo(p *Processor, m ...interface{}) {
|
|
|
+ ml.RingBuffer.Add(fmt.Sprintf("info: %v", fmt.Sprint(m...)))
|
|
|
+}
|
|
|
+
|
|
|
+/*
|
|
|
+LogDebug adds a new debug log message.
|
|
|
+*/
|
|
|
+func (ml *MemoryLogger) LogDebug(p *Processor, 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() Logger {
|
|
|
+ return &StdOutLogger{log.Print}
|
|
|
+}
|
|
|
+
|
|
|
+/*
|
|
|
+LogError adds a new error log message.
|
|
|
+*/
|
|
|
+func (sl *StdOutLogger) LogError(p *Processor, m ...interface{}) {
|
|
|
+ sl.stdlog(fmt.Sprintf("error: %v", fmt.Sprint(m...)))
|
|
|
+}
|
|
|
+
|
|
|
+/*
|
|
|
+LogInfo adds a new info log message.
|
|
|
+*/
|
|
|
+func (sl *StdOutLogger) LogInfo(p *Processor, m ...interface{}) {
|
|
|
+ sl.stdlog(fmt.Sprintf("error: %v", fmt.Sprint(m...)))
|
|
|
+}
|
|
|
+
|
|
|
+/*
|
|
|
+LogDebug adds a new debug log message.
|
|
|
+*/
|
|
|
+func (sl *StdOutLogger) LogDebug(p *Processor, m ...interface{}) {
|
|
|
+ sl.stdlog(fmt.Sprintf("error: %v", fmt.Sprint(m...)))
|
|
|
+}
|
|
|
+
|
|
|
+/*
|
|
|
+NullLogger discards log messages.
|
|
|
+*/
|
|
|
+type NullLogger struct {
|
|
|
+}
|
|
|
+
|
|
|
+/*
|
|
|
+NewNullLogger returns a null logger instance.
|
|
|
+*/
|
|
|
+func NewNullLogger() Logger {
|
|
|
+ return &NullLogger{}
|
|
|
+}
|
|
|
+
|
|
|
+/*
|
|
|
+LogError adds a new error log message.
|
|
|
+*/
|
|
|
+func (nl *NullLogger) LogError(p *Processor, m ...interface{}) {
|
|
|
+}
|
|
|
+
|
|
|
+/*
|
|
|
+LogInfo adds a new info log message.
|
|
|
+*/
|
|
|
+func (nl *NullLogger) LogInfo(p *Processor, m ...interface{}) {
|
|
|
+}
|
|
|
+
|
|
|
+/*
|
|
|
+LogDebug adds a new debug log message.
|
|
|
+*/
|
|
|
+func (nl *NullLogger) LogDebug(p *Processor, m ...interface{}) {
|
|
|
+}
|