2019-10-07 08:14:47 -06:00
// Copyright (c) Mainflux
2018-10-31 18:53:25 +01:00
// SPDX-License-Identifier: Apache-2.0
// +build !test
package api
import (
2020-09-22 11:59:10 +02:00
"context"
2018-10-31 18:53:25 +01:00
"fmt"
"time"
"github.com/mainflux/mainflux/coap"
log "github.com/mainflux/mainflux/logger"
2020-06-03 15:16:19 +02:00
"github.com/mainflux/mainflux/pkg/messaging"
2018-10-31 18:53:25 +01:00
)
var _ coap . Service = ( * loggingMiddleware ) ( nil )
type loggingMiddleware struct {
logger log . Logger
svc coap . Service
}
// LoggingMiddleware adds logging facilities to the adapter.
func LoggingMiddleware ( svc coap . Service , logger log . Logger ) coap . Service {
return & loggingMiddleware { logger , svc }
}
2020-09-22 11:59:10 +02:00
func ( lm * loggingMiddleware ) Publish ( ctx context . Context , key string , msg messaging . Message ) ( err error ) {
2018-10-31 18:53:25 +01:00
defer func ( begin time . Time ) {
2019-03-15 18:38:07 +01:00
destChannel := msg . Channel
if msg . Subtopic != "" {
destChannel = fmt . Sprintf ( "%s.%s" , destChannel , msg . Subtopic )
}
2020-09-22 11:59:10 +02:00
message := fmt . Sprintf ( "Method publish to %s took %s to complete" , destChannel , time . Since ( begin ) )
2018-10-31 18:53:25 +01:00
if err != nil {
lm . logger . Warn ( fmt . Sprintf ( "%s with error: %s." , message , err ) )
return
}
lm . logger . Info ( fmt . Sprintf ( "%s without errors." , message ) )
} ( time . Now ( ) )
2020-09-22 11:59:10 +02:00
return lm . svc . Publish ( ctx , key , msg )
2018-10-31 18:53:25 +01:00
}
2020-09-22 11:59:10 +02:00
func ( lm * loggingMiddleware ) Subscribe ( ctx context . Context , key , chanID , subtopic string , c coap . Client ) ( err error ) {
2018-10-31 18:53:25 +01:00
defer func ( begin time . Time ) {
2019-03-15 18:38:07 +01:00
destChannel := chanID
if subtopic != "" {
destChannel = fmt . Sprintf ( "%s.%s" , destChannel , subtopic )
}
2020-09-22 11:59:10 +02:00
message := fmt . Sprintf ( "Method subscribe to %s for client %s took %s to complete" , destChannel , c . Token ( ) , time . Since ( begin ) )
2018-10-31 18:53:25 +01:00
if err != nil {
lm . logger . Warn ( fmt . Sprintf ( "%s with error: %s." , message , err ) )
return
}
lm . logger . Info ( fmt . Sprintf ( "%s without errors." , message ) )
} ( time . Now ( ) )
2020-09-22 11:59:10 +02:00
return lm . svc . Subscribe ( ctx , key , chanID , subtopic , c )
2018-10-31 18:53:25 +01:00
}
2020-09-22 11:59:10 +02:00
func ( lm * loggingMiddleware ) Unsubscribe ( ctx context . Context , key , chanID , subtopic , token string ) error {
2018-10-31 18:53:25 +01:00
defer func ( begin time . Time ) {
2020-09-22 11:59:10 +02:00
message := fmt . Sprintf ( "Method unsubscribe for the client %s from the channel %s and subtopic %s took %s to complete without errors." , token , chanID , subtopic , time . Since ( begin ) )
2018-10-31 18:53:25 +01:00
lm . logger . Info ( fmt . Sprintf ( message ) )
} ( time . Now ( ) )
2020-09-22 11:59:10 +02:00
return lm . svc . Unsubscribe ( ctx , key , chanID , subtopic , token )
2018-10-31 18:53:25 +01:00
}