Chromium Code Reviews| Index: components/metrics/metrics_utils_chromeos.cc |
| diff --git a/components/metrics/metrics_utils_chromeos.cc b/components/metrics/metrics_utils_chromeos.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..50cf62649adf287fffcb1bb52db6b1970b0eb940 |
| --- /dev/null |
| +++ b/components/metrics/metrics_utils_chromeos.cc |
| @@ -0,0 +1,120 @@ |
| +// 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/metrics_utils_chromeos.h" |
| + |
| +#include <string> |
| + |
| +#include "base/file_util.h" |
| +#include "base/files/file_path.h" |
| +#include "base/logging.h" |
| +#include "base/memory/scoped_vector.h" |
| +#include "base/strings/string_split.h" |
| +#include "base/strings/string_util.h" |
| +#include "components/metrics/crash_sample_chromeos.h" |
| +#include "components/metrics/histogram_sample_chromeos.h" |
| +#include "components/metrics/linearhistogram_sample_chromeos.h" |
| +#include "components/metrics/metric_sample_chromeos.h" |
| +#include "components/metrics/sparsehistogram_sample_chromeos.h" |
| +#include "components/metrics/useraction_sample_chromeos.h" |
| + |
| +using base::File; |
| +using base::FilePath; |
| +using base::SplitString; |
| +using std::string; |
| +using std::vector; |
| + |
| +namespace metrics { |
| + |
| +MetricSample* MetricsUtils::ReadSample(const string& sample) { |
| + vector<string> vector; |
| + SplitString(sample, '\0', &vector); |
| + // We should have a two null terminated strings so split should produce |
| + // three chuncks. |
| + if (vector.size() != 3) { |
| + DLOG(ERROR) << "wrong length" << vector.size(); |
| + return NULL; |
| + } |
| + const string name = vector[0]; |
|
Ben Chan
2014/04/18 20:27:08
const string&
|
| + const string value = vector[1]; |
| + |
| + if (LowerCaseEqualsASCII(name, "crash")) { |
| + return CrashSample::ReadCrash(value); |
| + } else if (LowerCaseEqualsASCII(name, "histogram")) { |
| + return HistogramSample::ReadHistogram(value); |
| + } else if (LowerCaseEqualsASCII(name, "linearhistogram")) { |
| + return LinearHistogramSample::ReadLinearHistogram(value); |
| + } else if (LowerCaseEqualsASCII(name, "sparsehistogram")) { |
| + return SparseHistogramSample::ReadSparseHistogram(value); |
| + } else if (LowerCaseEqualsASCII(name, "useraction")) { |
| + return UserActionSample::ReadUserAction(value); |
| + } else { |
| + DLOG(ERROR) << "invalid event type: " << name; |
|
Ben Chan
2014/04/18 20:27:08
would it be useful to print the value as well?
|
| + } |
| + return NULL; |
| +} |
| + |
| +bool MetricsUtils::WriteMetricToFile(const MetricSample& sample, |
| + const std::string& filename) { |
| + if (!sample.IsValid()) return false; |
| + File file = |
| + File(FilePath(filename), File::FLAG_OPEN_ALWAYS | File::FLAG_APPEND); |
| + File::Error err = file.Lock(); |
| + if (err != File::FILE_OK) { |
| + DLOG(ERROR) << "could not lock the file"; |
| + return false; |
| + } |
| + |
| + const 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) { |
| + File file = File(FilePath(filename), |
| + File::FLAG_OPEN | File::FLAG_READ | File::FLAG_WRITE); |
| + File::Error err = file.Lock(); |
| + if (err != 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; |
| + |
| + 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(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(string(serialized_sample, message_length))); |
| + } |
| + file.SetLength(0); |
| +} |
| + |
| +} // namespace metrics |