Skip to content

Commit

Permalink
Make usage of loggers explicit (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
andig committed Apr 21, 2020
1 parent f739470 commit 0e352c7
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 47 deletions.
13 changes: 8 additions & 5 deletions provider/cache.go
Expand Up @@ -3,11 +3,13 @@ package provider
import (
"time"

"github.com/andig/evcc/api"
"github.com/benbjohnson/clock"
)

// Cached wraps a getter with a cache
type Cached struct {
log *api.Logger
clock clock.Clock
updated time.Time
cache time.Duration
Expand All @@ -16,8 +18,9 @@ type Cached struct {
}

// NewCached wraps a getter with a cache
func NewCached(getter interface{}, cache time.Duration) *Cached {
func NewCached(log *api.Logger, getter interface{}, cache time.Duration) *Cached {
return &Cached{
log: log,
clock: clock.New(),
getter: getter,
cache: cache,
Expand All @@ -29,7 +32,7 @@ func (c *Cached) FloatGetter() FloatGetter {
g, ok := c.getter.(FloatGetter)
if !ok {
if g, ok = c.getter.(func() (float64, error)); !ok {
log.FATAL.Fatalf("invalid type: %T", c.getter)
c.log.FATAL.Fatalf("invalid type: %T", c.getter)
}
g = FloatGetter(g)
}
Expand All @@ -54,7 +57,7 @@ func (c *Cached) IntGetter() IntGetter {
g, ok := c.getter.(IntGetter)
if !ok {
if g, ok = c.getter.(func() (int64, error)); !ok {
log.FATAL.Fatalf("invalid type: %T", c.getter)
c.log.FATAL.Fatalf("invalid type: %T", c.getter)
}
g = IntGetter(g)
}
Expand All @@ -79,7 +82,7 @@ func (c *Cached) StringGetter() StringGetter {
g, ok := c.getter.(StringGetter)
if !ok {
if g, ok = c.getter.(func() (string, error)); !ok {
log.FATAL.Fatalf("invalid type: %T", c.getter)
c.log.FATAL.Fatalf("invalid type: %T", c.getter)
}
g = StringGetter(g)
}
Expand All @@ -105,7 +108,7 @@ func (c *Cached) BoolGetter() BoolGetter {
g, ok := c.getter.(BoolGetter)
if !ok {
if g, ok = c.getter.(func() (bool, error)); !ok {
log.FATAL.Fatalf("invalid type: %T", g)
c.log.FATAL.Fatalf("invalid type: %T", g)
}
g = BoolGetter(g)
}
Expand Down
2 changes: 1 addition & 1 deletion provider/cache_test.go
Expand Up @@ -27,7 +27,7 @@ func TestCachedGetter(t *testing.T) {
}

duration := time.Second
c := NewCached(g, duration)
c := NewCached(nil, g, duration)
clck := clock.NewMock()
c.clock = clck
getter := c.FloatGetter()
Expand Down
8 changes: 4 additions & 4 deletions provider/config.go
Expand Up @@ -70,7 +70,7 @@ func NewFloatGetterFromConfig(log *api.Logger, config Config) (res FloatGetter)
pc := scriptFromConfig(log, config.Other)
res = NewScriptProvider(pc.Timeout).FloatGetter(pc.Cmd)
if pc.Cache > 0 {
res = NewCached(res, pc.Cache).FloatGetter()
res = NewCached(log, res, pc.Cache).FloatGetter()
}
case "modbus-rtu", "modbus-tcp", "modbus-rtuovertcp", "modbus-tcprtu", "modbus-rtutcp":
res = FloatGetter(NewModbusFromConfig(log, config.Type, config.Other).FloatGetter)
Expand All @@ -91,7 +91,7 @@ func NewIntGetterFromConfig(log *api.Logger, config Config) (res IntGetter) {
pc := scriptFromConfig(log, config.Other)
res = NewScriptProvider(pc.Timeout).IntGetter(pc.Cmd)
if pc.Cache > 0 {
res = NewCached(res, pc.Cache).IntGetter()
res = NewCached(log, res, pc.Cache).IntGetter()
}
case "modbus-rtu", "modbus-tcp", "modbus-rtuovertcp", "modbus-tcprtu", "modbus-rtutcp":
res = IntGetter(NewModbusFromConfig(log, config.Type, config.Other).IntGetter)
Expand All @@ -112,7 +112,7 @@ func NewStringGetterFromConfig(log *api.Logger, config Config) (res StringGetter
pc := scriptFromConfig(log, config.Other)
res = NewScriptProvider(pc.Timeout).StringGetter(pc.Cmd)
if pc.Cache > 0 {
res = NewCached(res, pc.Cache).StringGetter()
res = NewCached(log, res, pc.Cache).StringGetter()
}
case "combined", "openwb":
res = openWBStatusFromConfig(log, config.Other)
Expand All @@ -133,7 +133,7 @@ func NewBoolGetterFromConfig(log *api.Logger, config Config) (res BoolGetter) {
pc := scriptFromConfig(log, config.Other)
res = NewScriptProvider(pc.Timeout).BoolGetter(pc.Cmd)
if pc.Cache > 0 {
res = NewCached(res, pc.Cache).BoolGetter()
res = NewCached(log, res, pc.Cache).BoolGetter()
}
default:
log.FATAL.Fatalf("invalid provider type %s", config.Type)
Expand Down
8 changes: 4 additions & 4 deletions provider/exec.go
Expand Up @@ -12,17 +12,17 @@ import (
"github.com/kballard/go-shellquote"
)

var log = api.NewLogger("exec")

// Script implements shell script-based providers and setters
type Script struct {
log *api.Logger
timeout time.Duration
}

// NewScriptProvider creates a script provider.
// Script execution is aborted after given timeout.
func NewScriptProvider(timeout time.Duration) *Script {
return &Script{
log: api.NewLogger("exec"),
timeout: timeout,
}
}
Expand Down Expand Up @@ -53,11 +53,11 @@ func (e *Script) StringGetter(script string) StringGetter {
s = strings.TrimSpace(string(ee.Stderr))
}

log.ERROR.Printf("%s: %s", strings.Join(args, " "), s)
e.log.ERROR.Printf("%s: %s", strings.Join(args, " "), s)
return "", err
}

log.TRACE.Printf("%s: %s", strings.Join(args, " "), s)
e.log.TRACE.Printf("%s: %s", strings.Join(args, " "), s)
return s, nil
}
}
Expand Down
49 changes: 22 additions & 27 deletions provider/mqtt.go
Expand Up @@ -2,6 +2,7 @@ package provider

import (
"fmt"
"math"
"strconv"
"sync"
"time"
Expand All @@ -15,10 +16,9 @@ const (
waitTimeout = 50 * time.Millisecond // polling interval when waiting for initial value
)

var mlog = api.NewLogger("mqtt")

// MqttClient is a paho publisher
type MqttClient struct {
log *api.Logger
mux sync.Mutex
Client mqtt.Client
broker string
Expand All @@ -34,9 +34,11 @@ func NewMqttClient(
clientID string,
qos byte,
) *MqttClient {
mlog.INFO.Printf("connecting %s at %s", clientID, broker)
log := api.NewLogger("mqtt")
log.INFO.Printf("connecting %s at %s", clientID, broker)

mc := &MqttClient{
log: log,
broker: broker,
qos: qos,
listener: make(map[string]func(string)),
Expand All @@ -63,18 +65,18 @@ func NewMqttClient(

// ConnectionLostHandler logs cause of connection loss as warning
func (m *MqttClient) ConnectionLostHandler(client mqtt.Client, reason error) {
mlog.WARN.Printf("%s connection lost: %v", m.broker, reason.Error())
m.log.WARN.Printf("%s connection lost: %v", m.broker, reason.Error())
}

// ConnectionHandler restores listeners
func (m *MqttClient) ConnectionHandler(client mqtt.Client) {
mlog.TRACE.Printf("%s connected", m.broker)
m.log.TRACE.Printf("%s connected", m.broker)

m.mux.Lock()
defer m.mux.Unlock()

for topic, l := range m.listener {
mlog.TRACE.Printf("%s subscribe %s", m.broker, topic)
m.log.TRACE.Printf("%s subscribe %s", m.broker, topic)
go m.listen(topic, l)
}
}
Expand All @@ -83,7 +85,7 @@ func (m *MqttClient) ConnectionHandler(client mqtt.Client) {
func (m *MqttClient) Listen(topic string, callback func(string)) {
m.mux.Lock()
if _, ok := m.listener[topic]; ok {
mlog.FATAL.Fatalf("%s: duplicate listener not allowed", topic)
m.log.FATAL.Fatalf("%s: duplicate listener not allowed", topic)
}
m.listener[topic] = callback
m.mux.Unlock()
Expand All @@ -105,6 +107,7 @@ func (m *MqttClient) listen(topic string, callback func(string)) {
// FloatGetter creates handler for float64 from MQTT topic that returns cached value
func (m *MqttClient) FloatGetter(topic string, multiplier float64, timeout time.Duration) FloatGetter {
h := &msgHandler{
log: m.log,
topic: topic,
multiplier: multiplier,
timeout: timeout,
Expand All @@ -117,6 +120,7 @@ func (m *MqttClient) FloatGetter(topic string, multiplier float64, timeout time.
// IntGetter creates handler for int64 from MQTT topic that returns cached value
func (m *MqttClient) IntGetter(topic string, multiplier int64, timeout time.Duration) IntGetter {
h := &msgHandler{
log: m.log,
topic: topic,
multiplier: float64(multiplier),
timeout: timeout,
Expand All @@ -129,6 +133,7 @@ func (m *MqttClient) IntGetter(topic string, multiplier int64, timeout time.Dura
// StringGetter creates handler for string from MQTT topic that returns cached value
func (m *MqttClient) StringGetter(topic string, timeout time.Duration) StringGetter {
h := &msgHandler{
log: m.log,
topic: topic,
timeout: timeout,
}
Expand All @@ -140,6 +145,7 @@ func (m *MqttClient) StringGetter(topic string, timeout time.Duration) StringGet
// BoolGetter creates handler for string from MQTT topic that returns cached value
func (m *MqttClient) BoolGetter(topic string, timeout time.Duration) BoolGetter {
h := &msgHandler{
log: m.log,
topic: topic,
timeout: timeout,
}
Expand Down Expand Up @@ -167,7 +173,7 @@ func (m *MqttClient) IntSetter(param, topic, message string) IntSetter {
return err
}

mlog.TRACE.Printf("send %s: '%s'", topic, payload)
m.log.TRACE.Printf("send %s: '%s'", topic, payload)
token := m.Client.Publish(topic, m.qos, false, payload)
if token.WaitTimeout(publishTimeout) {
return token.Error()
Expand All @@ -185,7 +191,7 @@ func (m *MqttClient) BoolSetter(param, topic, message string) BoolSetter {
return err
}

mlog.TRACE.Printf("send %s: '%s'", topic, payload)
m.log.TRACE.Printf("send %s: '%s'", topic, payload)
token := m.Client.Publish(topic, m.qos, false, payload)
if token.WaitTimeout(publishTimeout) {
return token.Error()
Expand All @@ -199,14 +205,15 @@ func (m *MqttClient) BoolSetter(param, topic, message string) BoolSetter {
func (m *MqttClient) WaitForToken(token mqtt.Token) {
if token.WaitTimeout(publishTimeout) {
if token.Error() != nil {
mlog.ERROR.Printf("error: %s", token.Error())
m.log.ERROR.Printf("error: %s", token.Error())
}
} else {
mlog.DEBUG.Println("timeout")
m.log.DEBUG.Println("timeout")
}
}

type msgHandler struct {
log *api.Logger
once sync.Once
mux sync.Mutex
updated time.Time
Expand All @@ -217,7 +224,7 @@ type msgHandler struct {
}

func (h *msgHandler) Receive(payload string) {
mlog.TRACE.Printf("recv %s: '%s'", h.topic, payload)
h.log.TRACE.Printf("recv %s: '%s'", h.topic, payload)

h.mux.Lock()
defer h.mux.Unlock()
Expand All @@ -231,7 +238,7 @@ func (h *msgHandler) waitForInitialValue() {
defer h.mux.Unlock()

if h.updated.IsZero() {
mlog.TRACE.Printf("%s wait for initial value", h.topic)
h.log.TRACE.Printf("%s wait for initial value", h.topic)

// wait for initial update
for h.updated.IsZero() {
Expand Down Expand Up @@ -260,20 +267,8 @@ func (h *msgHandler) floatGetter() (float64, error) {
}

func (h *msgHandler) intGetter() (int64, error) {
h.once.Do(h.waitForInitialValue)
h.mux.Lock()
defer h.mux.Unlock()

if elapsed := time.Since(h.updated); h.timeout != 0 && elapsed > h.timeout {
return 0, fmt.Errorf("%s outdated: %v", h.topic, elapsed.Truncate(time.Second))
}

val, err := strconv.ParseInt(h.payload, 10, 64)
if err != nil {
return 0, fmt.Errorf("%s invalid: '%s'", h.topic, h.payload)
}

return int64(h.multiplier) * val, nil
f, err := h.floatGetter()
return int64(math.Round(f)), err
}

func (h *msgHandler) stringGetter() (string, error) {
Expand Down
2 changes: 1 addition & 1 deletion vehicle/audi.go
Expand Up @@ -70,7 +70,7 @@ func NewAudiFromConfig(log *api.Logger, other map[string]interface{}) api.Vehicl
vin: cc.VIN,
}

v.chargeStateG = provider.NewCached(v.chargeState, cc.Cache).FloatGetter()
v.chargeStateG = provider.NewCached(log, v.chargeState, cc.Cache).FloatGetter()

return v
}
Expand Down
2 changes: 1 addition & 1 deletion vehicle/bmw.go
Expand Up @@ -52,7 +52,7 @@ func NewBMWFromConfig(log *api.Logger, other map[string]interface{}) api.Vehicle
vin: cc.VIN,
}

v.chargeStateG = provider.NewCached(v.chargeState, cc.Cache).FloatGetter()
v.chargeStateG = provider.NewCached(log, v.chargeState, cc.Cache).FloatGetter()

return v
}
Expand Down
2 changes: 1 addition & 1 deletion vehicle/nissan.go
Expand Up @@ -43,7 +43,7 @@ func NewNissanFromConfig(log *api.Logger, other map[string]interface{}) api.Vehi
session: session,
}

v.chargeStateG = provider.NewCached(v.chargeState, cc.Cache).FloatGetter()
v.chargeStateG = provider.NewCached(log, v.chargeState, cc.Cache).FloatGetter()

return v
}
Expand Down
4 changes: 2 additions & 2 deletions vehicle/tesla.go
Expand Up @@ -57,8 +57,8 @@ func NewTeslaFromConfig(log *api.Logger, other map[string]interface{}) api.Vehic
log.FATAL.Fatal("cannot create tesla: vin not found")
}

v.chargeStateG = provider.NewCached(v.chargeState, cc.Cache).FloatGetter()
v.chargedEnergyG = provider.NewCached(v.chargedEnergy, cc.Cache).FloatGetter()
v.chargeStateG = provider.NewCached(log, v.chargeState, cc.Cache).FloatGetter()
v.chargedEnergyG = provider.NewCached(log, v.chargedEnergy, cc.Cache).FloatGetter()

return v
}
Expand Down
2 changes: 1 addition & 1 deletion vehicle/vehicle.go
Expand Up @@ -40,7 +40,7 @@ func NewConfigurableFromConfig(log *api.Logger, other map[string]interface{}) ap

getter := provider.NewFloatGetterFromConfig(log, cc.Charge)
if cc.Cache > 0 {
getter = provider.NewCached(getter, cc.Cache).FloatGetter()
getter = provider.NewCached(log, getter, cc.Cache).FloatGetter()
}

return &Vehicle{
Expand Down

0 comments on commit 0e352c7

Please sign in to comment.