syncthing-arm/lib/syncthing/auditservice.go

55 lines
1.1 KiB
Go
Raw 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 (
"encoding/json"
"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
}
2015-12-23 15:31:12 +00:00
func newAuditService(w io.Writer) *auditService {
s := &auditService{
w: w,
sub: events.Default.Subscribe(events.AllEvents),
2015-04-25 18:21:47 +09:00
}
s.Service = util.AsService(s.serve)
return s
2015-04-25 18:21:47 +09:00
}
// serve runs the audit service.
func (s *auditService) serve(stop chan struct{}) {
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 <-stop:
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()
events.Default.Unsubscribe(s.sub)
2015-04-25 18:21:47 +09:00
}