Add better logs.

This commit is contained in:
David Newhall II 2019-04-15 02:08:58 -07:00
parent e15602ccfa
commit f5f4cb0720
1 changed files with 8 additions and 5 deletions

View File

@ -16,6 +16,8 @@ func (u *Unifi) GetClients(sites []string) (*Clients, error) {
var response struct { var response struct {
Data []UCL `json:"data"` Data []UCL `json:"data"`
} }
u.dLogf("Polling Site '%s' Clients", site)
u.dLogf("Unmarshalling Device Type: ucl")
clientPath := fmt.Sprintf(ClientPath, site) clientPath := fmt.Sprintf(ClientPath, site)
if err := u.GetData(clientPath, &response); err != nil { if err := u.GetData(clientPath, &response); err != nil {
return nil, err return nil, err
@ -29,6 +31,7 @@ func (u *Unifi) GetClients(sites []string) (*Clients, error) {
func (u *Unifi) GetDevices(sites []string) (*Devices, error) { func (u *Unifi) GetDevices(sites []string) (*Devices, error) {
var data []json.RawMessage var data []json.RawMessage
for _, site := range sites { for _, site := range sites {
u.dLogf("Polling Site '%s' Devices", site)
var response struct { var response struct {
Data []json.RawMessage `json:"data"` Data []json.RawMessage `json:"data"`
} }
@ -56,7 +59,7 @@ func (u *Unifi) GetSites() ([]string, error) {
for i := range response.Data { for i := range response.Data {
output = append(output, response.Data[i].Name) output = append(output, response.Data[i].Name)
} }
u.dLogf("Found %d sites: %v", len(output), strings.Join(output, ",")) u.dLogf("Found %d sites: %s", len(output), strings.Join(output, ","))
return output, nil return output, nil
} }
@ -64,19 +67,19 @@ func (u *Unifi) GetSites() ([]string, error) {
func (u *Unifi) GetData(methodPath string, v interface{}) error { func (u *Unifi) GetData(methodPath string, v interface{}) error {
req, err := u.UniReq(methodPath, "") req, err := u.UniReq(methodPath, "")
if err != nil { if err != nil {
return errors.Wrapf(err, "c.UniReq(%v)", methodPath) return errors.Wrapf(err, "c.UniReq(%s)", methodPath)
} }
resp, err := u.Do(req) resp, err := u.Do(req)
if err != nil { if err != nil {
return errors.Wrapf(err, "c.Do(%v)", methodPath) return errors.Wrapf(err, "c.Do(%s)", methodPath)
} }
defer func() { defer func() {
_ = resp.Body.Close() _ = resp.Body.Close()
}() }()
if body, err := ioutil.ReadAll(resp.Body); err != nil { if body, err := ioutil.ReadAll(resp.Body); err != nil {
return errors.Wrapf(err, "ioutil.ReadAll(%v)", methodPath) return errors.Wrapf(err, "ioutil.ReadAll(%s)", methodPath)
} else if err = json.Unmarshal(body, v); err != nil { } else if err = json.Unmarshal(body, v); err != nil {
return errors.Wrapf(err, "json.Unmarshal(%v)", methodPath) return errors.Wrapf(err, "json.Unmarshal(%s)", methodPath)
} }
return nil return nil
} }