Index: components/metrics/chromeos/metrics_utils.cc |
diff --git a/components/metrics/chromeos/metrics_utils.cc b/components/metrics/chromeos/metrics_utils.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..0715aaf5c96c7ac35246b88317439dc45b43e818 |
--- /dev/null |
+++ b/components/metrics/chromeos/metrics_utils.cc |
@@ -0,0 +1,122 @@ |
+// 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/chromeos/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/chromeos/crash_sample.h" |
+#include "components/metrics/chromeos/histogram_sample.h" |
+#include "components/metrics/chromeos/linearhistogram_sample.h" |
+#include "components/metrics/chromeos/metric_sample.h" |
+#include "components/metrics/chromeos/sparsehistogram_sample.h" |
+#include "components/metrics/chromeos/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 |
Luigi Semenzato
2014/05/08 17:19:37
Typo: "... have a two null ..."
|
+ // three chuncks. |
Luigi Semenzato
2014/05/08 17:19:37
Chunks.
|
+ if (parts.size() != 3) { |
+ DLOG(ERROR) << "wrong length" << parts.size(); |
Luigi Semenzato
2014/05/08 17:19:37
I like more descriptive messages, for instance:
"
|
+ 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::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; |
+ |
+ 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; |
Luigi Semenzato
2014/05/08 17:19:37
I am not sure the style code accepts a statement o
bsimonnet
2014/05/08 20:02:58
It is authorized to enhance readability but I feel
|
+ |
+ 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 |