syncthing-arm/lib/syncthing/auditservice.go

61 lines
1.3 KiB
Go
Raw Permalink Normal View History

2015-04-25 18:21:47 +09:00
// Copyright (C) 2015 The Syncthing Authors.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.
2015-04-25 18:21:47 +09:00
package syncthing
2015-04-25 18:21:47 +09:00
import (
"context"
2015-04-25 18:21:47 +09:00
"encoding/json"
"fmt"
2015-04-25 18:21:47 +09:00
"io"
"github.com/thejerf/suture"
2015-08-06 11:29:25 +02:00
"github.com/syncthing/syncthing/lib/events"
"github.com/syncthing/syncthing/lib/util"
2015-04-25 18:21:47 +09:00
)
2015-12-23 15:31:12 +00:00
// The auditService subscribes to events and writes these in JSON format, one
2015-04-25 18:21:47 +09:00
// event per line, to the specified writer.
2015-12-23 15:31:12 +00:00
type auditService struct {
suture.Service
w io.Writer // audit destination
sub events.Subscription
2015-04-25 18:21:47 +09:00
}
func newAuditService(w io.Writer, evLogger events.Logger) *auditService {
s := &auditService{
w: w,
sub: evLogger.Subscribe(events.AllEvents),
2015-04-25 18:21:47 +09:00
}
s.Service = util.AsService(s.serve, s.String())
return s
2015-04-25 18:21:47 +09:00
}
// serve runs the audit service.
func (s *auditService) serve(ctx context.Context) {
2015-04-25 18:21:47 +09:00
enc := json.NewEncoder(s.w)
for {
select {
case ev := <-s.sub.C():
2019-02-02 12:16:27 +01:00
enc.Encode(ev)
case <-ctx.Done():
2015-04-25 18:21:47 +09:00
return
}
}
}
// Stop stops the audit service.
2015-12-23 15:31:12 +00:00
func (s *auditService) Stop() {
s.Service.Stop()
s.sub.Unsubscribe()
2015-04-25 18:21:47 +09:00
}
func (s *auditService) String() string {
return fmt.Sprintf("auditService@%p", s)
}