Skip to content

Commit

Permalink
Added timestamp to logging.
Browse files Browse the repository at this point in the history
  • Loading branch information
xStrom committed Apr 16, 2017
1 parent 473f0b8 commit 3040430
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 26 deletions.
24 changes: 24 additions & 0 deletions log/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2017 Kaur Kuut
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package log

import (
"fmt"
"time"
)

func Infof(format string, args ...interface{}) {
fmt.Printf(time.Now().Format("2006-01-02 15:04:05 ")+format+"\n", args...)
}
6 changes: 3 additions & 3 deletions painter/painter.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
package painter

import (
"fmt"
"sync"
"time"

"github.com/xStrom/patriot/art"
"github.com/xStrom/patriot/log"
"github.com/xStrom/patriot/sp"
"github.com/xStrom/patriot/work/shutdown"
)
Expand All @@ -35,7 +35,7 @@ func Work(wg *sync.WaitGroup) {
shutdown.ShutdownLock.RLock()
if shutdown.Shutdown {
shutdown.ShutdownLock.RUnlock()
fmt.Printf("Shutting down painter\n")
log.Infof("Shutting down painter")
wg.Done()
break
}
Expand All @@ -48,7 +48,7 @@ func Work(wg *sync.WaitGroup) {
queueLock.Unlock()
if p != nil {
if err := sp.DrawPixel(p.X, p.Y, p.C); err != nil {
fmt.Printf("Failed drawing %v:%v to %v, because: %v", p.X, p.Y, p.C, err)
log.Infof("Failed drawing %v:%v to %v, because: %v", p.X, p.Y, p.C, err)
queueLock.Lock()
queue = append(queue, p)
queueLock.Unlock()
Expand Down
10 changes: 5 additions & 5 deletions patriot.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
package main

import (
"fmt"
"os"
"os/signal"
"sync"

"github.com/xStrom/patriot/log"
"github.com/xStrom/patriot/realtime"
"github.com/xStrom/patriot/work"
"github.com/xStrom/patriot/work/shutdown"
Expand All @@ -31,7 +31,7 @@ func main() {

wg := &sync.WaitGroup{}

fmt.Println("Launching work engine ...")
log.Infof("Launching work engine ...")
wg.Add(1)
go work.Work(wg)

Expand All @@ -55,7 +55,7 @@ mainLoop:
for {
select {
case <-interrupt:
fmt.Printf("interrupt -- starting shutdown sequence ..\n")
log.Infof("interrupt -- starting shutdown sequence ..")
shutdown.ShutdownLock.Lock()
shutdown.Shutdown = true
shutdown.ShutdownLock.Unlock()
Expand All @@ -64,7 +64,7 @@ mainLoop:
}
}

fmt.Printf("Waiting for clean shutdown ..\n")
log.Infof("Waiting for clean shutdown ..")
wg.Wait()
fmt.Printf("Clean shutdown done :>\n")
log.Infof("Clean shutdown done :>")
}
21 changes: 11 additions & 10 deletions realtime/realtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/gorilla/websocket"

"github.com/xStrom/patriot/art/estflag"
"github.com/xStrom/patriot/log"
)

var c *websocket.Conn
Expand All @@ -35,26 +36,26 @@ func Realtime(wg *sync.WaitGroup, startVersion int) {
u := url.URL{Scheme: "wss", Host: "josephg.com", Path: "/sp/ws", RawQuery: fmt.Sprintf("from=%v", startVersion)}

connect:
fmt.Printf("connecting to %s\n", u.String())
log.Infof("connecting to %s", u.String())
var err error
c, _, err = websocket.DefaultDialer.Dial(u.String(), nil)
if err != nil {
fmt.Printf("dial err: %v\n", err)
log.Infof("dial err: %v", err)
goto connect
}

for {
_, message, err := c.ReadMessage()
if err != nil {
fmt.Printf("read error: %v\n", err)
log.Infof("read error: %v", err)
break
}
if bytes.Compare(message, []byte("reload")) == 0 {
fmt.Printf("Got reload command\n")
log.Infof("Got reload command")
break
}
if bytes.Compare(message, []byte("refresh")) == 0 {
fmt.Printf("Got refresh command\n")
log.Infof("Got refresh command")
break
}
if len(message) >= 7 {
Expand All @@ -67,11 +68,11 @@ connect:
}
//fmt.Printf("\n")
} else {
fmt.Printf("recv unknown: %v\n", message)
log.Infof("recv unknown: %v", message)
}
}

fmt.Printf("Close in Realtime\n")
log.Infof("Close in Realtime")
c.Close()
close(done)
wg.Done()
Expand Down Expand Up @@ -99,16 +100,16 @@ func Shutdown() {
// frame and wait for the server to close the connection.
err := c.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
if err != nil {
fmt.Printf("write close error: %v\n", err)
log.Infof("write close error: %v", err)
return
}
select {
case <-done:
case <-time.After(time.Second):
}
fmt.Printf("Close in Shutdown\n")
log.Infof("Close in Shutdown")
err = c.Close()
if err != nil {
fmt.Printf("close error: %v\n", err)
log.Infof("close error: %v", err)
}
}
8 changes: 5 additions & 3 deletions sp/sp.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"time"

"github.com/pkg/errors"

"github.com/xStrom/patriot/log"
)

const UserAgent = "Patriot/1.0 (https://github.com/xStrom/patriot)"
Expand Down Expand Up @@ -66,7 +68,7 @@ func FetchImage() ([]byte, int, error) {
version = int(ver)
}
}
fmt.Printf("Image version: %v\n", version)
log.Infof("Image version: %v", version)
if b, err := ioutil.ReadAll(resp.Body); err != nil {
return nil, -1, errors.Wrap(err, "Failed reading response")
} else {
Expand All @@ -90,8 +92,8 @@ func DrawPixel(x, y, c int) error {
if b, err := ioutil.ReadAll(resp.Body); err != nil {
return errors.Wrap(err, "Failed reading response")
} else if len(b) > 0 {
fmt.Printf("Got response:\n%v\n", string(b))
log.Infof("Got response:\n%v", string(b))
}
fmt.Printf("Drew: %v - %v - %v\n", x, y, c)
log.Infof("Drew: %v - %v - %v", x, y, c)
return nil
}
10 changes: 5 additions & 5 deletions work/work.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,27 @@
package work

import (
"fmt"
"sync"

"github.com/xStrom/patriot/art"
"github.com/xStrom/patriot/art/estflag"
"github.com/xStrom/patriot/log"
"github.com/xStrom/patriot/painter"
"github.com/xStrom/patriot/realtime"
"github.com/xStrom/patriot/sp"
"github.com/xStrom/patriot/work/shutdown"
)

func Work(wg *sync.WaitGroup) {
fmt.Println("Launching painter ...")
log.Infof("Launching painter ...")
wg.Add(1)
go painter.Work(wg)

for {
shutdown.ShutdownLock.RLock()
if shutdown.Shutdown {
shutdown.ShutdownLock.RUnlock()
fmt.Printf("Shutting down work engine\n")
log.Infof("Shutting down work engine")
wg.Done()
break
}
Expand All @@ -49,10 +49,10 @@ func Work(wg *sync.WaitGroup) {

func FetchImageAndCheck() int {
start:
fmt.Printf("Fetching image ..\n")
log.Infof("Fetching image ..")
data, version, err := sp.FetchImage()
if err != nil {
fmt.Printf("Failed to fetch image: %v\n", err)
log.Infof("Failed to fetch image: %v", err)
goto start
}
img, err := art.ParseImage(data)
Expand Down

0 comments on commit 3040430

Please sign in to comment.