Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / 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 // -----------------------
28 namespace ceph {
29
30 HTMLFormatter::HTMLFormatter(bool pretty)
31 : XMLFormatter(pretty), m_status(0), m_status_name(NULL)
32 {
33 }
34
35 HTMLFormatter::~HTMLFormatter()
36 {
37   if (m_status_name) {
38     free((void*)m_status_name);
39     m_status_name = NULL;
40   }
41 }
42
43 void 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
54 void 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
62 void 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
86 template <typename T>
87 void 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
95 void HTMLFormatter::dump_unsigned(const char *name, uint64_t u)
96 {
97   dump_template(name, u);
98 }
99
100 void HTMLFormatter::dump_int(const char *name, int64_t u)
101 {
102   dump_template(name, u);
103 }
104
105 void HTMLFormatter::dump_float(const char *name, double d)
106 {
107   dump_template(name, d);
108 }
109
110 void HTMLFormatter::dump_string(const char *name, const std::string& s)
111 {
112   dump_template(name, escape_xml_str(s.c_str()));
113 }
114
115 void HTMLFormatter::dump_string_with_attrs(const char *name, const std::string& s, const FormatterAttrs& attrs)
116 {
117   std::string e(name);
118   std::string attrs_str;
119   get_attrs_str(&attrs, attrs_str);
120   print_spaces();
121   m_ss << "<li>" << e << ": " << escape_xml_str(s.c_str()) << attrs_str << "</li>";
122   if (m_pretty)
123     m_ss << "\n";
124 }
125
126 std::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
134 void 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