xref: /aosp_15_r20/external/zucchini/io_utils.cc (revision a03ca8b91e029cd15055c20c78c2e087c84792e4)
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 
StreamBuf(std::ostream & os,int limit)13 LimitedOutputStream::StreamBuf::StreamBuf(std::ostream& os, int limit)
14     : os_(os), limit_(limit) {}
15 
~StreamBuf()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 
sync()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 
LimitedOutputStream(std::ostream & os,int limit)39 LimitedOutputStream::LimitedOutputStream(std::ostream& os, int limit)
40     : std::ostream(&buf_), buf_(os, limit) {}
41 
42 /******** PrefixSep ********/
43 
operator <<(std::ostream & ostr,PrefixSep & obj)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
53