OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_COMMON_METRICS_METRICS_LOG_MANAGER_H_ | 5 #ifndef CHROME_COMMON_METRICS_METRICS_LOG_MANAGER_H_ |
6 #define CHROME_COMMON_METRICS_METRICS_LOG_MANAGER_H_ | 6 #define CHROME_COMMON_METRICS_METRICS_LOG_MANAGER_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
(...skipping 10 matching lines...) Expand all Loading... | |
21 public: | 21 public: |
22 MetricsLogManager(); | 22 MetricsLogManager(); |
23 ~MetricsLogManager(); | 23 ~MetricsLogManager(); |
24 | 24 |
25 // Stores both XML and protocol buffer serializations for a log. | 25 // Stores both XML and protocol buffer serializations for a log. |
26 struct SerializedLog { | 26 struct SerializedLog { |
27 public: | 27 public: |
28 // Exposed to reduce code churn as we transition from the XML pipeline to | 28 // Exposed to reduce code churn as we transition from the XML pipeline to |
29 // the protocol buffer pipeline. | 29 // the protocol buffer pipeline. |
30 bool empty() const; | 30 bool empty() const; |
31 void swap(SerializedLog& log); | |
Ilya Sherman
2012/02/29 18:58:51
nit: I think the typical Chromium style is to pass
stuartmorgan
2012/02/29 19:04:01
Yep, that was the intent. I went back and forth, a
| |
31 | 32 |
32 std::string xml; | 33 std::string xml; |
33 std::string proto; | 34 std::string proto; |
34 }; | 35 }; |
35 | 36 |
36 // Takes ownership of |log|, and makes it the current_log. | 37 enum LogType { |
37 // This should only be called if there is not a current log. | 38 INITIAL_LOG, // The first log of a session. |
38 void BeginLoggingWithLog(MetricsLogBase* log); | 39 ONGOING_LOG, // Subsequent logs in a session. |
40 }; | |
41 | |
42 // Takes ownership of |log|, which has type |log_type|, and makes it the | |
43 // current_log. This should only be called if there is not a current log. | |
44 void BeginLoggingWithLog(MetricsLogBase* log, LogType log_type); | |
39 | 45 |
40 // Returns the in-progress log. | 46 // Returns the in-progress log. |
41 MetricsLogBase* current_log() { return current_log_.get(); } | 47 MetricsLogBase* current_log() { return current_log_.get(); } |
42 | 48 |
43 // Closes |current_log| and stages it for upload, leaving |current_log| NULL. | 49 // Closes current_log(), compresses it, and stores the compressed log for |
44 void StageCurrentLogForUpload(); | 50 // later, leaving current_log() NULL. |
51 void FinishCurrentLog(); | |
52 | |
53 // Returns true if there are any logs waiting to be uploaded. | |
54 bool has_unsent_logs() const { | |
55 return !unsent_initial_logs_.empty() || !unsent_ongoing_logs_.empty(); | |
56 } | |
57 | |
58 // Populates staged_log_text() with the next stored log to send. | |
59 // Should only be called if has_unsent_logs() is true. | |
60 void StageNextLogForUpload(); | |
45 | 61 |
46 // Returns true if there is a log that needs to be, or is being, uploaded. | 62 // Returns true if there is a log that needs to be, or is being, uploaded. |
47 // Note that this returns true even if compressing the log text failed. | |
48 bool has_staged_log() const; | 63 bool has_staged_log() const; |
49 | 64 |
50 // Returns true if there is a protobuf log that needs to be uploaded. | 65 // Returns true if there is a protobuf log that needs to be uploaded. |
51 // In the case that an XML upload needs to be re-issued due to a previous | 66 // In the case that an XML upload needs to be re-issued due to a previous |
52 // failure, |has_staged_log()| will return true while this returns false. | 67 // failure, has_staged_log() will return true while this returns false. |
53 bool has_staged_log_proto() const; | 68 bool has_staged_log_proto() const; |
54 | 69 |
55 // The text of the staged log, in compressed XML or protobuf format. Empty if | 70 // The text of the staged log, in compressed XML or protobuf format. Empty if |
56 // there is no staged log, or if compression of the staged log failed. | 71 // there is no staged log, or if compression of the staged log failed. |
57 const SerializedLog& staged_log_text() const { | 72 const SerializedLog& staged_log_text() const { |
58 return staged_log_text_; | 73 return staged_log_text_; |
59 } | 74 } |
60 | 75 |
61 // Discards the staged log (both the XML and the protobuf data). | 76 // Discards the staged log (both the XML and the protobuf data). |
62 void DiscardStagedLog(); | 77 void DiscardStagedLog(); |
63 | 78 |
64 // Discards the protobuf data in the staged log. | 79 // Discards the protobuf data in the staged log. |
65 // This is useful to prevent needlessly re-issuing successful protobuf uploads | 80 // This is useful to prevent needlessly re-issuing successful protobuf uploads |
66 // due to XML upload failures. | 81 // due to XML upload failures. |
67 void DiscardStagedLogProto(); | 82 void DiscardStagedLogProto(); |
68 | 83 |
69 // Closes and discards |current_log|. | 84 // Closes and discards |current_log|. |
70 void DiscardCurrentLog(); | 85 void DiscardCurrentLog(); |
71 | 86 |
72 // Sets current_log to NULL, but saves the current log for future use with | 87 // Sets current_log to NULL, but saves the current log for future use with |
73 // ResumePausedLog(). Only one log may be paused at a time. | 88 // ResumePausedLog(). Only one log may be paused at a time. |
74 // TODO(stuartmorgan): Pause/resume support is really a workaround for a | 89 // TODO(stuartmorgan): Pause/resume support is really a workaround for a |
75 // design issue in initial log writing; that should be fixed, and pause/resume | 90 // design issue in initial log writing; that should be fixed, and pause/resume |
76 // removed. | 91 // removed. |
77 void PauseCurrentLog(); | 92 void PauseCurrentLog(); |
78 | 93 |
79 // Restores the previously paused log (if any) to current_log. | 94 // Restores the previously paused log (if any) to current_log(). |
80 // This should only be called if there is not a current log. | 95 // This should only be called if there is not a current log. |
81 void ResumePausedLog(); | 96 void ResumePausedLog(); |
82 | 97 |
83 // Returns true if there are any logs left over from previous sessions that | 98 // Saves the staged log, then clears staged_log(). |
84 // need to be uploaded. | 99 // This can only be called if has_staged_log() is true. |
85 bool has_unsent_logs() const { | 100 void StoreStagedLogAsUnsent(); |
86 return !unsent_initial_logs_.empty() || !unsent_ongoing_logs_.empty(); | |
87 } | |
88 | 101 |
89 enum LogType { | 102 // Sets the threshold for how large an onging log can be and still be written |
90 INITIAL_LOG, // The first log of a session. | 103 // to persistant storage. Ongoing logs larger than this will be discarded |
91 ONGOING_LOG, // Subsequent logs in a session. | 104 // before persisting. 0 is interpreted as no limit. |
92 }; | |
93 | |
94 // Saves the staged log as the given type (or discards it in accordance with | |
95 // set_max_ongoing_log_store_size), then clears the staged log. | |
96 // This can only be called after StageCurrentLogForUpload. | |
97 void StoreStagedLogAsUnsent(LogType log_type); | |
98 | |
99 // Populates staged_log_text with the next stored log to send. | |
100 void StageNextStoredLogForUpload(); | |
101 | |
102 // Sets the threshold for how large an onging log can be and still be stored. | |
103 // Ongoing logs larger than this will be discarded. 0 is interpreted as no | |
104 // limit. | |
105 void set_max_ongoing_log_store_size(size_t max_size) { | 105 void set_max_ongoing_log_store_size(size_t max_size) { |
106 max_ongoing_log_store_size_ = max_size; | 106 max_ongoing_log_store_size_ = max_size; |
107 } | 107 } |
108 | 108 |
109 // Interface for a utility class to serialize and deserialize logs for | 109 // Interface for a utility class to serialize and deserialize logs for |
110 // persistent storage. | 110 // persistent storage. |
111 class LogSerializer { | 111 class LogSerializer { |
112 public: | 112 public: |
113 virtual ~LogSerializer() {} | 113 virtual ~LogSerializer() {} |
114 | 114 |
(...skipping 16 matching lines...) Expand all Loading... | |
131 | 131 |
132 // Saves any unsent logs to persistent storage using the current log | 132 // Saves any unsent logs to persistent storage using the current log |
133 // serializer. Can only be called after set_log_serializer. | 133 // serializer. Can only be called after set_log_serializer. |
134 void PersistUnsentLogs(); | 134 void PersistUnsentLogs(); |
135 | 135 |
136 // Loads any unsent logs from persistent storage using the current log | 136 // Loads any unsent logs from persistent storage using the current log |
137 // serializer. Can only be called after set_log_serializer. | 137 // serializer. Can only be called after set_log_serializer. |
138 void LoadPersistedUnsentLogs(); | 138 void LoadPersistedUnsentLogs(); |
139 | 139 |
140 private: | 140 private: |
141 // Compresses |staged_log_| and stores the result in | 141 // Saves |log_text| as the given type (or discards it in accordance with |
142 // |compressed_staged_xml_log_text_|. | 142 // |max_ongoing_log_store_size_|). |
143 void CompressStagedLog(); | 143 // NOTE: This clears the contents of |log_text| (to avoid an expensive |
144 // string copy), so the log should be discarded after this call. | |
145 void StoreLog(SerializedLog* log_text, LogType log_type); | |
146 | |
147 // Compresses current_log_ into compressed_log. | |
148 void CompressCurrentLog(SerializedLog* compressed_log); | |
144 | 149 |
145 // Compresses the text in |input| using bzip2, store the result in |output|. | 150 // Compresses the text in |input| using bzip2, store the result in |output|. |
146 static bool Bzip2Compress(const std::string& input, std::string* output); | 151 static bool Bzip2Compress(const std::string& input, std::string* output); |
147 | 152 |
148 // The log that we are still appending to. | 153 // The log that we are still appending to. |
149 scoped_ptr<MetricsLogBase> current_log_; | 154 scoped_ptr<MetricsLogBase> current_log_; |
155 LogType current_log_type_; | |
150 | 156 |
151 // A paused, previously-current log. | 157 // A paused, previously-current log. |
152 scoped_ptr<MetricsLogBase> paused_log_; | 158 scoped_ptr<MetricsLogBase> paused_log_; |
153 | 159 |
154 // The log that we are currently transmiting, or about to try to transmit. | |
155 // Note that when using StageNextStoredLogForUpload, this can be NULL while | |
156 // |compressed_staged_xml_log_text_| is non-NULL. | |
157 scoped_ptr<MetricsLogBase> staged_log_; | |
158 | |
159 // Helper class to handle serialization/deserialization of logs for persistent | 160 // Helper class to handle serialization/deserialization of logs for persistent |
160 // storage. May be NULL. | 161 // storage. May be NULL. |
161 scoped_ptr<LogSerializer> log_serializer_; | 162 scoped_ptr<LogSerializer> log_serializer_; |
162 | 163 |
163 // The text representations of the staged log, ready for upload to the server. | 164 // The text representations of the staged log, ready for upload to the server. |
164 // The first item in the pair is the compressed XML representation; the second | 165 // The first item in the pair is the compressed XML representation; the second |
165 // is the protobuf representation. | 166 // is the protobuf representation. |
166 SerializedLog staged_log_text_; | 167 SerializedLog staged_log_text_; |
168 LogType staged_log_type_; | |
167 | 169 |
168 // Logs from a previous session that have not yet been sent. | 170 // Logs from a previous session that have not yet been sent. |
169 // The first item in each pair is the XML representation; the second item is | 171 // The first item in each pair is the XML representation; the second item is |
170 // the protobuf representation. | 172 // the protobuf representation. |
171 // Note that the vector has the oldest logs listed first (early in the | 173 // Note that the vector has the oldest logs listed first (early in the |
172 // vector), and we'll discard old logs if we have gathered too many logs. | 174 // vector), and we'll discard old logs if we have gathered too many logs. |
173 std::vector<SerializedLog> unsent_initial_logs_; | 175 std::vector<SerializedLog> unsent_initial_logs_; |
174 std::vector<SerializedLog> unsent_ongoing_logs_; | 176 std::vector<SerializedLog> unsent_ongoing_logs_; |
175 | 177 |
176 size_t max_ongoing_log_store_size_; | 178 size_t max_ongoing_log_store_size_; |
177 | 179 |
178 DISALLOW_COPY_AND_ASSIGN(MetricsLogManager); | 180 DISALLOW_COPY_AND_ASSIGN(MetricsLogManager); |
179 }; | 181 }; |
180 | 182 |
181 #endif // CHROME_COMMON_METRICS_METRICS_LOG_MANAGER_H_ | 183 #endif // CHROME_COMMON_METRICS_METRICS_LOG_MANAGER_H_ |
OLD | NEW |