diff --git a/include/cppkafka/topic_partition.h b/include/cppkafka/topic_partition.h index e3b23bb..f5cb47d 100644 --- a/include/cppkafka/topic_partition.h +++ b/include/cppkafka/topic_partition.h @@ -31,6 +31,7 @@ #define CPPKAFKA_TOPIC_PARTITION_H #include +#include #include namespace cppkafka { @@ -104,6 +105,11 @@ public: * Gets the offset */ int64_t get_offset() const; + + /** + * Print to a stream + */ + friend std::ostream& operator<<(std::ostream& output, const TopicPartition& rhs); private: std::string topic_; int partition_; diff --git a/include/cppkafka/topic_partition_list.h b/include/cppkafka/topic_partition_list.h index acdd83f..355cabc 100644 --- a/include/cppkafka/topic_partition_list.h +++ b/include/cppkafka/topic_partition_list.h @@ -31,6 +31,7 @@ #define CPPKAFKA_TOPIC_PARTITION_LIST_H #include +#include #include #include @@ -51,6 +52,8 @@ std::vector convert(const TopicPartitionsListPtr& topic_partitio std::vector convert(rd_kafka_topic_partition_list_t* topic_partitions); TopicPartitionsListPtr make_handle(rd_kafka_topic_partition_list_t* handle); +std::ostream& operator<<(std::ostream& output, const TopicPartitionList& rhs); + } // cppkafka #endif // CPPKAFKA_TOPIC_PARTITION_LIST_H diff --git a/src/topic_partition.cpp b/src/topic_partition.cpp index fc0158e..3259319 100644 --- a/src/topic_partition.cpp +++ b/src/topic_partition.cpp @@ -27,10 +27,12 @@ * */ +#include #include #include "topic_partition.h" using std::string; +using std::ostream; namespace cppkafka { @@ -71,4 +73,8 @@ int64_t TopicPartition::get_offset() const { return offset_; } +ostream& operator<<(ostream& output, const TopicPartition& rhs) { + return output << rhs.get_topic() << "[" << rhs.get_partition() << "]"; +} + } // cppkafka diff --git a/src/topic_partition_list.cpp b/src/topic_partition_list.cpp index 5baca6e..2fcae88 100644 --- a/src/topic_partition_list.cpp +++ b/src/topic_partition_list.cpp @@ -27,11 +27,13 @@ * */ +#include #include "topic_partition_list.h" #include "topic_partition.h" #include "exceptions.h" using std::vector; +using std::ostream; namespace cppkafka { @@ -65,4 +67,16 @@ TopicPartitionsListPtr make_handle(rd_kafka_topic_partition_list_t* handle) { return TopicPartitionsListPtr(handle, &rd_kafka_topic_partition_list_destroy); } +ostream& operator<<(ostream& output, const TopicPartitionList& rhs) { + output << "[ "; + for (auto iter = rhs.begin(); iter != rhs.end(); ++iter) { + if (iter != rhs.begin()) { + output << ", "; + } + output << *iter; + } + output << " ]"; + return output; +} + } // cppkafka diff --git a/tests/topic_partition_list_test.cpp b/tests/topic_partition_list_test.cpp index b801e7b..661aaaf 100644 --- a/tests/topic_partition_list_test.cpp +++ b/tests/topic_partition_list_test.cpp @@ -1,7 +1,10 @@ +#include #include #include "cppkafka/topic_partition_list.h" #include "cppkafka/topic_partition.h" +using std::ostringstream; + using namespace cppkafka; class TopicPartitionListTest : public testing::Test { @@ -26,3 +29,20 @@ TEST_F(TopicPartitionListTest, Conversion) { EXPECT_EQ(item1.get_offset(), item2.get_offset()); } } + +TEST_F(TopicPartitionListTest, AsString) { + ostringstream output; + TopicPartition topic_partition("foo", 5); + output << topic_partition; + EXPECT_EQ("foo[5]", output.str()); +} + +TEST_F(TopicPartitionListTest, ListAsString) { + ostringstream output; + TopicPartitionList list; + list.push_back("foo"); + list.push_back({ "bar", 2 }); + + output << list; + EXPECT_EQ("[ foo[-1], bar[2] ]", output.str()); +} \ No newline at end of file