Skip to content

Commit

Permalink
Merge pull request ceph#3366 from ceph/wip-formatter
Browse files Browse the repository at this point in the history
formatter: improve pretty output, rename factory method

Reviewed-by: Dan Mick <[email protected]>
  • Loading branch information
dmick committed Jan 14, 2015
2 parents 3f03a7b + 9264d25 commit 9542416
Show file tree
Hide file tree
Showing 23 changed files with 60 additions and 80 deletions.
4 changes: 1 addition & 3 deletions src/client/Client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,7 @@ Client::CommandHook::CommandHook(Client *client) :
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")
Expand Down
22 changes: 14 additions & 8 deletions src/common/Formatter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ Formatter::Formatter() { }

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);
Expand Down Expand Up @@ -123,6 +123,8 @@ void JSONFormatter::flush(std::ostream& os)
{
finish_pending_string();
os << m_ss.str();
if (m_pretty)
os << "\n";
m_ss.clear();
m_ss.str("");
}
Expand All @@ -146,7 +148,7 @@ void JSONFormatter::print_comma(json_formatter_stack_entry_d& entry)
} else {
m_ss << ",";
}
} else if (entry.is_array && m_pretty) {
} else if (m_pretty) {
m_ss << "\n";
for (unsigned i = 1; i < m_stack.size(); i++)
m_ss << " ";
Expand All @@ -173,10 +175,7 @@ void JSONFormatter::print_name(const char *name)
print_comma(entry);
if (!entry.is_array) {
if (m_pretty) {
if (entry.size)
m_ss << " ";
else
m_ss << " ";
m_ss << " ";
}
m_ss << "\"" << name << "\"";
if (m_pretty)
Expand Down Expand Up @@ -230,6 +229,11 @@ void JSONFormatter::close_section()
finish_pending_string();

struct json_formatter_stack_entry_d& entry = m_stack.back();
if (m_pretty && entry.size) {
m_ss << "\n";
for (unsigned i = 1; i < m_stack.size(); i++)
m_ss << " ";
}
m_ss << (entry.is_array ? ']' : '}');
m_stack.pop_back();
}
Expand Down Expand Up @@ -312,6 +316,8 @@ void XMLFormatter::flush(std::ostream& os)
{
finish_pending_string();
os << m_ss.str();
if (m_pretty)
os << "\n";
m_ss.clear();
m_ss.str("");
}
Expand Down
10 changes: 8 additions & 2 deletions src/common/Formatter.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// -*- 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

Expand Down Expand Up @@ -25,6 +27,12 @@ namespace ceph {

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();

Expand Down Expand Up @@ -72,8 +80,6 @@ namespace ceph {
}
};

Formatter *new_formatter(const std::string &type);

class JSONFormatter : public Formatter {
public:
JSONFormatter(bool p = false);
Expand Down
4 changes: 1 addition & 3 deletions src/common/admin_socket.cc
Original file line number Diff line number Diff line change
Expand Up @@ -448,9 +448,7 @@ class HelpHook : public AdminSocketHook {
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();
Expand Down
4 changes: 1 addition & 3 deletions src/common/ceph_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,7 @@ class CephContextHook : public AdminSocketHook {
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") {
Expand Down
4 changes: 1 addition & 3 deletions src/mds/MDS.cc
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,7 @@ bool MDS::asok_command(string command, cmdmap_t& cmdmap, string format,
{
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();
Expand Down
4 changes: 2 additions & 2 deletions src/mon/AuthMonitor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ bool AuthMonitor::preprocess_command(MMonCommand *m)

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;
Expand Down Expand Up @@ -685,7 +685,7 @@ bool AuthMonitor::prepare_command(MMonCommand *m)

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) {
Expand Down
2 changes: 1 addition & 1 deletion src/mon/MDSMonitor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,7 @@ bool MDSMonitor::preprocess_command(MMonCommand *m)
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) {
Expand Down
8 changes: 4 additions & 4 deletions src/mon/Monitor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ void Monitor::do_admin_command(string command, cmdmap_t& cmdmap, string format,
{
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();
Expand Down Expand Up @@ -2371,7 +2371,7 @@ void Monitor::handle_command(MMonCommand *m)
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;
Expand All @@ -2386,7 +2386,7 @@ void Monitor::handle_command(MMonCommand *m)

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];
Expand Down Expand Up @@ -2587,7 +2587,7 @@ void Monitor::handle_command(MMonCommand *m)

// 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());
Expand Down
2 changes: 1 addition & 1 deletion src/mon/MonmapMonitor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ bool MonmapMonitor::preprocess_command(MMonCommand *m)
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());
Expand Down
33 changes: 8 additions & 25 deletions src/mon/OSDMonitor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2334,7 +2334,7 @@ bool OSDMonitor::preprocess_command(MMonCommand *m)

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);
Expand Down Expand Up @@ -2479,10 +2479,7 @@ bool OSDMonitor::preprocess_command(MMonCommand *m)
}
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);
Expand All @@ -2508,9 +2505,7 @@ bool OSDMonitor::preprocess_command(MMonCommand *m)
}
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)
Expand Down Expand Up @@ -3007,10 +3002,7 @@ bool OSDMonitor::preprocess_command(MMonCommand *m)
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();
Expand All @@ -3023,10 +3015,7 @@ bool OSDMonitor::preprocess_command(MMonCommand *m)
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());
Expand All @@ -3047,10 +3036,7 @@ bool OSDMonitor::preprocess_command(MMonCommand *m)
} 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();
Expand All @@ -3061,10 +3047,7 @@ bool OSDMonitor::preprocess_command(MMonCommand *m)
} 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();
Expand Down Expand Up @@ -4102,7 +4085,7 @@ bool OSDMonitor::prepare_command_impl(MMonCommand *m,

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);
Expand Down
2 changes: 1 addition & 1 deletion src/mon/PGMonitor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1482,7 +1482,7 @@ bool PGMonitor::preprocess_command(MMonCommand *m)

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) {
Expand Down
2 changes: 1 addition & 1 deletion src/os/MemStore.cc
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ int MemStore::_save()

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();
Expand Down
8 changes: 3 additions & 5 deletions src/osd/OSD.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1626,9 +1626,7 @@ class OSDSocketHook : public AdminSocketHook {
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;
Expand Down Expand Up @@ -4897,7 +4895,7 @@ void OSD::do_command(Connection *con, ceph_tid_t tid, vector<string>& cmd, buffe
}

cmd_getval(cct, cmdmap, "format", format);
f.reset(new_formatter(format));
f.reset(Formatter::create(format));

if (prefix == "version") {
if (f) {
Expand Down Expand Up @@ -8220,7 +8218,7 @@ void OSD::ShardedOpWQ::_process(uint32_t thread_index, heartbeat_handle_d *hb )
}

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();
Expand Down
7 changes: 2 additions & 5 deletions src/osd/ReplicatedPG.cc
Original file line number Diff line number Diff line change
Expand Up @@ -603,10 +603,7 @@ int ReplicatedPG::do_command(cmdmap_t cmdmap, ostream& ss,
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);
Expand Down Expand Up @@ -12268,7 +12265,7 @@ bool ReplicatedPG::agent_maybe_evict(ObjectContextRef& obc)
<< ", 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();
Expand Down
4 changes: 1 addition & 3 deletions src/osdc/Objecter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4225,9 +4225,7 @@ Objecter::RequestStateHook::RequestStateHook(Objecter *objecter) :
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);
Expand Down
2 changes: 1 addition & 1 deletion src/test/bench/small_io_bench_fs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ struct MorePrinting : public DetailedStatCollector::AdditionalPrinting {
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;
Expand Down
2 changes: 1 addition & 1 deletion src/test/common/get_command_descriptions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ static void usage(ostream &out)
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());
Expand Down
Loading

0 comments on commit 9542416

Please sign in to comment.