blob: 957773b923e1100ac5d4900a69807f37ee9fbb89 [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/*
3 * Copyright (c) 2014-2018, 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
76formatTimestamp(time::system_clock::TimePoint t);
77
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
Eric Newberryde332452018-01-30 11:45:32 -0700189namespace detail {
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000190
Eric Newberryde332452018-01-30 11:45:32 -0700191template<typename DurationT>
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000192std::string
Eric Newberryde332452018-01-30 11:45:32 -0700193getTimeUnit(bool isLong);
194
195template<>
196inline std::string
197getTimeUnit<time::nanoseconds>(bool isLong)
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000198{
Eric Newberryde332452018-01-30 11:45:32 -0700199 return isLong ? "nanoseconds" : "ns";
200}
201
202template<>
203inline std::string
204getTimeUnit<time::microseconds>(bool isLong)
205{
206 return isLong ? "microseconds" : "us";
207}
208
209template<>
210inline std::string
211getTimeUnit<time::milliseconds>(bool isLong)
212{
213 return isLong ? "milliseconds" : "ms";
214}
215
216template<>
217inline std::string
218getTimeUnit<time::seconds>(bool isLong)
219{
220 return isLong ? "seconds" : "s";
221}
222
223template<>
224inline std::string
225getTimeUnit<time::minutes>(bool isLong)
226{
227 return isLong ? "minutes" : "m";
228}
229
230template<>
231inline std::string
232getTimeUnit<time::hours>(bool isLong)
233{
234 return isLong ? "hours" : "h";
235}
236
237template<>
238inline std::string
239getTimeUnit<time::days>(bool isLong)
240{
241 return isLong ? "days" : "d";
242}
243
244} // namespace detail
245
246template<typename OutputPrecision>
247std::string
248formatDuration(time::nanoseconds d, bool isLong = false)
249{
250 return to_string(time::duration_cast<OutputPrecision>(d).count()) +
251 (isLong ? " " : "") + detail::getTimeUnit<OutputPrecision>(isLong);
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000252}
253
254std::string
255formatTimestamp(time::system_clock::TimePoint t);
256
257} // namespace text
258
Junxiao Shi331ade72016-08-19 14:07:19 +0000259} // namespace nfdc
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000260} // namespace tools
261} // namespace nfd
262
Junxiao Shi331ade72016-08-19 14:07:19 +0000263#endif // NFD_TOOLS_NFDC_FORMAT_HELPERS_HPP