Skip to content

Commit

Permalink
Use fmt.Print instead of log.Print
Browse files Browse the repository at this point in the history
While time-annotated outputs are nice, typically deckmaster would
be run as a service, where the output already gets timestamped.

This also means we're now printing to stdout instead of -err for
regular output.
  • Loading branch information
muesli committed Jun 8, 2021
1 parent 272f939 commit a163a72
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 45 deletions.
25 changes: 12 additions & 13 deletions deck.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"image"
"image/draw"
"log"
"os"
"os/exec"
"path/filepath"
Expand All @@ -30,7 +29,7 @@ func LoadDeck(dev *streamdeck.Device, base string, deck string) (*Deck, error) {
if err != nil {
return nil, err
}
log.Println("Loading deck:", path)
fmt.Println("Loading deck:", path)

dc, err := LoadConfig(path)
if err != nil {
Expand Down Expand Up @@ -142,7 +141,7 @@ func emulateKeyPresses(keys string) {
// emulates a (multi-)key press.
func emulateKeyPress(keys string) {
if keyboard == nil {
log.Println("Keyboard emulation is disabled!")
fmt.Println("Keyboard emulation is disabled!")
return
}

Expand All @@ -151,7 +150,7 @@ func emulateKeyPress(keys string) {
k = formatKeycodes(strings.TrimSpace(k))
kc, err := strconv.Atoi(k)
if err != nil {
log.Fatalf("%s is not a valid keycode: %s", k, err)
fatalf("%s is not a valid keycode: %s\n", k, err)
}

if i+1 < len(kk) {
Expand All @@ -167,7 +166,7 @@ func emulateKeyPress(keys string) {
func emulateClipboard(text string) {
err := clipboard.WriteAll(text)
if err != nil {
log.Fatalf("Pasting to clipboard failed: %s", err)
fatalf("Pasting to clipboard failed: %s\n", err)
}

// paste the string
Expand All @@ -178,7 +177,7 @@ func emulateClipboard(text string) {
func executeDBusMethod(object, path, method, args string) {
call := dbusConn.Object(object, dbus.ObjectPath(path)).Call(method, 0, args)
if call.Err != nil {
log.Printf("dbus call failed: %s", call.Err)
fmt.Printf("dbus call failed: %s\n", call.Err)
}
}

Expand All @@ -187,12 +186,12 @@ func executeCommand(cmd string) {
args := strings.Split(cmd, " ")
c := exec.Command(args[0], args[1:]...) //nolint:gosec
if err := c.Start(); err != nil {
log.Printf("command failed: %s", err)
fmt.Printf("command failed: %s\n", err)
return
}

if err := c.Wait(); err != nil {
log.Printf("command failed: %s", err)
fmt.Printf("command failed: %s\n", err)
}
}

Expand All @@ -208,15 +207,15 @@ func (d *Deck) triggerAction(dev *streamdeck.Device, index uint8, hold bool) {
}

if a != nil {
// log.Println("Executing overloaded action")
// fmt.Println("Executing overloaded action")
if a.Deck != "" {
d, err := LoadDeck(dev, filepath.Dir(d.File), a.Deck)
if err != nil {
log.Fatal(err)
fatal(err)
}
err = dev.Clear()
if err != nil {
log.Fatal(err)
fatal(err)
}

deck = d
Expand Down Expand Up @@ -248,9 +247,9 @@ func (d *Deck) updateWidgets(dev *streamdeck.Device) {
continue
}

// log.Printf("Repaint %d", w.Key())
// fmt.Println("Repaint", w.Key())
if err := w.Update(dev); err != nil {
log.Fatalf("error: %v", err)
fatalf("error: %v\n", err)
}
}
}
10 changes: 5 additions & 5 deletions desktop_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ package main
import (
"bytes"
"errors"
"fmt"
"image"
"log"
"time"

"github.com/jezek/xgb"
Expand Down Expand Up @@ -161,7 +161,7 @@ func (x Xorg) CloseWindow(w Window) error {
func (x Xorg) atom(aname string) *xproto.InternAtomReply {
a, err := xproto.InternAtom(x.conn, true, uint16(len(aname)), aname).Reply()
if err != nil {
log.Fatal("atom:", err)
fatal("atom:", err)
}
return a
}
Expand Down Expand Up @@ -198,7 +198,7 @@ func (x Xorg) name(w xproto.Window) (string, error) {
func (x Xorg) icon(w xproto.Window) (image.Image, error) {
icon, err := xgraphics.FindIcon(x.util, w, 128, 128)
if err != nil {
log.Printf("Could not find icon for window %d.", w)
fmt.Printf("Could not find icon for window %d\n", w)
return nil, err
}

Expand Down Expand Up @@ -255,7 +255,7 @@ func (x Xorg) waitForEvent(events chan<- xgb.Event) {
for {
ev, err := x.conn.WaitForEvent()
if err != nil {
log.Println("wait for event:", err)
fmt.Println("wait for event:", err)
continue
}
events <- ev
Expand All @@ -266,7 +266,7 @@ func (x Xorg) waitForEvent(events chan<- xgb.Event) {
func (x Xorg) queryIdle() time.Duration {
info, err := screensaver.QueryInfo(x.conn, xproto.Drawable(x.root)).Reply()
if err != nil {
log.Println("query idle:", err)
fmt.Println("query idle:", err)
return 0
}
return time.Duration(info.MsSinceUserInput) * time.Millisecond
Expand Down
7 changes: 3 additions & 4 deletions fonts.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"image"
"image/color"
"io/ioutil"
"log"

"github.com/flopp/go-findfont"
"github.com/golang/freetype"
Expand Down Expand Up @@ -87,16 +86,16 @@ func init() {
var err error
ttfFont, err = loadFont("Roboto-Regular.ttf")
if err != nil {
log.Fatal(err)
fatal(err)
}

ttfThinFont, err = loadFont("Roboto-Thin.ttf")
if err != nil {
log.Fatal(err)
fatal(err)
}

ttfBoldFont, err = loadFont("Roboto-Bold.ttf")
if err != nil {
log.Fatal(err)
fatal(err)
}
}
37 changes: 23 additions & 14 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"flag"
"fmt"
"log"
"os"
"path/filepath"
"sync"
Expand Down Expand Up @@ -33,6 +32,16 @@ const (
longPressDuration = 350 * time.Millisecond
)

func fatal(v ...interface{}) {
fmt.Println(v...)
os.Exit(1)
}

func fatalf(format string, a ...interface{}) {
fmt.Printf(format, a...)
os.Exit(1)
}

func expandPath(base, path string) (string, error) {
var err error
path, err = homedir.Expand(path)
Expand All @@ -53,7 +62,7 @@ func eventLoop(dev *streamdeck.Device, tch chan interface{}) {

kch, err := dev.ReadKeys()
if err != nil {
log.Fatal(err)
fatal(err)
}
for {
select {
Expand All @@ -64,7 +73,7 @@ func eventLoop(dev *streamdeck.Device, tch chan interface{}) {
if !ok {
err = dev.Open()
if err != nil {
log.Fatal(err)
fatal(err)
}
continue
}
Expand All @@ -78,7 +87,7 @@ func eventLoop(dev *streamdeck.Device, tch chan interface{}) {
if state && !k.Pressed {
// key was released
if time.Since(keyTimestamps[k.Index]) < longPressDuration {
// log.Println("Triggering short action")
// fmt.Println("Triggering short action")
deck.triggerAction(dev, k.Index, false)
}
}
Expand All @@ -90,7 +99,7 @@ func eventLoop(dev *streamdeck.Device, tch chan interface{}) {

if state, ok := keyStates.Load(k.Index); ok && state.(bool) {
// key still pressed
// log.Println("Triggering long action")
// fmt.Println("Triggering long action")
deck.triggerAction(dev, k.Index, true)
}
}()
Expand All @@ -112,7 +121,7 @@ func eventLoop(dev *streamdeck.Device, tch chan interface{}) {
func initDevice() (*streamdeck.Device, error) {
d, err := streamdeck.Devices()
if err != nil {
log.Fatal(err)
fatal(err)
}
if len(d) == 0 {
return nil, fmt.Errorf("no Stream Deck devices found")
Expand All @@ -129,9 +138,9 @@ func initDevice() (*streamdeck.Device, error) {
}
}
if !found {
log.Println("can't find device. Available devices:")
fmt.Println("Can't find device. Available devices:")
for _, v := range d {
log.Printf("Serial %s (%d buttons)\n", v.Serial, v.Columns*v.Rows)
fmt.Printf("Serial %s (%d buttons)\n", v.Serial, v.Columns*v.Rows)
}
os.Exit(1)
}
Expand All @@ -144,7 +153,7 @@ func initDevice() (*streamdeck.Device, error) {
if err != nil {
return nil, err
}
log.Printf("Found device with serial %s (%d buttons, firmware %s)\n",
fmt.Printf("Found device with serial %s (%d buttons, firmware %s)\n",
dev.Serial, dev.Columns*dev.Rows, ver)

if err := dev.Reset(); err != nil {
Expand All @@ -167,13 +176,13 @@ func main() {
// initialize device
dev, err := initDevice()
if err != nil {
log.Fatal(err)
fatal(err)
}

// initialize dbus connection
dbusConn, err = dbus.SessionBus()
if err != nil {
log.Fatal(err)
fatal(err)
}

// initialize xorg connection and track window focus
Expand All @@ -187,16 +196,16 @@ func main() {
// initialize virtual keyboard
keyboard, err = uinput.CreateKeyboard("/dev/uinput", []byte("Deckmaster"))
if err != nil {
log.Printf("Could not create virtual input device (/dev/uinput): %s", err)
log.Println("Emulating keyboard events will be disabled!")
fmt.Printf("Could not create virtual input device (/dev/uinput): %s\n", err)
fmt.Println("Emulating keyboard events will be disabled!")
} else {
defer keyboard.Close() //nolint:errcheck
}

// load deck
deck, err = LoadDeck(dev, ".", *deckFile)
if err != nil {
log.Fatal(err)
fatal(err)
}
deck.updateWidgets(dev)

Expand Down
3 changes: 1 addition & 2 deletions widget.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"image"
"image/color"
"image/draw"
"log"
"os"
"time"

Expand Down Expand Up @@ -204,6 +203,6 @@ func drawString(img *image.RGBA, bounds image.Rectangle, ttf *truetype.Font, tex

c.SetSrc(image.NewUniform(color))
if _, err := c.DrawString(text, freetype.Pt(pt.X, pt.Y)); err != nil {
log.Fatal(err)
fatal(err)
}
}
4 changes: 2 additions & 2 deletions widget_recent_window.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package main

import (
"fmt"
"image"
"log"

"github.com/muesli/streamdeck"
)
Expand Down Expand Up @@ -74,7 +74,7 @@ func (w *RecentWindowWidget) Update(dev *streamdeck.Device) error {
// TriggerAction gets called when a button is pressed.
func (w *RecentWindowWidget) TriggerAction(hold bool) {
if xorg == nil {
log.Println("xorg support is disabled!")
fmt.Println("xorg support is disabled!")
return
}

Expand Down
5 changes: 2 additions & 3 deletions widget_top.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"image"
"image/color"
"image/draw"
"log"
"strconv"

"github.com/muesli/streamdeck"
Expand Down Expand Up @@ -51,7 +50,7 @@ func (w *TopWidget) Update(dev *streamdeck.Device) error {
case "cpu":
cpuUsage, err := cpu.Percent(0, false)
if err != nil {
log.Fatal(err)
fatal(err)
}

value = cpuUsage[0]
Expand All @@ -60,7 +59,7 @@ func (w *TopWidget) Update(dev *streamdeck.Device) error {
case "memory":
memory, err := mem.VirtualMemory()
if err != nil {
log.Fatal(err)
fatal(err)
}
value = memory.UsedPercent
label = "MEM"
Expand Down
4 changes: 2 additions & 2 deletions window.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package main

import (
"log"
"fmt"

"github.com/muesli/streamdeck"
)

func handleActiveWindowChanged(dev *streamdeck.Device, event ActiveWindowChangedEvent) {
log.Printf("Active window changed to %s (%d, %s)\n",
fmt.Printf("Active window changed to %s (%d, %s)\n",
event.Window.Class, event.Window.ID, event.Window.Name)

// remove dupes
Expand Down

0 comments on commit a163a72

Please sign in to comment.