mirror of
https://github.com/mainflux/mainflux.git
synced 2025-04-24 13:48:49 +08:00

* Update copyright comment for go files Signed-off-by: nwneisen <nwneisen@gmail.com> * Update copyright in assortment of file types Signed-off-by: nwneisen <nwneisen@gmail.com> * Remove missed copyright date Signed-off-by: nwneisen <nwneisen@gmail.com>
72 lines
2.0 KiB
Go
72 lines
2.0 KiB
Go
// Copyright (c) Mainflux
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
// +build !test
|
|
|
|
package api
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/mainflux/mainflux"
|
|
"github.com/mainflux/mainflux/coap"
|
|
log "github.com/mainflux/mainflux/logger"
|
|
)
|
|
|
|
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}
|
|
}
|
|
|
|
func (lm *loggingMiddleware) Publish(ctx context.Context, token string, msg mainflux.RawMessage) (err error) {
|
|
defer func(begin time.Time) {
|
|
destChannel := msg.Channel
|
|
if msg.Subtopic != "" {
|
|
destChannel = fmt.Sprintf("%s.%s", destChannel, msg.Subtopic)
|
|
}
|
|
message := fmt.Sprintf("Method publish to channel %s took %s to complete", destChannel, time.Since(begin))
|
|
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())
|
|
|
|
return lm.svc.Publish(ctx, token, msg)
|
|
}
|
|
|
|
func (lm *loggingMiddleware) Subscribe(chanID, subtopic, obsID string, o *coap.Observer) (err error) {
|
|
defer func(begin time.Time) {
|
|
destChannel := chanID
|
|
if subtopic != "" {
|
|
destChannel = fmt.Sprintf("%s.%s", destChannel, subtopic)
|
|
}
|
|
message := fmt.Sprintf("Method subscribe to channel %s for client %s took %s to complete", destChannel, obsID, time.Since(begin))
|
|
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())
|
|
|
|
return lm.svc.Subscribe(chanID, subtopic, obsID, o)
|
|
}
|
|
|
|
func (lm *loggingMiddleware) Unsubscribe(obsID string) {
|
|
defer func(begin time.Time) {
|
|
message := fmt.Sprintf("Method unsubscribe for the client %s took %s to complete without errors.", obsID, time.Since(begin))
|
|
lm.logger.Info(fmt.Sprintf(message))
|
|
}(time.Now())
|
|
|
|
lm.svc.Unsubscribe(obsID)
|
|
}
|