blob: 092ddbae2dc6fa262424d3a62d9d7577643f8ae8 [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
Luke Huangb257d612019-03-14 21:19:13 +080017#include "netdutils/DumpWriter.h"
Erik Kline2d3a1632016-03-15 16:33:48 +090018
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 {
Luke Huangb257d612019-03-14 21:19:13 +080028namespace netdutils {
Lorenzo Colitti7035f222017-02-13 18:29:00 +090029
Erik Kline2d3a1632016-03-15 16:33:48 +090030namespace {
31
32const char kIndentString[] = " ";
33const size_t kIndentStringLen = strlen(kIndentString);
34
35} // namespace
36
Erik Kline2d3a1632016-03-15 16:33:48 +090037DumpWriter::DumpWriter(int fd) : mIndentLevel(0), mFd(fd) {}
38
39void DumpWriter::incIndent() {
Erik Kline31ab8872018-05-25 15:35:03 +090040 if (mIndentLevel < std::numeric_limits<decltype(mIndentLevel)>::max()) {
Erik Kline2d3a1632016-03-15 16:33:48 +090041 mIndentLevel++;
42 }
43}
44
45void DumpWriter::decIndent() {
Erik Kline31ab8872018-05-25 15:35:03 +090046 if (mIndentLevel > std::numeric_limits<decltype(mIndentLevel)>::min()) {
Erik Kline2d3a1632016-03-15 16:33:48 +090047 mIndentLevel--;
48 }
49}
50
51void DumpWriter::println(const std::string& line) {
52 if (!line.empty()) {
53 for (int i = 0; i < mIndentLevel; i++) {
Erik Kline31ab8872018-05-25 15:35:03 +090054 ::write(mFd, kIndentString, kIndentStringLen);
Erik Kline2d3a1632016-03-15 16:33:48 +090055 }
Erik Kline31ab8872018-05-25 15:35:03 +090056 ::write(mFd, line.c_str(), line.size());
Erik Kline2d3a1632016-03-15 16:33:48 +090057 }
Erik Kline31ab8872018-05-25 15:35:03 +090058 ::write(mFd, "\n", 1);
Erik Kline2d3a1632016-03-15 16:33:48 +090059}
60
Bernie Innocenti15bb55c2018-06-03 16:19:51 +090061// NOLINTNEXTLINE(cert-dcl50-cpp): Grandfathered C-style variadic function.
Erik Kline2d3a1632016-03-15 16:33:48 +090062void 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
Luke Huangb257d612019-03-14 21:19:13 +080071} // namespace netdutils
Lorenzo Colitti7035f222017-02-13 18:29:00 +090072} // namespace android