new: centralized logging and implemented DELETE /api/events route, closes #5

This commit is contained in:
evilsocket 2018-01-08 06:39:44 +01:00
commit f1f146d3d7
21 changed files with 144 additions and 184 deletions

View file

@ -2,12 +2,22 @@ package session
import (
"fmt"
"os"
"sync"
"time"
"github.com/evilsocket/bettercap-ng/core"
)
const (
DEBUG = iota
INFO
IMPORTANT
WARNING
ERROR
FATAL
)
type Event struct {
Tag string `json:"tag"`
Time time.Time `json:"time"`
@ -23,16 +33,20 @@ func NewEvent(tag string, data interface{}) Event {
}
func (e Event) Print() {
fmt.Printf("[%s] [%s] [%s] %+v\n", e.Time, core.Bold("event"), core.Green(e.Tag), e.Data)
fmt.Printf("[%s] [%s] %v\n", e.Time, core.Green(e.Tag), e.Data)
}
type EventPool struct {
debug bool
silent bool
events []Event
lock *sync.Mutex
}
func NewEventPool() *EventPool {
func NewEventPool(debug bool, silent bool) *EventPool {
return &EventPool{
debug: debug,
silent: silent,
events: make([]Event, 0),
lock: &sync.Mutex{},
}
@ -46,6 +60,32 @@ func (p *EventPool) Add(tag string, data interface{}) {
e.Print()
}
func (p *EventPool) Log(level int, format string, args ...interface{}) {
if level == DEBUG && p.debug == false {
return
} else if level < ERROR && p.silent == true {
return
}
p.Add("sys.log", struct {
Level int
Message string
}{
level,
fmt.Sprintf(format, args...),
})
if level == FATAL {
os.Exit(1)
}
}
func (p *EventPool) Clear() {
p.lock.Lock()
defer p.lock.Unlock()
p.events = make([]Event, 0)
}
func (p *EventPool) Events() []Event {
p.lock.Lock()
defer p.lock.Unlock()