Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(388)

Side by Side Diff: components/metrics/persisted_logs.h

Issue 2358223002: Add log date to the metrics log (Closed)
Patch Set: Adding log date in persisted logs Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 COMPONENTS_METRICS_PERSISTED_LOGS_H_ 5 #ifndef COMPONENTS_METRICS_PERSISTED_LOGS_H_
6 #define COMPONENTS_METRICS_PERSISTED_LOGS_H_ 6 #define COMPONENTS_METRICS_PERSISTED_LOGS_H_
7 7
8 #include <stddef.h> 8 #include <stddef.h>
9 9
10 #include <string> 10 #include <string>
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
65 // Adds a log to the list. 65 // Adds a log to the list.
66 void StoreLog(const std::string& log_data); 66 void StoreLog(const std::string& log_data);
67 67
68 // Stages the most recent log. The staged_log will remain the same even if 68 // Stages the most recent log. The staged_log will remain the same even if
69 // additional logs are added. 69 // additional logs are added.
70 void StageLog(); 70 void StageLog();
71 71
72 // Remove the staged log. 72 // Remove the staged log.
73 void DiscardStagedLog(); 73 void DiscardStagedLog();
74 74
75 void MigrateFromOldFormat(const char* old_pref_name);
76
75 // True if a log has been staged. 77 // True if a log has been staged.
76 bool has_staged_log() const { return staged_log_index_ != -1; } 78 bool has_staged_log() const { return staged_log_index_ != -1; }
77 79
78 // Returns the element in the front of the list. 80 // Returns the element in the front of the list.
79 const std::string& staged_log() const { 81 const std::string& staged_log() const {
80 DCHECK(has_staged_log()); 82 DCHECK(has_staged_log());
81 return list_[staged_log_index_].compressed_log_data; 83 return list_[staged_log_index_].compressed_log_data;
82 } 84 }
83 85
84 // Returns the element in the front of the list. 86 // Returns the element in the front of the list.
85 const std::string& staged_log_hash() const { 87 const std::string& staged_log_hash() const {
86 DCHECK(has_staged_log()); 88 DCHECK(has_staged_log());
87 return list_[staged_log_index_].hash; 89 return list_[staged_log_index_].hash;
88 } 90 }
89 91
92 // Returns the timestamp of the element in the front of the list.
93 int staged_log_timestamp() const {
94 DCHECK(has_staged_log());
95 return list_[staged_log_index_].timestamp;
96 }
97
90 // The number of elements currently stored. 98 // The number of elements currently stored.
91 size_t size() const { return list_.size(); } 99 size_t size() const { return list_.size(); }
92 100
93 // True if there are no stored logs. 101 // True if there are no stored logs.
94 bool empty() const { return list_.empty(); } 102 bool empty() const { return list_.empty(); }
95 103
96 private: 104 private:
97 // Writes the list to the ListValue. 105 // Writes the list to the ListValue.
98 void WriteLogsToPrefList(base::ListValue* list) const; 106 void WriteLogsToPrefList(base::ListValue* list) const;
99 107
100 // Reads the list from the ListValue. 108 // Reads the list from the ListValue.
101 LogReadStatus ReadLogsFromPrefList(const base::ListValue& list); 109 LogReadStatus ReadLogsFromPrefList(const base::ListValue& list);
102 110
111 // Reads the list from the ListValue in the old Log-hash pair format.
112 LogReadStatus ReadLogsFromPrefList_OldFormat(const base::ListValue& list);
113
103 // A weak pointer to the PrefService object to read and write the preference 114 // A weak pointer to the PrefService object to read and write the preference
104 // from. Calling code should ensure this object continues to exist for the 115 // from. Calling code should ensure this object continues to exist for the
105 // lifetime of the PersistedLogs object. 116 // lifetime of the PersistedLogs object.
106 PrefService* local_state_; 117 PrefService* local_state_;
107 118
108 // The name of the preference to serialize logs to/from. 119 // The name of the preference to serialize logs to/from.
109 const char* pref_name_; 120 const char* pref_name_;
110 121
111 // We will keep at least this |min_log_count_| logs or |min_log_bytes_| bytes 122 // We will keep at least this |min_log_count_| logs or |min_log_bytes_| bytes
112 // of logs, whichever is greater, when writing to disk. These apply after 123 // of logs, whichever is greater, when writing to disk. These apply after
113 // skipping logs greater than |max_log_size_|. 124 // skipping logs greater than |max_log_size_|.
114 const size_t min_log_count_; 125 const size_t min_log_count_;
115 const size_t min_log_bytes_; 126 const size_t min_log_bytes_;
116 127
117 // Logs greater than this size will not be written to disk. 128 // Logs greater than this size will not be written to disk.
118 const size_t max_log_size_; 129 const size_t max_log_size_;
119 130
120 struct LogHashPair { 131 struct LogInfo {
121 // Initializes the members based on uncompressed |log_data|. 132 // Initializes the members based on uncompressed |log_data|.
122 void Init(const std::string& log_data); 133 void Init(const std::string& log_data);
123 134
124 // Compressed log data - a serialized protobuf that's been gzipped. 135 // Compressed log data - a serialized protobuf that's been gzipped.
125 std::string compressed_log_data; 136 std::string compressed_log_data;
126 137
127 // The SHA1 hash of log, stored to catch errors from memory corruption. 138 // The SHA1 hash of log, stored to catch errors from memory corruption.
128 std::string hash; 139 std::string hash;
140
141 // The timestamp of when the log was created in seconds. Seconds is enough
142 // as decision for removing old logs wouldn't need more granularity.
143 int timestamp;
Alexei Svitkine (slow) 2016/09/28 18:50:30 Add a ctor so that this value is initialized.
gayane -on leave until 09-2017 2016/09/28 20:50:50 Done.
129 }; 144 };
130 // A list of all of the stored logs, stored with SHA1 hashes to check for 145 // A list of all of the stored logs, stored with SHA1 hashes to check for
131 // corruption while they are stored in memory. 146 // corruption while they are stored in memory.
132 std::vector<LogHashPair> list_; 147 std::vector<LogInfo> list_;
133 148
134 // The index and type of the log staged for upload. If nothing has been 149 // The index and type of the log staged for upload. If nothing has been
135 // staged, the index will be -1. 150 // staged, the index will be -1.
136 int staged_log_index_; 151 int staged_log_index_;
137 152
138 DISALLOW_COPY_AND_ASSIGN(PersistedLogs); 153 DISALLOW_COPY_AND_ASSIGN(PersistedLogs);
139 }; 154 };
140 155
141 } // namespace metrics 156 } // namespace metrics
142 157
143 #endif // COMPONENTS_METRICS_PERSISTED_LOGS_H_ 158 #endif // COMPONENTS_METRICS_PERSISTED_LOGS_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698