blob: 655652d508f31d6b99fe00fcb9eae5cc6a04cd11 [file] [log] [blame]
Bertrand SIMONNET52e5b992015-08-10 15:18:00 -07001/*
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 Vakulenko788d3b62014-12-11 09:48:46 -080016
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
25namespace metrics {
26
27class MetricSample;
28
29// Metrics helpers to serialize and deserialize metrics collected by
30// ChromeOS.
31namespace 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.
36scoped_ptr<MetricSample> ParseSample(const std::string& sample);
37
James Hawkins5f646002015-09-08 15:18:17 -070038// Reads all samples from a file. The file contents remain unchanged.
39void ReadMetricsFromFile(const std::string& filename,
40 ScopedVector<MetricSample>* metrics);
41
42// Reads all samples from a file and truncates the file when done.
Alex Vakulenko788d3b62014-12-11 09:48:46 -080043void 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.
56bool WriteMetricToFile(const MetricSample& sample, const std::string& filename);
57
58// Maximum length of a serialized message
59static const int kMessageMaxLength = 1024;
60
61} // namespace SerializationUtils
62} // namespace metrics
63
64#endif // METRICS_SERIALIZATION_SERIALIZATION_UTILS_H_