2022-05-31 21:53:01 +02:00
|
|
|
package tracex
|
2021-02-02 12:05:47 +01:00
|
|
|
|
2022-06-01 07:44:54 +02:00
|
|
|
//
|
|
|
|
// Saver implementation
|
|
|
|
//
|
|
|
|
|
2021-02-02 12:05:47 +01:00
|
|
|
import "sync"
|
|
|
|
|
2022-06-01 07:44:54 +02:00
|
|
|
// The Saver saves a trace. The zero value of this type
|
2022-06-01 08:31:20 +02:00
|
|
|
// is valid and can be used without initialization.
|
2021-02-02 12:05:47 +01:00
|
|
|
type Saver struct {
|
2022-06-01 07:44:54 +02:00
|
|
|
// ops contains the saved events.
|
2021-02-02 12:05:47 +01:00
|
|
|
ops []Event
|
2022-06-01 07:44:54 +02:00
|
|
|
|
|
|
|
// mu provides mutual exclusion.
|
|
|
|
mu sync.Mutex
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read reads and returns events inside the trace. It advances
|
|
|
|
// the read pointer so you won't see such events again.
|
|
|
|
func (s *Saver) Read() []Event {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
v := s.ops
|
|
|
|
s.ops = nil
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write adds the given event to the trace. A subsequent call
|
|
|
|
// to Read will read this event.
|
|
|
|
func (s *Saver) Write(ev Event) {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
s.ops = append(s.ops, ev)
|
|
|
|
}
|