Erik Kline | 2d3a163 | 2016-03-15 16:33:48 +0900 | [diff] [blame] | 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 | |
Luke Huang | b257d61 | 2019-03-14 21:19:13 +0800 | [diff] [blame] | 17 | #include "netdutils/DumpWriter.h" |
Erik Kline | 2d3a163 | 2016-03-15 16:33:48 +0900 | [diff] [blame] | 18 | |
Erik Kline | 31ab887 | 2018-05-25 15:35:03 +0900 | [diff] [blame] | 19 | #include <unistd.h> |
| 20 | #include <limits> |
| 21 | |
Erik Kline | 2d3a163 | 2016-03-15 16:33:48 +0900 | [diff] [blame] | 22 | #include <android-base/stringprintf.h> |
| 23 | #include <utils/String8.h> |
| 24 | |
| 25 | using android::base::StringAppendV; |
Erik Kline | 2d3a163 | 2016-03-15 16:33:48 +0900 | [diff] [blame] | 26 | |
Lorenzo Colitti | 7035f22 | 2017-02-13 18:29:00 +0900 | [diff] [blame] | 27 | namespace android { |
Luke Huang | b257d61 | 2019-03-14 21:19:13 +0800 | [diff] [blame] | 28 | namespace netdutils { |
Lorenzo Colitti | 7035f22 | 2017-02-13 18:29:00 +0900 | [diff] [blame] | 29 | |
Erik Kline | 2d3a163 | 2016-03-15 16:33:48 +0900 | [diff] [blame] | 30 | namespace { |
| 31 | |
| 32 | const char kIndentString[] = " "; |
| 33 | const size_t kIndentStringLen = strlen(kIndentString); |
| 34 | |
| 35 | } // namespace |
| 36 | |
Erik Kline | 2d3a163 | 2016-03-15 16:33:48 +0900 | [diff] [blame] | 37 | DumpWriter::DumpWriter(int fd) : mIndentLevel(0), mFd(fd) {} |
| 38 | |
| 39 | void DumpWriter::incIndent() { |
Erik Kline | 31ab887 | 2018-05-25 15:35:03 +0900 | [diff] [blame] | 40 | if (mIndentLevel < std::numeric_limits<decltype(mIndentLevel)>::max()) { |
Erik Kline | 2d3a163 | 2016-03-15 16:33:48 +0900 | [diff] [blame] | 41 | mIndentLevel++; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | void DumpWriter::decIndent() { |
Erik Kline | 31ab887 | 2018-05-25 15:35:03 +0900 | [diff] [blame] | 46 | if (mIndentLevel > std::numeric_limits<decltype(mIndentLevel)>::min()) { |
Erik Kline | 2d3a163 | 2016-03-15 16:33:48 +0900 | [diff] [blame] | 47 | mIndentLevel--; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | void DumpWriter::println(const std::string& line) { |
| 52 | if (!line.empty()) { |
| 53 | for (int i = 0; i < mIndentLevel; i++) { |
Erik Kline | 31ab887 | 2018-05-25 15:35:03 +0900 | [diff] [blame] | 54 | ::write(mFd, kIndentString, kIndentStringLen); |
Erik Kline | 2d3a163 | 2016-03-15 16:33:48 +0900 | [diff] [blame] | 55 | } |
Erik Kline | 31ab887 | 2018-05-25 15:35:03 +0900 | [diff] [blame] | 56 | ::write(mFd, line.c_str(), line.size()); |
Erik Kline | 2d3a163 | 2016-03-15 16:33:48 +0900 | [diff] [blame] | 57 | } |
Erik Kline | 31ab887 | 2018-05-25 15:35:03 +0900 | [diff] [blame] | 58 | ::write(mFd, "\n", 1); |
Erik Kline | 2d3a163 | 2016-03-15 16:33:48 +0900 | [diff] [blame] | 59 | } |
| 60 | |
Bernie Innocenti | 15bb55c | 2018-06-03 16:19:51 +0900 | [diff] [blame] | 61 | // NOLINTNEXTLINE(cert-dcl50-cpp): Grandfathered C-style variadic function. |
Erik Kline | 2d3a163 | 2016-03-15 16:33:48 +0900 | [diff] [blame] | 62 | void 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 Colitti | 7035f22 | 2017-02-13 18:29:00 +0900 | [diff] [blame] | 70 | |
Luke Huang | b257d61 | 2019-03-14 21:19:13 +0800 | [diff] [blame] | 71 | } // namespace netdutils |
Lorenzo Colitti | 7035f22 | 2017-02-13 18:29:00 +0900 | [diff] [blame] | 72 | } // namespace android |