Update mojo sdk to rev c02a28868825edfa57ab77947b8cb15e741c5598
[chromium-blink-merge.git] / components / proximity_auth / logging / log_buffer.h
blob46b335ace256d29e19ff5885ac8abbc0731be89a
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 #ifndef COMPONENTS_PROXIMITY_AUTH_LOGGING_LOG_BUFFER_H
6 #define COMPONENTS_PROXIMITY_AUTH_LOGGING_LOG_BUFFER_H
8 #include <list>
10 #include "base/logging.h"
11 #include "base/macros.h"
12 #include "base/observer_list.h"
13 #include "base/time/time.h"
15 namespace proximity_auth {
17 // Contains logs specific to the Proximity Auth. This buffer has a maximum size
18 // and will discard entries in FIFO order.
19 // Call LogBuffer::GetInstance() to get the global LogBuffer instance.
20 class LogBuffer {
21 public:
22 // Represents a single log entry in the log buffer.
23 struct LogMessage {
24 const std::string text;
25 const base::Time time;
26 const std::string file;
27 const int line;
28 const logging::LogSeverity severity;
30 LogMessage(const std::string& text,
31 const base::Time& time,
32 const std::string& file,
33 int line,
34 logging::LogSeverity severity);
37 class Observer {
38 public:
39 // Called when a new message is added to the log buffer.
40 virtual void OnLogMessageAdded(const LogMessage& log_message) = 0;
42 // Called when all messages in the log buffer are cleared.
43 virtual void OnLogBufferCleared() = 0;
46 LogBuffer();
47 ~LogBuffer();
49 // Returns the global instance.
50 static LogBuffer* GetInstance();
52 // Adds and removes log buffer observers.
53 void AddObserver(Observer* observer);
54 void RemoveObserver(Observer* observer);
56 // Adds a new log message to the buffer. If the number of log messages exceeds
57 // the maximum, then the earliest added log will be removed.
58 void AddLogMessage(const LogMessage& log_message);
60 // Clears all logs in the buffer.
61 void Clear();
63 // Returns the maximum number of logs that can be stored.
64 size_t MaxBufferSize() const;
66 // Returns the list logs in the buffer, sorted chronologically.
67 const std::list<LogMessage>* logs() { return &log_messages_; }
69 private:
70 // The messages currently in the buffer.
71 std::list<LogMessage> log_messages_;
73 // List of observers.
74 base::ObserverList<Observer> observers_;
76 DISALLOW_COPY_AND_ASSIGN(LogBuffer);
79 } // namespace proximity_auth
81 #endif // COMPONENTS_PROXIMITY_AUTH_LOGGING_LOG_BUFFER_H