You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 lines
893 B

5 years ago
  1. package log_buffer
  2. import "time"
  3. type MemBuffer struct {
  4. buf []byte
  5. startTime time.Time
  6. stopTime time.Time
  7. }
  8. type SealedBuffers struct {
  9. buffers []*MemBuffer
  10. }
  11. func newSealedBuffers(size int) *SealedBuffers {
  12. sbs := &SealedBuffers{}
  13. sbs.buffers = make([]*MemBuffer, size)
  14. for i := 0; i < size; i++ {
  15. sbs.buffers[i] = &MemBuffer{
  16. buf: make([]byte, BufferSize),
  17. }
  18. }
  19. return sbs
  20. }
  21. func (sbs *SealedBuffers) SealBuffer(startTime, stopTime time.Time, buf []byte) (newBuf []byte) {
  22. oldMemBuffer := sbs.buffers[0]
  23. size := len(sbs.buffers)
  24. for i := 0; i < size-1; i++ {
  25. sbs.buffers[i].buf = sbs.buffers[i+1].buf
  26. sbs.buffers[i].startTime = sbs.buffers[i+1].startTime
  27. sbs.buffers[i].stopTime = sbs.buffers[i+1].stopTime
  28. }
  29. sbs.buffers[size-1].buf = buf
  30. sbs.buffers[size-1].startTime = startTime
  31. sbs.buffers[size-1].stopTime = stopTime
  32. return oldMemBuffer.buf
  33. }