Factor out base/indented_stream.h

This commit is contained in:
Joe Thornber 2014-01-31 13:43:39 +00:00
parent 8531a2befa
commit 344f4b1e08
4 changed files with 109 additions and 104 deletions

48
base/indented_stream.h Normal file
View File

@ -0,0 +1,48 @@
#ifndef BASE_INDENTED_STREAM_H
#define BASE_INDENTED_STREAM_H
#include <iostream>
//----------------------------------------------------------------
namespace {
class indented_stream {
public:
indented_stream(std::ostream &out)
: out_(out),
indent_(0) {
}
void indent() {
for (unsigned i = 0; i < indent_ * 2; i++)
out_ << ' ';
}
void inc() {
indent_++;
}
void dec() {
indent_--;
}
template <typename T>
indented_stream &operator <<(T const &t) {
out_ << t;
return *this;
}
indented_stream &operator <<(std::ostream &(*fp)(std::ostream &)) {
out_ << fp;
return *this;
}
private:
std::ostream &out_;
unsigned indent_;
};
}
//----------------------------------------------------------------
#endif

View File

@ -1,4 +1,5 @@
#include "base/base64.h" #include "base/base64.h"
#include "base/indented_stream.h"
#include "caching/xml_format.h" #include "caching/xml_format.h"
#include <boost/lexical_cast.hpp> #include <boost/lexical_cast.hpp>
@ -18,8 +19,7 @@ namespace {
class xml_emitter : public emitter { class xml_emitter : public emitter {
public: public:
xml_emitter(ostream &out) xml_emitter(ostream &out)
: out_(out), : out_(out) {
indent_(0) {
} }
void begin_superblock(std::string const &uuid, void begin_superblock(std::string const &uuid,
@ -27,37 +27,37 @@ namespace {
block_address nr_cache_blocks, block_address nr_cache_blocks,
std::string const &policy, std::string const &policy,
size_t hint_width) { size_t hint_width) {
indent(); out_.indent();
out_ << "<superblock uuid=\"" << uuid << "\"" out_ << "<superblock uuid=\"" << uuid << "\""
<< " block_size=\"" << block_size << "\"" << " block_size=\"" << block_size << "\""
<< " nr_cache_blocks=\"" << nr_cache_blocks << "\"" << " nr_cache_blocks=\"" << nr_cache_blocks << "\""
<< " policy=\"" << policy << "\"" << " policy=\"" << policy << "\""
<< " hint_width=\"" << hint_width << "\">" << endl; << " hint_width=\"" << hint_width << "\">" << endl;
inc(); out_.inc();
} }
virtual void end_superblock() { virtual void end_superblock() {
dec(); out_.dec();
indent(); out_.indent();
out_ << "</superblock>" << endl; out_ << "</superblock>" << endl;
} }
virtual void begin_mappings() { virtual void begin_mappings() {
indent(); out_.indent();
out_ << "<mappings>" << endl; out_ << "<mappings>" << endl;
inc(); out_.inc();
} }
virtual void end_mappings() { virtual void end_mappings() {
dec(); out_.dec();
indent(); out_.indent();
out_ << "</mappings>" << endl; out_ << "</mappings>" << endl;
} }
virtual void mapping(block_address cblock, virtual void mapping(block_address cblock,
block_address oblock, block_address oblock,
bool dirty) { bool dirty) {
indent(); out_.indent();
out_ << "<mapping" out_ << "<mapping"
<< " cache_block=\"" << cblock << "\"" << " cache_block=\"" << cblock << "\""
<< " origin_block=\"" << oblock << "\"" << " origin_block=\"" << oblock << "\""
@ -66,14 +66,14 @@ namespace {
} }
virtual void begin_hints() { virtual void begin_hints() {
indent(); out_.indent();
out_ << "<hints>" << endl; out_ << "<hints>" << endl;
inc(); out_.inc();
} }
virtual void end_hints() { virtual void end_hints() {
dec(); out_.dec();
indent(); out_.indent();
out_ << "</hints>" << endl; out_ << "</hints>" << endl;
} }
@ -81,7 +81,7 @@ namespace {
vector<unsigned char> const &data) { vector<unsigned char> const &data) {
using namespace base; using namespace base;
indent(); out_.indent();
out_ << "<hint" out_ << "<hint"
<< " cache_block=\"" << cblock << "\"" << " cache_block=\"" << cblock << "\""
<< " data=\"" << base64_encode(data) << "\"" << " data=\"" << base64_encode(data) << "\""
@ -89,19 +89,19 @@ namespace {
} }
virtual void begin_discards() { virtual void begin_discards() {
indent(); out_.indent();
out_ << "<discards>" << endl; out_ << "<discards>" << endl;
inc(); out_.inc();
} }
virtual void end_discards() { virtual void end_discards() {
dec(); out_.dec();
indent(); out_.indent();
out_ << "</discards>" << endl; out_ << "</discards>" << endl;
} }
virtual void discard(block_address dblock_b, block_address dblock_e) { virtual void discard(block_address dblock_b, block_address dblock_e) {
indent(); out_.indent();
out_ << "<discard dbegin=\"" << dblock_b << "\"" out_ << "<discard dbegin=\"" << dblock_b << "\""
<< " dend=\"" << dblock_e << "\"/>" << endl; << " dend=\"" << dblock_e << "\"/>" << endl;
} }
@ -111,22 +111,7 @@ namespace {
return v ? "true" : "false"; return v ? "true" : "false";
} }
// FIXME: factor out a common class with the thin_provisioning emitter indented_stream out_;
void indent() {
for (unsigned i = 0; i < indent_ * 2; i++)
out_ << ' ';
}
void inc() {
indent_++;
}
void dec() {
indent_--;
}
ostream &out_;
unsigned indent_;
}; };
//-------------------------------- //--------------------------------

View File

@ -1,5 +1,7 @@
#include "era/xml_format.h" #include "era/xml_format.h"
#include "base/indented_stream.h"
using namespace boost; using namespace boost;
using namespace era; using namespace era;
using namespace persistent_data; using namespace persistent_data;
@ -11,82 +13,67 @@ namespace {
class xml_emitter : public emitter { class xml_emitter : public emitter {
public: public:
xml_emitter(ostream &out) xml_emitter(ostream &out)
: out_(out), : out_(out) {
indent_(0) {
} }
void begin_superblock(std::string const &uuid, void begin_superblock(std::string const &uuid,
uint32_t block_size, uint32_t block_size,
pd::block_address nr_blocks, pd::block_address nr_blocks,
uint32_t current_era) { uint32_t current_era) {
indent(); out_.indent();
out_ << "<superblock uuid=\"" << uuid << "\"" out_ << "<superblock uuid=\"" << uuid << "\""
<< " block_size=\"" << block_size << "\"" << " block_size=\"" << block_size << "\""
<< " nr_blocks=\"" << nr_blocks << "\"" << " nr_blocks=\"" << nr_blocks << "\""
<< " current_era=\"" << current_era << "\">" << endl; << " current_era=\"" << current_era << "\">";
inc(); out_ << endl;
out_.inc();
} }
void end_superblock() { void end_superblock() {
dec(); out_.dec();
indent(); out_.indent();
out_ << "</superblock>" << endl; out_ << "</superblock>" << endl;
} }
void begin_writeset(uint32_t era, uint32_t nr_bits) { void begin_writeset(uint32_t era, uint32_t nr_bits) {
indent(); out_.indent();
out_ << "<writeset era=\"" << era << "\"" out_ << "<writeset era=\"" << era << "\""
<< " nr_bits=\"" << nr_bits << "\">" << endl; << " nr_bits=\"" << nr_bits << "\">" << endl;
inc(); out_.inc();
} }
void writeset_bit(uint32_t bit, bool value) { void writeset_bit(uint32_t bit, bool value) {
indent(); out_.indent();
// FIXME: collect all the bits, then uuencode // FIXME: collect all the bits, then uuencode
out_ << "<bit bit=\"" << bit << "\" value=\"" << value << "\">" << endl; out_ << "<bit bit=\"" << bit << "\" value=\"" << value << "\">" << endl;
} }
void end_writeset() { void end_writeset() {
dec(); out_.dec();
indent(); out_.indent();
out_ << "</writeset>" << endl; out_ << "</writeset>" << endl;
} }
void begin_era_array() { void begin_era_array() {
indent(); out_.indent();
out_ << "<era_array>" << endl; out_ << "<era_array>" << endl;
inc(); out_.inc();
} }
void era(pd::block_address block, uint32_t era) { void era(pd::block_address block, uint32_t era) {
indent(); out_.indent();
out_ << "<era block=\"" << block out_ << "<era block=\"" << block
<< "\" era=\"" << era << "\">" << endl; << "\" era=\"" << era << "\">" << endl;
} }
void end_era_array() { void end_era_array() {
dec(); out_.dec();
indent(); out_.indent();
out_ << "</era_array>" << endl; out_ << "</era_array>" << endl;
} }
private: private:
// FIXME: factor out a common class with the thin_provisioning emitter indented_stream out_;
void indent() {
for (unsigned i = 0; i < indent_ * 2; i++)
out_ << ' ';
}
void inc() {
indent_++;
}
void dec() {
indent_--;
}
ostream &out_;
unsigned indent_;
}; };
} }

View File

@ -17,6 +17,7 @@
// <http://www.gnu.org/licenses/>. // <http://www.gnu.org/licenses/>.
#include "xml_format.h" #include "xml_format.h"
#include "base/indented_stream.h"
#include <boost/lexical_cast.hpp> #include <boost/lexical_cast.hpp>
#include <boost/optional.hpp> #include <boost/optional.hpp>
@ -41,8 +42,7 @@ namespace {
class xml_emitter : public emitter { class xml_emitter : public emitter {
public: public:
xml_emitter(ostream &out) xml_emitter(ostream &out)
: out_(out), : out_(out) {
indent_(0) {
} }
void begin_superblock(string const &uuid, void begin_superblock(string const &uuid,
@ -51,7 +51,7 @@ namespace {
uint32_t data_block_size, uint32_t data_block_size,
uint64_t nr_data_blocks, uint64_t nr_data_blocks,
boost::optional<uint64_t> metadata_snap) { boost::optional<uint64_t> metadata_snap) {
indent(); out_.indent();
out_ << "<superblock uuid=\"" << uuid << "\"" out_ << "<superblock uuid=\"" << uuid << "\""
<< " time=\"" << time << "\"" << " time=\"" << time << "\""
<< " transaction=\"" << trans_id << "\"" << " transaction=\"" << trans_id << "\""
@ -61,14 +61,13 @@ namespace {
if (metadata_snap) if (metadata_snap)
out_ << "\" metadata_snap=\"" << *metadata_snap; out_ << "\" metadata_snap=\"" << *metadata_snap;
out_ << "\">" out_ << "\">" << endl;
<< endl; out_.inc();
inc();
} }
void end_superblock() { void end_superblock() {
dec(); out_.dec();
indent(); out_.indent();
out_ << "</superblock>" << endl; out_ << "</superblock>" << endl;
} }
@ -77,40 +76,40 @@ namespace {
uint64_t trans_id, uint64_t trans_id,
uint64_t creation_time, uint64_t creation_time,
uint64_t snap_time) { uint64_t snap_time) {
indent(); out_.indent();
out_ << "<device dev_id=\"" << dev_id << "\"" out_ << "<device dev_id=\"" << dev_id << "\""
<< " mapped_blocks=\"" << mapped_blocks << "\"" << " mapped_blocks=\"" << mapped_blocks << "\""
<< " transaction=\"" << trans_id << "\"" << " transaction=\"" << trans_id << "\""
<< " creation_time=\"" << creation_time << "\"" << " creation_time=\"" << creation_time << "\""
<< " snap_time=\"" << snap_time << "\">" << endl; << " snap_time=\"" << snap_time << "\">" << endl;
inc(); out_.inc();
} }
void end_device() { void end_device() {
dec(); out_.dec();
indent(); out_.indent();
out_ << "</device>" << endl; out_ << "</device>" << endl;
} }
void begin_named_mapping(string const &name) { void begin_named_mapping(string const &name) {
indent(); out_.indent();
out_ << "<named_mapping>" << endl; out_ << "<named_mapping>" << endl;
inc(); out_.inc();
} }
void end_named_mapping() { void end_named_mapping() {
dec(); out_.dec();
indent(); out_.indent();
out_ << "</named_mapping>" << endl; out_ << "</named_mapping>" << endl;
} }
void identifier(string const &name) { void identifier(string const &name) {
indent(); out_.indent();
out_ << "<identifier name=\"" << name << "\"/>" << endl; out_ << "<identifier name=\"" << name << "\"/>" << endl;
} }
void range_map(uint64_t origin_begin, uint64_t data_begin, uint32_t time, uint64_t len) { void range_map(uint64_t origin_begin, uint64_t data_begin, uint32_t time, uint64_t len) {
indent(); out_.indent();
out_ << "<range_mapping origin_begin=\"" << origin_begin << "\"" out_ << "<range_mapping origin_begin=\"" << origin_begin << "\""
<< " data_begin=\"" << data_begin << "\"" << " data_begin=\"" << data_begin << "\""
@ -120,7 +119,7 @@ namespace {
} }
void single_map(uint64_t origin_block, uint64_t data_block, uint32_t time) { void single_map(uint64_t origin_block, uint64_t data_block, uint32_t time) {
indent(); out_.indent();
out_ << "<single_mapping origin_block=\"" << origin_block << "\"" out_ << "<single_mapping origin_block=\"" << origin_block << "\""
<< " data_block=\"" << data_block << "\"" << " data_block=\"" << data_block << "\""
@ -129,21 +128,7 @@ namespace {
} }
private: private:
void indent() { indented_stream out_;
for (unsigned i = 0; i < indent_ * 2; i++)
out_ << ' ';
}
void inc() {
indent_++;
}
void dec() {
indent_--;
}
ostream &out_;
unsigned indent_;
}; };
//------------------------------------------------ //------------------------------------------------