Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1 | /* |
| 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 Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_INDENTER_H_ |
| 18 | #define ART_RUNTIME_INDENTER_H_ |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 19 | |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 20 | #include <ostream> |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 21 | #include <streambuf> |
| 22 | |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 23 | #include "base/logging.h" |
| 24 | #include "base/macros.h" |
| 25 | |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 26 | namespace art { |
| 27 | |
| 28 | constexpr char kIndentChar =' '; |
| 29 | constexpr size_t kIndentBy1Count = 2; |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 30 | |
| 31 | class Indenter : public std::streambuf { |
| 32 | public: |
| 33 | Indenter(std::streambuf* out, char text, size_t count) |
Vladimir Marko | 107b61b | 2015-06-23 15:39:01 +0100 | [diff] [blame] | 34 | : indent_next_(true), out_sbuf_(out), |
| 35 | text_{text, text, text, text, text, text, text, text}, // NOLINT(whitespace/braces) |
| 36 | count_(count) {} |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 37 | |
| 38 | private: |
Vladimir Marko | 107b61b | 2015-06-23 15:39:01 +0100 | [diff] [blame] | 39 | 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 Rogers | fa82427 | 2013-11-05 16:12:57 -0800 | [diff] [blame] | 57 | if (UNLIKELY(c == std::char_traits<char>::eof())) { |
| 58 | out_sbuf_->pubsync(); |
| 59 | return c; |
| 60 | } |
Vladimir Marko | 107b61b | 2015-06-23 15:39:01 +0100 | [diff] [blame] | 61 | char data[1] = { static_cast<char>(c) }; |
| 62 | Write(data, 1u); |
Ian Rogers | fa82427 | 2013-11-05 16:12:57 -0800 | [diff] [blame] | 63 | indent_next_ = (c == '\n'); |
Vladimir Marko | 107b61b | 2015-06-23 15:39:01 +0100 | [diff] [blame] | 64 | return c; |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | int sync() { |
| 68 | return out_sbuf_->pubsync(); |
| 69 | } |
| 70 | |
Vladimir Marko | 107b61b | 2015-06-23 15:39:01 +0100 | [diff] [blame] | 71 | 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 Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 97 | bool indent_next_; |
| 98 | |
| 99 | // Buffer to write output to. |
| 100 | std::streambuf* const out_sbuf_; |
| 101 | |
| 102 | // Text output as indent. |
Vladimir Marko | 107b61b | 2015-06-23 15:39:01 +0100 | [diff] [blame] | 103 | const char text_[8]; |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 104 | |
| 105 | // Number of times text is output. |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 106 | size_t count_; |
| 107 | |
| 108 | friend class VariableIndentationOutputStream; |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 109 | |
| 110 | DISALLOW_COPY_AND_ASSIGN(Indenter); |
| 111 | }; |
| 112 | |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 113 | class 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 | |
| 140 | class 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 Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 162 | #endif // ART_RUNTIME_INDENTER_H_ |