Skip to content

Commit

Permalink
use io.Copy to write to adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
philchia committed Dec 23, 2016
1 parent 54fe091 commit 81aa65e
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 11 deletions.
Binary file modified assets/bench.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 10 additions & 8 deletions gollog.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (

"errors"

"io"

"github.com/philchia/gol/adapter"
)

Expand All @@ -19,24 +21,25 @@ type gollog struct {
level LogLevel
option LogOption
adapters map[string]adapter.Adapter
logChan chan []byte
logChan chan *bytes.Buffer
doneChan chan struct{}
// mutex sync.RWMutex
}

func (l *gollog) msgPump() {

for msg := range l.logChan {
for buf := range l.logChan {
for k := range l.adapters {
l.adapters[k].Write(msg)
io.Copy(l.adapters[k], buf)
}
bufferPoolPut(buf)
}

close(l.doneChan)
}

func (l *gollog) put(msg []byte) {
l.logChan <- msg
func (l *gollog) put(buf *bytes.Buffer) {
l.logChan <- buf
}

// itoa: Cheap integer to fixed-width decimal ASCII. Give a negative width to avoid zero-padding.
Expand Down Expand Up @@ -128,9 +131,8 @@ func (l *gollog) generateLog(buf *bytes.Buffer, callDepth int, level LogLevel, m
func (l *gollog) output(callDepth int, level LogLevel, msg string) {
buf := bufferPoolGet()
l.generateLog(buf, callDepth, level, msg)
bts := buf.Bytes()
bufferPoolPut(buf)
l.put(bts)

l.put(buf)
}

// Debug will prinnt log as DEBUG level
Expand Down
4 changes: 3 additions & 1 deletion logger.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package gol

import (
"bytes"

"github.com/philchia/gol/adapter"
"github.com/philchia/gol/adapter/console"
)
Expand Down Expand Up @@ -38,7 +40,7 @@ func NewLogger(level LogLevel) Logger {
logger := &gollog{
level: level,
option: LstdFlags,
logChan: make(chan []byte, 10240),
logChan: make(chan *bytes.Buffer, 10240),
doneChan: make(chan struct{}),
adapters: make(map[string]adapter.Adapter, 1),
}
Expand Down
7 changes: 5 additions & 2 deletions logger_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package gol

import "testing"
import (
"bytes"
"testing"
)

func TestNewLogger(t *testing.T) {
type args struct {
Expand All @@ -19,7 +22,7 @@ func TestNewLogger(t *testing.T) {
&gollog{
level: DEBUG,
option: LstdFlags,
logChan: make(chan []byte, 1024),
logChan: make(chan *bytes.Buffer, 1024),
},
},
}
Expand Down

0 comments on commit 81aa65e

Please sign in to comment.