Chromium Code Reviews| Index: base/metrics/metrics_utils_chromeos.cc |
| diff --git a/base/metrics/metrics_utils_chromeos.cc b/base/metrics/metrics_utils_chromeos.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..34119c95082e125584836b4d463cde7f9ce13ee5 |
| --- /dev/null |
| +++ b/base/metrics/metrics_utils_chromeos.cc |
| @@ -0,0 +1,114 @@ |
| +// Copyright (c) 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 "base/metrics/metrics_utils_chromeos.h" |
| + |
| +#include <fcntl.h> |
|
Alexei Svitkine (slow)
2014/04/17 16:33:21
Are all these system includes needed?
|
| +#include <stdio.h> |
| +#include <stdlib.h> |
| +#include <sys/file.h> |
| +#include <sys/stat.h> |
| +#include <unistd.h> |
| + |
| +#include <list> |
| +#include <memory> |
| +#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/metrics/crash_sample_chromeos.h" |
| +#include "base/metrics/histogram_sample_chromeos.h" |
| +#include "base/metrics/linearhistogram_sample_chromeos.h" |
| +#include "base/metrics/metric_sample_chromeos.h" |
| +#include "base/metrics/sparsehistogram_sample_chromeos.h" |
| +#include "base/metrics/useraction_sample_chromeos.h" |
| +#include "base/strings/string_split.h" |
| +#include "base/strings/string_util.h" |
| + |
| +using std::string; |
| +using std::vector; |
| + |
| +namespace base { |
| + |
| +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]; |
| + 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; |
| + } |
| + 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); |
|
achaulk
2014/04/17 16:45:18
Check that msg.length() <= kMaxMessageLength to av
|
| + 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); |
| + char serialized_sample[kMessageMaxLength]; |
|
achaulk
2014/04/17 16:45:18
Sanity check that message_length <= kMessageMaxLen
|
| + 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 base |