diff --git a/config/config.go b/config/config.go index ac94efe..150005d 100644 --- a/config/config.go +++ b/config/config.go @@ -2,10 +2,13 @@ package config import ( "encoding/json" + "fmt" "io/ioutil" "log" "os" + daulog "github.com/tardisx/discord-auto-upload/log" + "github.com/mitchellh/go-homedir" ) @@ -24,11 +27,11 @@ const CurrentVersion string = "0.8" // Load the current config or initialise with defaults func LoadOrInit() { configPath := configPath() - log.Printf("Trying to load from %s\n", configPath) + daulog.SendLog(fmt.Sprintf("Trying to load config from %s", configPath), daulog.LogTypeDebug) _, err := os.Stat(configPath) if os.IsNotExist(err) { - log.Printf("NOTE: No config file, writing out sample configuration") - log.Printf("You need to set the configuration via the web interface") + daulog.SendLog("NOTE: No config file, writing out sample configuration", daulog.LogTypeInfo) + daulog.SendLog("You need to set the configuration via the web interface", daulog.LogTypeInfo) Config.WebHookURL = "" Config.Path = homeDir() + string(os.PathSeparator) + "screenshots" @@ -52,7 +55,7 @@ func LoadConfig() { } func SaveConfig() { - log.Print("saving configuration") + daulog.SendLog("saving configuration", daulog.LogTypeInfo) path := configPath() jsonString, _ := json.Marshal(Config) err := ioutil.WriteFile(path, jsonString, os.ModePerm) diff --git a/data/config.html b/data/config.html index 6a70450..9f40f01 100644 --- a/data/config.html +++ b/data/config.html @@ -123,10 +123,11 @@ $(document).ready(function() { .done(function(data) { var this_el = $(".config-item[data-key='"+key+"']").find('.rest-field'); if (this_el.hasClass('rest-field-boolean')) { - this_el.prop('checked', data.Value); + this_el.prop('checked', data.value); } else { - this_el.val(data.Value); + + this_el.val(data.value); } update_sadness(); @@ -142,7 +143,7 @@ $(document).ready(function() { } $.post('/rest/config/'+key, { value: val }) .done(function(d) { - if (d.Success) { + if (d.success) { alert('Updated config'); } else { alert("Error: " + d.Error); diff --git a/data/dau.css b/data/dau.css index cc7bbe2..35f740a 100644 --- a/data/dau.css +++ b/data/dau.css @@ -42,6 +42,10 @@ body { max-width: 42em; } +pre { + background-color: black; + color: aliceblue; +} /* * Header diff --git a/data/logs.html b/data/logs.html index 18975ba..37c1e4b 100644 --- a/data/logs.html +++ b/data/logs.html @@ -3,17 +3,8 @@
Discord-auto-upload logs
-time | entry | -
---|
+diff --git a/dau.go b/dau.go index 395e961..27d775d 100644 --- a/dau.go +++ b/dau.go @@ -29,6 +29,7 @@ import ( "golang.org/x/image/font/inconsolata" "github.com/tardisx/discord-auto-upload/config" + daulog "github.com/tardisx/discord-auto-upload/log" "github.com/tardisx/discord-auto-upload/web" ) @@ -45,7 +46,7 @@ func main() { checkUpdates() - sendLogToWeb(fmt.Sprintf("Waiting for images to appear in %s", config.Config.Path)) + daulog.SendLog(fmt.Sprintf("Waiting for images to appear in %s", config.Config.Path), daulog.LogTypeInfo) // wander the path, forever for { if checkPath(config.Config.Path) { @@ -56,7 +57,7 @@ func main() { } lastCheck = newLastCheck } - sendLogToWeb(fmt.Sprintf("sleeping for %ds before next check of %s", config.Config.Watch, config.Config.Path)) + daulog.SendLog(fmt.Sprintf("sleeping for %ds before next check of %s", config.Config.Watch, config.Config.Path), daulog.LogTypeDebug) time.Sleep(time.Duration(config.Config.Watch) * time.Second) } } @@ -83,12 +84,12 @@ func checkUpdates() { Body string } - sendLogToWeb("checking for new version") + daulog.SendLog("checking for new version", daulog.LogTypeInfo) client := &http.Client{Timeout: time.Second * 5} resp, err := client.Get("https://api.github.com/repos/tardisx/discord-auto-upload/releases/latest") if err != nil { - sendLogToWeb(fmt.Sprintf("WARNING: Update check failed: %v", err)) + daulog.SendLog(fmt.Sprintf("WARNING: Update check failed: %v", err), daulog.LogTypeError) return } defer resp.Body.Close() @@ -110,7 +111,7 @@ func checkUpdates() { fmt.Println(latest.Body) fmt.Println("------------------------------------") fmt.Println("Upgrade at https://github.com/tardisx/discord-auto-upload/releases/latest") - sendLogToWeb(fmt.Sprintf("New version available: %s - download at https://github.com/tardisx/discord-auto-upload/releases/latest")) + daulog.SendLog(fmt.Sprintf("New version available: %s - download at https://github.com/tardisx/discord-auto-upload/releases/latest"), daulog.LogTypeInfo) } } @@ -155,13 +156,6 @@ func checkFile(path string, f os.FileInfo, err error) error { return nil } -func sendLogToWeb(entry string) { - web.LogInput <- web.LogEntry{ - Timestamp: time.Now(), - Entry: entry, - } -} - func fileEligible(file string) bool { if config.Config.Exclude != "" && strings.Contains(file, config.Config.Exclude) { @@ -179,16 +173,16 @@ func fileEligible(file string) bool { func processFile(file string) { if !config.Config.NoWatermark { - sendLogToWeb("Copying to temp location and watermarking ") + daulog.SendLog("Copying to temp location and watermarking ", daulog.LogTypeInfo) file = mungeFile(file) } if config.Config.WebHookURL == "" { - sendLogToWeb("WebHookURL is not configured - cannot upload!") + daulog.SendLog("WebHookURL is not configured - cannot upload!", daulog.LogTypeError) return } - sendLogToWeb("Uploading") + daulog.SendLog("Uploading", daulog.LogTypeInfo) extraParams := map[string]string{} @@ -274,7 +268,7 @@ func processFile(file string) { } if !config.Config.NoWatermark { - sendLogToWeb(fmt.Sprintf("Removing temporary file: %s", file)) + daulog.SendLog(fmt.Sprintf("Removing temporary file: %s", file), daulog.LogTypeDebug) os.Remove(file) } @@ -288,7 +282,7 @@ func sleepForRetries(retry int) { return } retryTime := (6-retry)*(6-retry) + 6 - sendLogToWeb(fmt.Sprintf("Will retry in %d seconds (%d remaining attempts)", retryTime, retry)) + daulog.SendLog(fmt.Sprintf("Will retry in %d seconds (%d remaining attempts)", retryTime, retry), daulog.LogTypeError) time.Sleep(time.Duration(retryTime) * time.Second) } diff --git a/log/log.go b/log/log.go new file mode 100644 index 0000000..dda64b0 --- /dev/null +++ b/log/log.go @@ -0,0 +1,42 @@ +package log + +import ( + "time" +) + +type LogEntryType string + +type LogEntry struct { + Timestamp time.Time `json:"ts"` + Type LogEntryType `json:"type"` + Entry string `json:"log"` +} + +const ( + LogTypeInfo = "info" + LogTypeError = "error" + LogTypeDebug = "debug" +) + +var LogEntries []LogEntry +var logInput chan LogEntry + +func init() { + // wait for log entries + logInput = make(chan LogEntry) + go func() { + for { + aLog := <-logInput + LogEntries = append(LogEntries, aLog) + } + }() +} + +func SendLog(entry string, entryType LogEntryType) { + + logInput <- LogEntry{ + Timestamp: time.Now(), + Entry: entry, + Type: entryType, + } +} diff --git a/web/server.go b/web/server.go index 5c9b990..eb27bfc 100644 --- a/web/server.go +++ b/web/server.go @@ -11,10 +11,10 @@ import ( "regexp" "strconv" "text/template" - "time" "github.com/tardisx/discord-auto-upload/assets" "github.com/tardisx/discord-auto-upload/config" + daulog "github.com/tardisx/discord-auto-upload/log" ) // DAUWebServer - stuff for the web server @@ -37,14 +37,6 @@ type errorResponse struct { Error string `json:"error"` } -type LogEntry struct { - Timestamp time.Time `json:"ts"` - Entry string `json:"log"` -} - -var logEntries []LogEntry -var LogInput chan LogEntry - func getStatic(w http.ResponseWriter, r *http.Request) { // haha this is dumb and I should change it re := regexp.MustCompile(`[^a-zA-Z0-9\.]`) @@ -280,20 +272,21 @@ func getSetExclude(w http.ResponseWriter, r *http.Request) { } func getLogs(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "application/json") - js, _ := json.Marshal(logEntries) - w.Write(js) + w.Header().Set("Content-Type", "text/plain") + + text := "" + for _, log := range daulog.LogEntries { + text = text + fmt.Sprintf( + "%-6s %-19s %s\n", log.Type, log.Timestamp.Format("2006-01-02 15:04:05"), log.Entry, + ) + } + + // js, _ := json.Marshal(daulog.LogEntries) + w.Write([]byte(text)) + } func StartWebServer() { - // wait for log entries - LogInput = make(chan LogEntry) - go func() { - for { - aLog := <-LogInput - logEntries = append(logEntries, aLog) - } - }() http.HandleFunc("/", getStatic) http.HandleFunc("/rest/config/webhook", getSetWebhook)