blob: 38b398de4a74cd4db7b6c5da953f64fba0880978 [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
Ian Rogersfa824272013-11-05 16:12:57 -080020#include "base/logging.h"
Elliott Hughes76160052012-12-12 16:31:20 -080021#include "base/macros.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080022#include <streambuf>
23
24const char kIndentChar =' ';
25const size_t kIndentBy1Count = 2;
26
27class Indenter : public std::streambuf {
28 public:
29 Indenter(std::streambuf* out, char text, size_t count)
Vladimir Marko107b61b2015-06-23 15:39:01 +010030 : indent_next_(true), out_sbuf_(out),
31 text_{text, text, text, text, text, text, text, text}, // NOLINT(whitespace/braces)
32 count_(count) {}
Ian Rogers2bcb4a42012-11-08 10:39:18 -080033
34 private:
Vladimir Marko107b61b2015-06-23 15:39:01 +010035 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 Rogersfa824272013-11-05 16:12:57 -080053 if (UNLIKELY(c == std::char_traits<char>::eof())) {
54 out_sbuf_->pubsync();
55 return c;
56 }
Vladimir Marko107b61b2015-06-23 15:39:01 +010057 char data[1] = { static_cast<char>(c) };
58 Write(data, 1u);
Ian Rogersfa824272013-11-05 16:12:57 -080059 indent_next_ = (c == '\n');
Vladimir Marko107b61b2015-06-23 15:39:01 +010060 return c;
Ian Rogers2bcb4a42012-11-08 10:39:18 -080061 }
62
63 int sync() {
64 return out_sbuf_->pubsync();
65 }
66
Vladimir Marko107b61b2015-06-23 15:39:01 +010067 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 Rogers2bcb4a42012-11-08 10:39:18 -080093 bool indent_next_;
94
95 // Buffer to write output to.
96 std::streambuf* const out_sbuf_;
97
98 // Text output as indent.
Vladimir Marko107b61b2015-06-23 15:39:01 +010099 const char text_[8];
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800100
101 // Number of times text is output.
102 const size_t count_;
103
104 DISALLOW_COPY_AND_ASSIGN(Indenter);
105};
106
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700107#endif // ART_RUNTIME_INDENTER_H_