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..3ca9e41d1ae5214bf456b7c8c3a9ab32ac7b5c83 |
--- /dev/null |
+++ b/components/metrics/export/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/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_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" |
+ |
+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]; |
+ 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 |
+ << ", value: " << value; |
+ } |
+ return NULL; |
+} |
+ |
+bool MetricsUtils::WriteMetricToFile(const MetricSample& sample, |
+ const std::string& filename) { |
+ if (!sample.IsValid()) return false; |
Alexei Svitkine (slow)
2014/04/30 15:25:24
Put return on next line.
bsimonnet
2014/04/30 20:28:40
Done.
|
+ File file = |
+ File(FilePath(filename), File::FLAG_OPEN_ALWAYS | File::FLAG_APPEND); |
Alexei Svitkine (slow)
2014/04/30 15:25:24
Nit: You can just do:
File file(FilePath(filename
bsimonnet
2014/04/30 20:28:40
Done.
|
+ 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 |