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" |
11 | 11 |
12 #include <string> | 12 #include <string> |
13 #include <vector> | 13 #include <vector> |
14 | 14 |
15 class MetricsLogBase; | 15 class MetricsLogBase; |
16 | 16 |
17 // Manages all the log objects used by a MetricsService implementation. Keeps | 17 // Manages all the log objects used by a MetricsService implementation. Keeps |
18 // track of both an in progress log and a log that is staged for uploading as | 18 // track of both an in progress log and a log that is staged for uploading as |
19 // text, as well as saving logs to, and loading logs from, persistent storage. | 19 // text, as well as saving logs to, and loading logs from, persistent storage. |
20 class MetricsLogManager { | 20 class MetricsLogManager { |
21 public: | 21 public: |
22 MetricsLogManager(); | 22 MetricsLogManager(); |
23 ~MetricsLogManager(); | 23 ~MetricsLogManager(); |
24 | 24 |
25 // Takes ownership of |log|, and makes it the current_log. | 25 enum LogType { |
26 // This should only be called if there is not a current log. | 26 INITIAL_LOG, // The first log of a session. |
27 void BeginLoggingWithLog(MetricsLogBase* log); | 27 ONGOING_LOG, // Subsequent logs in a session. |
28 }; | |
29 | |
30 // Takes ownership of |log|, which has type |log_type|, and makes it the | |
31 // current_log. This should only be called if there is not a current log. | |
32 void BeginLoggingWithLog(MetricsLogBase* log, LogType log_type); | |
28 | 33 |
29 // Returns the in-progress log. | 34 // Returns the in-progress log. |
30 MetricsLogBase* current_log() { return current_log_.get(); } | 35 MetricsLogBase* current_log() { return current_log_.get(); } |
31 | 36 |
32 // Closes |current_log| and stages it for upload, leaving |current_log| NULL. | 37 // Closes |current_log|, compresses it, and stores the compressed log for |
33 void StageCurrentLogForUpload(); | 38 // later upload as |log_type|, leaving |current_log| NULL. |
Ilya Sherman
2012/02/29 01:25:41
nit: current_log -> current_log_; log_type -> log_
stuartmorgan
2012/02/29 13:26:15
Changed to current_log() (I prefer to document pub
| |
39 void FinishCurrentLog(); | |
40 | |
41 // Returns true if there are any logs waiting to be uploaded. | |
42 bool has_unsent_logs() const { | |
43 return !unsent_initial_logs_.empty() || !unsent_ongoing_logs_.empty(); | |
44 } | |
45 | |
46 // Populates staged_log_text with the next stored log to send. | |
Ilya Sherman
2012/02/29 01:25:41
nit: staged_log_text -> |staged_log_text_|
stuartmorgan
2012/02/29 13:26:15
Same.
| |
47 // Should only be called if has_unsent_logs is true. | |
Ilya Sherman
2012/02/29 01:25:41
nit: has_unsent_logs -> has_unsent_logs()
stuartmorgan
2012/02/29 13:26:15
Done.
| |
48 void StageNextLogForUpload(); | |
34 | 49 |
35 // Returns true if there is a log that needs to be, or is being, uploaded. | 50 // Returns true if there is a log that needs to be, or is being, uploaded. |
36 // Note that this returns true even if compressing the log text failed. | |
37 bool has_staged_log() const; | 51 bool has_staged_log() const; |
38 | 52 |
39 // The compressed text of the staged log. Empty if there is no staged log, | 53 // The compressed text of the staged log. Empty if there is no staged log, |
40 // or if compression of the staged log failed. | 54 // or if compression of the staged log failed. |
41 const std::string& staged_log_text() { return compressed_staged_log_text_; } | 55 const std::string& staged_log_text() { return compressed_staged_log_text_; } |
42 | 56 |
43 // Discards the staged log. | 57 // Discards the staged log. |
44 void DiscardStagedLog(); | 58 void DiscardStagedLog(); |
45 | 59 |
46 // Closes and discards |current_log|. | 60 // Closes and discards |current_log|. |
47 void DiscardCurrentLog(); | 61 void DiscardCurrentLog(); |
48 | 62 |
49 // Sets current_log to NULL, but saves the current log for future use with | 63 // Sets current_log to NULL, but saves the current log for future use with |
50 // ResumePausedLog(). Only one log may be paused at a time. | 64 // ResumePausedLog(). Only one log may be paused at a time. |
51 // TODO(stuartmorgan): Pause/resume support is really a workaround for a | 65 // TODO(stuartmorgan): Pause/resume support is really a workaround for a |
52 // design issue in initial log writing; that should be fixed, and pause/resume | 66 // design issue in initial log writing; that should be fixed, and pause/resume |
53 // removed. | 67 // removed. |
54 void PauseCurrentLog(); | 68 void PauseCurrentLog(); |
55 | 69 |
56 // Restores the previously paused log (if any) to current_log. | 70 // Restores the previously paused log (if any) to current_log. |
57 // This should only be called if there is not a current log. | 71 // This should only be called if there is not a current log. |
58 void ResumePausedLog(); | 72 void ResumePausedLog(); |
59 | 73 |
60 // Returns true if there are any logs left over from previous sessions that | 74 // Saves the staged log (or discards it in accordance with |
61 // need to be uploaded. | 75 // set_max_ongoing_log_store_size), then clears the staged log. |
Ilya Sherman
2012/02/29 01:25:41
nit: set_max_ongoing_log_store_size -> |max_ongoin
stuartmorgan
2012/02/29 13:26:15
That part of the comment was actually no longer ac
| |
62 bool has_unsent_logs() const { | 76 // This can only be called if has_staged_log() is true. |
63 return !unsent_initial_logs_.empty() || !unsent_ongoing_logs_.empty(); | 77 void StoreStagedLogAsUnsent(); |
64 } | |
65 | 78 |
66 enum LogType { | 79 // Sets the threshold for how large an onging log can be and still be written |
67 INITIAL_LOG, // The first log of a session. | 80 // to persistant storage. Ongoing logs larger than this will be discarded |
68 ONGOING_LOG, // Subsequent logs in a session. | 81 // before persisting. 0 is interpreted as no limit. |
69 }; | |
70 | |
71 // Saves the staged log as the given type (or discards it in accordance with | |
72 // set_max_ongoing_log_store_size), then clears the staged log. | |
73 // This can only be called after StageCurrentLogForUpload. | |
74 void StoreStagedLogAsUnsent(LogType log_type); | |
75 | |
76 // Populates staged_log_text with the next stored log to send. | |
77 void StageNextStoredLogForUpload(); | |
78 | |
79 // Sets the threshold for how large an onging log can be and still be stored. | |
80 // Ongoing logs larger than this will be discarded. 0 is interpreted as no | |
81 // limit. | |
82 void set_max_ongoing_log_store_size(size_t max_size) { | 82 void set_max_ongoing_log_store_size(size_t max_size) { |
83 max_ongoing_log_store_size_ = max_size; | 83 max_ongoing_log_store_size_ = max_size; |
84 } | 84 } |
85 | 85 |
86 // Interface for a utility class to serialize and deserialize logs for | 86 // Interface for a utility class to serialize and deserialize logs for |
87 // persistent storage. | 87 // persistent storage. |
88 class LogSerializer { | 88 class LogSerializer { |
89 public: | 89 public: |
90 virtual ~LogSerializer() {} | 90 virtual ~LogSerializer() {} |
91 | 91 |
(...skipping 16 matching lines...) Expand all Loading... | |
108 | 108 |
109 // Saves any unsent logs to persistent storage using the current log | 109 // Saves any unsent logs to persistent storage using the current log |
110 // serializer. Can only be called after set_log_serializer. | 110 // serializer. Can only be called after set_log_serializer. |
111 void PersistUnsentLogs(); | 111 void PersistUnsentLogs(); |
112 | 112 |
113 // Loads any unsent logs from persistent storage using the current log | 113 // Loads any unsent logs from persistent storage using the current log |
114 // serializer. Can only be called after set_log_serializer. | 114 // serializer. Can only be called after set_log_serializer. |
115 void LoadPersistedUnsentLogs(); | 115 void LoadPersistedUnsentLogs(); |
116 | 116 |
117 private: | 117 private: |
118 // Compresses staged_log_ and stores the result in | 118 // Saves |log_text| as the given type (or discards it in accordance with |
119 // compressed_staged_log_text_. | 119 // set_max_ongoing_log_store_size). |
120 void CompressStagedLog(); | 120 void StoreLog(const std::string& log_text, LogType log_type); |
121 | |
122 // Compresses current_log_ into compressed_log. | |
123 void CompressCurrentLog(std::string* compressed_log); | |
121 | 124 |
122 // Compresses the text in |input| using bzip2, store the result in |output|. | 125 // Compresses the text in |input| using bzip2, store the result in |output|. |
123 static bool Bzip2Compress(const std::string& input, std::string* output); | 126 static bool Bzip2Compress(const std::string& input, std::string* output); |
124 | 127 |
125 // The log that we are still appending to. | 128 // The log that we are still appending to. |
126 scoped_ptr<MetricsLogBase> current_log_; | 129 scoped_ptr<MetricsLogBase> current_log_; |
130 LogType current_log_type_; | |
127 | 131 |
128 // A paused, previously-current log. | 132 // A paused, previously-current log. |
129 scoped_ptr<MetricsLogBase> paused_log_; | 133 scoped_ptr<MetricsLogBase> paused_log_; |
130 | 134 |
131 // The log that we are currently transmiting, or about to try to transmit. | |
132 // Note that when using StageNextStoredLogForUpload, this can be NULL while | |
133 // compressed_staged_log_text_ is non-NULL. | |
134 scoped_ptr<MetricsLogBase> staged_log_; | |
135 | |
136 // Helper class to handle serialization/deserialization of logs for persistent | 135 // Helper class to handle serialization/deserialization of logs for persistent |
137 // storage. May be NULL. | 136 // storage. May be NULL. |
138 scoped_ptr<LogSerializer> log_serializer_; | 137 scoped_ptr<LogSerializer> log_serializer_; |
139 | 138 |
140 // The compressed text of the staged log, ready for upload to the server. | 139 // The compressed text of the staged log, ready for upload to the server. |
141 std::string compressed_staged_log_text_; | 140 std::string compressed_staged_log_text_; |
141 LogType staged_log_type_; | |
142 | 142 |
143 // Logs from a previous session that have not yet been sent. | 143 // Logs from a previous session that have not yet been sent. |
144 // Note that the vector has the oldest logs listed first (early in the | 144 // Note that the vector has the oldest logs listed first (early in the |
145 // vector), and we'll discard old logs if we have gathered too many logs. | 145 // vector), and we'll discard old logs if we have gathered too many logs. |
146 std::vector<std::string> unsent_initial_logs_; | 146 std::vector<std::string> unsent_initial_logs_; |
147 std::vector<std::string> unsent_ongoing_logs_; | 147 std::vector<std::string> unsent_ongoing_logs_; |
148 | 148 |
149 size_t max_ongoing_log_store_size_; | 149 size_t max_ongoing_log_store_size_; |
150 | 150 |
151 DISALLOW_COPY_AND_ASSIGN(MetricsLogManager); | 151 DISALLOW_COPY_AND_ASSIGN(MetricsLogManager); |
152 }; | 152 }; |
153 | 153 |
154 #endif // CHROME_COMMON_METRICS_METRICS_LOG_MANAGER_H_ | 154 #endif // CHROME_COMMON_METRICS_METRICS_LOG_MANAGER_H_ |
OLD | NEW |