Chromium Code Reviews| Index: components/metrics/export/metrics_utils.cc |
| diff --git a/components/metrics/export/metrics_utils.cc b/components/metrics/export/metrics_utils.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..edf0e9df2ca7b3212d910ad4e1e8bc4253d4940e |
| --- /dev/null |
| +++ b/components/metrics/export/metrics_utils.cc |
| @@ -0,0 +1,121 @@ |
| +// Copyright 2014 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 "components/metrics/export/metrics_utils.h" |
| + |
| +#include <string> |
| +#include <vector> |
| + |
| +#include "base/file_util.h" |
| +#include "base/files/file_path.h" |
| +#include "base/logging.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/memory/scoped_vector.h" |
| +#include "base/strings/string_split.h" |
| +#include "base/strings/string_util.h" |
| +#include "components/metrics/export/crash_sample.h" |
| +#include "components/metrics/export/histogram_sample.h" |
| +#include "components/metrics/export/linearhistogram_sample.h" |
| +#include "components/metrics/export/metric_sample.h" |
| +#include "components/metrics/export/sparsehistogram_sample.h" |
| +#include "components/metrics/export/useraction_sample.h" |
| + |
| +namespace metrics { |
| + |
| +scoped_ptr<MetricSample> MetricsUtils::ReadSample(const std::string& sample) { |
| + std::vector<std::string> parts; |
| + base::SplitString(sample, '\0', &parts); |
| + // We should have a two null terminated strings so split should produce |
| + // three chuncks. |
| + if (parts.size() != 3) { |
| + DLOG(ERROR) << "wrong length" << parts.size(); |
| + return scoped_ptr<MetricSample>(); |
| + } |
| + const std::string& name = parts[0]; |
| + const std::string& value = parts[1]; |
| + |
| + if (LowerCaseEqualsASCII(name, "crash")) { |
| + return CrashSample::ReadCrash(value).PassAs<MetricSample>(); |
| + } else if (LowerCaseEqualsASCII(name, "histogram")) { |
| + return HistogramSample::ReadHistogram(value).PassAs<MetricSample>(); |
| + } else if (LowerCaseEqualsASCII(name, "linearhistogram")) { |
| + return LinearHistogramSample::ReadLinearHistogram(value) |
| + .PassAs<MetricSample>(); |
| + } else if (LowerCaseEqualsASCII(name, "sparsehistogram")) { |
| + return SparseHistogramSample::ReadSparseHistogram(value) |
| + .PassAs<MetricSample>(); |
| + } else if (LowerCaseEqualsASCII(name, "useraction")) { |
| + return UserActionSample::ReadUserAction(value).PassAs<MetricSample>(); |
| + } else { |
| + DLOG(ERROR) << "invalid event type: " << name << ", value: " << value; |
| + } |
| + return scoped_ptr<MetricSample>(); |
| +} |
| + |
| +bool MetricsUtils::WriteMetricToFile(const MetricSample& sample, |
| + const std::string& filename) { |
| + if (!sample.IsValid()) |
| + return false; |
| + base::File file(base::FilePath(filename), |
| + base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_APPEND); |
| + base::File::Error err = file.Lock(); |
| + if (err != base::File::FILE_OK) { |
| + DLOG(ERROR) << "could not lock the file"; |
| + return false; |
| + } |
| + |
| + const std::string msg = sample.ToString(); |
| + int32_t size = msg.length() + sizeof(int32_t); |
| + if (msg.length() > size_t(kMessageMaxLength)) { |
| + DLOG(ERROR) << "cannot write message: too long"; |
| + return false; |
| + } |
| + file.WriteAtCurrentPos(reinterpret_cast<char*>(&size), sizeof(int32_t)); |
| + file.WriteAtCurrentPos(msg.c_str(), msg.length()); |
| + |
| + return true; |
| +} |
| + |
| +void MetricsUtils::ReadAndTruncateMetricsFromFile( |
| + const std::string& filename, |
| + ScopedVector<MetricSample>* metrics) { |
| + base::File file = base::File( |
|
Alexei Svitkine (slow)
2014/05/01 15:57:09
Nit: Just do base::File file(...);
bsimonnet
2014/05/01 20:34:21
Done.
|
| + base::FilePath(filename), |
| + base::File::FLAG_OPEN | base::File::FLAG_READ | base::File::FLAG_WRITE); |
| + base::File::Error err = file.Lock(); |
| + if (err != base::File::FILE_OK) { |
| + DLOG(ERROR) << "could not lock the file"; |
| + return; |
| + } |
| + for (;;) { |
| + int32 message_length; |
| + int read = file.ReadAtCurrentPos(reinterpret_cast<char*>(&message_length), |
| + sizeof(message_length)); |
| + if (read != sizeof(message_length)) break; |
|
Alexei Svitkine (slow)
2014/05/01 15:57:09
Nit: Put on next line.
bsimonnet
2014/05/01 20:34:21
Done.
|
| + |
| + message_length -= sizeof(message_length); |
| + if (message_length > kMessageMaxLength) { |
| + DLOG(ERROR) << "cannot read message: too long"; |
| + |
| + // Skip the invalid message and continue reading. |
| + if (file.Seek(base::File::FROM_CURRENT, message_length) != -1) continue; |
| + |
| + DLOG(ERROR) << "cannot skip long message"; |
| + // We do not know the size of the message. The rest of the file is not |
| + // readable. Truncate the file and exit. |
| + break; |
| + } |
| + char serialized_sample[kMessageMaxLength]; |
| + read = file.ReadAtCurrentPos(serialized_sample, message_length); |
| + if (read != message_length) { |
| + DLOG(ERROR) << "could not read message" << read; |
| + break; |
| + } |
| + metrics->push_back( |
| + ReadSample(std::string(serialized_sample, message_length)).release()); |
| + } |
| + file.SetLength(0); |
| +} |
| + |
| +} // namespace metrics |