1. package log: bugfix 日志业务方内容有换行,并且有源码行号时,整行日志添加换行 2. package connection: debug log

pull/2/head
q191201771 6 years ago
parent a9b2fe8f86
commit b2c187b59f

@ -38,13 +38,15 @@ type Connection interface {
// 如果使用了 bufio 写缓冲,则将缓冲中的数据发送出去 // 如果使用了 bufio 写缓冲,则将缓冲中的数据发送出去
// 如果使用了 channel 异步发送,则阻塞等待,直到之前 channel 中的数据全部发送完毕 // 如果使用了 channel 异步发送,则阻塞等待,直到之前 channel 中的数据全部发送完毕
// 一般在 Close 前,想要将剩余数据发送完毕时调用
Flush() error Flush() error
// 阻塞直到连接主动或被动关闭 // 阻塞直到连接关闭或发生错误
// @return 返回 nil 则是本端主动调用 Close 关闭 // @return 返回 nil 则是本端主动调用 Close 关闭
Done() <-chan error Done() <-chan error
// TODO chef: 这几个接口是否不提供 // TODO chef: 这几个接口是否不提供
// Mod类型函数不加锁需要调用方保证不发生竞态调用
ModWriteChanSize(n int) ModWriteChanSize(n int)
ModWriteBufSize(n int) ModWriteBufSize(n int)
ModReadTimeoutMS(n int) ModReadTimeoutMS(n int)
@ -60,7 +62,7 @@ type Config struct {
ReadTimeoutMS int ReadTimeoutMS int
WriteTimeoutMS int WriteTimeoutMS int
// 如果不0则写使用 channel 将数据发送到后台协程中发送 // 如果不0则写使用 channel 将数据发送到后台协程中发送
WChanSize int WChanSize int
} }
@ -70,7 +72,6 @@ const (
_ wMsgT = iota _ wMsgT = iota
wMsgTWrite wMsgTWrite
wMsgTFlush wMsgTFlush
wMsgTClose // TODO chef: 没有使用
) )
type wmsg struct { type wmsg struct {
@ -114,9 +115,6 @@ type connection struct {
closeOnce sync.Once closeOnce sync.Once
} }
// Mod类型函数不加锁
// 由调用方保证不和写操作并发执行
func (c *connection) ModWriteChanSize(n int) { func (c *connection) ModWriteChanSize(n int) {
if c.config.WChanSize > 0 { if c.config.WChanSize > 0 {
panic(connectionErr) panic(connectionErr)
@ -155,13 +153,13 @@ func (c *connection) ReadAtLeast(buf []byte, min int) (n int, err error) {
if c.config.ReadTimeoutMS > 0 { if c.config.ReadTimeoutMS > 0 {
err = c.SetReadDeadline(time.Now().Add(time.Duration(c.config.ReadTimeoutMS) * time.Millisecond)) err = c.SetReadDeadline(time.Now().Add(time.Duration(c.config.ReadTimeoutMS) * time.Millisecond))
if err != nil { if err != nil {
log.Debugf("error=%v", err) log.Debugf("nezha connection. error=%v, conn=%p", err, c)
return 0, err return 0, err
} }
} }
n, err = io.ReadAtLeast(c.r, buf, min) n, err = io.ReadAtLeast(c.r, buf, min)
if err != nil { if err != nil {
log.Debugf("error=%v", err) log.Debugf("nezha connection. error=%v, conn=%p", err, c)
c.close(err) c.close(err)
} }
return n, err return n, err
@ -176,13 +174,13 @@ func (c *connection) ReadLine() (line []byte, isPrefix bool, err error) {
if c.config.ReadTimeoutMS > 0 { if c.config.ReadTimeoutMS > 0 {
err = c.SetReadDeadline(time.Now().Add(time.Duration(c.config.ReadTimeoutMS) * time.Millisecond)) err = c.SetReadDeadline(time.Now().Add(time.Duration(c.config.ReadTimeoutMS) * time.Millisecond))
if err != nil { if err != nil {
log.Debugf("error=%v", err) log.Debugf("nezha connection. error=%v, conn=%p", err, c)
return nil, false, err return nil, false, err
} }
} }
line, isPrefix, err = bufioReader.ReadLine() line, isPrefix, err = bufioReader.ReadLine()
if err != nil { if err != nil {
log.Debugf("error=%v", err) log.Debugf("nezha connection. error=%v, conn=%p", err, c)
c.close(err) c.close(err)
} }
return line, isPrefix, err return line, isPrefix, err
@ -199,13 +197,13 @@ func (c *connection) Read(b []byte) (n int, err error) {
if c.config.ReadTimeoutMS > 0 { if c.config.ReadTimeoutMS > 0 {
err = c.SetReadDeadline(time.Now().Add(time.Duration(c.config.ReadTimeoutMS) * time.Millisecond)) err = c.SetReadDeadline(time.Now().Add(time.Duration(c.config.ReadTimeoutMS) * time.Millisecond))
if err != nil { if err != nil {
log.Debugf("error=%v", err) log.Debugf("nezha connection. error=%v, conn=%p", err, c)
return 0, err return 0, err
} }
} }
n, err = c.r.Read(b) n, err = c.r.Read(b)
if err != nil { if err != nil {
log.Debugf("error=%v", err) log.Debugf("nezha connection. error=%v, conn=%p", err, c)
c.close(err) c.close(err)
} }
return n, err return n, err
@ -223,13 +221,13 @@ func (c *connection) write(b []byte) (n int, err error) {
if c.config.WriteTimeoutMS > 0 { if c.config.WriteTimeoutMS > 0 {
err = c.SetWriteDeadline(time.Now().Add(time.Duration(c.config.WriteTimeoutMS) * time.Millisecond)) err = c.SetWriteDeadline(time.Now().Add(time.Duration(c.config.WriteTimeoutMS) * time.Millisecond))
if err != nil { if err != nil {
log.Debugf("error=%v", err) log.Debugf("nezha connection. error=%v, conn=%p", err, c)
return 0, err return 0, err
} }
} }
n, err = c.w.Write(b) n, err = c.w.Write(b)
if err != nil { if err != nil {
log.Debugf("error=%v", err) log.Debugf("nezha connection. error=%v, conn=%p", err, c)
c.close(err) c.close(err)
} }
return n, err return n, err
@ -245,18 +243,16 @@ func (c *connection) runWriteLoop() {
switch msg.t { switch msg.t {
case wMsgTWrite: case wMsgTWrite:
if _, err := c.write(msg.b); err != nil { if _, err := c.write(msg.b); err != nil {
log.Debugf("error=%v", err) log.Debugf("nezha connection. error=%v, conn=%p", err, c)
return return
} }
case wMsgTFlush: case wMsgTFlush:
if err := c.flush(); err != nil { if err := c.flush(); err != nil {
log.Debugf("error=%v", err) log.Debugf("nezha connection. error=%v, conn=%p", err, c)
c.flushDoneChan <- struct{}{} c.flushDoneChan <- struct{}{}
return return
} }
c.flushDoneChan <- struct{}{} c.flushDoneChan <- struct{}{}
case wMsgTClose:
// TODO chef: 是否需要
} }
} }
} }
@ -278,12 +274,12 @@ func (c *connection) flush() error {
if c.config.WriteTimeoutMS > 0 { if c.config.WriteTimeoutMS > 0 {
err := c.SetWriteDeadline(time.Now().Add(time.Duration(c.config.WriteTimeoutMS) * time.Millisecond)) err := c.SetWriteDeadline(time.Now().Add(time.Duration(c.config.WriteTimeoutMS) * time.Millisecond))
if err != nil { if err != nil {
log.Debugf("error=%v", err) log.Debugf("nezha connection. error=%v, conn=%p", err, c)
return err return err
} }
} }
if err := w.Flush(); err != nil { if err := w.Flush(); err != nil {
log.Debugf("error=%v", err) log.Debugf("nezha connection. error=%v, conn=%p", err, c)
c.close(err) c.close(err)
return err return err
} }
@ -292,13 +288,13 @@ func (c *connection) flush() error {
} }
func (c *connection) Close() error { func (c *connection) Close() error {
log.Debugf("Close.") log.Debugf("nezha connection Close. conn=%p", c)
c.close(nil) c.close(nil)
return nil return nil
} }
func (c *connection) close(err error) { func (c *connection) close(err error) {
log.Debugf("close. err=%v", err) log.Debugf("nezha connection close. err=%v, conn=%p", err, c)
c.closeOnce.Do(func() { c.closeOnce.Do(func() {
if c.config.WChanSize > 0 { if c.config.WChanSize > 0 {
c.exitChan <- struct{}{} c.exitChan <- struct{}{}
@ -310,15 +306,6 @@ func (c *connection) close(err error) {
func (c *connection) Done() <-chan error { func (c *connection) Done() <-chan error {
return c.doneChan return c.doneChan
//err := <-c.doneChan
//log.Debugf("Done. err=%v", err)
//if err != nil {
// c.close(err)
//}
//
//ch := make(chan error, 1)
//ch <- err
//return ch
} }
func (c *connection) LocalAddr() net.Addr { func (c *connection) LocalAddr() net.Addr {
@ -332,7 +319,7 @@ func (c *connection) RemoteAddr() net.Addr {
func (c *connection) SetDeadline(t time.Time) error { func (c *connection) SetDeadline(t time.Time) error {
err := c.Conn.SetDeadline(t) err := c.Conn.SetDeadline(t)
if err != nil { if err != nil {
log.Debugf("error=%v", err) log.Debugf("nezha connection. error=%v, conn=%p", err, c)
c.close(err) c.close(err)
} }
return err return err
@ -341,7 +328,7 @@ func (c *connection) SetDeadline(t time.Time) error {
func (c *connection) SetReadDeadline(t time.Time) error { func (c *connection) SetReadDeadline(t time.Time) error {
err := c.Conn.SetReadDeadline(t) err := c.Conn.SetReadDeadline(t)
if err != nil { if err != nil {
log.Debugf("error=%v", err) log.Debugf("nezha connection. error=%v, conn=%p", err, c)
c.close(err) c.close(err)
} }
return err return err
@ -350,7 +337,7 @@ func (c *connection) SetReadDeadline(t time.Time) error {
func (c *connection) SetWriteDeadline(t time.Time) error { func (c *connection) SetWriteDeadline(t time.Time) error {
err := c.Conn.SetWriteDeadline(t) err := c.Conn.SetWriteDeadline(t)
if err != nil { if err != nil {
log.Debugf("error=%v", err) log.Debugf("nezha connection. error=%v, conn=%p", err, c)
c.close(err) c.close(err)
} }
return err return err

@ -221,7 +221,7 @@ func (l *logger) Out(level Level, calldepth int, s string) {
if l.c.ShortFileFlag { if l.c.ShortFileFlag {
writeShortFile(&l.buf, calldepth) writeShortFile(&l.buf, calldepth)
} }
if len(s) == 0 || s[len(s)-1] != '\n' { if l.buf.Len() == 0 || l.buf.Bytes()[l.buf.Len()-1] != '\n' {
l.buf.WriteByte('\n') l.buf.WriteByte('\n')
} }

@ -1,6 +1,7 @@
package log package log
import ( import (
"encoding/hex"
"github.com/q191201771/nezha/pkg/assert" "github.com/q191201771/nezha/pkg/assert"
originLog "log" originLog "log"
"os" "os"
@ -16,6 +17,8 @@ func TestLogger(t *testing.T) {
} }
l, err := New(c) l, err := New(c)
assert.Equal(t, nil, err) assert.Equal(t, nil, err)
buf := []byte("1234567890987654321")
l.Error(hex.Dump(buf))
l.Debugf("l test msg by Debug%s", "f") l.Debugf("l test msg by Debug%s", "f")
l.Infof("l test msg by Info%s", "f") l.Infof("l test msg by Info%s", "f")
l.Warnf("l test msg by Warn%s", "f") l.Warnf("l test msg by Warn%s", "f")
@ -30,6 +33,8 @@ func TestLogger(t *testing.T) {
} }
func TestGlobal(t *testing.T) { func TestGlobal(t *testing.T) {
buf := []byte("1234567890987654321")
Error(hex.Dump(buf))
Debugf("g test msg by Debug%s", "f") Debugf("g test msg by Debug%s", "f")
Infof("g test msg by Info%s", "f") Infof("g test msg by Info%s", "f")
Warnf("g test msg by Warn%s", "f") Warnf("g test msg by Warn%s", "f")

Loading…
Cancel
Save