Skip to content

Commit

Permalink
修改函数实现Debugger,优化了部分代码
Browse files Browse the repository at this point in the history
  • Loading branch information
injoyai committed Dec 25, 2023
1 parent 2c822c8 commit 08bd513
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 33 deletions.
5 changes: 4 additions & 1 deletion dial/dial_serial.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ func Serial(cfg *SerialConfig) (io.ReadWriteCloser, string, error) {
cfg = &SerialConfig{}
}
if cfg.Address == "" {
cfg.Address = io.DefaultSerial
//获取串口列表的第一个
if list, _ := GetSerialPortList(); len(list) > 0 {
cfg.Address = list[0]
}
}
if cfg.BaudRate == 0 {
cfg.BaudRate = 9600
Expand Down
5 changes: 2 additions & 3 deletions extend/proxy/proxy_entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,9 @@ type Entity struct {
io.Logger
}

// Debug 调试模式
func (this *Entity) Debug(b ...bool) *Entity {
// Debug 调试模式,实现Debugger接口
func (this *Entity) Debug(b ...bool) {
this.debug = !(len(b) > 0 && !b[0])
return this
}

// SetWriteFunc 设置写入函数
Expand Down
4 changes: 2 additions & 2 deletions io_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,10 +228,10 @@ func (this *Client) SetKeepAlive(t time.Duration, keeps ...[]byte) {
//================================Logger================================

// Debug 调试模式,打印日志
func (this *Client) Debug(b ...bool) *Client {
// 为了实现Debugger接口,不需要返回值
func (this *Client) Debug(b ...bool) {
this.IWriter.Logger.Debug(b...)
this.IReadCloser.Debug(b...)
return this
}

// SetLogger 设置日志
Expand Down
1 change: 0 additions & 1 deletion io_const.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ const (

DefaultBufferSize = KB4 //默认buff大小,4KB
DefaultChannelSize = 100 //默认通道大小
DefaultSerial = "COM3" //默认串口
DefaultPort = 10086 //默认端口
DefaultPortStr = ":10086" //默认端口
DefaultConnectTimeout = time.Second * 2 //默认连接时间
Expand Down
5 changes: 2 additions & 3 deletions io_i_read_closer.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,10 @@ func (this *IReadCloser) SetReadIntervalTimeout(timeout time.Duration) *IReadClo

//================================Log================================

// Debug debug模式
func (this *IReadCloser) Debug(b ...bool) *IReadCloser {
// Debug debug模式,实现Debugger接口,不用返回值
func (this *IReadCloser) Debug(b ...bool) {
this.IReader.Logger.Debug(b...)
this.ICloser.Logger.Debug(b...)
return this
}

func (this *IReadCloser) SetLogger(logger Logger) *IReadCloser {
Expand Down
34 changes: 23 additions & 11 deletions io_i_write_closer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package io

import (
"context"
"sync/atomic"
"sync"
"time"
)

func NewIWriteCloser(writeCloser WriteCloser) *IWriteCloser {
Expand All @@ -19,8 +20,8 @@ func NewIWriteCloserWithContext(ctx context.Context, writeCloser WriteCloser) *I
type IWriteCloser struct {
*IWriter
*ICloser
queue chan []byte //写入队列
running uint32 //是否在运行
queue chan []byte //写入队列
once sync.Once //队列只执行一次
}

// SetKey 设置唯一标识
Expand All @@ -30,10 +31,12 @@ func (this *IWriteCloser) SetKey(key string) *IWriteCloser {
return this
}

func (this *IWriteCloser) Debug(b ...bool) *IWriteCloser {
// Debug 调试模式
// 实现Debugger接口
func (this *IWriteCloser) Debug(b ...bool) {
this.IWriter.Logger.Debug(b...)
this.ICloser.Logger.Debug(b...)
return this

}

// WriteQueue 写入队列
Expand All @@ -43,8 +46,19 @@ func (this *IWriteCloser) WriteQueue(p []byte) *IWriteCloser {
return this
}

// TryWriteQueue 尝试写入队列
func (this *IWriteCloser) TryWriteQueue(p []byte) *IWriteCloser {
// WriteQueueTimeout 写入队列,超时
func (this *IWriteCloser) WriteQueueTimeout(p []byte, timeout time.Duration) (int, error) {
this.runQueue()
select {
case this.queue <- p:
return len(p), nil
case <-time.After(timeout):
return 0, ErrWithWriteTimeout
}
}

// WriteQueueTry 尝试写入队列
func (this *IWriteCloser) WriteQueueTry(p []byte) *IWriteCloser {
this.runQueue()
select {
case this.queue <- p:
Expand All @@ -54,10 +68,8 @@ func (this *IWriteCloser) TryWriteQueue(p []byte) *IWriteCloser {
}

func (this *IWriteCloser) runQueue() {
if this.queue == nil {
this.once.Do(func() {
this.queue = this.NewWriteQueue(this.Ctx())
}
if atomic.SwapUint32(&this.running, 1) == 0 {
go this.For(func(ctx context.Context) error {
select {
case <-ctx.Done():
Expand All @@ -67,5 +79,5 @@ func (this *IWriteCloser) runQueue() {
return err
}
})
}
})
}
46 changes: 34 additions & 12 deletions io_log.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io

import (
"encoding/base64"
"fmt"
"github.com/injoyai/logs"
"os"
Expand All @@ -16,6 +17,12 @@ const (
LevelNone Level = 999
)

const (
codingHEX = "hex"
codingASCII = "ascii"
codingBase64 = "base64"
)

type Level int

var (
Expand Down Expand Up @@ -50,7 +57,7 @@ func newLogger(l Logger) *logger {
Logger: l,
level: LevelAll,
debug: true,
coding: "ascii",
coding: codingASCII,
}
}

Expand All @@ -65,18 +72,26 @@ func (this *logger) SetLevel(level Level) {
this.level = level
}

// Debug 实现Debugger接口,不用返回值
func (this *logger) Debug(b ...bool) {
this.debug = !(len(b) > 0 && !b[0])
}

// SetPrintWithHEX 设置字节编码方式hex
func (this *logger) SetPrintWithHEX() {
this.coding = "hex"
this.coding = codingHEX
}

// SetPrintWithASCII 设置字节编码方式ascii
func (this *logger) SetPrintWithASCII() {
this.coding = "ascii"
this.coding = codingASCII
}

func (this *logger) SetPrintWithBase64() {
this.coding = codingBase64
}

// Readln 打印读取到的数据
func (this *logger) Readln(prefix string, p []byte) {
if this.debug && LevelRead >= this.level {
switch this.coding {
Expand All @@ -88,23 +103,28 @@ func (this *logger) Readln(prefix string, p []byte) {
}
}

// Writeln 打印写入的数据
func (this *logger) Writeln(prefix string, p []byte) {
if this.debug && LevelWrite >= this.level {
switch this.coding {
case "hex":
case codingHEX:
this.Logger.Writef("%s%#x\n", prefix, p)
case "ascii":
case codingASCII:
this.Logger.Writef("%s%s\n", prefix, p)
case codingBase64:
this.Logger.Writef("%s%s\n", prefix, base64.StdEncoding.EncodeToString(p))
}
}
}

// Infof 打印信息
func (this *logger) Infof(format string, v ...interface{}) {
if this.debug && LevelInfo >= this.level {
this.Logger.Infof(format+"\n", v...)
}
}

// Errorf 打印错误
func (this *logger) Errorf(format string, v ...interface{}) {
if this.debug && LevelError >= this.level {
this.Logger.Errorf(format+"\n", v...)
Expand All @@ -119,7 +139,7 @@ func (this *logger) Errorf(format string, v ...interface{}) {

// NewLoggerWithWriter 新建输出到writer的日志
func NewLoggerWithWriter(w Writer) Logger {
return &printer{w}
return &logWriter{w}
}

// NewLoggerWithStdout 新建输出到终端的日志
Expand Down Expand Up @@ -150,14 +170,16 @@ type Logger interface {
Errorf(format string, v ...interface{})
}

type printer struct{ Writer }
//==========================================logWriter==========================================

type logWriter struct{ Writer }

func (p printer) Readf(format string, v ...interface{}) { p.printf(LevelRead, format, v...) }
func (p printer) Writef(format string, v ...interface{}) { p.printf(LevelWrite, format, v...) }
func (p printer) Infof(format string, v ...interface{}) { p.printf(LevelInfo, format, v...) }
func (p printer) Errorf(format string, v ...interface{}) { p.printf(LevelError, format, v...) }
func (p logWriter) Readf(format string, v ...interface{}) { p.printf(LevelRead, format, v...) }
func (p logWriter) Writef(format string, v ...interface{}) { p.printf(LevelWrite, format, v...) }
func (p logWriter) Infof(format string, v ...interface{}) { p.printf(LevelInfo, format, v...) }
func (p logWriter) Errorf(format string, v ...interface{}) { p.printf(LevelError, format, v...) }

func (p printer) printf(level Level, format string, v ...interface{}) {
func (p logWriter) printf(level Level, format string, v ...interface{}) {
timeStr := time.Now().Format("2006-01-02 15:04:05 ")
_, err := p.Writer.Write([]byte(fmt.Sprintf(timeStr+"["+level.String()+"] "+format, v...)))
logs.PrintErr(err)
Expand Down

0 comments on commit 08bd513

Please sign in to comment.