]> git.proxmox.com Git - ceph.git/blame - ceph/src/common/HTMLFormatter.cc
update sources to v12.2.5
[ceph.git] / ceph / src / common / HTMLFormatter.cc
CommitLineData
7c673cae
FG
1// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2// vim: ts=8 sw=2 smarttab
3/*
4 * Ceph - scalable distributed file system
5 *
6 * Copyright (C) 2011 New Dream Network
7 *
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.
12 *
13 */
14
15#define LARGE_SIZE 1024
16
7c673cae 17#include "HTMLFormatter.h"
31f18b77 18#include "Formatter.h"
7c673cae
FG
19
20#include <sstream>
21#include <stdarg.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string>
25#include <string.h> // for strdup
26
27// -----------------------
28namespace ceph {
29
30HTMLFormatter::HTMLFormatter(bool pretty)
31: XMLFormatter(pretty), m_status(0), m_status_name(NULL)
32{
33}
34
35HTMLFormatter::~HTMLFormatter()
36{
37 if (m_status_name) {
38 free((void*)m_status_name);
39 m_status_name = NULL;
40 }
41}
42
43void HTMLFormatter::reset()
44{
45 XMLFormatter::reset();
46 m_header_done = false;
47 m_status = 0;
48 if (m_status_name) {
49 free((void*)m_status_name);
50 m_status_name = NULL;
51 }
52}
53
54void HTMLFormatter::set_status(int status, const char* status_name)
55{
56 m_status = status;
57 if (status_name) {
58 m_status_name = strdup(status_name);
59 }
60};
61
62void HTMLFormatter::output_header() {
63 if (!m_header_done) {
64 m_header_done = true;
65 char buf[16];
66 snprintf(buf, sizeof(buf), "%d", m_status);
67 std::string status_line(buf);
68 if (m_status_name) {
69 status_line += " ";
70 status_line += m_status_name;
71 }
72 open_object_section("html");
73 print_spaces();
74 m_ss << "<head><title>" << status_line << "</title></head>";
75 if (m_pretty)
76 m_ss << "\n";
77 open_object_section("body");
78 print_spaces();
79 m_ss << "<h1>" << status_line << "</h1>";
80 if (m_pretty)
81 m_ss << "\n";
82 open_object_section("ul");
83 }
84}
85
86template <typename T>
87void HTMLFormatter::dump_template(const char *name, T arg)
88{
89 print_spaces();
90 m_ss << "<li>" << name << ": " << arg << "</li>";
91 if (m_pretty)
92 m_ss << "\n";
93}
94
95void HTMLFormatter::dump_unsigned(const char *name, uint64_t u)
96{
97 dump_template(name, u);
98}
99
100void HTMLFormatter::dump_int(const char *name, int64_t u)
101{
102 dump_template(name, u);
103}
104
105void HTMLFormatter::dump_float(const char *name, double d)
106{
107 dump_template(name, d);
108}
109
94b18763 110void HTMLFormatter::dump_string(const char *name, boost::string_view s)
7c673cae 111{
94b18763 112 dump_template(name, escape_xml_str(s.data()));
7c673cae
FG
113}
114
94b18763 115void HTMLFormatter::dump_string_with_attrs(const char *name, boost::string_view s, const FormatterAttrs& attrs)
7c673cae
FG
116{
117 std::string e(name);
118 std::string attrs_str;
119 get_attrs_str(&attrs, attrs_str);
120 print_spaces();
94b18763 121 m_ss << "<li>" << e << ": " << escape_xml_str(s.data()) << attrs_str << "</li>";
7c673cae
FG
122 if (m_pretty)
123 m_ss << "\n";
124}
125
126std::ostream& HTMLFormatter::dump_stream(const char *name)
127{
128 print_spaces();
129 m_pending_string_name = "li";
130 m_ss << "<li>" << name << ": ";
131 return m_pending_string;
132}
133
134void HTMLFormatter::dump_format_va(const char* name, const char *ns, bool quoted, const char *fmt, va_list ap)
135{
136 char buf[LARGE_SIZE];
137 vsnprintf(buf, LARGE_SIZE, fmt, ap);
138
139 std::string e(name);
140 print_spaces();
141 if (ns) {
142 m_ss << "<li xmlns=\"" << ns << "\">" << e << ": " << escape_xml_str(buf) << "</li>";
143 } else {
144 m_ss << "<li>" << e << ": " << escape_xml_str(buf) << "</li>";
145 }
146
147 if (m_pretty)
148 m_ss << "\n";
149}
150
151} // namespace ceph