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