]>
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 #include "common/escape.h"
29 // -----------------------
32 HTMLFormatter::HTMLFormatter(bool pretty
)
33 : XMLFormatter(pretty
), m_status(0), m_status_name(NULL
)
37 HTMLFormatter::~HTMLFormatter()
40 free((void*)m_status_name
);
45 void HTMLFormatter::reset()
47 XMLFormatter::reset();
48 m_header_done
= false;
51 free((void*)m_status_name
);
56 void HTMLFormatter::set_status(int status
, const char* status_name
)
61 free((void*)m_status_name
);
63 m_status_name
= strdup(status_name
);
67 void HTMLFormatter::output_header() {
71 snprintf(buf
, sizeof(buf
), "%d", m_status
);
72 std::string
status_line(buf
);
75 status_line
+= m_status_name
;
77 open_object_section("html");
79 m_ss
<< "<head><title>" << status_line
<< "</title></head>";
82 open_object_section("body");
84 m_ss
<< "<h1>" << status_line
<< "</h1>";
87 open_object_section("ul");
92 void HTMLFormatter::dump_template(std::string_view name
, T arg
)
95 m_ss
<< "<li>" << name
<< ": " << arg
<< "</li>";
100 void HTMLFormatter::dump_unsigned(std::string_view name
, uint64_t u
)
102 dump_template(name
, u
);
105 void HTMLFormatter::dump_int(std::string_view name
, int64_t u
)
107 dump_template(name
, u
);
110 void HTMLFormatter::dump_float(std::string_view name
, double d
)
112 dump_template(name
, d
);
115 void HTMLFormatter::dump_string(std::string_view name
, std::string_view s
)
117 dump_template(name
, xml_stream_escaper(s
));
120 void HTMLFormatter::dump_string_with_attrs(std::string_view name
, std::string_view s
, const FormatterAttrs
& attrs
)
123 std::string attrs_str
;
124 get_attrs_str(&attrs
, attrs_str
);
126 m_ss
<< "<li>" << e
<< ": " << xml_stream_escaper(s
) << attrs_str
<< "</li>";
131 std::ostream
& HTMLFormatter::dump_stream(std::string_view name
)
134 m_pending_string_name
= "li";
135 m_ss
<< "<li>" << name
<< ": ";
136 return m_pending_string
;
139 void HTMLFormatter::dump_format_va(std::string_view name
, const char *ns
, bool quoted
, const char *fmt
, va_list ap
)
141 char buf
[LARGE_SIZE
];
142 size_t len
= vsnprintf(buf
, LARGE_SIZE
, fmt
, ap
);
147 m_ss
<< "<li xmlns=\"" << ns
<< "\">" << e
<< ": "
148 << xml_stream_escaper(std::string_view(buf
, len
)) << "</li>";
150 m_ss
<< "<li>" << e
<< ": "
151 << xml_stream_escaper(std::string_view(buf
, len
)) << "</li>";