-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio_i_printer.go
74 lines (61 loc) · 1.49 KB
/
io_i_printer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package io
import (
"fmt"
"log"
"strings"
)
func NewIPrinter() *IPrinter {
cp := &IPrinter{}
cp.SetPrintWithASCII()
return cp
}
type IPrinter struct {
debug bool
printFunc PrintFunc
}
// SetPrintFunc 设置打印函数
func (this *IPrinter) SetPrintFunc(fn PrintFunc) {
this.printFunc = fn
}
// SetPrintWithHEX 设置打印HEX
func (this *IPrinter) SetPrintWithHEX() {
this.printFunc = PrintWithHEX
}
// SetPrintWithASCII 设置打印ASCII
func (this *IPrinter) SetPrintWithASCII() {
this.printFunc = PrintWithASCII
}
// Print 打印输出
func (this *IPrinter) Print(msg Message, tag ...string) {
if this.debug && this.printFunc != nil {
this.printFunc(msg, tag...)
}
}
// Debug 调试模式
func (this *IPrinter) Debug(b ...bool) {
this.debug = !(len(b) > 0 && !b[0])
}
//================================Common================================
func PrintWithHEX(msg Message, tag ...string) {
log.Print(PrintfWithHEX(msg, tag...))
}
func PrintWithASCII(msg Message, tag ...string) {
log.Print(PrintfWithASCII(msg, tag...))
}
func PrintfWithHEX(msg Message, tag ...string) string {
t := strings.Join(tag, "][")
if len(t) > 0 {
t = "[" + t + "]"
}
if strings.Contains(t, TagRead) || strings.Contains(t, TagWrite) {
return fmt.Sprintf("%s %s", t, msg.HEX())
}
return fmt.Sprintf("%s %s", t, msg.ASCII())
}
func PrintfWithASCII(msg Message, tag ...string) string {
t := strings.Join(tag, "][")
if len(t) > 0 {
t = "[" + t + "]"
}
return fmt.Sprintf("%s %s", t, msg.ASCII())
}