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

Side by Side Diff: components/device_event_log/device_event_log_impl.cc

Issue 1140643002: Fix device_event_log component build, make NonThreadSafe (Take 2) (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 7 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 #include "components/device_event_log/device_event_log_impl.h" 5 #include "components/device_event_log/device_event_log_impl.h"
6 6
7 #include <cmath> 7 #include <cmath>
8 #include <list> 8 #include <list>
9 #include <set> 9 #include <set>
10 10
11 #include "base/bind.h"
11 #include "base/containers/adapters.h" 12 #include "base/containers/adapters.h"
12 #include "base/json/json_string_value_serializer.h" 13 #include "base/json/json_string_value_serializer.h"
13 #include "base/json/json_writer.h" 14 #include "base/json/json_writer.h"
15 #include "base/location.h"
14 #include "base/logging.h" 16 #include "base/logging.h"
15 #include "base/memory/scoped_ptr.h" 17 #include "base/memory/scoped_ptr.h"
16 #include "base/strings/string_tokenizer.h" 18 #include "base/strings/string_tokenizer.h"
17 #include "base/strings/string_util.h" 19 #include "base/strings/string_util.h"
18 #include "base/strings/stringprintf.h" 20 #include "base/strings/stringprintf.h"
19 #include "base/strings/utf_string_conversions.h" 21 #include "base/strings/utf_string_conversions.h"
20 #include "base/values.h" 22 #include "base/values.h"
21 #include "net/base/escape.h" 23 #include "net/base/escape.h"
22 24
23 namespace device_event_log { 25 namespace device_event_log {
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after
240 if (type != LOG_TYPE_UNKNOWN) 242 if (type != LOG_TYPE_UNKNOWN)
241 exclude_types->insert(type); 243 exclude_types->insert(type);
242 } else { 244 } else {
243 LogType type = LogTypeFromString(tok); 245 LogType type = LogTypeFromString(tok);
244 if (type != LOG_TYPE_UNKNOWN) 246 if (type != LOG_TYPE_UNKNOWN)
245 include_types->insert(type); 247 include_types->insert(type);
246 } 248 }
247 } 249 }
248 } 250 }
249 251
252 // Update count and time for identical events to avoid log spam.
253 void IncreaseLogEntryCount(const DeviceEventLogImpl::LogEntry& new_entry,
254 DeviceEventLogImpl::LogEntry* cur_entry) {
255 ++cur_entry->count;
256 cur_entry->log_level = std::min(cur_entry->log_level, new_entry.log_level);
257 cur_entry->time = base::Time::Now();
258 }
259
250 } // namespace 260 } // namespace
251 261
252 // static 262 // static
253 void DeviceEventLogImpl::SendToVLogOrErrorLog(const char* file, 263 void DeviceEventLogImpl::SendToVLogOrErrorLog(const char* file,
254 int file_line, 264 int file_line,
255 LogType log_type, 265 LogType log_type,
256 LogLevel log_level, 266 LogLevel log_level,
257 const std::string& event) { 267 const std::string& event) {
258 LogEntry entry(file, file_line, log_type, log_level, event); 268 LogEntry entry(file, file_line, log_type, log_level, event);
259 SendLogEntryToVLogOrErrorLog(entry); 269 SendLogEntryToVLogOrErrorLog(entry);
260 } 270 }
261 271
262 DeviceEventLogImpl::DeviceEventLogImpl(size_t max_entries) 272 DeviceEventLogImpl::DeviceEventLogImpl(
263 : max_entries_(max_entries) { 273 scoped_refptr<base::SingleThreadTaskRunner> task_runner,
274 size_t max_entries)
275 : task_runner_(task_runner),
276 max_entries_(max_entries),
277 weak_ptr_factory_(this) {
278 DCHECK(task_runner_);
264 } 279 }
265 280
266 DeviceEventLogImpl::~DeviceEventLogImpl() { 281 DeviceEventLogImpl::~DeviceEventLogImpl() {
267 } 282 }
268 283
269 void DeviceEventLogImpl::AddEntry(const char* file, 284 void DeviceEventLogImpl::AddEntry(const char* file,
270 int file_line, 285 int file_line,
271 LogType log_type, 286 LogType log_type,
272 LogLevel log_level, 287 LogLevel log_level,
273 const std::string& event) { 288 const std::string& event) {
274 LogEntry entry(file, file_line, log_type, log_level, event); 289 LogEntry entry(file, file_line, log_type, log_level, event);
290 if (!task_runner_->RunsTasksOnCurrentThread()) {
291 task_runner_->PostTask(FROM_HERE,
292 base::Bind(&DeviceEventLogImpl::AddLogEntry,
293 weak_ptr_factory_.GetWeakPtr(), entry));
294 return;
295 }
275 AddLogEntry(entry); 296 AddLogEntry(entry);
276 } 297 }
277 298
278 void DeviceEventLogImpl::AddLogEntry(const LogEntry& entry) { 299 void DeviceEventLogImpl::AddLogEntry(const LogEntry& entry) {
300 DCHECK(task_runner_->RunsTasksOnCurrentThread());
279 if (!entries_.empty()) { 301 if (!entries_.empty()) {
280 LogEntry& last = entries_.back(); 302 LogEntry& last = entries_.back();
281 if (LogEntryMatches(last, entry)) { 303 if (LogEntryMatches(last, entry)) {
282 // Update count and time for identical events to avoid log spam. 304 IncreaseLogEntryCount(entry, &last);
283 ++last.count;
284 last.log_level = std::min(last.log_level, entry.log_level);
285 last.time = base::Time::Now();
286 return; 305 return;
287 } 306 }
288 } 307 }
289 if (entries_.size() >= max_entries_) { 308 if (entries_.size() >= max_entries_)
290 const size_t max_error_entries = max_entries_ / 2; 309 RemoveEntry();
291 // Remove the first (oldest) non-error entry, or the oldest entry if more
292 // than half the entries are errors.
293 size_t error_count = 0;
294 for (LogEntryList::iterator iter = entries_.begin(); iter != entries_.end();
295 ++iter) {
296 if (iter->log_level != LOG_LEVEL_ERROR) {
297 entries_.erase(iter);
298 break;
299 }
300 if (++error_count > max_error_entries) {
301 // Too many error entries, remove the oldest entry.
302 entries_.pop_front();
303 break;
304 }
305 }
306 }
307 entries_.push_back(entry); 310 entries_.push_back(entry);
308 SendLogEntryToVLogOrErrorLog(entry); 311 SendLogEntryToVLogOrErrorLog(entry);
309 } 312 }
310 313
314 void DeviceEventLogImpl::RemoveEntry() {
315 const size_t max_error_entries = max_entries_ / 2;
316 DCHECK(max_error_entries < entries_.size());
317 // Remove the first (oldest) non-error entry, or the oldest entry if more
318 // than half the entries are errors.
319 size_t error_count = 0;
320 for (LogEntryList::iterator iter = entries_.begin(); iter != entries_.end();
321 ++iter) {
322 if (iter->log_level != LOG_LEVEL_ERROR) {
323 entries_.erase(iter);
324 return;
325 }
326 if (++error_count > max_error_entries)
327 break;
328 }
329 // Too many error entries, remove the oldest entry.
330 entries_.pop_front();
331 }
332
311 std::string DeviceEventLogImpl::GetAsString(StringOrder order, 333 std::string DeviceEventLogImpl::GetAsString(StringOrder order,
312 const std::string& format, 334 const std::string& format,
313 const std::string& types, 335 const std::string& types,
314 LogLevel max_level, 336 LogLevel max_level,
315 size_t max_events) { 337 size_t max_events) {
338 DCHECK(task_runner_->RunsTasksOnCurrentThread());
316 if (entries_.empty()) 339 if (entries_.empty())
317 return "No Log Entries."; 340 return "No Log Entries.";
318 341
319 bool show_time, show_file, show_type, show_level, format_html, format_json; 342 bool show_time, show_file, show_type, show_level, format_html, format_json;
320 GetFormat(format, &show_time, &show_file, &show_type, &show_level, 343 GetFormat(format, &show_time, &show_file, &show_type, &show_level,
321 &format_html, &format_json); 344 &format_html, &format_json);
322 345
323 std::set<LogType> include_types, exclude_types; 346 std::set<LogType> include_types, exclude_types;
324 GetLogTypes(types, &include_types, &exclude_types); 347 GetLogTypes(types, &include_types, &exclude_types);
325 348
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
401 if (filedesc) { 424 if (filedesc) {
402 file = filedesc; 425 file = filedesc;
403 size_t last_slash_pos = file.find_last_of("\\/"); 426 size_t last_slash_pos = file.find_last_of("\\/");
404 if (last_slash_pos != std::string::npos) { 427 if (last_slash_pos != std::string::npos) {
405 file.erase(0, last_slash_pos + 1); 428 file.erase(0, last_slash_pos + 1);
406 } 429 }
407 } 430 }
408 } 431 }
409 432
410 } // namespace device_event_log 433 } // namespace device_event_log
OLDNEW
« no previous file with comments | « components/device_event_log/device_event_log_impl.h ('k') | components/device_event_log/device_event_log_impl_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698