log instead of fmt

This commit is contained in:
varyoo
2017-09-29 15:57:42 +02:00
parent 830f637e06
commit 88f09c3939
2 changed files with 13 additions and 6 deletions

View File

@@ -1,17 +1,20 @@
package dnstapio
import (
"fmt"
"io"
"log"
"os"
tap "github.com/dnstap/golang-dnstap"
"github.com/golang/protobuf/proto"
)
func init() {
log.SetOutput(os.Stdout)
}
// DnstapIO wraps the dnstap I/O routine.
type DnstapIO struct {
iolog io.Writer
writer io.WriteCloser
queue chan tap.Dnstap
stop chan bool
@@ -27,7 +30,7 @@ type Protocol interface {
// New dnstap I/O routine from Protocol.
func New(w Protocol) *DnstapIO {
dio := DnstapIO{iolog: os.Stdout}
dio := DnstapIO{}
dio.writer = w
dio.queue = make(chan tap.Dnstap, 10)
dio.stop = make(chan bool)
@@ -40,7 +43,7 @@ func (dio *DnstapIO) Dnstap(payload tap.Dnstap) {
select {
case dio.queue <- payload:
default:
fmt.Fprintln(dio.iolog, "[WARN] Dnstap payload dropped.")
log.Println("[WARN] Dnstap payload dropped.")
}
}
@@ -52,7 +55,7 @@ func (dio *DnstapIO) serve() {
if err == nil {
dio.writer.Write(frame)
} else {
fmt.Fprintf(dio.iolog, "[ERROR] Invalid dnstap payload dropped: %s\n", err)
log.Printf("[ERROR] Invalid dnstap payload dropped: %s\n", err)
}
case <-dio.stop:
close(dio.queue)

View File

@@ -3,6 +3,7 @@ package dnstapio
import (
"bytes"
"io/ioutil"
"log"
"sync"
"testing"
"time"
@@ -10,6 +11,10 @@ import (
tap "github.com/dnstap/golang-dnstap"
)
func init() {
log.SetOutput(ioutil.Discard)
}
type buf struct {
*bytes.Buffer
cost time.Duration
@@ -27,7 +32,6 @@ func (b buf) Close() error {
func TestRace(t *testing.T) {
b := buf{&bytes.Buffer{}, 100 * time.Millisecond}
dio := New(b)
dio.iolog = ioutil.Discard // don't flood Travis
wg := &sync.WaitGroup{}
wg.Add(10)
for i := 0; i < 10; i++ {