Skip to content

Commit

Permalink
feat: add log interface to Ohbem conf struct (#15)
Browse files Browse the repository at this point in the history
* fix: log to stdout by default

* feat: add log interface to Ohbem conf struct

* feat: rework log
  • Loading branch information
lenisko authored Jun 23, 2023
1 parent cd091c5 commit 47338b0
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
13 changes: 6 additions & 7 deletions ohbem.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package gohbem

import (
"encoding/json"
"log"
"math"
"os"
"reflect"
Expand Down Expand Up @@ -60,7 +59,7 @@ func (o *Ohbem) WatchPokemonData() error {
return ErrWatcherStarted
}

log.Printf("MasterFile Watcher Started")
o.log("MasterFile Watcher Started")
o.watcherChan = make(chan bool)
var interval time.Duration

Expand All @@ -77,20 +76,20 @@ func (o *Ohbem) WatchPokemonData() error {
for {
select {
case <-o.watcherChan:
log.Printf("MasterFile Watcher Stopped")
o.log("MasterFile Watcher Stopped")
ticker.Stop()
return
case <-ticker.C:
log.Printf("Checking remote MasterFile")
o.log("Checking remote MasterFile")
pokemonData, err := fetchMasterFile()
if err != nil {
log.Printf("Remote MasterFile fetch failed")
o.log("Remote MasterFile fetch failed")
continue
}
if reflect.DeepEqual(o.PokemonData, pokemonData) {
continue
} else {
log.Printf("New MasterFile found! Updating PokemonData")
o.log("New MasterFile found! Updating PokemonData")
o.PokemonData = pokemonData // overwrite PokemonData using new MasterFile
o.PokemonData.Initialized = true
o.ClearCache() // clean compactRankCache cache
Expand All @@ -114,7 +113,7 @@ func (o *Ohbem) StopWatchingPokemonData() error {
func (o *Ohbem) ClearCache() {
if !o.DisableCache {
o.compactRankCache = sync.Map{}
log.Printf("Cache cleaned")
o.log("Cache cleaned")
}
}

Expand Down
19 changes: 19 additions & 0 deletions structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,25 @@ type Ohbem struct {
WatcherInterval time.Duration
compactRankCache sync.Map
watcherChan chan bool
Logger Logger
}

// Logger interface
//
// Example implementation:
//
// type CustomLogger struct{}
// func (cl *CustomLogger) Print(message string) {
// fmt.Println("CustomLogger:", message)
// }
// logger := &CustomLogger{}
// ohbem := Ohbem{Logger: logger, ...}
//
// Notes:
// - The implementation of the Logger interface defines the specific behavior of the logging operation.
// - The method should handle the formatting and output of the Log message according to the logger's rules.
type Logger interface {
Print(message string)
}

// League struct is holding one entry of League configuration passed to Ohbem struct.
Expand Down
7 changes: 7 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,10 @@ func safetyCheck(o *Ohbem) error {
}
return nil
}

// log logs the given message using the provided logger, if available. If no logger is set, the message is ignored.
func (o *Ohbem) log(message string) {
if o.Logger != nil {
o.Logger.Print(message)
}
}

0 comments on commit 47338b0

Please sign in to comment.