blob: d055d4e3f4318cb93150183d0eb4f30483042384 [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)
30 : indent_next_(true), out_sbuf_(out), text_(text), count_(count) {}
31
32 private:
33 int_type overflow(int_type c) {
Ian Rogersfa824272013-11-05 16:12:57 -080034 if (UNLIKELY(c == std::char_traits<char>::eof())) {
35 out_sbuf_->pubsync();
36 return c;
37 }
38 if (indent_next_) {
39 for (size_t i = 0; i < count_; ++i) {
40 int_type r = out_sbuf_->sputc(text_);
41 if (UNLIKELY(r != text_)) {
42 out_sbuf_->pubsync();
43 r = out_sbuf_->sputc(text_);
44 CHECK_EQ(r, text_) << "Error writing to buffer. Disk full?";
Ian Rogers2bcb4a42012-11-08 10:39:18 -080045 }
46 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -080047 }
Ian Rogersfa824272013-11-05 16:12:57 -080048 indent_next_ = (c == '\n');
49 int_type r = out_sbuf_->sputc(c);
50 if (UNLIKELY(r != c)) {
51 out_sbuf_->pubsync();
52 r = out_sbuf_->sputc(c);
53 CHECK_EQ(r, c) << "Error writing to buffer. Disk full?";
54 }
55 return r;
Ian Rogers2bcb4a42012-11-08 10:39:18 -080056 }
57
58 int sync() {
59 return out_sbuf_->pubsync();
60 }
61
62 bool indent_next_;
63
64 // Buffer to write output to.
65 std::streambuf* const out_sbuf_;
66
67 // Text output as indent.
68 const char text_;
69
70 // Number of times text is output.
71 const size_t count_;
72
73 DISALLOW_COPY_AND_ASSIGN(Indenter);
74};
75
Brian Carlstromfc0e3212013-07-17 14:40:12 -070076#endif // ART_RUNTIME_INDENTER_H_