Samuel Huang | 06f1ae9 | 2018-03-13 18:19:34 +0000 | [diff] [blame] | 1 | // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "components/zucchini/io_utils.h" |
| 6 | |
| 7 | #include <iostream> |
| 8 | |
| 9 | namespace zucchini { |
| 10 | |
| 11 | /******** LimitedOutputStream::StreamBuf ********/ |
| 12 | |
| 13 | LimitedOutputStream::StreamBuf::StreamBuf(std::ostream& os, int limit) |
| 14 | : os_(os), limit_(limit) {} |
| 15 | |
| 16 | LimitedOutputStream::StreamBuf::~StreamBuf() { |
| 17 | // Display warning in case we forget to flush data with std::endl. |
| 18 | if (!str().empty()) { |
| 19 | std::cerr << "Warning: LimitedOutputStream has " << str().length() |
| 20 | << " bytes of unflushed output." << std::endl; |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | int LimitedOutputStream::StreamBuf::sync() { |
| 25 | if (full()) { |
| 26 | str(""); |
| 27 | return 0; |
| 28 | } |
| 29 | os_ << str(); |
| 30 | str(""); |
| 31 | if (++counter_ >= limit_) |
| 32 | os_ << "(Additional output suppressed)\n"; |
| 33 | os_.flush(); |
| 34 | return 0; |
| 35 | } |
| 36 | |
| 37 | /******** LimitedOutputStream ********/ |
| 38 | |
| 39 | LimitedOutputStream::LimitedOutputStream(std::ostream& os, int limit) |
| 40 | : std::ostream(&buf_), buf_(os, limit) {} |
| 41 | |
| 42 | /******** PrefixSep ********/ |
| 43 | |
| 44 | std::ostream& operator<<(std::ostream& ostr, PrefixSep& obj) { |
| 45 | if (obj.first_) |
| 46 | obj.first_ = false; |
| 47 | else |
| 48 | ostr << obj.sep_str_; |
| 49 | return ostr; |
| 50 | } |
| 51 | |
| 52 | } // namespace zucchini |