OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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 "base/metrics/single_sample_metrics.h" |
| 6 |
| 7 #include "base/memory/ptr_util.h" |
| 8 #include "base/metrics/histogram.h" |
| 9 |
| 10 namespace base { |
| 11 |
| 12 static SingleSampleMetricsFactory* g_factory = nullptr; |
| 13 |
| 14 // static |
| 15 SingleSampleMetricsFactory* SingleSampleMetricsFactory::Get() { |
| 16 if (!g_factory) |
| 17 g_factory = new DefaultSingleSampleMetricsFactory(); |
| 18 |
| 19 return g_factory; |
| 20 } |
| 21 |
| 22 // static |
| 23 void SingleSampleMetricsFactory::SetFactory( |
| 24 SingleSampleMetricsFactory* factory) { |
| 25 DCHECK(!g_factory); |
| 26 g_factory = factory; |
| 27 } |
| 28 |
| 29 // static |
| 30 void SingleSampleMetricsFactory::DeleteFactoryForTesting() { |
| 31 DCHECK(g_factory); |
| 32 delete g_factory; |
| 33 g_factory = nullptr; |
| 34 } |
| 35 |
| 36 std::unique_ptr<SingleSampleMetric> |
| 37 DefaultSingleSampleMetricsFactory::CreateCustomCountsMetric( |
| 38 const std::string& histogram_name, |
| 39 HistogramBase::Sample min, |
| 40 HistogramBase::Sample max, |
| 41 uint32_t bucket_count) { |
| 42 return MakeUnique<DefaultSingleSampleMetric>( |
| 43 histogram_name, min, max, bucket_count, |
| 44 HistogramBase::kUmaTargetedHistogramFlag); |
| 45 } |
| 46 |
| 47 DefaultSingleSampleMetric::DefaultSingleSampleMetric( |
| 48 const std::string& histogram_name, |
| 49 HistogramBase::Sample min, |
| 50 HistogramBase::Sample max, |
| 51 uint32_t bucket_count, |
| 52 int32_t flags) |
| 53 : histogram_(Histogram::FactoryGet(histogram_name, |
| 54 min, |
| 55 max, |
| 56 bucket_count, |
| 57 flags)) { |
| 58 DCHECK(histogram_); |
| 59 } |
| 60 |
| 61 DefaultSingleSampleMetric::~DefaultSingleSampleMetric() { |
| 62 if (sample_ < 0) |
| 63 return; |
| 64 histogram_->Add(sample_); |
| 65 } |
| 66 |
| 67 void DefaultSingleSampleMetric::SetSample(HistogramBase::Sample sample) { |
| 68 DCHECK_GE(sample, 0); |
| 69 sample_ = sample; |
| 70 } |
| 71 |
| 72 } // namespace base |
OLD | NEW |