1 // Copyright 2015 The Chromium OS 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 "data_writer.h"
6 
7 #include <stdint.h>
8 
9 #include "base/logging.h"
10 
11 #include "perf_data_utils.h"
12 
13 namespace quipper {
14 
WriteDataValue(const void * src,const size_t size,const string & value_name)15 bool DataWriter::WriteDataValue(const void* src, const size_t size,
16                                 const string& value_name) {
17   if (WriteData(src, size)) return true;
18   LOG(ERROR) << "Unable to write " << value_name << ". Requested " << size
19              << " bytes, " << size_ - Tell() << " bytes remaining.";
20   return false;
21 }
22 
WriteStringWithSizeToData(const string & src)23 bool DataWriter::WriteStringWithSizeToData(const string& src) {
24   uint32_t len = GetUint64AlignedStringLength(src);
25   if (!CanWriteSize(len + sizeof(len))) {
26     LOG(ERROR) << "Not enough space to write string.";
27     return false;
28   }
29 
30   if (!WriteDataValue(&len, sizeof(len), "string length") ||
31       !WriteString(src, len)) {
32     LOG(ERROR) << "Failed to write string.";
33     return false;
34   }
35   return true;
36 }
37 
38 }  // namespace quipper
39