Bertrand SIMONNET | 52e5b99 | 2015-08-10 15:18:00 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Alex Vakulenko | 788d3b6 | 2014-12-11 09:48:46 -0800 | [diff] [blame] | 16 | |
| 17 | #ifndef METRICS_SERIALIZATION_SERIALIZATION_UTILS_H_ |
| 18 | #define METRICS_SERIALIZATION_SERIALIZATION_UTILS_H_ |
| 19 | |
| 20 | #include <string> |
| 21 | |
| 22 | #include "base/memory/scoped_ptr.h" |
| 23 | #include "base/memory/scoped_vector.h" |
| 24 | |
| 25 | namespace metrics { |
| 26 | |
| 27 | class MetricSample; |
| 28 | |
| 29 | // Metrics helpers to serialize and deserialize metrics collected by |
| 30 | // ChromeOS. |
| 31 | namespace SerializationUtils { |
| 32 | |
| 33 | // Deserializes a sample passed as a string and return a sample. |
| 34 | // The return value will either be a scoped_ptr to a Metric sample (if the |
| 35 | // deserialization was successful) or a NULL scoped_ptr. |
| 36 | scoped_ptr<MetricSample> ParseSample(const std::string& sample); |
| 37 | |
James Hawkins | 5f64600 | 2015-09-08 15:18:17 -0700 | [diff] [blame] | 38 | // Reads all samples from a file. The file contents remain unchanged. |
| 39 | void ReadMetricsFromFile(const std::string& filename, |
| 40 | ScopedVector<MetricSample>* metrics); |
| 41 | |
| 42 | // Reads all samples from a file and truncates the file when done. |
Alex Vakulenko | 788d3b6 | 2014-12-11 09:48:46 -0800 | [diff] [blame] | 43 | void ReadAndTruncateMetricsFromFile(const std::string& filename, |
| 44 | ScopedVector<MetricSample>* metrics); |
| 45 | |
| 46 | // Serializes a sample and write it to filename. |
| 47 | // The format for the message is: |
| 48 | // message_size, serialized_message |
| 49 | // where |
| 50 | // * message_size is the total length of the message (message_size + |
| 51 | // serialized_message) on 4 bytes |
| 52 | // * serialized_message is the serialized version of sample (using ToString) |
| 53 | // |
| 54 | // NB: the file will never leave the device so message_size will be written |
| 55 | // with the architecture's endianness. |
| 56 | bool WriteMetricToFile(const MetricSample& sample, const std::string& filename); |
| 57 | |
| 58 | // Maximum length of a serialized message |
| 59 | static const int kMessageMaxLength = 1024; |
| 60 | |
| 61 | } // namespace SerializationUtils |
| 62 | } // namespace metrics |
| 63 | |
| 64 | #endif // METRICS_SERIALIZATION_SERIALIZATION_UTILS_H_ |