operation log

This commit is contained in:
llaoj 2022-11-11 17:06:22 +08:00
parent 16c534e1a9
commit 3e9fbfc86e
2 changed files with 60 additions and 2 deletions

48
utils/log.go Normal file
View File

@ -0,0 +1,48 @@
package utils
func FormatOperationLog(line *[]byte) (log string) {
ascii := map[byte]string{
0: "", //NUL
1: "SOH",
2: "STX",
3: "ETX",
4: "EOT",
5: "ENQ",
6: "ACK",
7: "BEL",
8: "BS",
9: "HT",
10: "LF",
11: "VT",
12: "FF",
13: "CR",
14: "SO",
15: "SI",
16: "DLE",
17: "DCI",
18: "DC2",
19: "DC3",
20: "DC4",
21: "NAK",
22: "SYN",
23: "TB",
24: "CAN",
25: "EM",
26: "SUB",
27: "ESC",
28: "FS",
29: "GS",
30: "RS",
31: "US",
127: "DEL",
}
for _, word := range *line {
if value, ok := ascii[word]; ok {
log += value
continue
}
log += string(word)
}
return
}

View File

@ -4,6 +4,8 @@ import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"github.com/sorenisanerd/gotty/utils"
"sync"
"github.com/pkg/errors"
@ -88,13 +90,14 @@ func (wt *WebTTY) Run(ctx context.Context) error {
go func() {
errs <- func() error {
buffer := make([]byte, wt.bufferSize)
lineBuffer := make([]byte, 1)
for {
n, err := wt.masterConn.Read(buffer)
if err != nil {
return ErrMasterClosed
}
err = wt.handleMasterReadEvent(buffer[:n])
err = wt.handleMasterReadEvent(buffer[:n], &lineBuffer)
if err != nil {
return err
}
@ -163,7 +166,7 @@ func (wt *WebTTY) masterWrite(data []byte) error {
return nil
}
func (wt *WebTTY) handleMasterReadEvent(data []byte) error {
func (wt *WebTTY) handleMasterReadEvent(data []byte, line *[]byte) error {
if len(data) == 0 {
return errors.New("unexpected zero length read from master")
}
@ -184,6 +187,13 @@ func (wt *WebTTY) handleMasterReadEvent(data []byte) error {
return errors.Wrapf(err, "failed to decode received data")
}
*line = append(*line, decodedBuffer[:n]...)
//fmt.Printf("master read: %v -> %v\n", decodedBuffer[:n], string(decodedBuffer[:n]))
if decodedBuffer[n-1] == 13 {
fmt.Printf("master read line: %v\n", utils.FormatOperationLog(line))
*line = nil
}
_, err = wt.slave.Write(decodedBuffer[:n])
if err != nil {
return errors.Wrapf(err, "failed to write received data to slave")