blob: e45a76d3f55d0d7bb7ad6c7a6229c9dc15ae561a [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#ifndef NETD_SERVER_DUMPWRITER_H_
18#define NETD_SERVER_DUMPWRITER_H_
19
20#include <string>
Erik Kline2d3a1632016-03-15 16:33:48 +090021
Lorenzo Colitti7035f222017-02-13 18:29:00 +090022namespace android {
23namespace net {
Erik Kline2d3a1632016-03-15 16:33:48 +090024
25class DumpWriter {
Erik Klineb31fd692018-06-06 20:50:11 +090026 public:
Erik Kline2d3a1632016-03-15 16:33:48 +090027 DumpWriter(int fd);
28
29 void incIndent();
30 void decIndent();
31
32 void println(const std::string& line);
Erik Klineb31fd692018-06-06 20:50:11 +090033 template <size_t n>
34 void println(const char line[n]) { println(std::string(line)); }
35 // Hint to the compiler that it should apply printf validation of
36 // arguments (beginning at position 3) of the format (specified in
37 // position 2). Note that position 1 is the implicit "this" argument.
38 void println(const char* fmt, ...) __attribute__((__format__(__printf__, 2, 3)));
Erik Kline2d3a1632016-03-15 16:33:48 +090039 void blankline() { println(""); }
40
Erik Klineb31fd692018-06-06 20:50:11 +090041 private:
Erik Kline2d3a1632016-03-15 16:33:48 +090042 uint8_t mIndentLevel;
43 int mFd;
44};
45
Erik Klineb31fd692018-06-06 20:50:11 +090046class ScopedIndent {
47 public:
48 ScopedIndent() = delete;
49 ScopedIndent(const ScopedIndent&) = delete;
50 ScopedIndent(ScopedIndent&&) = delete;
51 explicit ScopedIndent(DumpWriter& dw) : mDw(dw) { mDw.incIndent(); }
52 ~ScopedIndent() { mDw.decIndent(); }
53 ScopedIndent& operator=(const ScopedIndent&) = delete;
54 ScopedIndent& operator=(ScopedIndent&&) = delete;
55
56 // TODO: consider additional {inc,dec}Indent methods and a counter that
57 // can be used to unwind all pending increments on exit.
58
59 private:
60 DumpWriter& mDw;
61};
62
Lorenzo Colitti7035f222017-02-13 18:29:00 +090063} // namespace net
64} // namespace android
65
Erik Kline2d3a1632016-03-15 16:33:48 +090066#endif // NETD_SERVER_DUMPWRITER_H_