Skip to content

Commit

Permalink
changed LogManager.Run() function signature, added LogManager.Send(..…
Browse files Browse the repository at this point in the history
….) function and made *LogMessage channel an attribute.
  • Loading branch information
steenzout committed Nov 17, 2015
1 parent 3041fbd commit 3dae189
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 26 deletions.
6 changes: 2 additions & 4 deletions gol.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,10 @@ type LoggerManager interface {
Enable(n string) error
IsEnabled(n string) (bool, error)
List() []string
Run(<-chan *LogMessage)
Register(n string, l Logger) error
Run()
Send(*LogMessage) (err error)
}

// Manager is the instance responsible for handling log messages and sending them to all registered loggers.
var Manager LoggerManager

// LogChannel the channel to pass log messages from the application to the logger manager.
var LogChannel = make(chan *LogMessage, 1024)
45 changes: 34 additions & 11 deletions manager/simple/simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package simple

import (
"fmt"
"sync"

"github.com/mediaFORGE/gol"
)
Expand All @@ -31,12 +32,18 @@ type entry struct {
// Manager generic struct for a logger manager.
type Manager struct {
loggers map[string]entry
channel chan *gol.LogMessage
status bool
}

// mutex lock to guarantee only one Run() goroutine is running per LogManager instance.
var mutex = &sync.Mutex{}

// New creates a simple implementation of a logger manager.
func New() gol.LoggerManager {
func New(cap int) gol.LoggerManager {
return &Manager{
loggers: make(map[string]entry),
channel: make(chan *gol.LogMessage, cap),
}
}

Expand Down Expand Up @@ -113,19 +120,35 @@ func (m *Manager) Register(n string, l gol.Logger) error {
return nil
}

// Run keep reading from the input LogMessage channel and send messages to each logger.
func (m *Manager) Run(c <-chan *gol.LogMessage) {
go func() {
for msg := range c {
if m != nil {
for _, l := range m.loggers {
if l.status {
l.logger.Send(msg)
// Run start a goroutine that will distribute all messages in
// the LogManager channel to each registered and enabled logger.
func (m *Manager) Run() {
mutex.Lock()
if !m.status {
m.status = true
mutex.Unlock()

go func() {
for msg := range m.channel {
if m != nil {
for _, l := range m.loggers {
if l.status {
l.logger.Send(msg)
}
}
}
}
}
}()
}()
}
}

// Send process log message.
func (m *Manager) Send(msg *gol.LogMessage) (err error) {
if !m.status {
return fmt.Errorf("manager.simple.LogManager is not running")
}
m.channel <- msg
return nil
}

var _ gol.LoggerManager = (*Manager)(nil)
27 changes: 16 additions & 11 deletions manager/simple/simple_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,14 @@ import (
"github.com/stretchr/testify/suite"
)

const (
// Capacity the number of messages the log message channel can hold.
Capacity = 10
)

type ManagerTestSuite struct {
suite.Suite
manager gol.LoggerManager
testChannel chan *gol.LogMessage
manager gol.LoggerManager
}

func (s *ManagerTestSuite) testIsEnabled(n string, b bool, e error) {
Expand All @@ -50,12 +54,7 @@ func (s *ManagerTestSuite) testIsEnabled(n string, b bool, e error) {
}

func (s *ManagerTestSuite) SetupTest() {
s.manager = simple.New()
s.testChannel = make(chan *gol.LogMessage, 1)
}

func (s *ManagerTestSuite) TearDownTest() {
close(s.testChannel)
s.manager = simple.New(Capacity)
}

func (s *ManagerTestSuite) TestDeregister() {
Expand Down Expand Up @@ -140,7 +139,7 @@ func (s *ManagerTestSuite) TestRegister() {
assert.NotNil(s.T(), s.manager.Register("mock", nil))
}

func (s *ManagerTestSuite) TestRun() {
func (s *ManagerTestSuite) TestSend() {
m := gol.NewEmergency("field", "value")

// l1 will not filter the message
Expand All @@ -162,8 +161,8 @@ func (s *ManagerTestSuite) TestRun() {
s.manager.Register("l1", l1)
s.manager.Register("l2", l2)

go s.manager.Run(s.testChannel)
s.testChannel <- m
s.manager.Run()
assert.Nil(s.T(), s.manager.Send(m))
time.Sleep(1 * time.Second)

mf1.AssertExpectations(s.T())
Expand All @@ -174,3 +173,9 @@ func (s *ManagerTestSuite) TestRun() {
mfmt2.AssertExpectations(s.T())
mw2.AssertExpectations(s.T())
}

func (s *ManagerTestSuite) TestSendWithoutRun() {
m := gol.NewEmergency("field", "value")

assert.Equal(s.T(), s.manager.Send(m), fmt.Errorf("manager.simple.LogManager is not running"))
}

0 comments on commit 3dae189

Please sign in to comment.