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 | |
Ian Rogers | fa82427 | 2013-11-05 16:12:57 -0800 | [diff] [blame] | 20 | #include "base/logging.h" |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 21 | #include "base/macros.h" |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 22 | #include <streambuf> |
| 23 | |
| 24 | const char kIndentChar =' '; |
| 25 | const size_t kIndentBy1Count = 2; |
| 26 | |
| 27 | class Indenter : public std::streambuf { |
| 28 | public: |
| 29 | Indenter(std::streambuf* out, char text, size_t count) |
Vladimir Marko | 107b61b | 2015-06-23 15:39:01 +0100 | [diff] [blame] | 30 | : indent_next_(true), out_sbuf_(out), |
| 31 | text_{text, text, text, text, text, text, text, text}, // NOLINT(whitespace/braces) |
| 32 | count_(count) {} |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 33 | |
| 34 | private: |
Vladimir Marko | 107b61b | 2015-06-23 15:39:01 +0100 | [diff] [blame] | 35 | std::streamsize xsputn(const char* s, std::streamsize n) OVERRIDE { |
| 36 | std::streamsize result = n; // Aborts on failure. |
| 37 | const char* eol = static_cast<const char*>(memchr(s, '\n', n)); |
| 38 | while (eol != nullptr) { |
| 39 | size_t to_write = eol + 1 - s; |
| 40 | Write(s, to_write); |
| 41 | s += to_write; |
| 42 | n -= to_write; |
| 43 | indent_next_ = true; |
| 44 | eol = static_cast<const char*>(memchr(s, '\n', n)); |
| 45 | } |
| 46 | if (n != 0u) { |
| 47 | Write(s, n); |
| 48 | } |
| 49 | return result; |
| 50 | } |
| 51 | |
| 52 | int_type overflow(int_type c) OVERRIDE { |
Ian Rogers | fa82427 | 2013-11-05 16:12:57 -0800 | [diff] [blame] | 53 | if (UNLIKELY(c == std::char_traits<char>::eof())) { |
| 54 | out_sbuf_->pubsync(); |
| 55 | return c; |
| 56 | } |
Vladimir Marko | 107b61b | 2015-06-23 15:39:01 +0100 | [diff] [blame] | 57 | char data[1] = { static_cast<char>(c) }; |
| 58 | Write(data, 1u); |
Ian Rogers | fa82427 | 2013-11-05 16:12:57 -0800 | [diff] [blame] | 59 | indent_next_ = (c == '\n'); |
Vladimir Marko | 107b61b | 2015-06-23 15:39:01 +0100 | [diff] [blame] | 60 | return c; |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | int sync() { |
| 64 | return out_sbuf_->pubsync(); |
| 65 | } |
| 66 | |
Vladimir Marko | 107b61b | 2015-06-23 15:39:01 +0100 | [diff] [blame] | 67 | void Write(const char* s, std::streamsize n) { |
| 68 | if (indent_next_) { |
| 69 | size_t remaining = count_; |
| 70 | while (remaining != 0u) { |
| 71 | size_t to_write = std::min(remaining, sizeof(text_)); |
| 72 | RawWrite(text_, to_write); |
| 73 | remaining -= to_write; |
| 74 | } |
| 75 | indent_next_ = false; |
| 76 | } |
| 77 | RawWrite(s, n); |
| 78 | } |
| 79 | |
| 80 | void RawWrite(const char* s, std::streamsize n) { |
| 81 | size_t written = out_sbuf_->sputn(s, n); |
| 82 | s += written; |
| 83 | n -= written; |
| 84 | while (n != 0u) { |
| 85 | out_sbuf_->pubsync(); |
| 86 | written = out_sbuf_->sputn(s, n); |
| 87 | CHECK_NE(written, 0u) << "Error writing to buffer. Disk full?"; |
| 88 | s += written; |
| 89 | n -= written; |
| 90 | } |
| 91 | } |
| 92 | |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 93 | bool indent_next_; |
| 94 | |
| 95 | // Buffer to write output to. |
| 96 | std::streambuf* const out_sbuf_; |
| 97 | |
| 98 | // Text output as indent. |
Vladimir Marko | 107b61b | 2015-06-23 15:39:01 +0100 | [diff] [blame] | 99 | const char text_[8]; |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 100 | |
| 101 | // Number of times text is output. |
| 102 | const size_t count_; |
| 103 | |
| 104 | DISALLOW_COPY_AND_ASSIGN(Indenter); |
| 105 | }; |
| 106 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 107 | #endif // ART_RUNTIME_INDENTER_H_ |