blob: aa493d03c8af9dead60f68c61d8d5c14f91093b0 [file] [log] [blame]
Samuel Huang06f1ae92018-03-13 18:19:34 +00001// 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
9namespace zucchini {
10
11/******** LimitedOutputStream::StreamBuf ********/
12
13LimitedOutputStream::StreamBuf::StreamBuf(std::ostream& os, int limit)
14 : os_(os), limit_(limit) {}
15
16LimitedOutputStream::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
24int 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
39LimitedOutputStream::LimitedOutputStream(std::ostream& os, int limit)
40 : std::ostream(&buf_), buf_(os, limit) {}
41
42/******** PrefixSep ********/
43
44std::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