blob: cc6d4c4e96a24847ee074d4eea34ed1e38f12440 [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 Gampe8cf9cb32017-07-19 09:28:38 -070023#include "base/logging.h"
24#include "base/macros.h"
25
Vladimir Marko8f1e08a2015-06-26 12:06:30 +010026namespace art {
27
28constexpr char kIndentChar =' ';
29constexpr size_t kIndentBy1Count = 2;
Ian Rogers2bcb4a42012-11-08 10:39:18 -080030
31class Indenter : public std::streambuf {
32 public:
33 Indenter(std::streambuf* out, char text, size_t count)
Vladimir Marko107b61b2015-06-23 15:39:01 +010034 : indent_next_(true), out_sbuf_(out),
35 text_{text, text, text, text, text, text, text, text}, // NOLINT(whitespace/braces)
36 count_(count) {}
Ian Rogers2bcb4a42012-11-08 10:39:18 -080037
38 private:
Vladimir Marko107b61b2015-06-23 15:39:01 +010039 std::streamsize xsputn(const char* s, std::streamsize n) OVERRIDE {
40 std::streamsize result = n; // Aborts on failure.
41 const char* eol = static_cast<const char*>(memchr(s, '\n', n));
42 while (eol != nullptr) {
43 size_t to_write = eol + 1 - s;
44 Write(s, to_write);
45 s += to_write;
46 n -= to_write;
47 indent_next_ = true;
48 eol = static_cast<const char*>(memchr(s, '\n', n));
49 }
50 if (n != 0u) {
51 Write(s, n);
52 }
53 return result;
54 }
55
56 int_type overflow(int_type c) OVERRIDE {
Ian Rogersfa824272013-11-05 16:12:57 -080057 if (UNLIKELY(c == std::char_traits<char>::eof())) {
58 out_sbuf_->pubsync();
59 return c;
60 }
Vladimir Marko107b61b2015-06-23 15:39:01 +010061 char data[1] = { static_cast<char>(c) };
62 Write(data, 1u);
Ian Rogersfa824272013-11-05 16:12:57 -080063 indent_next_ = (c == '\n');
Vladimir Marko107b61b2015-06-23 15:39:01 +010064 return c;
Ian Rogers2bcb4a42012-11-08 10:39:18 -080065 }
66
67 int sync() {
68 return out_sbuf_->pubsync();
69 }
70
Vladimir Marko107b61b2015-06-23 15:39:01 +010071 void Write(const char* s, std::streamsize n) {
72 if (indent_next_) {
73 size_t remaining = count_;
74 while (remaining != 0u) {
75 size_t to_write = std::min(remaining, sizeof(text_));
76 RawWrite(text_, to_write);
77 remaining -= to_write;
78 }
79 indent_next_ = false;
80 }
81 RawWrite(s, n);
82 }
83
84 void RawWrite(const char* s, std::streamsize n) {
85 size_t written = out_sbuf_->sputn(s, n);
86 s += written;
87 n -= written;
88 while (n != 0u) {
89 out_sbuf_->pubsync();
90 written = out_sbuf_->sputn(s, n);
91 CHECK_NE(written, 0u) << "Error writing to buffer. Disk full?";
92 s += written;
93 n -= written;
94 }
95 }
96
Ian Rogers2bcb4a42012-11-08 10:39:18 -080097 bool indent_next_;
98
99 // Buffer to write output to.
100 std::streambuf* const out_sbuf_;
101
102 // Text output as indent.
Vladimir Marko107b61b2015-06-23 15:39:01 +0100103 const char text_[8];
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800104
105 // Number of times text is output.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100106 size_t count_;
107
108 friend class VariableIndentationOutputStream;
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800109
110 DISALLOW_COPY_AND_ASSIGN(Indenter);
111};
112
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100113class VariableIndentationOutputStream {
114 public:
115 explicit VariableIndentationOutputStream(std::ostream* os, char text = kIndentChar)
116 : indenter_(os->rdbuf(), text, 0u),
117 indented_os_(&indenter_) {
118 }
119
120 std::ostream& Stream() {
121 return indented_os_;
122 }
123
124 void IncreaseIndentation(size_t adjustment) {
125 indenter_.count_ += adjustment;
126 }
127
128 void DecreaseIndentation(size_t adjustment) {
129 DCHECK_GE(indenter_.count_, adjustment);
130 indenter_.count_ -= adjustment;
131 }
132
133 private:
134 Indenter indenter_;
135 std::ostream indented_os_;
136
137 DISALLOW_COPY_AND_ASSIGN(VariableIndentationOutputStream);
138};
139
140class ScopedIndentation {
141 public:
142 explicit ScopedIndentation(VariableIndentationOutputStream* vios,
143 size_t adjustment = kIndentBy1Count)
144 : vios_(vios),
145 adjustment_(adjustment) {
146 vios_->IncreaseIndentation(adjustment_);
147 }
148
149 ~ScopedIndentation() {
150 vios_->DecreaseIndentation(adjustment_);
151 }
152
153 private:
154 VariableIndentationOutputStream* const vios_;
155 const size_t adjustment_;
156
157 DISALLOW_COPY_AND_ASSIGN(ScopedIndentation);
158};
159
160} // namespace art
161
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700162#endif // ART_RUNTIME_INDENTER_H_