Also make the default formatter selection more explicit.
Signed-off-by: Sage Weil <sage@redhat.com>
bool Client::CommandHook::call(std::string command, cmdmap_t& cmdmap,
std::string format, bufferlist& out)
{
- Formatter *f = new_formatter(format);
- if (!f)
- f = new_formatter("json-pretty");
+ Formatter *f = Formatter::create(format);
f->open_object_section("result");
m_client->client_lock.Lock();
if (command == "mds_requests")
Formatter::~Formatter() { }
-Formatter *
-new_formatter(const std::string &type)
+Formatter *Formatter::create(const std::string &type,
+ const std::string& default_type)
{
std::string mytype = type;
if (mytype == "")
- mytype = "json-pretty";
+ mytype = default_type;
if (mytype == "json")
return new JSONFormatter(false);
+// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
+// vim: ts=8 sw=2 smarttab
#ifndef CEPH_FORMATTER_H
#define CEPH_FORMATTER_H
class Formatter {
public:
+ static Formatter *create(const std::string& type,
+ const std::string& default_type);
+ static Formatter *create(const std::string& type) {
+ return create(type, "json-pretty");
+ }
+
Formatter();
virtual ~Formatter();
}
};
- Formatter *new_formatter(const std::string &type);
-
class JSONFormatter : public Formatter {
public:
JSONFormatter(bool p = false);
public:
HelpHook(AdminSocket *as) : m_as(as) {}
bool call(string command, cmdmap_t &cmdmap, string format, bufferlist& out) {
- Formatter *f = new_formatter(format);
- if (!f)
- f = new_formatter("json-pretty");
+ Formatter *f = Formatter::create(format);
f->open_object_section("help");
for (map<string,string>::iterator p = m_as->m_help.begin();
p != m_as->m_help.end();
void CephContext::do_command(std::string command, cmdmap_t& cmdmap,
std::string format, bufferlist *out)
{
- Formatter *f = new_formatter(format);
- if (!f)
- f = new_formatter("json-pretty");
+ Formatter *f = Formatter::create(format);
stringstream ss;
for (cmdmap_t::iterator it = cmdmap.begin(); it != cmdmap.end(); ++it) {
if (it->first != "prefix") {
{
dout(1) << "asok_command: " << command << " (starting...)" << dendl;
- Formatter *f = new_formatter(format);
- if (!f)
- f = new_formatter("json-pretty");
+ Formatter *f = Formatter::create(format);
if (command == "status") {
const OSDMap *osdmap = objecter->get_osdmap_read();
string format;
cmd_getval(g_ceph_context, cmdmap, "format", format, string("plain"));
- boost::scoped_ptr<Formatter> f(new_formatter(format));
+ boost::scoped_ptr<Formatter> f(Formatter::create(format));
if (prefix == "auth export") {
KeyRing keyring;
string format;
cmd_getval(g_ceph_context, cmdmap, "format", format, string("plain"));
- boost::scoped_ptr<Formatter> f(new_formatter(format));
+ boost::scoped_ptr<Formatter> f(Formatter::create(format));
MonSession *session = m->get_session();
if (!session) {
cmd_getval(g_ceph_context, cmdmap, "prefix", prefix);
string format;
cmd_getval(g_ceph_context, cmdmap, "format", format, string("plain"));
- boost::scoped_ptr<Formatter> f(new_formatter(format));
+ boost::scoped_ptr<Formatter> f(Formatter::create(format));
MonSession *session = m->get_session();
if (!session) {
{
Mutex::Locker l(lock);
- boost::scoped_ptr<Formatter> f(new_formatter(format));
+ boost::scoped_ptr<Formatter> f(Formatter::create(format));
string args;
for (cmdmap_t::iterator p = cmdmap.begin();
cmd_getval(g_ceph_context, cmdmap, "prefix", prefix);
if (prefix == "get_command_descriptions") {
bufferlist rdata;
- Formatter *f = new_formatter("json");
+ Formatter *f = Formatter::create("json");
format_command_descriptions(leader_supported_mon_commands,
leader_supported_mon_commands_size, f, &rdata);
delete f;
string format;
cmd_getval(g_ceph_context, cmdmap, "format", format, string("plain"));
- boost::scoped_ptr<Formatter> f(new_formatter(format));
+ boost::scoped_ptr<Formatter> f(Formatter::create(format));
get_str_vec(prefix, fullcmd);
module = fullcmd[0];
// this must be formatted, in its current form
if (!f)
- f.reset(new_formatter("json-pretty"));
+ f.reset(Formatter::create("json-pretty"));
f->open_object_section("report");
f->dump_stream("cluster_fingerprint") << fingerprint;
f->dump_string("version", ceph_version_to_str());
string format;
cmd_getval(g_ceph_context, cmdmap, "format", format, string("plain"));
stringstream ds;
- boost::scoped_ptr<Formatter> f(new_formatter(format));
+ boost::scoped_ptr<Formatter> f(Formatter::create(format));
if (f) {
f->open_object_section("monmap");
p->dump(f.get());
string format;
cmd_getval(g_ceph_context, cmdmap, "format", format, string("plain"));
- boost::scoped_ptr<Formatter> f(new_formatter(format));
+ boost::scoped_ptr<Formatter> f(Formatter::create(format));
if (prefix == "osd stat") {
osdmap.print_summary(f.get(), ds);
}
string format;
cmd_getval(g_ceph_context, cmdmap, "format", format, string("json-pretty"));
- boost::scoped_ptr<Formatter> f(new_formatter(format));
- if (!f)
- f.reset(new_formatter("json-pretty"));
-
+ boost::scoped_ptr<Formatter> f(Formatter::create(format));
f->open_object_section("osd_location");
f->dump_int("osd", osd);
f->dump_stream("ip") << osdmap.get_addr(osd);
}
string format;
cmd_getval(g_ceph_context, cmdmap, "format", format, string("json-pretty"));
- boost::scoped_ptr<Formatter> f(new_formatter(format));
- if (!f)
- f.reset(new_formatter("json-pretty"));
+ boost::scoped_ptr<Formatter> f(Formatter::create(format));
f->open_object_section("osd_metadata");
r = dump_osd_metadata(osd, f.get(), &ss);
if (r < 0)
prefix == "osd crush rule ls") {
string format;
cmd_getval(g_ceph_context, cmdmap, "format", format, string("json-pretty"));
- Formatter *fp = new_formatter(format);
- if (!fp)
- fp = new_formatter("json-pretty");
- boost::scoped_ptr<Formatter> f(fp);
+ boost::scoped_ptr<Formatter> f(Formatter::create(format));
f->open_array_section("rules");
osdmap.crush->list_rules(f.get());
f->close_section();
cmd_getval(g_ceph_context, cmdmap, "name", name);
string format;
cmd_getval(g_ceph_context, cmdmap, "format", format, string("json-pretty"));
- Formatter *fp = new_formatter(format);
- if (!fp)
- fp = new_formatter("json-pretty");
- boost::scoped_ptr<Formatter> f(fp);
+ boost::scoped_ptr<Formatter> f(Formatter::create(format));
if (name == "") {
f->open_array_section("rules");
osdmap.crush->dump_rules(f.get());
} else if (prefix == "osd crush dump") {
string format;
cmd_getval(g_ceph_context, cmdmap, "format", format, string("json-pretty"));
- Formatter *fp = new_formatter(format);
- if (!fp)
- fp = new_formatter("json-pretty");
- boost::scoped_ptr<Formatter> f(fp);
+ boost::scoped_ptr<Formatter> f(Formatter::create(format));
f->open_object_section("crush_map");
osdmap.crush->dump(f.get());
f->close_section();
} else if (prefix == "osd crush show-tunables") {
string format;
cmd_getval(g_ceph_context, cmdmap, "format", format, string("json-pretty"));
- Formatter *fp = new_formatter(format);
- if (!fp)
- fp = new_formatter("json-pretty");
- boost::scoped_ptr<Formatter> f(fp);
+ boost::scoped_ptr<Formatter> f(Formatter::create(format));
f->open_object_section("crush_map_tunables");
osdmap.crush->dump_tunables(f.get());
f->close_section();
string format;
cmd_getval(g_ceph_context, cmdmap, "format", format, string("plain"));
- boost::scoped_ptr<Formatter> f(new_formatter(format));
+ boost::scoped_ptr<Formatter> f(Formatter::create(format));
string prefix;
cmd_getval(g_ceph_context, cmdmap, "prefix", prefix);
string format;
cmd_getval(g_ceph_context, cmdmap, "format", format, string("plain"));
- boost::scoped_ptr<Formatter> f(new_formatter(format));
+ boost::scoped_ptr<Formatter> f(Formatter::create(format));
if (prefix == "pg stat") {
if (f) {
void MemStore::dump_all()
{
- Formatter *f = new_formatter("json-pretty");
+ Formatter *f = Formatter::create("json-pretty");
f->open_object_section("store");
dump(f);
f->close_section();
bool OSD::asok_command(string command, cmdmap_t& cmdmap, string format,
ostream& ss)
{
- Formatter *f = new_formatter(format);
- if (!f)
- f = new_formatter("json-pretty");
+ Formatter *f = Formatter::create(format);
if (command == "status") {
f->open_object_section("status");
f->dump_stream("cluster_fsid") << superblock.cluster_fsid;
}
cmd_getval(cct, cmdmap, "format", format);
- f.reset(new_formatter(format));
+ f.reset(Formatter::create(format));
if (prefix == "version") {
if (f) {
}
lgeneric_subdout(osd->cct, osd, 30) << "dequeue status: ";
- Formatter *f = new_formatter("json");
+ Formatter *f = Formatter::create("json");
f->open_object_section("q");
dump(f);
f->close_section();
string format;
cmd_getval(cct, cmdmap, "format", format);
- boost::scoped_ptr<Formatter> f(new_formatter(format));
- // demand that we have a formatter
- if (!f)
- f.reset(new_formatter("json"));
+ boost::scoped_ptr<Formatter> f(Formatter::create(format, "json"));
string command;
cmd_getval(cct, cmdmap, "cmd", command);
<< ", evict_effort " << agent_state->evict_effort
<< dendl;
dout(30) << "agent_state:\n";
- Formatter *f = new_formatter("");
+ Formatter *f = Formatter::create("");
f->open_object_section("agent_state");
agent_state->dump(f);
f->close_section();
bool Objecter::RequestStateHook::call(std::string command, cmdmap_t& cmdmap,
std::string format, bufferlist& out)
{
- Formatter *f = new_formatter(format);
- if (!f)
- f = new_formatter("json-pretty");
+ Formatter *f = Formatter::create(format);
RWLock::RLocker rl(m_objecter->rwlock);
m_objecter->dump_requests(f);
f->flush(out);
MorePrinting(CephContext *cct) : cct(cct) {}
void operator()(std::ostream *out) {
bufferlist bl;
- Formatter *f = new_formatter("json-pretty");
+ Formatter *f = Formatter::create("json-pretty");
cct->get_perfcounters_collection()->dump_formatted(f, 0);
f->flush(bl);
delete f;
static void json_print(const MonCommand *mon_commands, int size)
{
bufferlist rdata;
- Formatter *f = new_formatter("json");
+ Formatter *f = Formatter::create("json");
Monitor::format_command_descriptions(mon_commands, size, f, &rdata);
delete f;
string data(rdata.c_str(), rdata.length());
void dump(const SloppyCRCMap& scm)
{
- Formatter *f = new_formatter("json-pretty");
+ Formatter *f = Formatter::create("json-pretty");
f->open_object_section("map");
scm.dump(f);
f->close_section();
TEST(tableformatter, stream)
{
std::stringstream sout;
- TableFormatter* formatter = (TableFormatter*) new_formatter("table");
+ TableFormatter* formatter = (TableFormatter*) Formatter::create("table");
formatter->dump_stream("integer") << 10;
formatter->dump_stream("float") << 10.0;
formatter->dump_stream("string") << "string";
TEST(tableformatter, multiline_keyval)
{
std::stringstream sout;
- TableFormatter* formatter = (TableFormatter*) new_formatter("table-kv");
+ TableFormatter* formatter = (TableFormatter*) Formatter::create("table-kv");
formatter->dump_int("integer", 10);
formatter->dump_float("float", 10.0);
formatter->dump_string("string", "string");
// no ruleset by default
{
- Formatter *f = new_formatter("json-pretty");
+ Formatter *f = Formatter::create("json-pretty");
c->dump_rules(f);
stringstream ss;
f->flush(ss);
EXPECT_EQ(0, ruleset);
{
- Formatter *f = new_formatter("xml");
+ Formatter *f = Formatter::create("xml");
c->dump_rules(f);
stringstream ss;
f->flush(ss);
}
{
- Formatter *f = new_formatter("xml");
+ Formatter *f = Formatter::create("xml");
c->dump_rule(ruleset, f);
stringstream ss;
f->flush(ss);
c->set_rule_name(ruleno, "data");
if (false) {
- Formatter *f = new_formatter("json-pretty");
+ Formatter *f = Formatter::create("json-pretty");
f->open_object_section("crush_map");
c->dump(f);
f->close_section();
/*
osdmap.print(cout);
- Formatter *f = new_formatter("json-pretty");
+ Formatter *f = Formatter::create("json-pretty");
f->open_object_section("CRUSH");
osdmap.crush->dump(f);
f->close_section();