Skip to content
This repository has been archived by the owner on Apr 4, 2023. It is now read-only.

Commit

Permalink
switched to global logger
Browse files Browse the repository at this point in the history
  • Loading branch information
farshidtz committed Feb 29, 2020
1 parent 800a46d commit 5725153
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 42 deletions.
5 changes: 3 additions & 2 deletions catalog/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package catalog

import (
"fmt"
"log"
"strings"
"time"

Expand Down Expand Up @@ -137,10 +138,10 @@ func (c *Controller) cleanExpired() {
}

for i := range expiredServices {
logger.Printf("cleanExpired() Removing expired registration: %s", expiredServices[i].ID)
log.Printf("cleanExpired() Removing expired registration: %s", expiredServices[i].ID)
err := c.storage.delete(expiredServices[i].ID)
if err != nil {
logger.Printf("cleanExpired() Error removing expired registration: %s: %s", expiredServices[i].ID, err)
log.Printf("cleanExpired() Error removing expired registration: %s: %s", expiredServices[i].ID, err)
continue
}
}
Expand Down
3 changes: 2 additions & 1 deletion catalog/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package catalog

import (
"encoding/json"
"log"
"net/http"
"strings"
)
Expand Down Expand Up @@ -39,7 +40,7 @@ func ErrorResponse(w http.ResponseWriter, code int, msgs ...string) {
msg,
}
if code >= 500 {
logger.Println("ERROR:", msg)
log.Println("ERROR:", msg)
}
b, _ := json.Marshal(e)
w.Header().Set("Content-Type", "application/json;version="+ApiVersion)
Expand Down
4 changes: 2 additions & 2 deletions catalog/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@ func HTTPDoAuth(req *http.Request, ticket *obtainer.Client) (*http.Response, err

if res.StatusCode == http.StatusUnauthorized {
// Get a new ticket and retry again
logger.Println("HTTPDoAuth() Invalid authentication ticket.")
log.Println("HTTPDoAuth() Invalid authentication ticket.")
bearer, err = ticket.Renew()
if err != nil {
return nil, err
}
logger.Println("HTTPDoAuth() Ticket was renewed.")
log.Println("HTTPDoAuth() Ticket was renewed.")

// Reset the header and try again
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", bearer))
Expand Down
20 changes: 0 additions & 20 deletions catalog/init.go

This file was deleted.

5 changes: 3 additions & 2 deletions catalog/ldbstorage.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package catalog
import (
"encoding/json"
"fmt"
"log"
"net/url"
"sync"

Expand Down Expand Up @@ -182,15 +183,15 @@ func (s *LevelDBStorage) iterator() <-chan *ThingDescription {
var td ThingDescription
err := json.Unmarshal(iter.Value(), &td)
if err != nil {
logger.Printf("LevelDB Error: %s", err)
log.Printf("LevelDB Error: %s", err)
return
}
serviceIter <- &td
}

err := iter.Error()
if err != nil {
logger.Printf("LevelDB Error: %s", err)
log.Printf("LevelDB Error: %s", err)
}
}()

Expand Down
24 changes: 18 additions & 6 deletions init.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,28 @@ package main
import (
"log"
"os"
"strconv"
)

var logger *log.Logger
const (
EnvVerbose = "VERBOSE" // print extra information e.g. line number)
EnvDisableLogTime = "DISABLE_LOG_TIME" // disable timestamp in logs
)

func init() {
logger = log.New(os.Stdout, "[main] ", 0)
log.SetOutput(os.Stdout)
log.SetFlags(0)

v, err := strconv.Atoi(os.Getenv("DEBUG"))
if err == nil && v == 1 {
logger.SetFlags(log.Ltime | log.Lshortfile)
logFlags := log.LstdFlags
if evalEnv(EnvDisableLogTime) {
logFlags = 0
}
if evalEnv(EnvVerbose) {
logFlags = logFlags | log.Lshortfile
}
log.SetFlags(logFlags)
}

// evalEnv returns the boolean value of the env variable with the given key
func evalEnv(key string) bool {
return os.Getenv(key) == "1" || os.Getenv(key) == "true" || os.Getenv(key) == "TRUE"
}
18 changes: 9 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func main() {

config, err := loadConfig(*confPath)
if err != nil {
logger.Fatalf("Error reading config file %v: %v", *confPath, err)
log.Fatalf("Error reading config file %v: %v", *confPath, err)
}
if config.ServiceID == "" {
config.ServiceID = uuid.NewV4().String()
Expand All @@ -41,7 +41,7 @@ func main() {

router, shutdownAPI, err := setupRouter(config)
if err != nil {
logger.Fatal(err.Error())
log.Fatal(err.Error())
}

// Announce service using DNS-SD
Expand All @@ -54,9 +54,9 @@ func main() {
[]string{"uri=/"},
nil)
if err != nil {
logger.Printf("Failed to register DNS-SD service: %s", err.Error())
log.Printf("Failed to register DNS-SD service: %s", err.Error())
} else {
logger.Println("Registered service via DNS-SD using type", catalog.DNSSDServiceType)
log.Println("Registered service via DNS-SD using type", catalog.DNSSDServiceType)
}
}

Expand All @@ -72,7 +72,7 @@ func main() {

err = mime.AddExtensionType(".jsonld", "application/ld+json")
if err != nil {
logger.Println("ERROR: ", err.Error())
log.Println("ERROR: ", err.Error())
}

// Configure the middleware
Expand All @@ -85,14 +85,14 @@ func main() {

// Start listener
endpoint := fmt.Sprintf("%s:%s", config.BindAddr, strconv.Itoa(config.BindPort))
logger.Printf("Starting standalone Resource Catalog at %v", endpoint)
log.Printf("Starting standalone Resource Catalog at %v", endpoint)
go n.Run(endpoint)

// Ctrl+C / Kill handling
handler := make(chan os.Signal, 1)
signal.Notify(handler, os.Interrupt, os.Kill)
<-handler
logger.Println("Shutting down...")
log.Println("Shutting down...")

// Stop bonjour registration
if bonjourS != nil {
Expand All @@ -103,10 +103,10 @@ func main() {
// Shutdown catalog API
err = shutdownAPI()
if err != nil {
logger.Println(err)
log.Println(err)
}

logger.Println("Stopped")
log.Println("Stopped")
}

func setupRouter(config *Config) (*router, func() error, error) {
Expand Down

0 comments on commit 5725153

Please sign in to comment.