1 // Copyright 2019 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "chrome/browser/media/webrtc/webrtc_log_buffer.h"
6 
7 #include "base/check_op.h"
8 
WebRtcLogBuffer()9 WebRtcLogBuffer::WebRtcLogBuffer()
10     : buffer_(),
11       circular_(&buffer_[0], sizeof(buffer_), sizeof(buffer_) / 2, false),
12       read_only_(false) {}
13 
~WebRtcLogBuffer()14 WebRtcLogBuffer::~WebRtcLogBuffer() {
15 #if DCHECK_IS_ON()
16   DCHECK(read_only_ || sequence_checker_.CalledOnValidSequence());
17 #endif
18 }
19 
Log(const std::string & message)20 void WebRtcLogBuffer::Log(const std::string& message) {
21   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
22   DCHECK(!read_only_);
23   circular_.Write(message.c_str(), message.length());
24   const char eol = '\n';
25   circular_.Write(&eol, 1);
26 }
27 
Read()28 webrtc_logging::PartialCircularBuffer WebRtcLogBuffer::Read() {
29   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
30   DCHECK(read_only_);
31   return webrtc_logging::PartialCircularBuffer(&buffer_[0], sizeof(buffer_));
32 }
33 
SetComplete()34 void WebRtcLogBuffer::SetComplete() {
35   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
36   DCHECK(!read_only_) << "Already set? (programmer error)";
37   read_only_ = true;
38   // Detach from the current sequence so that we can check reads on a different
39   // sequence. This is to make sure that Read()s still happen on one sequence
40   // only.
41   DETACH_FROM_SEQUENCE(sequence_checker_);
42 }
43