]>
git.proxmox.com Git - ceph.git/blob - ceph/src/common/HTMLFormatter.cc
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 "Formatter.h"
25 #include <string.h> // for strdup
27 // -----------------------
30 HTMLFormatter::HTMLFormatter(bool pretty
)
31 : XMLFormatter(pretty
), m_status(0), m_status_name(NULL
)
35 HTMLFormatter::~HTMLFormatter()
38 free((void*)m_status_name
);
43 void HTMLFormatter::reset()
45 XMLFormatter::reset();
46 m_header_done
= false;
49 free((void*)m_status_name
);
54 void HTMLFormatter::set_status(int status
, const char* status_name
)
58 m_status_name
= strdup(status_name
);
62 void HTMLFormatter::output_header() {
66 snprintf(buf
, sizeof(buf
), "%d", m_status
);
67 std::string
status_line(buf
);
70 status_line
+= m_status_name
;
72 open_object_section("html");
74 m_ss
<< "<head><title>" << status_line
<< "</title></head>";
77 open_object_section("body");
79 m_ss
<< "<h1>" << status_line
<< "</h1>";
82 open_object_section("ul");
87 void HTMLFormatter::dump_template(const char *name
, T arg
)
90 m_ss
<< "<li>" << name
<< ": " << arg
<< "</li>";
95 void HTMLFormatter::dump_unsigned(const char *name
, uint64_t u
)
97 dump_template(name
, u
);
100 void HTMLFormatter::dump_int(const char *name
, int64_t u
)
102 dump_template(name
, u
);
105 void HTMLFormatter::dump_float(const char *name
, double d
)
107 dump_template(name
, d
);
110 void HTMLFormatter::dump_string(const char *name
, boost::string_view s
)
112 dump_template(name
, escape_xml_str(s
.data()));
115 void HTMLFormatter::dump_string_with_attrs(const char *name
, boost::string_view s
, const FormatterAttrs
& attrs
)
118 std::string attrs_str
;
119 get_attrs_str(&attrs
, attrs_str
);
121 m_ss
<< "<li>" << e
<< ": " << escape_xml_str(s
.data()) << attrs_str
<< "</li>";
126 std::ostream
& HTMLFormatter::dump_stream(const char *name
)
129 m_pending_string_name
= "li";
130 m_ss
<< "<li>" << name
<< ": ";
131 return m_pending_string
;
134 void HTMLFormatter::dump_format_va(const char* name
, const char *ns
, bool quoted
, const char *fmt
, va_list ap
)
136 char buf
[LARGE_SIZE
];
137 vsnprintf(buf
, LARGE_SIZE
, fmt
, ap
);
142 m_ss
<< "<li xmlns=\"" << ns
<< "\">" << e
<< ": " << escape_xml_str(buf
) << "</li>";
144 m_ss
<< "<li>" << e
<< ": " << escape_xml_str(buf
) << "</li>";