1 /*
2 * Copyright (C) 2016 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 */
16
17 #include "DumpWriter.h"
18
19 #include <android-base/stringprintf.h>
20 #include <utils/String8.h>
21
22 using android::base::StringAppendV;
23 using android::String16;
24 using android::Vector;
25
26 namespace {
27
28 const char kIndentString[] = " ";
29 const size_t kIndentStringLen = strlen(kIndentString);
30
31 } // namespace
32
33
DumpWriter(int fd)34 DumpWriter::DumpWriter(int fd) : mIndentLevel(0), mFd(fd) {}
35
incIndent()36 void DumpWriter::incIndent() {
37 if (mIndentLevel < 255) {
38 mIndentLevel++;
39 }
40 }
41
decIndent()42 void DumpWriter::decIndent() {
43 if (mIndentLevel > 0) {
44 mIndentLevel--;
45 }
46 }
47
println(const std::string & line)48 void DumpWriter::println(const std::string& line) {
49 if (!line.empty()) {
50 for (int i = 0; i < mIndentLevel; i++) {
51 write(mFd, kIndentString, kIndentStringLen);
52 }
53 write(mFd, line.c_str(), line.size());
54 }
55 write(mFd, "\n", 1);
56 }
57
println(const char * fmt,...)58 void DumpWriter::println(const char* fmt, ...) {
59 std::string line;
60 va_list ap;
61 va_start(ap, fmt);
62 StringAppendV(&line, fmt, ap);
63 va_end(ap);
64 println(line);
65 }
66