From 75b0734bf8a2f0d0d6585dab64bd82680f996efb Mon Sep 17 00:00:00 2001 From: Mike Cheng Date: Mon, 4 May 2015 20:03:17 -0400 Subject: [PATCH] Add logging --- api/api.go | 4 ---- dyndns/daemon.go | 26 ++++++++++++++------------ log/log.go | 12 ++++++++++++ main.go | 30 ++++++++++++++++++++++++++---- 4 files changed, 52 insertions(+), 20 deletions(-) create mode 100644 log/log.go diff --git a/api/api.go b/api/api.go index 2df7df3..ff65b9f 100644 --- a/api/api.go +++ b/api/api.go @@ -116,8 +116,6 @@ func (api API) DeleteDNSRecord(domain, recordId string) error { return err } - fmt.Println("Body:", string(b)) - resp, err := api.performRequest( "POST", fmt.Sprintf("%s%s%s", api.baseUrl, "api/dns/delete/", domain), @@ -127,8 +125,6 @@ func (api API) DeleteDNSRecord(domain, recordId string) error { return err } - fmt.Println("Response:", string(resp)) - var result struct { Result resultResponse } diff --git a/dyndns/daemon.go b/dyndns/daemon.go index 4fb9a84..09947ac 100644 --- a/dyndns/daemon.go +++ b/dyndns/daemon.go @@ -1,8 +1,8 @@ package dyndns import ( - "fmt" "github.com/mfycheng/name-dyndns/api" + "github.com/mfycheng/name-dyndns/log" "sync" "time" ) @@ -10,14 +10,13 @@ import ( var wg sync.WaitGroup func updateDNSRecord(a api.API, domain, recordId string, newRecord api.DNSRecord) error { - fmt.Println("Deleting record...") + log.Logger.Printf("Deleting DNS record for %s.%s.\n", newRecord.Name, domain) err := a.DeleteDNSRecord(domain, newRecord.RecordId) if err != nil { return err } - // Does a /create/ overwrite? or do we have to delete first? - fmt.Println("Creating record") + log.Logger.Printf("Creating DNS record for %s.%s: %s\n", newRecord.Name, domain, newRecord) return a.CreateDNSRecord(domain, newRecord) } @@ -26,14 +25,15 @@ func runConfig(c api.Config, daemon bool) { a := api.NewAPIFromConfig(c) for { + log.Logger.Printf("Running update check for %s.", c.Domain) ip, err := GetExternalIP() if err != nil { - fmt.Print("Fail to retreive IP: ") + log.Logger.Print("Failed to retreive IP: ") if daemon { - fmt.Println("Will retry...") + log.Logger.Println("Will retry...") continue } else { - fmt.Println("Giving up.") + log.Logger.Println("Giving up.") break } } @@ -44,28 +44,30 @@ func runConfig(c api.Config, daemon bool) { // update it. records, err := a.GetRecords(c.Domain) if err != nil { - fmt.Print("Failed to retreive records for:%s\n", c.Domain) + log.Logger.Printf("Failed to retreive records for%s\n", c.Domain) if daemon { - fmt.Println("Will retry...") + log.Logger.Print("Will retry...") continue } else { - fmt.Println("Giving up.") + log.Logger.Print("Giving up.") break } } for _, r := range records { if r.Content != ip { - fmt.Printf("Updating record %s for %s\n", r.RecordId, c.Domain) r.Content = ip err = updateDNSRecord(a, c.Domain, r.RecordId, r) if err != nil { - fmt.Println("Failed to update record.", err) + log.Logger.Printf("Failed to update record %s [%s.%s] with IP: %s\n\t%s\n", r.RecordId, r.Name, c.Domain, ip, err) + } else { + log.Logger.Printf("Attempting to update record %s [%s.%s] with IP: %s\n", r.RecordId, r.Name, c.Domain, ip) } } } if !daemon { + log.Logger.Println("Non daemon mode, stopping.") return } diff --git a/log/log.go b/log/log.go new file mode 100644 index 0000000..050fb52 --- /dev/null +++ b/log/log.go @@ -0,0 +1,12 @@ +package log + +import ( + "io" + "log" +) + +var Logger *log.Logger + +func Init(writer io.Writer) { + Logger = log.New(writer, "", log.Ldate|log.Ltime) +} diff --git a/main.go b/main.go index 9d66793..72948f5 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,7 @@ import ( "fmt" "github.com/mfycheng/name-dyndns/api" "github.com/mfycheng/name-dyndns/dyndns" + "github.com/mfycheng/name-dyndns/log" "os" ) @@ -22,17 +23,38 @@ func main() { configPath := flag.String("config", "./config.json", "Specify the configuration file") daemon := flag.Bool("daemon", false, "Operate in daemon mode.") dev := flag.Bool("dev", false, "Use development configurations instead.") + logFile := flag.String("log", "", "Specify a logfile. If no file is provided, uses stdout.") flag.Parse() + var file *os.File + defer file.Close() + + if *logFile == "" { + file = os.Stdout + } else { + var err error + file, err = os.OpenFile(*logFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666) + if err != nil { + fmt.Println("Could not open log for reading") + os.Exit(1) + } + } + + log.Init(file) + configs, err := api.LoadConfigs(*configPath) if err != nil { - fmt.Println("Error loading config:", err) - os.Exit(1) - return + log.Logger.Fatalln("Error loading config:", err) + } + + for _, config := range configs { + if config.Domain == "" || len(config.Hostnames) == 0 { + log.Logger.Fatalf("Empty configuration detected. Exiting.") + } } configs = filterConfigs(configs, *dev) - fmt.Printf("Successfully loaded %d configs\n", len(configs)) + log.Logger.Printf("Successfully loaded %d configs\n", len(configs)) dyndns.Run(configs, *daemon) }