blob: 6361dd2092c71fa2e9f28a6dfa62bef5ab121af5 [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
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_INDENTER_H_
18#define ART_RUNTIME_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
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070025#include "base/macros.h"
26
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:
Vladimir Marko107b61b2015-06-23 15:39:01 +010040 std::streamsize xsputn(const char* s, std::streamsize n) OVERRIDE {
41 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
57 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
68 int sync() {
69 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
125 void IncreaseIndentation(size_t adjustment) {
126 indenter_.count_ += adjustment;
127 }
128
129 void DecreaseIndentation(size_t adjustment) {
130 DCHECK_GE(indenter_.count_, adjustment);
131 indenter_.count_ -= adjustment;
132 }
133
134 private:
135 Indenter indenter_;
136 std::ostream indented_os_;
137
138 DISALLOW_COPY_AND_ASSIGN(VariableIndentationOutputStream);
139};
140
141class ScopedIndentation {
142 public:
143 explicit ScopedIndentation(VariableIndentationOutputStream* vios,
144 size_t adjustment = kIndentBy1Count)
145 : vios_(vios),
146 adjustment_(adjustment) {
147 vios_->IncreaseIndentation(adjustment_);
148 }
149
150 ~ScopedIndentation() {
151 vios_->DecreaseIndentation(adjustment_);
152 }
153
154 private:
155 VariableIndentationOutputStream* const vios_;
156 const size_t adjustment_;
157
158 DISALLOW_COPY_AND_ASSIGN(ScopedIndentation);
159};
160
161} // namespace art
162
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700163#endif // ART_RUNTIME_INDENTER_H_