Skip to content

Commit 68e4f24

Browse files
committed
change logger and cleanup
1 parent 6e6fdd4 commit 68e4f24

17 files changed

+71
-109
lines changed

conn.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
package main
44

55
import (
6+
log "github.com/Sirupsen/logrus"
67
"github.com/gin-gonic/gin"
78
"github.com/googollee/go-socket.io"
8-
"log"
99
"net/http"
1010
)
1111

discovery.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
package main
3030

3131
import (
32+
log "github.com/Sirupsen/logrus"
3233
"github.com/oleksandr/bonjour"
33-
"log"
3434
"net"
3535
"strings"
3636
"time"

download.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ package main
33

44
import (
55
"errors"
6+
log "github.com/Sirupsen/logrus"
67
"io"
78
"io/ioutil"
8-
"log"
99
"net/http"
1010
"os"
1111
"path/filepath"
@@ -14,7 +14,7 @@ import (
1414

1515
func saveFileonTempDir(filename string, sketch io.Reader) (path string, err error) {
1616
// create tmp dir
17-
tmpdir, err := ioutil.TempDir("", "serial-port-json-server")
17+
tmpdir, err := ioutil.TempDir("", "arduino-create-agent")
1818
if err != nil {
1919
return "", errors.New("Could not create temp directory to store downloaded file. Do you have permissions?")
2020
}
@@ -47,7 +47,7 @@ func downloadFromUrl(url string) (filename string, err error) {
4747
url = strings.TrimSpace(url)
4848

4949
// create tmp dir
50-
tmpdir, err := ioutil.TempDir("", "serial-port-json-server")
50+
tmpdir, err := ioutil.TempDir("", "arduino-create-agent")
5151
if err != nil {
5252
return "", errors.New("Could not create temp directory to store downloaded file. Do you have permissions?")
5353
}

dummy.go

-76
This file was deleted.

home.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<html>
22
<head>
3-
<title>Serial Port Example</title>
3+
<title>Arduino Create Agent Debug Interface</title>
44
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
55
<script type="text/javascript">
66
$(function() {

hub.go

+20-1
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@ package main
22

33
import (
44
"fmt"
5+
log "github.com/Sirupsen/logrus"
56
"github.com/kardianos/osext"
6-
"log"
77
//"os"
88
"os/exec"
99
//"path"
1010
//"path/filepath"
1111
//"runtime"
1212
//"debug"
1313
"encoding/json"
14+
"io"
15+
"os"
1416
"runtime"
1517
"runtime/debug"
1618
"strconv"
@@ -202,6 +204,8 @@ func checkCmd(m []byte) {
202204
//go getListViaWmiPnpEntity()
203205
} else if strings.HasPrefix(sl, "bufferalgorithm") {
204206
go spBufferAlgorithms()
207+
} else if strings.HasPrefix(sl, "log") {
208+
go logAction(sl)
205209
} else if strings.HasPrefix(sl, "baudrate") {
206210
go spBaudRates()
207211
} else if strings.HasPrefix(sl, "broadcast") {
@@ -227,6 +231,21 @@ func checkCmd(m []byte) {
227231
//log.Print("Done with checkCmd")
228232
}
229233

234+
var multi_writer = io.MultiWriter(&logger_ws, os.Stderr)
235+
236+
func logAction(sl string) {
237+
if strings.HasPrefix(sl, "log on") {
238+
*logDump = "on"
239+
log.SetOutput(multi_writer)
240+
} else if strings.HasPrefix(sl, "log off") {
241+
*logDump = "off"
242+
log.SetOutput(os.Stderr)
243+
} else if strings.HasPrefix(sl, "log show") {
244+
// TODO: send all the saved log to websocket
245+
//h.broadcastSys <- []byte("{\"BufFlowDebug\" : \"" + *logDump + "\"}")
246+
}
247+
}
248+
230249
func bufflowdebug(sl string) {
231250
log.Println("bufflowdebug start")
232251
if strings.HasPrefix(sl, "bufflowdebug on") {

main.go

+20-6
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@ package main
55

66
import (
77
"flag"
8-
"fmt"
8+
log "github.com/Sirupsen/logrus"
99
"go/build"
10-
"log"
1110
"os"
1211
"path/filepath"
1312
//"net/http/pprof"
@@ -52,6 +51,8 @@ var (
5251
// whether to do buffer flow debugging
5352
bufFlowDebugType = flag.String("bufflowdebug", "off", "off = (default) We do not send back any debug JSON, on = We will send back a JSON response with debug info based on the configuration of the buffer flow that the user picked")
5453

54+
logDump = flag.String("log", "off", "off = (default)")
55+
5556
// hostname. allow user to override, otherwise we look it up
5657
hostname = flag.String("hostname", "unknown-hostname", "Override the hostname we get from the OS")
5758

@@ -74,6 +75,15 @@ func defaultAssetPath() string {
7475
return p.Dir
7576
}
7677

78+
type logWriter struct{}
79+
80+
func (u *logWriter) Write(p []byte) (n int, err error) {
81+
h.broadcastSys <- p
82+
return 0, nil
83+
}
84+
85+
var logger_ws logWriter
86+
7787
func homeHandler(c *gin.Context) {
7888
homeTemplate.Execute(c.Writer, c.Request.Host)
7989
}
@@ -157,8 +167,11 @@ func startDaemon() {
157167
}
158168
}
159169

160-
// setup logging
161-
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
170+
//log.SetFormatter(&log.JSONFormatter{})
171+
172+
log.SetLevel(log.InfoLevel)
173+
174+
log.SetOutput(os.Stderr)
162175

163176
// see if we are supposed to wait 5 seconds
164177
if *isLaunchSelf {
@@ -274,13 +287,13 @@ func startDaemon() {
274287
r.Handle("WSS", "/socket.io/", socketHandler)
275288
go func() {
276289
if err := r.RunTLS(*addrSSL, filepath.Join(dest, "cert.pem"), filepath.Join(dest, "key.pem")); err != nil {
277-
fmt.Printf("Error trying to bind to port: %v, so exiting...", err)
290+
log.Printf("Error trying to bind to port: %v, so exiting...", err)
278291
log.Fatal("Error ListenAndServe:", err)
279292
}
280293
}()
281294

282295
if err := r.Run(*addr); err != nil {
283-
fmt.Printf("Error trying to bind to port: %v, so exiting...", err)
296+
log.Printf("Error trying to bind to port: %v, so exiting...", err)
284297
log.Fatal("Error ListenAndServe:", err)
285298
}
286299
}()
@@ -389,6 +402,7 @@ body {
389402
<form id="form">
390403
<input type="submit" value="Send" />
391404
<input type="text" id="msg" size="64"/>
405+
<input name="pause" type="checkbox" value="pause" id="myCheck"/> Pause <br>
392406
</form>
393407
</body>
394408
</html>

programmer.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import (
55
"bytes"
66
"encoding/json"
77
"fmt"
8+
log "github.com/Sirupsen/logrus"
89
"github.com/facchinm/go-serial"
910
"github.com/kardianos/osext"
1011
"io"
11-
"log"
1212
"mime/multipart"
1313
"net/http"
1414
"os"
@@ -109,6 +109,7 @@ func spProgramNetwork(portname string, boardname string, filePath string) error
109109

110110
// Check the response
111111
if res.StatusCode != http.StatusOK {
112+
log.Errorf("bad status: %s", res.Status)
112113
err = fmt.Errorf("bad status: %s", res.Status)
113114
}
114115

serial.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
//"github.com/mikepb/go-serial"
1313
//"github.com/facchinm/go-serial"
1414
//"github.com/kardianos/osext"
15-
"log"
15+
log "github.com/Sirupsen/logrus"
1616
//"os"
1717
"regexp"
1818
"runtime/debug"

seriallist.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
package main
44

55
import (
6+
log "github.com/Sirupsen/logrus"
67
"github.com/facchinm/go-serial"
7-
"log"
88
//"os"
99
"regexp"
1010
)
@@ -52,7 +52,7 @@ func GetList(network bool) ([]OsSerialPort, error) {
5252
} else if reFilter.MatchString(element.FriendlyName) {
5353
newarrPorts = append(newarrPorts, element)
5454
} else {
55-
log.Printf("serial port did not match. port: %v\n", element)
55+
log.Debug("serial port did not match. port: %v\n", element)
5656
}
5757

5858
}

seriallist_darwin.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package main
33
import (
44
//"fmt"
55
//"github.com/tarm/goserial"
6-
"log"
6+
log "github.com/Sirupsen/logrus"
77
"os"
88
"strings"
99
//"encoding/binary"

seriallist_linux.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ import (
1313
//"fmt"
1414
//"io"
1515
"bytes"
16+
log "github.com/Sirupsen/logrus"
1617
"io/ioutil"
17-
"log"
1818
"path/filepath"
1919
"regexp"
2020
"sort"

seriallist_windows.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import (
77
"github.com/mattn/go-ole/oleutil"
88
//"github.com/tarm/goserial"
99
//"github.com/johnlauer/goserial"
10+
log "github.com/Sirupsen/logrus"
1011
"github.com/facchinm/go-serial"
11-
"log"
1212
"os"
1313
"strings"
1414
//"encoding/binary"

serialport.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import (
44
"bytes"
55
"encoding/json"
66
//"github.com/johnlauer/goserial"
7+
log "github.com/Sirupsen/logrus"
78
"github.com/facchinm/go-serial"
89
"io"
9-
"log"
1010
"strconv"
1111
"time"
1212
)

trayicon.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
package main
3232

3333
import (
34-
"fmt"
34+
log "github.com/Sirupsen/logrus"
35+
"github.com/facchinm/go-serial"
3536
"github.com/facchinm/systray"
3637
"github.com/facchinm/systray/example/icon"
3738
"github.com/skratchdot/open-golang/open"

0 commit comments

Comments
 (0)