Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "components/metrics/chromeos/metrics_utils.h" | |
| 6 | |
| 7 #include <string> | |
| 8 #include <vector> | |
| 9 | |
| 10 #include "base/file_util.h" | |
| 11 #include "base/files/file_path.h" | |
| 12 #include "base/logging.h" | |
| 13 #include "base/memory/scoped_ptr.h" | |
| 14 #include "base/memory/scoped_vector.h" | |
| 15 #include "base/strings/string_split.h" | |
| 16 #include "base/strings/string_util.h" | |
| 17 #include "components/metrics/chromeos/crash_sample.h" | |
| 18 #include "components/metrics/chromeos/histogram_sample.h" | |
| 19 #include "components/metrics/chromeos/linearhistogram_sample.h" | |
| 20 #include "components/metrics/chromeos/metric_sample.h" | |
| 21 #include "components/metrics/chromeos/sparsehistogram_sample.h" | |
| 22 #include "components/metrics/chromeos/useraction_sample.h" | |
| 23 | |
| 24 namespace metrics { | |
| 25 | |
| 26 scoped_ptr<MetricSample> MetricsUtils::ReadSample(const std::string& sample) { | |
| 27 std::vector<std::string> parts; | |
| 28 base::SplitString(sample, '\0', &parts); | |
| 29 // 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 ..."
| |
| 30 // three chuncks. | |
|
Luigi Semenzato
2014/05/08 17:19:37
Chunks.
| |
| 31 if (parts.size() != 3) { | |
| 32 DLOG(ERROR) << "wrong length" << parts.size(); | |
|
Luigi Semenzato
2014/05/08 17:19:37
I like more descriptive messages, for instance:
"
| |
| 33 return scoped_ptr<MetricSample>(); | |
| 34 } | |
| 35 const std::string& name = parts[0]; | |
| 36 const std::string& value = parts[1]; | |
| 37 | |
| 38 if (LowerCaseEqualsASCII(name, "crash")) { | |
| 39 return CrashSample::ReadCrash(value).PassAs<MetricSample>(); | |
| 40 } else if (LowerCaseEqualsASCII(name, "histogram")) { | |
| 41 return HistogramSample::ReadHistogram(value).PassAs<MetricSample>(); | |
| 42 } else if (LowerCaseEqualsASCII(name, "linearhistogram")) { | |
| 43 return LinearHistogramSample::ReadLinearHistogram(value) | |
| 44 .PassAs<MetricSample>(); | |
| 45 } else if (LowerCaseEqualsASCII(name, "sparsehistogram")) { | |
| 46 return SparseHistogramSample::ReadSparseHistogram(value) | |
| 47 .PassAs<MetricSample>(); | |
| 48 } else if (LowerCaseEqualsASCII(name, "useraction")) { | |
| 49 return UserActionSample::ReadUserAction(value).PassAs<MetricSample>(); | |
| 50 } else { | |
| 51 DLOG(ERROR) << "invalid event type: " << name << ", value: " << value; | |
| 52 } | |
| 53 return scoped_ptr<MetricSample>(); | |
| 54 } | |
| 55 | |
| 56 bool MetricsUtils::WriteMetricToFile(const MetricSample& sample, | |
| 57 const std::string& filename) { | |
| 58 if (!sample.IsValid()) | |
| 59 return false; | |
| 60 base::File file(base::FilePath(filename), | |
| 61 base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_APPEND); | |
| 62 base::File::Error err = file.Lock(); | |
| 63 if (err != base::File::FILE_OK) { | |
| 64 DLOG(ERROR) << "could not lock the file"; | |
| 65 return false; | |
| 66 } | |
| 67 | |
| 68 const std::string msg = sample.ToString(); | |
| 69 int32_t size = msg.length() + sizeof(int32_t); | |
| 70 if (msg.length() > size_t(kMessageMaxLength)) { | |
| 71 DLOG(ERROR) << "cannot write message: too long"; | |
| 72 return false; | |
| 73 } | |
| 74 file.WriteAtCurrentPos(reinterpret_cast<char*>(&size), sizeof(int32_t)); | |
| 75 file.WriteAtCurrentPos(msg.c_str(), msg.length()); | |
| 76 | |
| 77 return true; | |
| 78 } | |
| 79 | |
| 80 void MetricsUtils::ReadAndTruncateMetricsFromFile( | |
| 81 const std::string& filename, | |
| 82 ScopedVector<MetricSample>* metrics) { | |
| 83 base::File file( | |
| 84 base::FilePath(filename), | |
| 85 base::File::FLAG_OPEN | base::File::FLAG_READ | base::File::FLAG_WRITE); | |
| 86 base::File::Error err = file.Lock(); | |
| 87 if (err != base::File::FILE_OK) { | |
| 88 DLOG(ERROR) << "could not lock the file"; | |
| 89 return; | |
| 90 } | |
| 91 for (;;) { | |
| 92 int32 message_length; | |
| 93 int read = file.ReadAtCurrentPos(reinterpret_cast<char*>(&message_length), | |
| 94 sizeof(message_length)); | |
| 95 if (read != sizeof(message_length)) | |
| 96 break; | |
| 97 | |
| 98 message_length -= sizeof(message_length); | |
| 99 if (message_length > kMessageMaxLength) { | |
| 100 DLOG(ERROR) << "cannot read message: too long"; | |
| 101 | |
| 102 // Skip the invalid message and continue reading. | |
| 103 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
| |
| 104 | |
| 105 DLOG(ERROR) << "cannot skip long message"; | |
| 106 // We do not know the size of the message. The rest of the file is not | |
| 107 // readable. Truncate the file and exit. | |
| 108 break; | |
| 109 } | |
| 110 char serialized_sample[kMessageMaxLength]; | |
| 111 read = file.ReadAtCurrentPos(serialized_sample, message_length); | |
| 112 if (read != message_length) { | |
| 113 DLOG(ERROR) << "could not read message" << read; | |
| 114 break; | |
| 115 } | |
| 116 metrics->push_back( | |
| 117 ReadSample(std::string(serialized_sample, message_length)).release()); | |
| 118 } | |
| 119 file.SetLength(0); | |
| 120 } | |
| 121 | |
| 122 } // namespace metrics | |
| OLD | NEW |