]> git.proxmox.com Git - ceph.git/blame - ceph/src/common/HTMLFormatter.cc
update sources to ceph Nautilus 14.2.1
[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
11fdf7f2
TL
27#include "common/escape.h"
28
7c673cae
FG
29// -----------------------
30namespace ceph {
31
32HTMLFormatter::HTMLFormatter(bool pretty)
33: XMLFormatter(pretty), m_status(0), m_status_name(NULL)
34{
35}
36
37HTMLFormatter::~HTMLFormatter()
38{
39 if (m_status_name) {
40 free((void*)m_status_name);
41 m_status_name = NULL;
42 }
43}
44
45void 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
56void HTMLFormatter::set_status(int status, const char* status_name)
57{
58 m_status = status;
59 if (status_name) {
11fdf7f2
TL
60 if (m_status_name) {
61 free((void*)m_status_name);
62 }
7c673cae
FG
63 m_status_name = strdup(status_name);
64 }
65};
66
67void 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
91template <typename T>
92void HTMLFormatter::dump_template(const char *name, T arg)
93{
94 print_spaces();
95 m_ss << "<li>" << name << ": " << arg << "</li>";
96 if (m_pretty)
97 m_ss << "\n";
98}
99
100void HTMLFormatter::dump_unsigned(const char *name, uint64_t u)
101{
102 dump_template(name, u);
103}
104
105void HTMLFormatter::dump_int(const char *name, int64_t u)
106{
107 dump_template(name, u);
108}
109
110void HTMLFormatter::dump_float(const char *name, double d)
111{
112 dump_template(name, d);
113}
114
11fdf7f2 115void HTMLFormatter::dump_string(const char *name, std::string_view s)
7c673cae 116{
11fdf7f2 117 dump_template(name, xml_stream_escaper(s));
7c673cae
FG
118}
119
11fdf7f2 120void HTMLFormatter::dump_string_with_attrs(const char *name, std::string_view s, const FormatterAttrs& attrs)
7c673cae
FG
121{
122 std::string e(name);
123 std::string attrs_str;
124 get_attrs_str(&attrs, attrs_str);
125 print_spaces();
11fdf7f2 126 m_ss << "<li>" << e << ": " << xml_stream_escaper(s) << attrs_str << "</li>";
7c673cae
FG
127 if (m_pretty)
128 m_ss << "\n";
129}
130
131std::ostream& HTMLFormatter::dump_stream(const char *name)
132{
133 print_spaces();
134 m_pending_string_name = "li";
135 m_ss << "<li>" << name << ": ";
136 return m_pending_string;
137}
138
139void HTMLFormatter::dump_format_va(const char* name, const char *ns, bool quoted, const char *fmt, va_list ap)
140{
141 char buf[LARGE_SIZE];
11fdf7f2 142 size_t len = vsnprintf(buf, LARGE_SIZE, fmt, ap);
7c673cae
FG
143
144 std::string e(name);
145 print_spaces();
146 if (ns) {
11fdf7f2
TL
147 m_ss << "<li xmlns=\"" << ns << "\">" << e << ": "
148 << xml_stream_escaper(std::string_view(buf, len)) << "</li>";
7c673cae 149 } else {
11fdf7f2
TL
150 m_ss << "<li>" << e << ": "
151 << xml_stream_escaper(std::string_view(buf, len)) << "</li>";
7c673cae
FG
152 }
153
154 if (m_pretty)
155 m_ss << "\n";
156}
157
158} // namespace ceph