diff options
author | Collin Guarino <collin.guarino@gmail.com> | 2016-01-23 13:04:26 -0500 |
---|---|---|
committer | Collin Guarino <collin.guarino@gmail.com> | 2016-01-23 13:04:26 -0500 |
commit | 9ae1ee53a4bb503b068b1fcdadd5c18bebb2d19e (patch) | |
tree | 99d9fccc41674ab54c80e1d0cba1cb83c4d3f89e | |
parent | d4fa3dbb5b1bb79d8c60885e3550f1d566e8a4c6 (diff) |
Added error tracing and better log output for GetBridgeInfo.
-rw-r--r-- | bridge.go | 33 |
1 files changed, 25 insertions, 8 deletions
@@ -6,11 +6,15 @@ import ( "encoding/xml" "net/http" "io/ioutil" + "runtime" + "fmt" ) func main() { bridge := NewBridge("192.168.1.128") log.Println(bridge.IPAddress) + + //trace("this is a trace") } type Bridge struct { @@ -51,26 +55,39 @@ func NewBridge(ip string) *Bridge { // Go to http://<bridge_ip>/description.xml set the bridge.Info func GetBridgeInfo(self *Bridge) { response, error := http.Get("http://" + self.IPAddress + "/description.xml") - if response.StatusCode != 200 { - log.Println("Bridge status error: %d", response.StatusCode) - os.Exit(1) - } else if error != nil { - log.Println("Error: ", error) + if error != nil { + trace("", error) + } else if response.StatusCode != 200 { + trace(fmt.Sprintf("Bridge status error: %d", response.StatusCode), nil) os.Exit(1) } body, error := ioutil.ReadAll(response.Body) defer response.Body.Close() if error != nil { - log.Println("Error parsing bridge description XML") + trace("Error parsing bridge description xml.", nil) os.Exit(1) } data := new(BridgeInfo) error = xml.Unmarshal(body, &data) if error != nil { - log.Println("GetBridgeInfo error. Cannot parse data from description.") - log.Println(error) + trace("Error using unmarshal to split xml.", nil) + os.Exit(1) } self.Info = *data } + +// Log the date, time, file location, line number, and function. +// Message can be "" or Err can be nil (not both) +func trace(message string, err error) { + pc := make([]uintptr, 10) + runtime.Callers(2, pc) + f := runtime.FuncForPC(pc[0]) + file, line := f.FileLine(pc[0]) + if err != nil { + log.Printf("%s:%d %s: %s\n", file, line, f.Name(), err) + } else { + log.Printf("%s:%d %s: %s\n", file, line, f.Name(), message) + } +} |