blob: 95f3310e88ebf5c8e2a28aa09d6cec78b2508b44 [file] [log] [blame]
Junxiao Shi38f4ce92016-08-04 10:01:52 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Eric Newberryde332452018-01-30 11:45:32 -07002/*
Davide Pesavento412c9822021-07-02 00:21:05 -04003 * Copyright (c) 2014-2021, Regents of the University of California,
Junxiao Shi38f4ce92016-08-04 10:01:52 +00004 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis.
10 *
11 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
24 */
25
Junxiao Shi331ade72016-08-19 14:07:19 +000026#ifndef NFD_TOOLS_NFDC_FORMAT_HELPERS_HPP
27#define NFD_TOOLS_NFDC_FORMAT_HELPERS_HPP
Junxiao Shi38f4ce92016-08-04 10:01:52 +000028
Junxiao Shi9f5b01d2016-08-05 03:54:28 +000029#include "core/common.hpp"
Junxiao Shi38f4ce92016-08-04 10:01:52 +000030
31namespace nfd {
32namespace tools {
Junxiao Shi331ade72016-08-19 14:07:19 +000033namespace nfdc {
Junxiao Shi38f4ce92016-08-04 10:01:52 +000034
35namespace xml {
36
37void
38printHeader(std::ostream& os);
39
40void
41printFooter(std::ostream& os);
42
43struct Text
44{
45 const std::string& s;
46};
47
48/** \brief print XML text with special character represented as predefined entities
49 */
50std::ostream&
51operator<<(std::ostream& os, const Text& text);
52
Junxiao Shi7a36ac72018-03-21 15:23:22 +000053/** \brief print true as an empty element and false as nothing
54 */
55struct Flag
56{
57 const char* elementName;
58 bool flag;
59};
60
61std::ostream&
62operator<<(std::ostream& os, Flag v);
63
Eric Newberryde332452018-01-30 11:45:32 -070064/** \return duration in XML duration format
65 *
66 * Definition of this format: https://www.w3.org/TR/xmlschema11-2/#duration
67 */
Junxiao Shi38f4ce92016-08-04 10:01:52 +000068std::string
Eric Newberryde332452018-01-30 11:45:32 -070069formatDuration(time::nanoseconds d);
Junxiao Shi38f4ce92016-08-04 10:01:52 +000070
Eric Newberryde332452018-01-30 11:45:32 -070071/** \return timestamp in XML dateTime format
72 *
73 * Definition of this format: https://www.w3.org/TR/xmlschema11-2/#dateTime
74 */
Junxiao Shi38f4ce92016-08-04 10:01:52 +000075std::string
Davide Pesavento412c9822021-07-02 00:21:05 -040076formatTimestamp(time::system_clock::time_point t);
Junxiao Shi38f4ce92016-08-04 10:01:52 +000077
78} // namespace xml
79
80namespace text {
81
Junxiao Shi6c135622016-11-21 14:30:33 +000082/** \brief print a number of whitespaces
83 */
84struct Spaces
85{
86 int nSpaces; ///< number of spaces; print nothing if negative
87};
88
89std::ostream&
90operator<<(std::ostream& os, const Spaces& spaces);
91
Junxiao Shi38f4ce92016-08-04 10:01:52 +000092/** \brief print different string on first and subsequent usage
93 *
94 * \code
95 * Separator sep(",");
96 * for (int i = 1; i <= 3; ++i) {
97 * os << sep << i;
98 * }
99 * // prints: 1,2,3
100 * \endcode
101 */
Junxiao Shi1f481fa2017-01-26 15:14:43 +0000102class Separator : noncopyable
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000103{
104public:
105 Separator(const std::string& first, const std::string& subsequent);
106
107 explicit
108 Separator(const std::string& subsequent);
109
Junxiao Shic143afe2016-09-20 13:04:51 +0000110 int
111 getCount() const
112 {
113 return m_count;
114 }
115
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000116private:
117 std::string m_first;
118 std::string m_subsequent;
119 int m_count;
120
121 friend std::ostream& operator<<(std::ostream& os, Separator& sep);
122};
123
124std::ostream&
125operator<<(std::ostream& os, Separator& sep);
126
Junxiao Shi1f481fa2017-01-26 15:14:43 +0000127/** \brief print attributes of an item
128 *
129 * \code
130 * ItemAttributes ia(wantMultiLine, 3);
131 * os << ia("id") << 500
132 * << ia("uri") << "udp4://192.0.2.1:6363"
133 * << ia.end();
134 *
135 * // prints in single-line style (wantMultiLine==false):
136 * // id=500 uri=udp4://192.0.2.1:6363 [no-newline]
137 *
138 * // prints in multi-line style (wantMultiLine==true):
139 * // id=500
140 * // uri=udp4://192.0.2.1:6363 [newline]
141 * \endcode
142 */
143class ItemAttributes : noncopyable
144{
145public:
146 /** \brief constructor
147 * \param wantMultiLine true to select multi-line style, false to use single-line style
148 * \param maxAttributeWidth maximum width of attribute names, for alignment in multi-line style
149 */
150 explicit
151 ItemAttributes(bool wantMultiLine = false, int maxAttributeWidth = 0);
152
153 struct Attribute
154 {
Junxiao Shi056815e2017-01-29 16:39:19 +0000155 ItemAttributes& ia;
Junxiao Shi1f481fa2017-01-26 15:14:43 +0000156 std::string attribute;
157 };
158
Junxiao Shi056815e2017-01-29 16:39:19 +0000159 /** \note Caller must ensure ItemAttributes object is alive until after all Attribute objects are
160 * destructed.
161 */
Junxiao Shi1f481fa2017-01-26 15:14:43 +0000162 Attribute
163 operator()(const std::string& attribute);
164
165 std::string
166 end() const;
167
168private:
169 bool m_wantMultiLine;
170 int m_maxAttributeWidth;
171 int m_count;
Junxiao Shi056815e2017-01-29 16:39:19 +0000172
173 friend std::ostream& operator<<(std::ostream& os, const ItemAttributes::Attribute& attr);
Junxiao Shi1f481fa2017-01-26 15:14:43 +0000174};
175
176std::ostream&
177operator<<(std::ostream& os, const ItemAttributes::Attribute& attr);
178
Junxiao Shi8dc473a2018-03-02 15:10:18 +0000179/** \brief print boolean as 'on' or 'off'
180 */
181struct OnOff
182{
183 bool flag;
184};
185
186std::ostream&
187operator<<(std::ostream& os, OnOff v);
188
Junxiao Shia4d7fe02018-07-20 06:51:41 -0600189/** \brief print boolean as 'yes' or 'no'
190 */
191struct YesNo
192{
193 bool flag;
194};
195
196std::ostream&
197operator<<(std::ostream& os, YesNo v);
198
Eric Newberryde332452018-01-30 11:45:32 -0700199namespace detail {
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000200
Eric Newberryde332452018-01-30 11:45:32 -0700201template<typename DurationT>
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000202std::string
Eric Newberryde332452018-01-30 11:45:32 -0700203getTimeUnit(bool isLong);
204
205template<>
206inline std::string
207getTimeUnit<time::nanoseconds>(bool isLong)
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000208{
Eric Newberryde332452018-01-30 11:45:32 -0700209 return isLong ? "nanoseconds" : "ns";
210}
211
212template<>
213inline std::string
214getTimeUnit<time::microseconds>(bool isLong)
215{
216 return isLong ? "microseconds" : "us";
217}
218
219template<>
220inline std::string
221getTimeUnit<time::milliseconds>(bool isLong)
222{
223 return isLong ? "milliseconds" : "ms";
224}
225
226template<>
227inline std::string
228getTimeUnit<time::seconds>(bool isLong)
229{
230 return isLong ? "seconds" : "s";
231}
232
233template<>
234inline std::string
235getTimeUnit<time::minutes>(bool isLong)
236{
237 return isLong ? "minutes" : "m";
238}
239
240template<>
241inline std::string
242getTimeUnit<time::hours>(bool isLong)
243{
244 return isLong ? "hours" : "h";
245}
246
247template<>
248inline std::string
249getTimeUnit<time::days>(bool isLong)
250{
251 return isLong ? "days" : "d";
252}
253
254} // namespace detail
255
256template<typename OutputPrecision>
257std::string
258formatDuration(time::nanoseconds d, bool isLong = false)
259{
260 return to_string(time::duration_cast<OutputPrecision>(d).count()) +
261 (isLong ? " " : "") + detail::getTimeUnit<OutputPrecision>(isLong);
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000262}
263
264std::string
Davide Pesavento412c9822021-07-02 00:21:05 -0400265formatTimestamp(time::system_clock::time_point t);
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000266
267} // namespace text
268
Junxiao Shi331ade72016-08-19 14:07:19 +0000269} // namespace nfdc
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000270} // namespace tools
271} // namespace nfd
272
Junxiao Shi331ade72016-08-19 14:07:19 +0000273#endif // NFD_TOOLS_NFDC_FORMAT_HELPERS_HPP