Get rid of intermediate logger.

This commit is contained in:
David Newhall II 2019-06-19 01:26:21 -07:00
parent 3a8fcda20a
commit a423a8aebe
3 changed files with 14 additions and 21 deletions

View File

@ -13,7 +13,7 @@ func (u *Unifi) parseDevices(data []json.RawMessage, siteName string) *Devices {
} else if t, ok := o["type"].(string); ok { } else if t, ok := o["type"].(string); ok {
assetType = t assetType = t
} }
u.dLogf("Unmarshalling Device Type: %v, site %s ", assetType, siteName) u.DebugLog("Unmarshalling Device Type: %v, site %s ", assetType, siteName)
// Choose which type to unmarshal into based on the "type" json key. // Choose which type to unmarshal into based on the "type" json key.
switch assetType { // Unmarshal again into the correct type.. switch assetType { // Unmarshal again into the correct type..
case "uap": case "uap":
@ -32,7 +32,7 @@ func (u *Unifi) parseDevices(data []json.RawMessage, siteName string) *Devices {
devices.USWs = append(devices.USWs, usw) devices.USWs = append(devices.USWs, usw)
} }
default: default:
u.eLogf("unknown asset type - %v - skipping", assetType) u.ErrorLog("unknown asset type - %v - skipping", assetType)
} }
} }
return devices return devices
@ -41,12 +41,12 @@ func (u *Unifi) parseDevices(data []json.RawMessage, siteName string) *Devices {
// unmarshalDevice handles logging for the unmarshal operations in parseDevices(). // unmarshalDevice handles logging for the unmarshal operations in parseDevices().
func (u *Unifi) unmarshalDevice(dev string, data json.RawMessage, v interface{}) (err error) { func (u *Unifi) unmarshalDevice(dev string, data json.RawMessage, v interface{}) (err error) {
if err = json.Unmarshal(data, v); err != nil { if err = json.Unmarshal(data, v); err != nil {
u.eLogf("json.Unmarshal(%v): %v", dev, err) u.ErrorLog("json.Unmarshal(%v): %v", dev, err)
u.eLogf("Enable Debug Logging to output the failed payload.") u.ErrorLog("Enable Debug Logging to output the failed payload.")
json, err := data.MarshalJSON() json, err := data.MarshalJSON()
u.dLogf("Failed Payload: %s (marshal err: %v)", json, err) u.DebugLog("Failed Payload: %s (marshal err: %v)", json, err)
u.dLogf("The above payload can prove useful during torubleshooting when you open an Issue:") u.DebugLog("The above payload can prove useful during torubleshooting when you open an Issue:")
u.dLogf("==- https://github.com/golift/unifi/issues/new -==") u.DebugLog("==- https://github.com/golift/unifi/issues/new -==")
} }
return err return err
} }

View File

@ -32,18 +32,9 @@ const (
// that matches this interface to capture debug and error logs. // that matches this interface to capture debug and error logs.
type Logger func(msg string, fmt ...interface{}) type Logger func(msg string, fmt ...interface{})
// dLogf logs a debug message. // DiscardLogs is the default logger.
func (u *Unifi) dLogf(msg string, v ...interface{}) { func DiscardLogs(msg string, v ...interface{}) {
if u.DebugLog != nil { // do nothing.
u.DebugLog("[DEBUG] "+msg, v...)
}
}
// eLogf logs an error message.
func (u *Unifi) eLogf(msg string, v ...interface{}) {
if u.ErrorLog != nil {
u.ErrorLog("[ERROR] "+msg, v...)
}
} }
// Devices contains a list of all the unifi devices from a controller. // Devices contains a list of all the unifi devices from a controller.

View File

@ -32,6 +32,8 @@ func NewUnifi(user, pass, url string, verifySSL bool) (*Unifi, error) {
Transport: &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: !verifySSL}}, Transport: &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: !verifySSL}},
Jar: jar, Jar: jar,
}, },
ErrorLog: DiscardLogs,
DebugLog: DiscardLogs,
} }
return u, u.getController(user, pass) return u, u.getController(user, pass)
} }
@ -74,7 +76,7 @@ func (u *Unifi) GetClients(sites []Site) (Clients, error) {
var response struct { var response struct {
Data []Client `json:"data"` Data []Client `json:"data"`
} }
u.dLogf("Polling Controller, retreiving Unifi Clients, site %s (%s) ", site.Name, site.Desc) u.DebugLog("Polling Controller, retreiving Unifi Clients, site %s (%s) ", site.Name, site.Desc)
clientPath := fmt.Sprintf(ClientPath, site.Name) clientPath := fmt.Sprintf(ClientPath, site.Name)
if err := u.GetData(clientPath, &response); err != nil { if err := u.GetData(clientPath, &response); err != nil {
return nil, err return nil, err
@ -135,7 +137,7 @@ func (u *Unifi) GetSites() (Sites, error) {
response.Data[i].SiteName = response.Data[i].Desc + " (" + response.Data[i].Name + ")" response.Data[i].SiteName = response.Data[i].Desc + " (" + response.Data[i].Name + ")"
sites = append(sites, response.Data[i].Name) sites = append(sites, response.Data[i].Name)
} }
u.dLogf("Found %d site(s): %s", len(sites), strings.Join(sites, ",")) u.DebugLog("Found %d site(s): %s", len(sites), strings.Join(sites, ","))
return response.Data, nil return response.Data, nil
} }