summaryrefslogtreecommitdiffstats
path: root/net/base/load_log.cc
blob: 451d4e338a7896f9bc82a19fd5c31fd220ca590c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "net/base/load_log.h"
#include "base/logging.h"

namespace net {

LoadLog::LoadLog(size_t max_num_entries)
    : num_entries_truncated_(0), max_num_entries_(max_num_entries) {
  DCHECK_GT(max_num_entries, 0u);
}

// static
const char* LoadLog::EventTypeToString(EventType event) {
  switch (event) {
#define EVENT_TYPE(label) case TYPE_ ## label: return #label;
#include "net/base/load_log_event_type_list.h"
#undef EVENT_TYPE
  }
  return NULL;
}

void LoadLog::Add(const Event& event) {
  // Minor optimization. TODO(eroman): use StackVector instead.
  if (events_.empty())
    events_.reserve(10);  // It is likely we will have at least 10 entries.

  // Enforce a bound of |max_num_entries_| -- once we reach it, keep overwriting
  // the final entry in the log.

  if (events_.size() + 1 <= max_num_entries_ ||
      max_num_entries_ == kUnbounded) {
    events_.push_back(event);
  } else {
    num_entries_truncated_ += 1;
    events_[max_num_entries_ - 1] = event;
  }
}

void LoadLog::Append(const LoadLog* log) {
  for (size_t i = 0; i < log->events().size(); ++i)
    Add(log->events()[i]);
  num_entries_truncated_ += log->num_entries_truncated();
}

}  // namespace net