Skip to content

Commit

Permalink
Improve logging and comments
Browse files Browse the repository at this point in the history
  • Loading branch information
bb4L committed Jun 25, 2021
1 parent e91cea7 commit 563a30a
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 88 deletions.
43 changes: 23 additions & 20 deletions api/alarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ import (
"io/ioutil"
"log"
"net/http"
"os"
"strconv"

"github.com/bb4L/rpi-radio-alarm-go-library/logging"
"github.com/bb4L/rpi-radio-alarm-go-library/types"
)

// Get all alarms
var logger = logging.GetLogger("alarm", os.Stdout)

// GetAlarms gets all alarms
func (helper *Helper) GetAlarms() ([]types.Alarm, error) {
url := helper.AlarmURL + "/alarm"
req, err := http.NewRequest("GET", url, nil)
Expand All @@ -23,10 +26,10 @@ func (helper *Helper) GetAlarms() ([]types.Alarm, error) {
}

res, err := helper.prepareAndDoRequest(req)
logging.GetInfoLogger().Println(res)
logger.Println(res)

if err != nil {
logging.GetErrorLogger().Println(err)
logger.Println(err)
return nil, err
}

Expand All @@ -40,14 +43,14 @@ func (helper *Helper) GetAlarms() ([]types.Alarm, error) {
err = json.Unmarshal(jsonData, &data)

if err != nil {
logging.GetErrorLogger().Println(err)
logger.Println(err)
return nil, err
}

return data, nil
}

// Get a specific alarm by index
// GetAlarm gets a specific alarm by index
func (helper *Helper) GetAlarm(idx int) (types.Alarm, error) {
url := helper.AlarmURL + "/alarm/" + strconv.Itoa(idx)
req, err := http.NewRequest("GET", url, nil)
Expand All @@ -56,10 +59,10 @@ func (helper *Helper) GetAlarm(idx int) (types.Alarm, error) {
}

res, err := helper.prepareAndDoRequest(req)
logging.GetInfoLogger().Println(res)
logger.Println(res)

if err != nil {
logging.GetErrorLogger().Println(err)
logger.Println(err)
return types.Alarm{}, err
}

Expand All @@ -73,14 +76,14 @@ func (helper *Helper) GetAlarm(idx int) (types.Alarm, error) {
err = json.Unmarshal(jsonData, &data)

if err != nil {
logging.GetErrorLogger().Println(err)
logger.Println(err)
return types.Alarm{}, err
}

return data, nil
}

// Change the alarm on the given index with the data of the passed instance
// ChangeAlarm changes the alarm on the given index with the data of the passed instance
func (helper *Helper) ChangeAlarm(alarm types.Alarm, idx int) (types.Alarm, error) {
url := helper.AlarmURL + "/alarm/" + strconv.Itoa(idx)

Expand All @@ -95,10 +98,10 @@ func (helper *Helper) ChangeAlarm(alarm types.Alarm, idx int) (types.Alarm, erro
}

res, err := helper.prepareAndDoRequest(req)
logging.GetInfoLogger().Println(res)
logger.Println(res)

if err != nil {
logging.GetErrorLogger().Println(err)
logger.Println(err)
return types.Alarm{}, err
}

Expand All @@ -112,14 +115,14 @@ func (helper *Helper) ChangeAlarm(alarm types.Alarm, idx int) (types.Alarm, erro
err = json.Unmarshal(jsonData, &data)

if err != nil {
logging.GetErrorLogger().Println(err)
logger.Println(err)
return types.Alarm{}, fmt.Errorf("could not unmarshal result")
}

return data, nil
}

// Adds the given alarm
// AddAlarm adds the given alarm
func (helper *Helper) AddAlarm(alarm types.Alarm) ([]types.Alarm, error) {
url := helper.AlarmURL + "/alarm"

Expand All @@ -135,9 +138,9 @@ func (helper *Helper) AddAlarm(alarm types.Alarm) ([]types.Alarm, error) {
}

res, err := helper.prepareAndDoRequest(req)
logging.GetInfoLogger().Println(res)
logger.Println(res)
if err != nil {
logging.GetErrorLogger().Println(err)
logger.Println(err)
return nil, err
}

Expand All @@ -150,14 +153,14 @@ func (helper *Helper) AddAlarm(alarm types.Alarm) ([]types.Alarm, error) {
var data []types.Alarm
err = json.Unmarshal(jsonData, &data)
if err != nil {
logging.GetErrorLogger().Println(err)
logger.Println(err)
return nil, err
}

return data, nil
}

// Delete the alarm with the given index
// DeleteAlarm deletes the alarm with the given index
func (helper *Helper) DeleteAlarm(idx int) ([]types.Alarm, error) {
url := helper.AlarmURL + "/alarm/" + strconv.Itoa(idx)

Expand All @@ -167,9 +170,9 @@ func (helper *Helper) DeleteAlarm(idx int) ([]types.Alarm, error) {
}

res, err := helper.prepareAndDoRequest(req)
logging.GetInfoLogger().Println(res)
logger.Println(res)
if err != nil {
logging.GetErrorLogger().Println(err)
logger.Println(err)
return nil, err
}

Expand All @@ -182,7 +185,7 @@ func (helper *Helper) DeleteAlarm(idx int) ([]types.Alarm, error) {
var data []types.Alarm
err = json.Unmarshal(jsonData, &data)
if err != nil {
logging.GetErrorLogger().Println(err)
logger.Println(err)
return nil, err
}

Expand Down
4 changes: 2 additions & 2 deletions api/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import (
"net/http"
)

// Struct to define the api settings being used
// Helper struct to define the api settings being used
type Helper struct {
AlarmURL string
ExtraHeader string
ExtreaHeaderValue string
}

// Checks wether the api is reachable and returns 200 on the health endpoint
// CheckHealth checks wether the api is reachable and returns 200 on the health endpoint
func (helper *Helper) CheckHealth() error {
url := helper.AlarmURL + "/health"
req, err := http.NewRequest("GET", url, nil)
Expand Down
25 changes: 12 additions & 13 deletions api/radio.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ import (
"io/ioutil"
"net/http"

"github.com/bb4L/rpi-radio-alarm-go-library/logging"
"github.com/bb4L/rpi-radio-alarm-go-library/types"
)

// Get the radio status
// GetRadio returns the radio status
func (helper *Helper) GetRadio() (types.Radio, error) {
url := helper.AlarmURL + "/radio"
req, err := http.NewRequest("GET", url, nil)
Expand All @@ -20,10 +19,10 @@ func (helper *Helper) GetRadio() (types.Radio, error) {
}

res, err := helper.prepareAndDoRequest(req)
logging.GetInfoLogger().Println(res)
logger.Println(res)

if err != nil {
logging.GetErrorLogger().Println(err)
logger.Println(err)
return types.Radio{}, err
}

Expand All @@ -37,14 +36,14 @@ func (helper *Helper) GetRadio() (types.Radio, error) {
err = json.Unmarshal(jsonData, &data)

if err != nil {
logging.GetErrorLogger().Println(err)
logger.Println(err)
return types.Radio{}, err
}

return data, nil
}

// Start the radio
// StartRadio starts the radio
func (helper *Helper) StartRadio() (types.Radio, error) {
values := map[string]string{"switch": "on"}
jsonData, _ := json.Marshal(values)
Expand All @@ -56,10 +55,10 @@ func (helper *Helper) StartRadio() (types.Radio, error) {
}

res, err := helper.prepareAndDoRequest(req)
logging.GetInfoLogger().Println(res)
logger.Println(res)

if err != nil {
logging.GetErrorLogger().Println(err)
logger.Println(err)
return types.Radio{}, err
}

Expand All @@ -73,14 +72,14 @@ func (helper *Helper) StartRadio() (types.Radio, error) {
err = json.Unmarshal(jsonData, &data)

if err != nil {
logging.GetErrorLogger().Println(err)
logger.Println(err)
return types.Radio{}, err
}

return data, nil
}

// Stop the radio
// StopRadio stops the radio
func (helper *Helper) StopRadio() (types.Radio, error) {
values := map[string]string{"switch": "off"}
jsonData, _ := json.Marshal(values)
Expand All @@ -92,10 +91,10 @@ func (helper *Helper) StopRadio() (types.Radio, error) {
}

res, err := helper.prepareAndDoRequest(req)
logging.GetInfoLogger().Println(res)
logger.Println(res)

if err != nil {
logging.GetErrorLogger().Println(err)
logger.Println(err)
return types.Radio{}, err
}

Expand All @@ -109,7 +108,7 @@ func (helper *Helper) StopRadio() (types.Radio, error) {
err = json.Unmarshal(jsonData, &data)

if err != nil {
logging.GetErrorLogger().Println(err)
logger.Println(err)
return types.Radio{}, err
}

Expand Down
43 changes: 6 additions & 37 deletions logging/loggers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,15 @@
package logging

import (
"io"
"log"
"os"
"sync"
)

const commonPrefix = "[rpi-radio-alarm-library]\t"
const commonPrefix = "[rpi-radio-alarm-library] "

var (
// InfoLogger logging infos
InfoLogger *log.Logger
var prefixes = log.Ldate | log.Ltime | log.Lmsgprefix

// ErrorLogger logging errors
ErrorLogger *log.Logger

// FatalLogger logging fatal
FatalLogger *log.Logger

once sync.Once
)

// Get the InfoLogger
func GetInfoLogger() *log.Logger {
once.Do(func() {
InfoLogger = log.New(os.Stderr, commonPrefix+"INFO: ", log.Ldate|log.Ltime|log.Lmsgprefix)
})
return InfoLogger
}

// Get the ErrorLogger
func GetErrorLogger() *log.Logger {
once.Do(func() {
ErrorLogger = log.New(os.Stderr, commonPrefix+"ERROR: ", log.Ldate|log.Ltime|log.Lmsgprefix)
})
return ErrorLogger
}

// Get the FatalLogger
func GetFatalLogger() *log.Logger {
once.Do(func() {
FatalLogger = log.New(os.Stderr, commonPrefix+"FATAL: ", log.Ldate|log.Ltime|log.Lmsgprefix)
})
return FatalLogger
// GetLogger returns a logger with given addional prefix
func GetLogger(place string, output io.Writer) *log.Logger {
return log.New(output, commonPrefix+"["+place+"] ", prefixes)
}
10 changes: 6 additions & 4 deletions rpi-radio-alarm-go-library.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,25 @@ import (
"github.com/joho/godotenv"
)

// Returns the working helper object with the config loaded from a .env file
var logger = logging.GetLogger("main", os.Stdout)

// GetHelperFromEnv Returns the working helper object with the config loaded from a .env file
func GetHelperFromEnv() api.Helper {

err := godotenv.Load()
if err != nil {
logging.GetFatalLogger().Fatal("error loading .env file")
logger.Fatal("error loading .env file")
}

if os.Getenv("ALARMURL") == "" {
logging.GetFatalLogger().Fatal("you have to specify ALARMURL in the .env")
logger.Fatal("you have to specify ALARMURL in the .env")
}

helper := api.Helper{AlarmURL: os.Getenv("ALARMURL"), ExtraHeader: os.Getenv("EXTRAHEADER"), ExtreaHeaderValue: os.Getenv("EXTRAHEADERVALUE")}

err = helper.CheckHealth()
if err != nil {
logging.GetFatalLogger().Fatalf("health check failed with: %s", err)
logger.Fatalf("health check failed with: %s", err)
}

return helper
Expand Down
2 changes: 1 addition & 1 deletion types/alarm.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Package types contains all the relevant structs/types for the library
package types

// Represent a alarm
// Alarm containing all the relevant data
type Alarm struct {
Name string `yaml:"name" json:"name"`
Hour int `yaml:"hour" json:"hour"`
Expand Down
Loading

0 comments on commit 563a30a

Please sign in to comment.