Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e8ee5897 authored by Ajay Panicker's avatar Ajay Panicker
Browse files

Add helper classes to auto indent newlines for ostreams

Bug: 79167906
Test: adb shell dumpsys bluetooth_manager
Change-Id: I54484181abc438e90ade771fca6943c2b303e4db
parent 796c5b3b
Loading
Loading
Loading
Loading
+6 −2
Original line number Diff line number Diff line
@@ -414,9 +414,13 @@ void AvrcpService::DebugDump(int fd) {
          device_list.size());

  std::stringstream stream;
  {
    ScopedIndent indent(stream);
    for (auto device : device_list) {
      stream << *device << std::endl;
    }
  }

  dprintf(fd, "%s", stream.str().c_str());
}

+49 −0
Original line number Diff line number Diff line
@@ -771,4 +771,53 @@ inline std::string& AppendField(std::string* p_result, bool append,
  return *p_result;
}

// This object puts the stream in a state where every time that a new line
// occurs, the next line is indented a certain number of spaces. The stream is
// reset to its previous state when the object is destroyed.
class ScopedIndent {
 public:
  ScopedIndent(std::ostream& stream, int indent_size = DEFAULT_TAB)
      : indented_buf_(stream, indent_size) {
    old_stream_ = &stream;
    old_stream_buf_ = stream.rdbuf();
    stream.rdbuf(&indented_buf_);
  }

  ~ScopedIndent() { old_stream_->rdbuf(old_stream_buf_); }

  static const size_t DEFAULT_TAB = 2;

 private:
  class IndentedStreamBuf : public std::streambuf {
   public:
    IndentedStreamBuf(std::ostream& stream, int indent_size)
        : wrapped_buf_(stream.rdbuf()),
          indent_size_(indent_size),
          indent_next_line_(true){};

   protected:
    virtual int overflow(int character) override {
      if (indent_next_line_ && character != '\n') {
        for (int i = 0; i < indent_size_; i++) wrapped_buf_->sputc(' ');
      }

      indent_next_line_ = false;
      if (character == '\n') {
        indent_next_line_ = true;
      }

      return wrapped_buf_->sputc(character);
    }

   private:
    std::streambuf* wrapped_buf_;
    int indent_size_;
    bool indent_next_line_;
  };

  std::ostream* old_stream_;
  std::streambuf* old_stream_buf_;
  IndentedStreamBuf indented_buf_;
};

#endif
+19 −30
Original line number Diff line number Diff line
@@ -1141,39 +1141,28 @@ static std::string volumeToStr(int8_t volume) {
}

std::ostream& operator<<(std::ostream& out, const Device& d) {
  out << "  " << d.address_.ToString();
  out << d.address_.ToString();
  if (d.IsActive()) out << " <Active>";
  out << std::endl;

  ScopedIndent indent(out);
  out << "Current Volume: " << volumeToStr(d.volume_) << std::endl;
  out << "Current Browsed Player ID: " << d.curr_browsed_player_id_
      << std::endl;
  out << "    Registered Notifications: " << std::endl;
  if (d.track_changed_.first) {
    out << "      Track Changed" << std::endl;
  }
  if (d.play_status_changed_.first) {
    out << "      Play Status" << std::endl;
  }
  if (d.play_pos_changed_.first) {
    out << "      Play Position" << std::endl;
  }
  if (d.now_playing_changed_.first) {
    out << "      Now Playing" << std::endl;
  }
  if (d.addr_player_changed_.first) {
    out << "      Addressed Player" << std::endl;
  out << "Registered Notifications:\n";
  {
    ScopedIndent indent(out);
    if (d.track_changed_.first) out << "Track Changed\n";
    if (d.play_status_changed_.first) out << "Play Status\n";
    if (d.play_pos_changed_.first) out << "Play Position\n";
    if (d.now_playing_changed_.first) out << "Now Playing\n";
    if (d.addr_player_changed_.first) out << "Addressed Player\n";
    if (d.avail_players_changed_.first) out << "Available Players\n";
    if (d.uids_changed_.first) out << "UIDs Changed\n";
  }
  if (d.avail_players_changed_.first) {
    out << "      Available Players" << std::endl;
  }
  if (d.uids_changed_.first) {
    out << "      UIDs Changed" << std::endl;
  }

  out << "Last Play State: " << d.last_play_status_.state << std::endl;
  out << "    Last Song Sent ID: \"" << d.last_song_info_.media_id << "\""
      << std::endl;
  out << "    Current Folder: \"" << d.CurrentFolder() << "\"" << std::endl;
  out << "Last Song Sent ID: \"" << d.last_song_info_.media_id << "\"\n";
  out << "Current Folder: \"" << d.CurrentFolder() << "\"\n";
  out << "MTU Sizes: CTRL=" << d.ctrl_mtu_ << " BROWSE=" << d.browse_mtu_
      << std::endl;
  // TODO (apanicke): Add supported features as well as media keys