From 796d0a2632996ba22ec10b536992950835b58c1d Mon Sep 17 00:00:00 2001 From: Christopher Young Date: Mon, 7 Mar 2016 23:51:53 -0500 Subject: [PATCH] /view_logs page. Fixes #232. --- main/managementinterface.go | 91 ++++++++++++++++++++++++++++++++++++- web/plates/logs.html | 2 +- 2 files changed, 91 insertions(+), 2 deletions(-) diff --git a/main/managementinterface.go b/main/managementinterface.go index dc945c9c2..d5447e08d 100644 --- a/main/managementinterface.go +++ b/main/managementinterface.go @@ -13,13 +13,16 @@ import ( "encoding/hex" "encoding/json" "fmt" + humanize "github.com/dustin/go-humanize" "golang.org/x/net/websocket" "io" + "io/ioutil" "log" "net/http" "os" "strings" "syscall" + "text/template" "time" ) @@ -35,7 +38,6 @@ var trafficUpdate *uibroadcaster /* The /weather websocket starts off by sending the current buffer of weather messages, then sends updates as they are received. */ - func handleWeatherWS(conn *websocket.Conn) { // Subscribe the socket to receive updates. weatherUpdate.AddSocket(conn) @@ -310,12 +312,99 @@ func defaultServer(w http.ResponseWriter, r *http.Request) { http.FileServer(http.Dir("/var/www")).ServeHTTP(w, r) } +// https://gist.github.com/alexisrobert/982674. +// Copyright (c) 2010-2014 Alexis ROBERT . +const dirlisting_tpl = ` + + + + +Index of {{.Name}} + + + +

Index of {{.Name}}

+
+ + + +{{range .Children_files}} + +{{end}} + +
NameLast ModifiedSize (bytes)Options
{{.Name}}{{.Mtime}}{{.Size}}Download
+
+
{{.ServerUA}}
+ +` + +type fileInfo struct { + Name string + Mtime string + Size string +} + +// Manages directory listings +type dirlisting struct { + Name string + Children_files []fileInfo + ServerUA string +} + +func viewLogs(w http.ResponseWriter, r *http.Request) { + + names, err := ioutil.ReadDir("/var/log/stratux/") + if err != nil { + return + } + + fi := make([]fileInfo, 0) + for _, val := range names { + if val.Name()[0] == '.' { + continue + } // Remove hidden files from listing + + if !val.IsDir() { + mtime := val.ModTime().Format("2006-Jan-02 15:04:05") + sz := humanize.Comma(val.Size()) + fi = append(fi, fileInfo{Name: val.Name(), Mtime: mtime, Size: sz}) + } + } + + tpl, err := template.New("tpl").Parse(dirlisting_tpl) + if err != nil { + return + } + data := dirlisting{Name: r.URL.Path, ServerUA: "Stratux " + stratuxVersion + "/" + stratuxBuild, + Children_files: fi} + + err = tpl.Execute(w, data) + if err != nil { + log.Printf("viewLogs() error: %s\n", err.Error()) + } + +} + func managementInterface() { weatherUpdate = NewUIBroadcaster() trafficUpdate = NewUIBroadcaster() http.HandleFunc("/", defaultServer) http.Handle("/logs/", http.StripPrefix("/logs/", http.FileServer(http.Dir("/var/log")))) + http.HandleFunc("/view_logs/", viewLogs) http.HandleFunc("/status", func(w http.ResponseWriter, req *http.Request) { diff --git a/web/plates/logs.html b/web/plates/logs.html index f430b641a..6f8365577 100755 --- a/web/plates/logs.html +++ b/web/plates/logs.html @@ -10,7 +10,7 @@

Logs

stratux.log
- SDR, AHRS, and GPS logs + SDR, AHRS, and GPS logs
(Enable device logging on "Settings" page)