]> git.proxmox.com Git - ceph.git/blob - ceph/src/common/HTMLFormatter.cc
update ceph source to reef 18.2.0
[ceph.git] / 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
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
17 #include "HTMLFormatter.h"
18 #include "Formatter.h"
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 #include "common/escape.h"
28
29 // -----------------------
30 namespace ceph {
31
32 HTMLFormatter::HTMLFormatter(bool pretty)
33 : XMLFormatter(pretty), m_status(0), m_status_name(NULL)
34 {
35 }
36
37 HTMLFormatter::~HTMLFormatter()
38 {
39 if (m_status_name) {
40 free((void*)m_status_name);
41 m_status_name = NULL;
42 }
43 }
44
45 void HTMLFormatter::reset()
46 {
47 XMLFormatter::reset();
48 m_header_done = false;
49 m_status = 0;
50 if (m_status_name) {
51 free((void*)m_status_name);
52 m_status_name = NULL;
53 }
54 }
55
56 void HTMLFormatter::set_status(int status, const char* status_name)
57 {
58 m_status = status;
59 if (status_name) {
60 if (m_status_name) {
61 free((void*)m_status_name);
62 }
63 m_status_name = strdup(status_name);
64 }
65 };
66
67 void HTMLFormatter::output_header() {
68 if (!m_header_done) {
69 m_header_done = true;
70 char buf[16];
71 snprintf(buf, sizeof(buf), "%d", m_status);
72 std::string status_line(buf);
73 if (m_status_name) {
74 status_line += " ";
75 status_line += m_status_name;
76 }
77 open_object_section("html");
78 print_spaces();
79 m_ss << "<head><title>" << status_line << "</title></head>";
80 if (m_pretty)
81 m_ss << "\n";
82 open_object_section("body");
83 print_spaces();
84 m_ss << "<h1>" << status_line << "</h1>";
85 if (m_pretty)
86 m_ss << "\n";
87 open_object_section("ul");
88 }
89 }
90
91 template <typename T>
92 void HTMLFormatter::dump_template(std::string_view name, T arg)
93 {
94 print_spaces();
95 m_ss << "<li>" << name << ": " << arg << "</li>";
96 if (m_pretty)
97 m_ss << "\n";
98 }
99
100 void HTMLFormatter::dump_unsigned(std::string_view name, uint64_t u)
101 {
102 dump_template(name, u);
103 }
104
105 void HTMLFormatter::dump_int(std::string_view name, int64_t u)
106 {
107 dump_template(name, u);
108 }
109
110 void HTMLFormatter::dump_float(std::string_view name, double d)
111 {
112 dump_template(name, d);
113 }
114
115 void HTMLFormatter::dump_string(std::string_view name, std::string_view s)
116 {
117 dump_template(name, xml_stream_escaper(s));
118 }
119
120 void HTMLFormatter::dump_string_with_attrs(std::string_view name, std::string_view s, const FormatterAttrs& attrs)
121 {
122 std::string e(name);
123 std::string attrs_str;
124 get_attrs_str(&attrs, attrs_str);
125 print_spaces();
126 m_ss << "<li>" << e << ": " << xml_stream_escaper(s) << attrs_str << "</li>";
127 if (m_pretty)
128 m_ss << "\n";
129 }
130
131 std::ostream& HTMLFormatter::dump_stream(std::string_view name)
132 {
133 print_spaces();
134 m_pending_string_name = "li";
135 m_ss << "<li>" << name << ": ";
136 return m_pending_string;
137 }
138
139 void HTMLFormatter::dump_format_va(std::string_view name, const char *ns, bool quoted, const char *fmt, va_list ap)
140 {
141 char buf[LARGE_SIZE];
142 size_t len = vsnprintf(buf, LARGE_SIZE, fmt, ap);
143
144 std::string e(name);
145 print_spaces();
146 if (ns) {
147 m_ss << "<li xmlns=\"" << ns << "\">" << e << ": "
148 << xml_stream_escaper(std::string_view(buf, len)) << "</li>";
149 } else {
150 m_ss << "<li>" << e << ": "
151 << xml_stream_escaper(std::string_view(buf, len)) << "</li>";
152 }
153
154 if (m_pretty)
155 m_ss << "\n";
156 }
157
158 } // namespace ceph