Skip to content

Commit 06f05ae

Browse files
matteosuppofacchinm
authored andcommitted
Add logger
1 parent 188337f commit 06f05ae

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

conn.go

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"encoding/json"
1313
"encoding/pem"
1414
"errors"
15+
"fmt"
1516
"io/ioutil"
1617
"net/http"
1718
"path/filepath"
@@ -129,17 +130,19 @@ func uploadHandler(c *gin.Context) {
129130
// Resolve commandline
130131
commandline, err := programmer.Resolve(data.Port, data.Board, filePath, data.Commandline, data.Extra, &Tools)
131132
if err != nil {
132-
133+
send(map[string]string{"ProgrammerStatus": "Error", "Msg": err.Error()})
133134
return
134135
}
135136

137+
l := PLogger{Verbose: data.Extra.Verbose}
138+
136139
// Upload
137140
if data.Extra.Network {
138141
send(map[string]string{"ProgrammerStatus": "Starting", "Cmd": "Network"})
139-
err = programmer.Network(data.Port, data.Board, filePath, commandline, data.Extra.Auth, nil)
142+
err = programmer.Network(data.Port, data.Board, filePath, commandline, data.Extra.Auth, l)
140143
} else {
141144
send(map[string]string{"ProgrammerStatus": "Starting", "Cmd": "Serial"})
142-
err = programmer.Serial(data.Port, commandline, data.Extra, nil)
145+
err = programmer.Serial(data.Port, commandline, data.Extra, l)
143146
}
144147

145148
// Handle result
@@ -153,6 +156,24 @@ func uploadHandler(c *gin.Context) {
153156
c.String(http.StatusAccepted, "")
154157
}
155158

159+
// PLogger sends the info from the programmer to the websocket
160+
type PLogger struct {
161+
Verbose bool
162+
}
163+
164+
// Debug only sends messages if verbose is true
165+
func (l PLogger) Debug(args ...interface{}) {
166+
if l.Verbose {
167+
l.Info(args...)
168+
}
169+
}
170+
171+
// Info always send messages
172+
func (l PLogger) Info(args ...interface{}) {
173+
output := fmt.Sprint(args...)
174+
send(map[string]string{"ProgrammerStatus": "Busy", "Msg": output})
175+
}
176+
156177
func send(args map[string]string) {
157178
mapB, _ := json.Marshal(args)
158179
h.broadcastSys <- mapB

0 commit comments

Comments
 (0)