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 android {
27 namespace net {
28 
29 namespace {
30 
31 const char kIndentString[] = "  ";
32 const size_t kIndentStringLen = strlen(kIndentString);
33 
34 }  // namespace
35 
36 
DumpWriter(int fd)37 DumpWriter::DumpWriter(int fd) : mIndentLevel(0), mFd(fd) {}
38 
incIndent()39 void DumpWriter::incIndent() {
40     if (mIndentLevel < 255) {
41         mIndentLevel++;
42     }
43 }
44 
decIndent()45 void DumpWriter::decIndent() {
46     if (mIndentLevel > 0) {
47         mIndentLevel--;
48     }
49 }
50 
println(const std::string & line)51 void DumpWriter::println(const std::string& line) {
52     if (!line.empty()) {
53         for (int i = 0; i < mIndentLevel; i++) {
54             write(mFd, kIndentString, kIndentStringLen);
55         }
56         write(mFd, line.c_str(), line.size());
57     }
58     write(mFd, "\n", 1);
59 }
60 
println(const char * fmt,...)61 void DumpWriter::println(const char* fmt, ...) {
62     std::string line;
63     va_list ap;
64     va_start(ap, fmt);
65     StringAppendV(&line, fmt, ap);
66     va_end(ap);
67     println(line);
68 }
69 
70 }  // namespace net
71 }  // namespace android
72