1 // Copyright 2015 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.
5 #include "components/proximity_auth/logging/log_buffer.h"
7 #include "base/lazy_instance.h"
9 namespace proximity_auth
{
13 // The maximum number of logs that can be stored in the buffer.
14 const size_t kMaxBufferSize
= 1000;
16 // The global instance returned by LogBuffer::GetInstance().
17 base::LazyInstance
<LogBuffer
>::Leaky g_log_buffer
= LAZY_INSTANCE_INITIALIZER
;
21 LogBuffer::LogMessage::LogMessage(const std::string
& text
,
22 const base::Time
& time
,
23 const std::string
& file
,
25 logging::LogSeverity severity
)
26 : text(text
), time(time
), file(file
), line(line
), severity(severity
) {
29 LogBuffer::LogBuffer() {
32 LogBuffer::~LogBuffer() {
36 LogBuffer
* LogBuffer::GetInstance() {
37 return &g_log_buffer
.Get();
40 void LogBuffer::AddObserver(Observer
* observer
) {
41 observers_
.AddObserver(observer
);
44 void LogBuffer::RemoveObserver(Observer
* observer
) {
45 observers_
.RemoveObserver(observer
);
48 void LogBuffer::AddLogMessage(const LogMessage
& log_message
) {
49 // Note: We may want to sort the messages by timestamp if there are cases
50 // where logs are not added chronologically.
51 log_messages_
.push_back(log_message
);
52 if (log_messages_
.size() > MaxBufferSize())
53 log_messages_
.pop_front();
54 FOR_EACH_OBSERVER(Observer
, observers_
, OnLogMessageAdded(log_message
));
57 void LogBuffer::Clear() {
58 log_messages_
.clear();
59 FOR_EACH_OBSERVER(Observer
, observers_
, OnLogBufferCleared());
62 size_t LogBuffer::MaxBufferSize() const {
63 return kMaxBufferSize
;