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

Side by Side Diff: components/metrics/export/sparsehistogram_sample.cc

Issue 227873002: Create a histogram serialization mechanism in components/metrics (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Convert to use scoped_ptr. Remove using declarations. Fixed linting. Created 6 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "components/metrics/export/sparsehistogram_sample.h"
6
7 #include <string>
8 #include <vector>
9
10 #include "base/memory/scoped_ptr.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "base/strings/string_split.h"
13 #include "base/strings/stringprintf.h"
14 #include "components/metrics/export/metric_sample.h"
15
16 namespace metrics {
17
18 SparseHistogramSample::SparseHistogramSample(const std::string& histogram_name,
19 int sample)
20 : MetricSample(MetricSample::SPARSE_HISTOGRAM, histogram_name),
21 sample_(sample) {}
22
23 SparseHistogramSample::~SparseHistogramSample() {}
24
25 std::string SparseHistogramSample::ToString() const {
26 return base::StringPrintf(
27 "sparsehistogram%c%s %d%c", '\0', name().c_str(), sample_, '\0');
28 }
29
30 // static
31 // Read a sparse histogram from a string listing |name| and |sample| separated
32 // by a space.
33 scoped_ptr<SparseHistogramSample> SparseHistogramSample::ReadSparseHistogram(
34 const std::string& histogram_serialized) {
Alexei Svitkine (slow) 2014/05/01 15:57:09 Nit: serialized_histogram which also makes it cons
35 std::vector<std::string> parts;
36 base::SplitString(histogram_serialized, ' ', &parts);
37 if (parts.size() != 2)
38 return scoped_ptr<SparseHistogramSample>();
39 int sample;
40 if (parts[0].length() == 0 || !base::StringToInt(parts[1], &sample))
41 return scoped_ptr<SparseHistogramSample>();
42
43 return scoped_ptr<SparseHistogramSample>(
44 new SparseHistogramSample(parts[0], sample));
45 }
46
47 } // namespace metrics
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698