blob: 44e5e9e617fc08eee67e77ea7d07354f347d7630 [file] [log] [blame]
Erik Kline2d3a1632016-03-15 16:33:48 +09001/*
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
Erik Kline31ab8872018-05-25 15:35:03 +090019#include <unistd.h>
20#include <limits>
21
Erik Kline2d3a1632016-03-15 16:33:48 +090022#include <android-base/stringprintf.h>
23#include <utils/String8.h>
24
25using android::base::StringAppendV;
Erik Kline2d3a1632016-03-15 16:33:48 +090026
Lorenzo Colitti7035f222017-02-13 18:29:00 +090027namespace android {
28namespace net {
29
Erik Kline2d3a1632016-03-15 16:33:48 +090030namespace {
31
32const char kIndentString[] = " ";
33const size_t kIndentStringLen = strlen(kIndentString);
34
35} // namespace
36
37
38DumpWriter::DumpWriter(int fd) : mIndentLevel(0), mFd(fd) {}
39
40void DumpWriter::incIndent() {
Erik Kline31ab8872018-05-25 15:35:03 +090041 if (mIndentLevel < std::numeric_limits<decltype(mIndentLevel)>::max()) {
Erik Kline2d3a1632016-03-15 16:33:48 +090042 mIndentLevel++;
43 }
44}
45
46void DumpWriter::decIndent() {
Erik Kline31ab8872018-05-25 15:35:03 +090047 if (mIndentLevel > std::numeric_limits<decltype(mIndentLevel)>::min()) {
Erik Kline2d3a1632016-03-15 16:33:48 +090048 mIndentLevel--;
49 }
50}
51
52void DumpWriter::println(const std::string& line) {
53 if (!line.empty()) {
54 for (int i = 0; i < mIndentLevel; i++) {
Erik Kline31ab8872018-05-25 15:35:03 +090055 ::write(mFd, kIndentString, kIndentStringLen);
Erik Kline2d3a1632016-03-15 16:33:48 +090056 }
Erik Kline31ab8872018-05-25 15:35:03 +090057 ::write(mFd, line.c_str(), line.size());
Erik Kline2d3a1632016-03-15 16:33:48 +090058 }
Erik Kline31ab8872018-05-25 15:35:03 +090059 ::write(mFd, "\n", 1);
Erik Kline2d3a1632016-03-15 16:33:48 +090060}
61
62void DumpWriter::println(const char* fmt, ...) {
63 std::string line;
64 va_list ap;
65 va_start(ap, fmt);
66 StringAppendV(&line, fmt, ap);
67 va_end(ap);
68 println(line);
69}
Lorenzo Colitti7035f222017-02-13 18:29:00 +090070
71} // namespace net
72} // namespace android