1
0
Fork 0
mirror of https://github.com/Luzifer/gpxhydrant.git synced 2024-11-08 23:40:01 +00:00

Improve logging, fix tagging error

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2017-06-05 17:23:52 +02:00
parent 02aadca109
commit be0704451a
Signed by: luzifer
GPG key ID: DC2729FDD34BE99E
2 changed files with 88 additions and 30 deletions

42
main.go
View file

@ -3,7 +3,6 @@ package main
import ( import (
"errors" "errors"
"fmt" "fmt"
"log"
"os" "os"
"strconv" "strconv"
@ -11,13 +10,15 @@ import (
"github.com/Luzifer/gpxhydrant/gpx" "github.com/Luzifer/gpxhydrant/gpx"
"github.com/Luzifer/gpxhydrant/osm" "github.com/Luzifer/gpxhydrant/osm"
"github.com/Luzifer/rconfig" "github.com/Luzifer/rconfig"
log "github.com/Sirupsen/logrus"
) )
var ( var (
cfg = struct { cfg = struct {
Comment string `flag:"comment,c" default:"Added hydrants from GPX file" description:"Comment for the changeset"` Comment string `flag:"comment,c" default:"Added hydrants from GPX file" description:"Comment for the changeset"`
Debug bool `flag:"debug,d" default:"false" description:"Enable debug logging"` Debug bool `flag:"debug,d" default:"false" description:"Enable debug logging (Deprecated: Use --log-level=debug)"`
GPXFile string `flag:"gpx-file,f" description:"File containing GPX waypoints"` GPXFile string `flag:"gpx-file,f" description:"File containing GPX waypoints"`
LogLevel string `flag:"log-level" default:"info" description:"Log level (debug, info, warn, error)"`
MachRange int64 `flag:"match-range" default:"5" description:"Range of meters to match GPX hydrants to OSM nodes"` MachRange int64 `flag:"match-range" default:"5" description:"Range of meters to match GPX hydrants to OSM nodes"`
NoOp bool `flag:"noop,n" default:"false" description:"Fetch data from OSM but do not write"` NoOp bool `flag:"noop,n" default:"false" description:"Fetch data from OSM but do not write"`
OSM struct { OSM struct {
@ -60,6 +61,17 @@ func init() {
os.Exit(0) os.Exit(0)
} }
if l, err := log.ParseLevel(cfg.LogLevel); err == nil {
log.SetLevel(l)
} else {
log.Fatalf("Unable to parse log level: %s", err)
}
// Support deprecated parameter to overwrite log level
if cfg.Debug {
log.SetLevel(log.DebugLevel)
}
if cfg.GPXFile == "" { if cfg.GPXFile == "" {
log.Fatalf("gpx-file is a required parameter") log.Fatalf("gpx-file is a required parameter")
} }
@ -69,12 +81,6 @@ func init() {
} }
} }
func logDebugf(format string, values ...interface{}) {
if cfg.Debug {
log.Printf(format, values...)
}
}
func hydrantsFromGPXFile() ([]*hydrant, bounds) { func hydrantsFromGPXFile() ([]*hydrant, bounds) {
// Read and parse GPX file // Read and parse GPX file
gpsFile, err := os.Open(cfg.GPXFile) gpsFile, err := os.Open(cfg.GPXFile)
@ -94,12 +100,12 @@ func hydrantsFromGPXFile() ([]*hydrant, bounds) {
for _, wp := range gpxData.Waypoints { for _, wp := range gpxData.Waypoints {
h, e := parseWaypoint(wp) h, e := parseWaypoint(wp)
if e != nil { if e != nil {
if cfg.Debug || e != errWrongGPXComment { if e != errWrongGPXComment {
log.Printf("Found waypoint not suitable for converting: %s (Reason: %s)", wp.Name, e) log.Debugf("Found waypoint not suitable for converting: %s (Reason: %s)", wp.Name, e)
} }
continue continue
} }
logDebugf("Found a hydrant from waypoint %s: %#v", wp.Name, h) log.Debugf("Found a hydrant from waypoint %s: %#v", wp.Name, h)
hydrants = append(hydrants, h) hydrants = append(hydrants, h)
bds.Update(h.Latitude, h.Longitude) bds.Update(h.Latitude, h.Longitude)
@ -118,10 +124,10 @@ func createChangeset(osmClient *osm.Client) *osm.Changeset {
log.Fatalf("Unable to create changeset: %s", err) log.Fatalf("Unable to create changeset: %s", err)
} }
logDebugf("Working on Changeset %d", cs.ID) log.Debugf("Working on Changeset %d", cs.ID)
cs.Tags = []osm.Tag{ cs.Tags = []osm.Tag{
{Key: "comment", Value: cfg.Comment}, //{Key: "comment", Value: cfg.Comment},
{Key: "created_by", Value: fmt.Sprintf("gpxhydrant %s", version)}, {Key: "created_by", Value: fmt.Sprintf("gpxhydrant %s", version)},
} }
@ -141,7 +147,7 @@ func getHydrantsFromOSM(osmClient *osm.Client, bds bounds) []*hydrant {
log.Fatalf("Unable to get map data: %s", err) log.Fatalf("Unable to get map data: %s", err)
} }
logDebugf("Retrieved %d nodes from map", len(mapData.Nodes)) log.Debugf("Retrieved %d nodes from map", len(mapData.Nodes))
availableHydrants := []*hydrant{} availableHydrants := []*hydrant{}
for _, n := range mapData.Nodes { for _, n := range mapData.Nodes {
@ -165,6 +171,8 @@ func main() {
log.Fatalf("Unable to log into OSM: %s", err) log.Fatalf("Unable to log into OSM: %s", err)
} }
osmClient.DebugHTTPRequests = log.GetLevel() == log.DebugLevel
// Retrieve currently available information from OSM // Retrieve currently available information from OSM
availableHydrants := getHydrantsFromOSM(osmClient, bds) availableHydrants := getHydrantsFromOSM(osmClient, bds)
@ -189,7 +197,7 @@ func updateOrCreateHydrants(hydrants, availableHydrants []*hydrant, osmClient *o
if err := osmClient.SaveNode(h.ToNode(), createChangeset(osmClient)); err != nil { if err := osmClient.SaveNode(h.ToNode(), createChangeset(osmClient)); err != nil {
log.Fatalf("Unable to create node using the OSM API: %s", err) log.Fatalf("Unable to create node using the OSM API: %s", err)
} }
logDebugf("Created a hydrant: %s", h.Name) log.Debugf("Created a hydrant: %s", h.Name)
}, },
) )
continue continue
@ -201,7 +209,7 @@ func updateOrCreateHydrants(hydrants, availableHydrants []*hydrant, osmClient *o
} }
if !found.NeedsUpdate(h) { if !found.NeedsUpdate(h) {
logDebugf("Found a good looking hydrant which needs no update: %#v", h) log.Debugf("Found a good looking hydrant which needs no update: %#v", h)
// Everything matches, we don't care // Everything matches, we don't care
continue continue
} }
@ -214,7 +222,7 @@ func updateOrCreateHydrants(hydrants, availableHydrants []*hydrant, osmClient *o
if err := osmClient.SaveNode(h.ToNode(), createChangeset(osmClient)); err != nil { if err := osmClient.SaveNode(h.ToNode(), createChangeset(osmClient)); err != nil {
log.Fatalf("Unable to create node using the OSM API: %s", err) log.Fatalf("Unable to create node using the OSM API: %s", err)
} }
logDebugf("Changed a hydrant: %s", h.Name) log.Debugf("Changed a hydrant: %s", h.Name)
}, },
) )
} }

View file

@ -6,6 +6,7 @@ import (
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
"math"
"net/http" "net/http"
"strconv" "strconv"
"time" "time"
@ -24,6 +25,8 @@ type Client struct {
APIBaseURL string APIBaseURL string
HTTPClient *http.Client HTTPClient *http.Client
CurrentUser *User CurrentUser *User
DebugHTTPRequests bool
} }
// New instantiates a new client and retrieves information about the current user. Set useDevServer to true to change the API URL to the api06.dev.openstreetmap.org server. // New instantiates a new client and retrieves information about the current user. Set useDevServer to true to change the API URL to the api06.dev.openstreetmap.org server.
@ -34,6 +37,8 @@ func New(username, password string, useDevServer bool) (*Client, error) {
APIBaseURL: liveAPIBaseURL, APIBaseURL: liveAPIBaseURL,
HTTPClient: http.DefaultClient, HTTPClient: http.DefaultClient,
DebugHTTPRequests: false,
} }
if useDevServer { if useDevServer {
@ -65,25 +70,58 @@ func (c *Client) doPlain(method, path string, body io.Reader) (string, error) {
} }
func (c *Client) do(method, path string, body io.Reader) (io.ReadCloser, error) { func (c *Client) do(method, path string, body io.Reader) (io.ReadCloser, error) {
var reqBodyBuffer *bytes.Buffer = nil
if body != nil {
reqBodyBuffer = bytes.NewBufferString("")
io.Copy(reqBodyBuffer, body)
body = bytes.NewBuffer(reqBodyBuffer.Bytes())
}
req, _ := http.NewRequest(method, c.APIBaseURL+path, body) req, _ := http.NewRequest(method, c.APIBaseURL+path, body)
req.SetBasicAuth(c.username, c.password) req.SetBasicAuth(c.username, c.password)
if method != "GET" {
req.Header.Set("Content-Type", "text/xml; charset=utf-8")
}
res, err := c.HTTPClient.Do(req) res, err := c.HTTPClient.Do(req)
if err != nil { if err != nil {
return nil, err return nil, err
} }
resBody := bytes.NewBufferString("")
io.Copy(resBody, res.Body)
if c.DebugHTTPRequests {
buf := bytes.NewBufferString("")
fmt.Fprintf(buf, "---------- REQUEST ----------\n")
fmt.Fprintf(buf, "%s %s\n", method, req.URL.String())
for k, v := range req.Header {
fmt.Fprintf(buf, "%s: %s\n", k, v[0])
}
fmt.Fprintf(buf, "\n")
if reqBodyBuffer != nil {
trunc := int(math.Min(float64(reqBodyBuffer.Len()), 1024))
fmt.Fprintf(buf, "%s\n\n", reqBodyBuffer.String()[0:trunc])
}
fmt.Fprintf(buf, "---------- RESPONSE ----------\n")
for k, v := range res.Header {
fmt.Fprintf(buf, "%s: %s\n", k, v[0])
}
fmt.Fprintf(buf, "\n")
trunc := int(math.Min(float64(resBody.Len()), 1024))
fmt.Fprintf(buf, "%s\n", resBody.String()[0:trunc])
fmt.Println(buf.String())
}
if res.StatusCode != http.StatusOK { if res.StatusCode != http.StatusOK {
d, e := ioutil.ReadAll(res.Body) return nil, fmt.Errorf("OSM API responded with status code %d", res.StatusCode)
if e != nil {
return nil, fmt.Errorf("OSM API responded with status code %d and reading response failed", res.StatusCode)
} }
res.Body.Close() return ioutil.NopCloser(resBody), nil
return nil, fmt.Errorf("OSM API responded with status code %d (%s)", res.StatusCode, d)
}
return res.Body, nil
} }
func (c *Client) doParse(method, path string, body io.Reader, output interface{}) error { func (c *Client) doParse(method, path string, body io.Reader, output interface{}) error {
@ -137,8 +175,12 @@ func (c *Client) GetMyChangesets(onlyOpen bool) ([]*Changeset, error) {
// CreateChangeset creates a new changeset // CreateChangeset creates a new changeset
func (c *Client) CreateChangeset() (*Changeset, error) { func (c *Client) CreateChangeset() (*Changeset, error) {
body := bytes.NewBuffer([]byte{}) body := bytes.NewBufferString(xml.Header)
if err := xml.NewEncoder(body).Encode(Wrap{Changesets: []*Changeset{{}}}); err != nil {
enc := xml.NewEncoder(body)
enc.Indent("", " ")
if err := enc.Encode(Wrap{Changesets: []*Changeset{{}}}); err != nil {
return nil, err return nil, err
} }
@ -169,8 +211,12 @@ func (c *Client) SaveChangeset(cs *Changeset) error {
data := Wrap{Changesets: []*Changeset{cs}} data := Wrap{Changesets: []*Changeset{cs}}
body := bytes.NewBuffer([]byte{}) body := bytes.NewBufferString(xml.Header)
if err := xml.NewEncoder(body).Encode(data); err != nil {
enc := xml.NewEncoder(body)
enc.Indent("", " ")
if err := enc.Encode(data); err != nil {
return err return err
} }
@ -225,8 +271,12 @@ func (c *Client) SaveNode(n *Node, cs *Changeset) error {
data := Wrap{Nodes: []*Node{n}} data := Wrap{Nodes: []*Node{n}}
body := bytes.NewBuffer([]byte{}) body := bytes.NewBufferString(xml.Header)
if err := xml.NewEncoder(body).Encode(data); err != nil {
enc := xml.NewEncoder(body)
enc.Indent("", " ")
if err := enc.Encode(data); err != nil {
return err return err
} }