1 // Copyright (c) 2009 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. 4 5 #include "net/base/load_log.h" 6 #include "base/logging.h" 7 8 namespace net { 9 10 LoadLog::LoadLog(size_t max_num_entries) 11 : num_entries_truncated_(0), max_num_entries_(max_num_entries) { 12 DCHECK_GT(max_num_entries, 0u); 13 } 14 15 // static 16 const char* LoadLog::EventTypeToString(EventType event) { 17 switch (event) { 18 #define EVENT_TYPE(label) case TYPE_ ## label: return #label; 19 #include "net/base/load_log_event_type_list.h" 20 #undef EVENT_TYPE 21 } 22 return NULL; 23 } 24 25 void LoadLog::Add(const Entry& entry) { 26 // Minor optimization. TODO(eroman): use StackVector instead. 27 if (entries_.empty()) 28 entries_.reserve(10); // It is likely we will have at least 10 entries. 29 30 // Enforce a bound of |max_num_entries_| -- once we reach it, keep overwriting 31 // the final entry in the log. 32 33 if (entries_.size() + 1 <= max_num_entries_ || 34 max_num_entries_ == kUnbounded) { 35 entries_.push_back(entry); 36 } else { 37 num_entries_truncated_ += 1; 38 entries_[max_num_entries_ - 1] = entry; 39 } 40 } 41 42 void LoadLog::Append(const LoadLog* log) { 43 for (size_t i = 0; i < log->entries().size(); ++i) 44 Add(log->entries()[i]); 45 num_entries_truncated_ += log->num_entries_truncated(); 46 } 47 48 } // namespace net 49