Skip to content

Commit

Permalink
Add custom logger interface
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzodonini committed Mar 21, 2021
1 parent ec0af49 commit ca11385
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
7 changes: 4 additions & 3 deletions ocpp/ocpp.go
Expand Up @@ -4,8 +4,9 @@ package ocpp

import (
"fmt"
errors2 "github.com/pkg/errors"
"reflect"

"github.com/pkg/errors"
)

// Feature represents a single functionality, associated to a unique name.
Expand Down Expand Up @@ -92,7 +93,7 @@ func (p *Profile) GetFeature(name string) Feature {
func (p *Profile) ParseRequest(featureName string, rawRequest interface{}, requestParser func(raw interface{}, requestType reflect.Type) (Request, error)) (Request, error) {
feature, ok := p.Features[featureName]
if !ok {
return nil, errors2.Errorf("Feature %s not found", featureName)
return nil, errors.Errorf("Feature %s not found", featureName)
}
requestType := feature.GetRequestType()
return requestParser(rawRequest, requestType)
Expand All @@ -103,7 +104,7 @@ func (p *Profile) ParseRequest(featureName string, rawRequest interface{}, reque
func (p *Profile) ParseResponse(featureName string, rawResponse interface{}, responseParser func(raw interface{}, responseType reflect.Type) (Response, error)) (Response, error) {
feature, ok := p.Features[featureName]
if !ok {
return nil, errors2.Errorf("Feature %s not found", featureName)
return nil, errors.Errorf("Feature %s not found", featureName)
}
responseType := feature.GetResponseType()
return responseParser(rawResponse, responseType)
Expand Down
24 changes: 24 additions & 0 deletions ocpp/types.go
@@ -0,0 +1,24 @@
package ocpp

// Logger is the adapter interface that needs to be implemented, if the library should internally print logs.
//
// This allows to hook up your logger of choice.
type Logger interface {
Debug(args ...interface{})
Debugf(format string, args ...interface{})
Info(args ...interface{})
Infof(format string, args ...interface{})
Error(args ...interface{})
Errorf(format string, args ...interface{})
}

// VoidLogger is an empty implementation of the Logger interface, which doesn't actually process any logs.
// It may be used as a dummy implementation, if no logs should be visible.
type VoidLogger struct{}

func (l *VoidLogger) Debug(args ...interface{}) {}
func (l *VoidLogger) Debugf(format string, args ...interface{}) {}
func (l *VoidLogger) Info(args ...interface{}) {}
func (l *VoidLogger) Infof(format string, args ...interface{}) {}
func (l *VoidLogger) Error(args ...interface{}) {}
func (l *VoidLogger) Errorf(format string, args ...interface{}) {}

0 comments on commit ca11385

Please sign in to comment.