blob: 215bf88ce3beea74c90fe58436c478c3e2057849 [file] [log] [blame]
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001/*
2 * Copyright (C) 2012 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
David Sehr9c4a0152018-04-05 12:23:54 -070017#ifndef ART_LIBARTBASE_BASE_INDENTER_H_
18#define ART_LIBARTBASE_BASE_INDENTER_H_
Ian Rogers2bcb4a42012-11-08 10:39:18 -080019
Vladimir Marko8f1e08a2015-06-26 12:06:30 +010020#include <ostream>
Ian Rogers2bcb4a42012-11-08 10:39:18 -080021#include <streambuf>
22
Andreas Gampe57943812017-12-06 21:39:13 -080023#include <android-base/logging.h>
24
David Sehr1979c642018-04-26 14:41:18 -070025#include "macros.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070026
Vladimir Marko8f1e08a2015-06-26 12:06:30 +010027namespace art {
28
29constexpr char kIndentChar =' ';
30constexpr size_t kIndentBy1Count = 2;
Ian Rogers2bcb4a42012-11-08 10:39:18 -080031
32class Indenter : public std::streambuf {
33 public:
34 Indenter(std::streambuf* out, char text, size_t count)
Vladimir Marko107b61b2015-06-23 15:39:01 +010035 : indent_next_(true), out_sbuf_(out),
Igor Murashkin5573c372017-11-16 13:34:30 -080036 text_{text, text, text, text, text, text, text, text},
Vladimir Marko107b61b2015-06-23 15:39:01 +010037 count_(count) {}
Ian Rogers2bcb4a42012-11-08 10:39:18 -080038
39 private:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010040 std::streamsize xsputn(const char* s, std::streamsize n) override {
Vladimir Marko107b61b2015-06-23 15:39:01 +010041 std::streamsize result = n; // Aborts on failure.
42 const char* eol = static_cast<const char*>(memchr(s, '\n', n));
43 while (eol != nullptr) {
44 size_t to_write = eol + 1 - s;
45 Write(s, to_write);
46 s += to_write;
47 n -= to_write;
48 indent_next_ = true;
49 eol = static_cast<const char*>(memchr(s, '\n', n));
50 }
51 if (n != 0u) {
52 Write(s, n);
53 }
54 return result;
55 }
56
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010057 int_type overflow(int_type c) override {
Ian Rogersfa824272013-11-05 16:12:57 -080058 if (UNLIKELY(c == std::char_traits<char>::eof())) {
59 out_sbuf_->pubsync();
60 return c;
61 }
Vladimir Marko107b61b2015-06-23 15:39:01 +010062 char data[1] = { static_cast<char>(c) };
63 Write(data, 1u);
Ian Rogersfa824272013-11-05 16:12:57 -080064 indent_next_ = (c == '\n');
Vladimir Marko107b61b2015-06-23 15:39:01 +010065 return c;
Ian Rogers2bcb4a42012-11-08 10:39:18 -080066 }
67
Yi Kong39402542019-03-24 02:47:16 -070068 int sync() override {
Ian Rogers2bcb4a42012-11-08 10:39:18 -080069 return out_sbuf_->pubsync();
70 }
71
Vladimir Marko107b61b2015-06-23 15:39:01 +010072 void Write(const char* s, std::streamsize n) {
73 if (indent_next_) {
74 size_t remaining = count_;
75 while (remaining != 0u) {
76 size_t to_write = std::min(remaining, sizeof(text_));
77 RawWrite(text_, to_write);
78 remaining -= to_write;
79 }
80 indent_next_ = false;
81 }
82 RawWrite(s, n);
83 }
84
85 void RawWrite(const char* s, std::streamsize n) {
86 size_t written = out_sbuf_->sputn(s, n);
87 s += written;
88 n -= written;
89 while (n != 0u) {
90 out_sbuf_->pubsync();
91 written = out_sbuf_->sputn(s, n);
92 CHECK_NE(written, 0u) << "Error writing to buffer. Disk full?";
93 s += written;
94 n -= written;
95 }
96 }
97
Ian Rogers2bcb4a42012-11-08 10:39:18 -080098 bool indent_next_;
99
100 // Buffer to write output to.
101 std::streambuf* const out_sbuf_;
102
103 // Text output as indent.
Vladimir Marko107b61b2015-06-23 15:39:01 +0100104 const char text_[8];
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800105
106 // Number of times text is output.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100107 size_t count_;
108
109 friend class VariableIndentationOutputStream;
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800110
111 DISALLOW_COPY_AND_ASSIGN(Indenter);
112};
113
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100114class VariableIndentationOutputStream {
115 public:
116 explicit VariableIndentationOutputStream(std::ostream* os, char text = kIndentChar)
117 : indenter_(os->rdbuf(), text, 0u),
118 indented_os_(&indenter_) {
119 }
120
121 std::ostream& Stream() {
122 return indented_os_;
123 }
124
David Srbecky86decb62018-06-05 06:41:10 +0100125 size_t GetIndentation() const {
126 return indenter_.count_;
127 }
128
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100129 void IncreaseIndentation(size_t adjustment) {
130 indenter_.count_ += adjustment;
131 }
132
133 void DecreaseIndentation(size_t adjustment) {
134 DCHECK_GE(indenter_.count_, adjustment);
135 indenter_.count_ -= adjustment;
136 }
137
138 private:
139 Indenter indenter_;
140 std::ostream indented_os_;
141
142 DISALLOW_COPY_AND_ASSIGN(VariableIndentationOutputStream);
143};
144
145class ScopedIndentation {
146 public:
147 explicit ScopedIndentation(VariableIndentationOutputStream* vios,
148 size_t adjustment = kIndentBy1Count)
149 : vios_(vios),
150 adjustment_(adjustment) {
151 vios_->IncreaseIndentation(adjustment_);
152 }
153
154 ~ScopedIndentation() {
155 vios_->DecreaseIndentation(adjustment_);
156 }
157
158 private:
159 VariableIndentationOutputStream* const vios_;
160 const size_t adjustment_;
161
162 DISALLOW_COPY_AND_ASSIGN(ScopedIndentation);
163};
164
165} // namespace art
166
David Sehr9c4a0152018-04-05 12:23:54 -0700167#endif // ART_LIBARTBASE_BASE_INDENTER_H_