]> git.proxmox.com Git - ceph.git/blobdiff - ceph/src/common/Formatter.h
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / common / Formatter.h
index 14d5f41ca3c393d3cdcddb3a64c257f20eb3c7b5..1363f1f6768772a6c081bd0427f7d8fd5040d4d2 100644 (file)
@@ -6,8 +6,6 @@
 #include "include/int_types.h"
 #include "include/buffer_fwd.h"
 
-#include <boost/utility/string_view.hpp>
-
 #include <deque>
 #include <list>
 #include <vector>
@@ -25,14 +23,43 @@ namespace ceph {
 
   class Formatter {
   public:
-    static Formatter *create(boost::string_view type,
-                            boost::string_view default_type,
-                            boost::string_view fallback);
-    static Formatter *create(boost::string_view type,
-                            boost::string_view default_type) {
+    class ObjectSection {
+      Formatter& formatter;
+
+    public:
+      ObjectSection(Formatter& f, const char *name) : formatter(f) {
+        formatter.open_object_section(name);
+      }
+      ObjectSection(Formatter& f, const char *name, const char *ns) : formatter(f) {
+        formatter.open_object_section_in_ns(name, ns);
+      }
+      ~ObjectSection() {
+        formatter.close_section();
+      }
+    };
+    class ArraySection {
+      Formatter& formatter;
+
+    public:
+      ArraySection(Formatter& f, const char *name) : formatter(f) {
+        formatter.open_array_section(name);
+      }
+      ArraySection(Formatter& f, const char *name, const char *ns) : formatter(f) {
+        formatter.open_array_section_in_ns(name, ns);
+      }
+      ~ArraySection() {
+        formatter.close_section();
+      }
+    };
+
+    static Formatter *create(std::string_view type,
+                            std::string_view default_type,
+                            std::string_view fallback);
+    static Formatter *create(std::string_view type,
+                            std::string_view default_type) {
       return create(type, default_type, "");
     }
-    static Formatter *create(boost::string_view type) {
+    static Formatter *create(std::string_view type) {
       return create(type, "json-pretty", "");
     }
 
@@ -56,7 +83,7 @@ namespace ceph {
     virtual void dump_unsigned(const char *name, uint64_t u) = 0;
     virtual void dump_int(const char *name, int64_t s) = 0;
     virtual void dump_float(const char *name, double d) = 0;
-    virtual void dump_string(const char *name, boost::string_view s) = 0;
+    virtual void dump_string(const char *name, std::string_view s) = 0;
     virtual void dump_bool(const char *name, bool b)
     {
       dump_format_unquoted(name, "%s", (b ? "true" : "false"));
@@ -83,12 +110,24 @@ namespace ceph {
     {
       open_object_section(name);
     }
-    virtual void dump_string_with_attrs(const char *name, boost::string_view s, const FormatterAttrs& attrs)
+    virtual void dump_string_with_attrs(const char *name, std::string_view s, const FormatterAttrs& attrs)
     {
       dump_string(name, s);
     }
   };
 
+  class copyable_sstream : public std::stringstream {
+  public:
+    copyable_sstream() {}
+    copyable_sstream(const copyable_sstream& rhs) {
+      str(rhs.str());
+    }
+    copyable_sstream& operator=(const copyable_sstream& rhs) {
+      str(rhs.str());
+      return *this;
+    }
+  };
+
   class JSONFormatter : public Formatter {
   public:
     explicit JSONFormatter(bool p = false);
@@ -106,14 +145,27 @@ namespace ceph {
     void open_object_section_in_ns(const char *name, const char *ns) override;
     void close_section() override;
     void dump_unsigned(const char *name, uint64_t u) override;
-    void dump_int(const char *name, int64_t u) override;
+    void dump_int(const char *name, int64_t s) override;
     void dump_float(const char *name, double d) override;
-    void dump_string(const char *name, boost::string_view s) override;
+    void dump_string(const char *name, std::string_view s) override;
     std::ostream& dump_stream(const char *name) override;
     void dump_format_va(const char *name, const char *ns, bool quoted, const char *fmt, va_list ap) override;
     int get_len() const override;
     void write_raw_data(const char *data) override;
 
+  protected:
+    virtual bool handle_value(const char *name, std::string_view s, bool quoted) {
+      return false; /* is handling done? */
+    }
+
+    virtual bool handle_open_section(const char *name, const char *ns, bool is_array) {
+      return false; /* is handling done? */
+    }
+
+    virtual bool handle_close_section() {
+      return false; /* is handling done? */
+    }
+
   private:
 
     struct json_formatter_stack_entry_d {
@@ -123,18 +175,27 @@ namespace ceph {
     };
 
     bool m_pretty;
-    void open_section(const char *name, bool is_array);
-    void print_quoted_string(boost::string_view s);
+    void open_section(const char *name, const char *ns, bool is_array);
+    void print_quoted_string(std::string_view s);
     void print_name(const char *name);
     void print_comma(json_formatter_stack_entry_d& entry);
     void finish_pending_string();
 
-    std::stringstream m_ss, m_pending_string;
+    template <class T>
+    void add_value(const char *name, T val);
+    void add_value(const char *name, std::string_view val, bool quoted);
+
+    copyable_sstream m_ss;
+    copyable_sstream m_pending_string;
+    std::string m_pending_name;
     std::list<json_formatter_stack_entry_d> m_stack;
     bool m_is_pending_string;
     bool m_line_break_enabled = false;
   };
 
+  template <class T>
+  void add_value(const char *name, T val);
+
   class XMLFormatter : public Formatter {
   public:
     static const char *XML_1_DTD;
@@ -154,9 +215,9 @@ namespace ceph {
     void open_object_section_in_ns(const char *name, const char *ns) override;
     void close_section() override;
     void dump_unsigned(const char *name, uint64_t u) override;
-    void dump_int(const char *name, int64_t u) override;
+    void dump_int(const char *name, int64_t s) override;
     void dump_float(const char *name, double d) override;
-    void dump_string(const char *name, boost::string_view s) override;
+    void dump_string(const char *name, std::string_view s) override;
     std::ostream& dump_stream(const char *name) override;
     void dump_format_va(const char *name, const char *ns, bool quoted, const char *fmt, va_list ap) override;
     int get_len() const override;
@@ -165,13 +226,12 @@ namespace ceph {
     /* with attrs */
     void open_array_section_with_attrs(const char *name, const FormatterAttrs& attrs) override;
     void open_object_section_with_attrs(const char *name, const FormatterAttrs& attrs) override;
-    void dump_string_with_attrs(const char *name, boost::string_view s, const FormatterAttrs& attrs) override;
+    void dump_string_with_attrs(const char *name, std::string_view s, const FormatterAttrs& attrs) override;
 
   protected:
     void open_section_in_ns(const char *name, const char *ns, const FormatterAttrs *attrs);
     void finish_pending_string();
     void print_spaces();
-    static std::string escape_xml_str(boost::string_view str);
     void get_attrs_str(const FormatterAttrs *attrs, std::string& attrs_str);
     char to_lower_underscore(char c) const;
 
@@ -183,6 +243,9 @@ namespace ceph {
     std::string m_pending_string_name;
     bool m_header_done;
     bool m_line_break_enabled = false;
+  private:
+    template <class T>
+    void add_value(const char *name, T val);
   };
 
   class TableFormatter : public Formatter {
@@ -206,11 +269,11 @@ namespace ceph {
 
     void close_section() override;
     void dump_unsigned(const char *name, uint64_t u) override;
-    void dump_int(const char *name, int64_t u) override;
+    void dump_int(const char *name, int64_t s) override;
     void dump_float(const char *name, double d) override;
-    void dump_string(const char *name, boost::string_view s) override;
+    void dump_string(const char *name, std::string_view s) override;
     void dump_format_va(const char *name, const char *ns, bool quoted, const char *fmt, va_list ap) override;
-    void dump_string_with_attrs(const char *name, boost::string_view s, const FormatterAttrs& attrs) override;
+    void dump_string_with_attrs(const char *name, std::string_view s, const FormatterAttrs& attrs) override;
     std::ostream& dump_stream(const char *name) override;
 
     int get_len() const override;
@@ -218,6 +281,8 @@ namespace ceph {
     void get_attrs_str(const FormatterAttrs *attrs, std::string& attrs_str);
 
   private:
+    template <class T>
+    void add_value(const char *name, T val);
     void open_section_in_ns(const char *name, const char *ns, const FormatterAttrs *attrs);
     std::vector< std::vector<std::pair<std::string, std::string> > > m_vec;
     std::stringstream m_ss;
@@ -234,6 +299,5 @@ namespace ceph {
     std::vector< std::string > m_column_name;
   };
 
-
 }
 #endif