Skip to content

Commit 10b4042

Browse files
committed
Fix logging API for GUI and add DC
In the brez GUI program there were lines missing sometimes. It also occured when switched to logging to a channel. It most likely had to do something with the string that was built. Now the log-to-channel is kept (seems more concurrent-safe).
1 parent f885a81 commit 10b4042

File tree

2 files changed

+18
-56
lines changed

2 files changed

+18
-56
lines changed

asdu.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ func (infoObj InfoObj) writeInfo(typeId TypeId, buf *bytes.Buffer) error {
234234
binary.Write(buf, binary.LittleEndian, int16(val))
235235
infoObj.writeQualitySeparateOctet(buf)
236236

237-
case C_SC_NA_1:
237+
case C_SC_NA_1, C_DC_NA_1:
238238
// command info, todo make configurable
239239
infoObj.CommandInfo.Quoc = Quoc{
240240
Select: false,

logging.go

+17-55
Original file line numberDiff line numberDiff line change
@@ -6,96 +6,58 @@ import (
66
)
77

88
var (
9-
logStrings []string
10-
logDebug *log.Logger
11-
logError *log.Logger
12-
logInfo *log.Logger
9+
LogChan chan string
10+
11+
logDebug *log.Logger
12+
logError *log.Logger
13+
logInfo *log.Logger
1314

1415
vv104loggerDebug vv104logger
1516
vv104loggerError vv104logger
1617
vv104loggerInfo vv104logger
17-
// e.g. for use with GUI: GUI can set its update function for the text box here
18-
LogCallBack func()
1918
)
2019

2120
type vv104logger struct {
22-
logToBuffer bool
21+
logToChan bool
2322
logToStdOut bool
24-
logString string
25-
}
26-
27-
func addLogEntry(s string) {
28-
logStrings = append(logStrings, s)
29-
}
30-
31-
// API to read from logs (concurrently?)
32-
func ReadLogEntry() string {
33-
var s string = ""
34-
for _, logString := range logStrings {
35-
s += logString
36-
logStrings = logStrings[1:]
37-
38-
}
39-
return s
4023
}
4124

4225
func (vvlog vv104logger) Write(p []byte) (n int, err error) {
4326

44-
if vvlog.logToBuffer {
45-
addLogEntry(string(p))
27+
if vvlog.logToChan {
28+
// addLogEntry(string(p))
29+
LogChan <- string(p)
4630
}
4731

4832
if vvlog.logToStdOut {
4933
n, err = fmt.Print(string(p))
5034
}
5135

52-
if LogCallBack != nil {
53-
LogCallBack()
54-
}
55-
5636
return n, err
5737
}
5838

39+
func NewLogChan() chan string {
40+
LogChan = make(chan string, 50)
41+
return LogChan
42+
}
43+
5944
func initLogger(config Config) {
6045

6146
vv104loggerDebug = vv104logger{
62-
logToBuffer: config.LogToBuffer,
47+
logToChan: config.LogToBuffer,
6348
logToStdOut: config.LogToStdOut,
6449
}
6550
vv104loggerError = vv104logger{
66-
logToBuffer: config.LogToBuffer,
51+
logToChan: config.LogToBuffer,
6752
logToStdOut: config.LogToStdOut,
6853
}
6954
vv104loggerInfo = vv104logger{
70-
logToBuffer: config.LogToBuffer,
55+
logToChan: config.LogToBuffer,
7156
logToStdOut: config.LogToStdOut,
7257
}
7358

74-
// var output io.Writer
75-
// // var multiOutput io.MultiWriter() todo?
76-
77-
// if config.LogToBuffer {
78-
// output = &LogBuf
79-
// } else {
80-
// output = os.Stdout
81-
// }
82-
83-
// logDebug = log.New(output, "🪲 ", log.Ltime|log.Lshortfile)
84-
// logError = log.New(output, "❌ ", log.Ltime|log.Lshortfile)
85-
// logInfo = log.New(output, "🥨 ", log.Ltime)
86-
8759
logDebug = log.New(vv104loggerDebug, "Debug: ", log.Ltime|log.Lshortfile)
8860
logError = log.New(vv104loggerError, "ERROR: ", log.Ltime|log.Lshortfile)
8961
logInfo = log.New(vv104loggerInfo, "", log.Ltime)
9062

9163
}
92-
93-
// func logInfof(v any) {
94-
// logDebug.Println(v)
95-
96-
// // call callback function
97-
// LogCallBack()
98-
99-
// }
100-
101-
// str, err := InfoLogBuf.ReadString(0x0a) // 0x0a = newline

0 commit comments

Comments
 (0)