1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2011 New Dream Network
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
15 #define LARGE_SIZE 1024
17 #include "HTMLFormatter.h"
18 #include "common/escape.h"
19 #include "include/buffer.h"
22 #include <boost/format.hpp>
24 // -----------------------
28 * FormatterAttrs(const char *attr, ...)
30 * Requires a list of attrs followed by NULL. The attrs should be char *
31 * pairs, first one is the name, second one is the value. E.g.,
33 * FormatterAttrs("name1", "value1", "name2", "value2", NULL);
35 FormatterAttrs::FormatterAttrs(const char *attr, ...)
41 const char *val = va_arg(ap, char *);
45 attrs.push_back(make_pair(std::string(s), std::string(val)));
46 s = va_arg(ap, char *);
51 Formatter::Formatter() { }
53 Formatter::~Formatter() { }
55 Formatter *Formatter::create(const std::string &type,
56 const std::string& default_type,
57 const std::string& fallback)
59 std::string mytype = type;
61 mytype = default_type;
64 return new JSONFormatter(false);
65 else if (mytype == "json-pretty")
66 return new JSONFormatter(true);
67 else if (mytype == "xml")
68 return new XMLFormatter(false);
69 else if (mytype == "xml-pretty")
70 return new XMLFormatter(true);
71 else if (mytype == "table")
72 return new TableFormatter();
73 else if (mytype == "table-kv")
74 return new TableFormatter(true);
75 else if (mytype == "html")
76 return new HTMLFormatter(false);
77 else if (mytype == "html-pretty")
78 return new HTMLFormatter(true);
79 else if (fallback != "")
80 return create(fallback, "", "");
82 return (Formatter *) NULL;
86 void Formatter::flush(bufferlist &bl)
93 void Formatter::dump_format(const char *name, const char *fmt, ...)
97 dump_format_va(name, NULL, true, fmt, ap);
101 void Formatter::dump_format_ns(const char *name, const char *ns, const char *fmt, ...)
105 dump_format_va(name, ns, true, fmt, ap);
110 void Formatter::dump_format_unquoted(const char *name, const char *fmt, ...)
114 dump_format_va(name, NULL, false, fmt, ap);
118 // -----------------------
120 JSONFormatter::JSONFormatter(bool p)
121 : m_pretty(p), m_is_pending_string(false)
126 void JSONFormatter::flush(std::ostream& os)
128 finish_pending_string();
130 if (m_line_break_enabled)
136 void JSONFormatter::reset()
141 m_pending_string.clear();
142 m_pending_string.str("");
145 void JSONFormatter::print_comma(json_formatter_stack_entry_d& entry)
150 for (unsigned i = 1; i < m_stack.size(); i++)
155 } else if (m_pretty) {
157 for (unsigned i = 1; i < m_stack.size(); i++)
160 if (m_pretty && entry.is_array)
164 void JSONFormatter::print_quoted_string(const std::string& s)
166 int len = escape_json_attr_len(s.c_str(), s.size());
168 escape_json_attr(s.c_str(), s.size(), escaped);
169 m_ss << '\"' << escaped << '\"';
172 void JSONFormatter::print_name(const char *name)
174 finish_pending_string();
177 struct json_formatter_stack_entry_d& entry = m_stack.back();
179 if (!entry.is_array) {
183 m_ss << "\"" << name << "\"";
192 void JSONFormatter::open_section(const char *name, bool is_array)
200 json_formatter_stack_entry_d n;
201 n.is_array = is_array;
202 m_stack.push_back(n);
205 void JSONFormatter::open_array_section(const char *name)
207 open_section(name, true);
210 void JSONFormatter::open_array_section_in_ns(const char *name, const char *ns)
212 std::ostringstream oss;
213 oss << name << " " << ns;
214 open_section(oss.str().c_str(), true);
217 void JSONFormatter::open_object_section(const char *name)
219 open_section(name, false);
222 void JSONFormatter::open_object_section_in_ns(const char *name, const char *ns)
224 std::ostringstream oss;
225 oss << name << " " << ns;
226 open_section(oss.str().c_str(), false);
229 void JSONFormatter::close_section()
231 assert(!m_stack.empty());
232 finish_pending_string();
234 struct json_formatter_stack_entry_d& entry = m_stack.back();
235 if (m_pretty && entry.size) {
237 for (unsigned i = 1; i < m_stack.size(); i++)
240 m_ss << (entry.is_array ? ']' : '}');
242 if (m_pretty && m_stack.empty())
246 void JSONFormatter::finish_pending_string()
248 if (m_is_pending_string) {
249 print_quoted_string(m_pending_string.str());
250 m_pending_string.str(std::string());
251 m_is_pending_string = false;
255 void JSONFormatter::dump_unsigned(const char *name, uint64_t u)
261 void JSONFormatter::dump_int(const char *name, int64_t s)
267 void JSONFormatter::dump_float(const char *name, double d)
271 snprintf(foo, sizeof(foo), "%lf", d);
275 void JSONFormatter::dump_string(const char *name, const std::string& s)
278 print_quoted_string(s);
281 std::ostream& JSONFormatter::dump_stream(const char *name)
284 m_is_pending_string = true;
285 return m_pending_string;
288 void JSONFormatter::dump_format_va(const char *name, const char *ns, bool quoted, const char *fmt, va_list ap)
290 char buf[LARGE_SIZE];
291 vsnprintf(buf, LARGE_SIZE, fmt, ap);
295 print_quoted_string(std::string(buf));
297 m_ss << std::string(buf);
301 int JSONFormatter::get_len() const
303 return m_ss.str().size();
306 void JSONFormatter::write_raw_data(const char *data)
311 const char *XMLFormatter::XML_1_DTD =
312 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
314 XMLFormatter::XMLFormatter(bool pretty, bool lowercased, bool underscored)
316 m_lowercased(lowercased),
317 m_underscored(underscored)
322 void XMLFormatter::flush(std::ostream& os)
324 finish_pending_string();
325 std::string m_ss_str = m_ss.str();
327 /* There is a small catch here. If the rest of the formatter had NO output,
328 * we should NOT output a newline. This primarily triggers on HTTP redirects */
329 if (m_pretty && !m_ss_str.empty())
331 else if (m_line_break_enabled)
337 void XMLFormatter::reset()
341 m_pending_string.clear();
342 m_pending_string.str("");
344 m_pending_string_name.clear();
345 m_header_done = false;
348 void XMLFormatter::output_header()
351 m_header_done = true;
352 write_raw_data(XMLFormatter::XML_1_DTD);;
358 void XMLFormatter::output_footer()
360 while(!m_sections.empty()) {
365 void XMLFormatter::open_object_section(const char *name)
367 open_section_in_ns(name, NULL, NULL);
370 void XMLFormatter::open_object_section_with_attrs(const char *name, const FormatterAttrs& attrs)
372 open_section_in_ns(name, NULL, &attrs);
375 void XMLFormatter::open_object_section_in_ns(const char *name, const char *ns)
377 open_section_in_ns(name, ns, NULL);
380 void XMLFormatter::open_array_section(const char *name)
382 open_section_in_ns(name, NULL, NULL);
385 void XMLFormatter::open_array_section_with_attrs(const char *name, const FormatterAttrs& attrs)
387 open_section_in_ns(name, NULL, &attrs);
390 void XMLFormatter::open_array_section_in_ns(const char *name, const char *ns)
392 open_section_in_ns(name, ns, NULL);
395 void XMLFormatter::close_section()
397 assert(!m_sections.empty());
398 finish_pending_string();
400 std::string section = m_sections.back();
401 std::transform(section.begin(), section.end(), section.begin(),
402 [this](char c) { return this->to_lower_underscore(c); });
403 m_sections.pop_back();
405 m_ss << "</" << section << ">";
410 void XMLFormatter::dump_unsigned(const char *name, uint64_t u)
413 std::transform(e.begin(), e.end(), e.begin(),
414 [this](char c) { return this->to_lower_underscore(c); });
417 m_ss << "<" << e << ">" << u << "</" << e << ">";
422 void XMLFormatter::dump_int(const char *name, int64_t u)
425 std::transform(e.begin(), e.end(), e.begin(),
426 [this](char c) { return this->to_lower_underscore(c); });
429 m_ss << "<" << e << ">" << u << "</" << e << ">";
434 void XMLFormatter::dump_float(const char *name, double d)
437 std::transform(e.begin(), e.end(), e.begin(),
438 [this](char c) { return this->to_lower_underscore(c); });
441 m_ss << "<" << e << ">" << d << "</" << e << ">";
446 void XMLFormatter::dump_string(const char *name, const std::string& s)
449 std::transform(e.begin(), e.end(), e.begin(),
450 [this](char c) { return this->to_lower_underscore(c); });
453 m_ss << "<" << e << ">" << escape_xml_str(s.c_str()) << "</" << e << ">";
458 void XMLFormatter::dump_string_with_attrs(const char *name, const std::string& s, const FormatterAttrs& attrs)
461 std::transform(e.begin(), e.end(), e.begin(),
462 [this](char c) { return this->to_lower_underscore(c); });
464 std::string attrs_str;
465 get_attrs_str(&attrs, attrs_str);
467 m_ss << "<" << e << attrs_str << ">" << escape_xml_str(s.c_str()) << "</" << e << ">";
472 std::ostream& XMLFormatter::dump_stream(const char *name)
475 m_pending_string_name = name;
476 m_ss << "<" << m_pending_string_name << ">";
477 return m_pending_string;
480 void XMLFormatter::dump_format_va(const char* name, const char *ns, bool quoted, const char *fmt, va_list ap)
482 char buf[LARGE_SIZE];
483 vsnprintf(buf, LARGE_SIZE, fmt, ap);
485 std::transform(e.begin(), e.end(), e.begin(),
486 [this](char c) { return this->to_lower_underscore(c); });
490 m_ss << "<" << e << " xmlns=\"" << ns << "\">" << buf << "</" << e << ">";
492 m_ss << "<" << e << ">" << escape_xml_str(buf) << "</" << e << ">";
499 int XMLFormatter::get_len() const
501 return m_ss.str().size();
504 void XMLFormatter::write_raw_data(const char *data)
509 void XMLFormatter::get_attrs_str(const FormatterAttrs *attrs, std::string& attrs_str)
511 std::stringstream attrs_ss;
513 for (std::list<std::pair<std::string, std::string> >::const_iterator iter = attrs->attrs.begin();
514 iter != attrs->attrs.end(); ++iter) {
515 std::pair<std::string, std::string> p = *iter;
516 attrs_ss << " " << p.first << "=" << "\"" << p.second << "\"";
519 attrs_str = attrs_ss.str();
522 void XMLFormatter::open_section_in_ns(const char *name, const char *ns, const FormatterAttrs *attrs)
525 std::string attrs_str;
528 get_attrs_str(attrs, attrs_str);
532 std::transform(e.begin(), e.end(), e.begin(),
533 [this](char c) { return this->to_lower_underscore(c); });
536 m_ss << "<" << e << attrs_str << " xmlns=\"" << ns << "\">";
538 m_ss << "<" << e << attrs_str << ">";
542 m_sections.push_back(name);
545 void XMLFormatter::finish_pending_string()
547 if (!m_pending_string_name.empty()) {
548 m_ss << escape_xml_str(m_pending_string.str().c_str())
549 << "</" << m_pending_string_name << ">";
550 m_pending_string_name.clear();
551 m_pending_string.str(std::string());
558 void XMLFormatter::print_spaces()
560 finish_pending_string();
562 std::string spaces(m_sections.size(), ' ');
567 std::string XMLFormatter::escape_xml_str(const char *str)
569 int len = escape_xml_attr_len(str);
570 std::vector<char> escaped(len, '\0');
571 escape_xml_attr(str, &escaped[0]);
572 return std::string(&escaped[0]);
575 char XMLFormatter::to_lower_underscore(char c) const
577 if (m_underscored && c == ' ') {
579 } else if (m_lowercased) {
580 return std::tolower(c);
585 TableFormatter::TableFormatter(bool keyval) : m_keyval(keyval)
590 void TableFormatter::flush(std::ostream& os)
592 finish_pending_string();
593 std::vector<size_t> column_size = m_column_size;
594 std::vector<std::string> column_name = m_column_name;
596 std::set<int> need_header_set;
598 // auto-sizing columns
599 for (size_t i = 0; i < m_vec.size(); i++) {
600 for (size_t j = 0; j < m_vec[i].size(); j++) {
601 column_size.resize(m_vec[i].size());
602 column_name.resize(m_vec[i].size());
604 if (m_vec[i - 1][j] != m_vec[i][j]) {
605 // changing row labels require to show the header
606 need_header_set.insert(i);
607 column_name[i] = m_vec[i][j].first;
610 column_name[i] = m_vec[i][j].first;
613 if (m_vec[i][j].second.length() > column_size[j])
614 column_size[j] = m_vec[i][j].second.length();
615 if (m_vec[i][j].first.length() > column_size[j])
616 column_size[j] = m_vec[i][j].first.length();
620 bool need_header = false;
621 if ((column_size.size() == m_column_size.size())) {
622 for (size_t i = 0; i < column_size.size(); i++) {
623 if (column_size[i] != m_column_size[i]) {
633 // first row always needs a header if there wasn't one before
634 need_header_set.insert(0);
637 m_column_size = column_size;
638 for (size_t i = 0; i < m_vec.size(); i++) {
640 if (need_header_set.count(i)) {
644 for (size_t j = 0; j < m_vec[i].size(); j++) {
645 for (size_t v = 0; v < m_column_size[j] + 3; v++)
652 for (size_t j = 0; j < m_vec[i].size(); j++) {
654 std::stringstream fs;
655 fs << boost::format("%%-%is") % (m_column_size[j] + 2);
656 os << boost::format(fs.str()) % m_vec[i][j].first;
661 for (size_t j = 0; j < m_vec[i].size(); j++) {
662 for (size_t v = 0; v < m_column_size[j] + 3; v++)
673 for (size_t j = 0; j < m_vec[i].size(); j++) {
676 std::stringstream fs;
680 os << m_vec[i][j].first;
683 os << m_vec[i][j].second;
686 fs << boost::format("%%-%is") % (m_column_size[j] + 2);
687 os << boost::format(fs.str()) % m_vec[i][j].second;
694 if (i == (m_vec.size() - 1)) {
697 for (size_t j = 0; j < m_vec[i].size(); j++) {
698 for (size_t v = 0; v < m_column_size[j] + 3; v++)
710 void TableFormatter::reset()
714 m_section_cnt.clear();
715 m_column_size.clear();
719 void TableFormatter::open_object_section(const char *name)
721 open_section_in_ns(name, NULL, NULL);
724 void TableFormatter::open_object_section_with_attrs(const char *name, const FormatterAttrs& attrs)
726 open_section_in_ns(name, NULL, NULL);
729 void TableFormatter::open_object_section_in_ns(const char *name, const char *ns)
731 open_section_in_ns(name, NULL, NULL);
734 void TableFormatter::open_array_section(const char *name)
736 open_section_in_ns(name, NULL, NULL);
739 void TableFormatter::open_array_section_with_attrs(const char *name, const FormatterAttrs& attrs)
741 open_section_in_ns(name, NULL, NULL);
744 void TableFormatter::open_array_section_in_ns(const char *name, const char *ns)
746 open_section_in_ns(name, NULL, NULL);
749 void TableFormatter::open_section_in_ns(const char *name, const char *ns, const FormatterAttrs *attrs)
751 m_section.push_back(name);
755 void TableFormatter::close_section()
759 if (m_section.size()) {
760 m_section_cnt[m_section.back()] = 0;
761 m_section.pop_back();
765 size_t TableFormatter::m_vec_index(const char *name)
767 std::string key(name);
769 size_t i = m_vec.size();
773 // make sure there are vectors to push back key/val pairs
778 if (m_vec[i].size()) {
779 if (m_vec[i][0].first == key) {
780 // start a new column if a key is repeated
781 m_vec.resize(m_vec.size() + 1);
790 std::string TableFormatter::get_section_name(const char* name)
792 std::string t_name = name;
793 for (size_t i = 0; i < m_section.size(); i++) {
794 t_name.insert(0, ":");
795 t_name.insert(0, m_section[i]);
797 if (m_section_open) {
798 std::stringstream lss;
801 lss << m_section_cnt[t_name]++;
809 void TableFormatter::dump_unsigned(const char *name, uint64_t u)
811 finish_pending_string();
812 size_t i = m_vec_index(name);
814 m_vec[i].push_back(std::make_pair(get_section_name(name), m_ss.str()));
819 void TableFormatter::dump_int(const char *name, int64_t u)
821 finish_pending_string();
822 size_t i = m_vec_index(name);
824 m_vec[i].push_back(std::make_pair(get_section_name(name), m_ss.str()));
829 void TableFormatter::dump_float(const char *name, double d)
831 finish_pending_string();
832 size_t i = m_vec_index(name);
835 m_vec[i].push_back(std::make_pair(get_section_name(name), m_ss.str()));
840 void TableFormatter::dump_string(const char *name, const std::string& s)
842 finish_pending_string();
843 size_t i = m_vec_index(name);
846 m_vec[i].push_back(std::make_pair(get_section_name(name), m_ss.str()));
851 void TableFormatter::dump_string_with_attrs(const char *name, const std::string& s, const FormatterAttrs& attrs)
853 finish_pending_string();
854 size_t i = m_vec_index(name);
856 std::string attrs_str;
857 get_attrs_str(&attrs, attrs_str);
858 m_ss << attrs_str << s;
860 m_vec[i].push_back(std::make_pair(get_section_name(name), m_ss.str()));
865 void TableFormatter::dump_format_va(const char* name, const char *ns, bool quoted, const char *fmt, va_list ap)
867 finish_pending_string();
868 char buf[LARGE_SIZE];
869 vsnprintf(buf, LARGE_SIZE, fmt, ap);
871 size_t i = m_vec_index(name);
873 m_ss << ns << "." << buf;
877 m_vec[i].push_back(std::make_pair(get_section_name(name), m_ss.str()));
882 std::ostream& TableFormatter::dump_stream(const char *name)
884 finish_pending_string();
885 // we don't support this
886 m_pending_name = name;
890 int TableFormatter::get_len() const
892 // we don't know the size until flush is called
896 void TableFormatter::write_raw_data(const char *data) {
900 void TableFormatter::get_attrs_str(const FormatterAttrs *attrs, std::string& attrs_str)
902 std::stringstream attrs_ss;
904 for (std::list<std::pair<std::string, std::string> >::const_iterator iter = attrs->attrs.begin();
905 iter != attrs->attrs.end(); ++iter) {
906 std::pair<std::string, std::string> p = *iter;
907 attrs_ss << " " << p.first << "=" << "\"" << p.second << "\"";
910 attrs_str = attrs_ss.str();
913 void TableFormatter::finish_pending_string()
915 if (m_pending_name.length()) {
916 std::string ss = m_ss.str();
919 std::string pending_name = m_pending_name;
921 dump_string(pending_name.c_str(), ss);