io_utils.cc 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. #include "components/zucchini/io_utils.h"
  5. #include <iostream>
  6. namespace zucchini {
  7. /******** LimitedOutputStream::StreamBuf ********/
  8. LimitedOutputStream::StreamBuf::StreamBuf(std::ostream& os, int limit)
  9. : os_(os), limit_(limit) {}
  10. LimitedOutputStream::StreamBuf::~StreamBuf() {
  11. // Display warning in case we forget to flush data with std::endl.
  12. if (!str().empty()) {
  13. std::cerr << "Warning: LimitedOutputStream has " << str().length()
  14. << " bytes of unflushed output." << std::endl;
  15. }
  16. }
  17. int LimitedOutputStream::StreamBuf::sync() {
  18. if (full()) {
  19. str("");
  20. return 0;
  21. }
  22. os_ << str();
  23. str("");
  24. if (++counter_ >= limit_)
  25. os_ << "(Additional output suppressed)\n";
  26. os_.flush();
  27. return 0;
  28. }
  29. /******** LimitedOutputStream ********/
  30. LimitedOutputStream::LimitedOutputStream(std::ostream& os, int limit)
  31. : std::ostream(&buf_), buf_(os, limit) {}
  32. /******** PrefixSep ********/
  33. std::ostream& operator<<(std::ostream& ostr, PrefixSep& obj) {
  34. if (obj.first_)
  35. obj.first_ = false;
  36. else
  37. ostr << obj.sep_str_;
  38. return ostr;
  39. }
  40. } // namespace zucchini