blob: 0f04c1641ad2b848c8cd1f4dd191c1c6311d1c9e [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 Pesaventoa3a7a4e2022-05-29 16:06:22 -04003 * Copyright (c) 2014-2022, 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
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040031namespace nfd::tools::nfdc {
Junxiao Shi38f4ce92016-08-04 10:01:52 +000032
33namespace xml {
34
35void
36printHeader(std::ostream& os);
37
38void
39printFooter(std::ostream& os);
40
41struct Text
42{
43 const std::string& s;
44};
45
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040046/** \brief Print XML text with special character represented as predefined entities.
Junxiao Shi38f4ce92016-08-04 10:01:52 +000047 */
48std::ostream&
49operator<<(std::ostream& os, const Text& text);
50
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040051/** \brief Print true as an empty element and false as nothing.
Junxiao Shi7a36ac72018-03-21 15:23:22 +000052 */
53struct Flag
54{
55 const char* elementName;
56 bool flag;
57};
58
59std::ostream&
60operator<<(std::ostream& os, Flag v);
61
Eric Newberryde332452018-01-30 11:45:32 -070062/** \return duration in XML duration format
63 *
64 * Definition of this format: https://www.w3.org/TR/xmlschema11-2/#duration
65 */
Junxiao Shi38f4ce92016-08-04 10:01:52 +000066std::string
Eric Newberryde332452018-01-30 11:45:32 -070067formatDuration(time::nanoseconds d);
Junxiao Shi38f4ce92016-08-04 10:01:52 +000068
Eric Newberryde332452018-01-30 11:45:32 -070069/** \return timestamp in XML dateTime format
70 *
71 * Definition of this format: https://www.w3.org/TR/xmlschema11-2/#dateTime
72 */
Junxiao Shi38f4ce92016-08-04 10:01:52 +000073std::string
Davide Pesavento412c9822021-07-02 00:21:05 -040074formatTimestamp(time::system_clock::time_point t);
Junxiao Shi38f4ce92016-08-04 10:01:52 +000075
76} // namespace xml
77
78namespace text {
79
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040080/** \brief Print a number of whitespaces.
Junxiao Shi6c135622016-11-21 14:30:33 +000081 */
82struct Spaces
83{
84 int nSpaces; ///< number of spaces; print nothing if negative
85};
86
87std::ostream&
88operator<<(std::ostream& os, const Spaces& spaces);
89
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040090/** \brief Print different string on first and subsequent usage.
Junxiao Shi38f4ce92016-08-04 10:01:52 +000091 *
92 * \code
93 * Separator sep(",");
94 * for (int i = 1; i <= 3; ++i) {
95 * os << sep << i;
96 * }
97 * // prints: 1,2,3
98 * \endcode
99 */
Junxiao Shi1f481fa2017-01-26 15:14:43 +0000100class Separator : noncopyable
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000101{
102public:
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400103 Separator(std::string_view first, std::string_view subsequent);
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000104
105 explicit
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400106 Separator(std::string_view subsequent);
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000107
Junxiao Shic143afe2016-09-20 13:04:51 +0000108 int
109 getCount() const
110 {
111 return m_count;
112 }
113
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000114private:
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400115 const std::string m_first;
116 const std::string m_subsequent;
117 int m_count = 0;
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000118
119 friend std::ostream& operator<<(std::ostream& os, Separator& sep);
120};
121
122std::ostream&
123operator<<(std::ostream& os, Separator& sep);
124
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400125/** \brief Print attributes of an item.
Junxiao Shi1f481fa2017-01-26 15:14:43 +0000126 *
127 * \code
128 * ItemAttributes ia(wantMultiLine, 3);
129 * os << ia("id") << 500
130 * << ia("uri") << "udp4://192.0.2.1:6363"
131 * << ia.end();
132 *
133 * // prints in single-line style (wantMultiLine==false):
134 * // id=500 uri=udp4://192.0.2.1:6363 [no-newline]
135 *
136 * // prints in multi-line style (wantMultiLine==true):
137 * // id=500
138 * // uri=udp4://192.0.2.1:6363 [newline]
139 * \endcode
140 */
141class ItemAttributes : noncopyable
142{
143public:
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400144 /** \brief Constructor.
Junxiao Shi1f481fa2017-01-26 15:14:43 +0000145 * \param wantMultiLine true to select multi-line style, false to use single-line style
146 * \param maxAttributeWidth maximum width of attribute names, for alignment in multi-line style
147 */
148 explicit
149 ItemAttributes(bool wantMultiLine = false, int maxAttributeWidth = 0);
150
151 struct Attribute
152 {
Junxiao Shi056815e2017-01-29 16:39:19 +0000153 ItemAttributes& ia;
Junxiao Shi1f481fa2017-01-26 15:14:43 +0000154 std::string attribute;
155 };
156
Junxiao Shi056815e2017-01-29 16:39:19 +0000157 /** \note Caller must ensure ItemAttributes object is alive until after all Attribute objects are
158 * destructed.
159 */
Junxiao Shi1f481fa2017-01-26 15:14:43 +0000160 Attribute
161 operator()(const std::string& attribute);
162
163 std::string
164 end() const;
165
166private:
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400167 const bool m_wantMultiLine;
168 const int m_maxAttributeWidth;
169 int m_count = 0;
Junxiao Shi056815e2017-01-29 16:39:19 +0000170
171 friend std::ostream& operator<<(std::ostream& os, const ItemAttributes::Attribute& attr);
Junxiao Shi1f481fa2017-01-26 15:14:43 +0000172};
173
174std::ostream&
175operator<<(std::ostream& os, const ItemAttributes::Attribute& attr);
176
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400177/** \brief Print boolean as 'on' or 'off'.
Junxiao Shi8dc473a2018-03-02 15:10:18 +0000178 */
179struct OnOff
180{
181 bool flag;
182};
183
184std::ostream&
185operator<<(std::ostream& os, OnOff v);
186
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400187/** \brief Print boolean as 'yes' or 'no'.
Junxiao Shia4d7fe02018-07-20 06:51:41 -0600188 */
189struct YesNo
190{
191 bool flag;
192};
193
194std::ostream&
195operator<<(std::ostream& os, YesNo v);
196
Eric Newberryde332452018-01-30 11:45:32 -0700197namespace detail {
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000198
Eric Newberryde332452018-01-30 11:45:32 -0700199template<typename DurationT>
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000200std::string
Eric Newberryde332452018-01-30 11:45:32 -0700201getTimeUnit(bool isLong);
202
203template<>
204inline std::string
205getTimeUnit<time::nanoseconds>(bool isLong)
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000206{
Eric Newberryde332452018-01-30 11:45:32 -0700207 return isLong ? "nanoseconds" : "ns";
208}
209
210template<>
211inline std::string
212getTimeUnit<time::microseconds>(bool isLong)
213{
214 return isLong ? "microseconds" : "us";
215}
216
217template<>
218inline std::string
219getTimeUnit<time::milliseconds>(bool isLong)
220{
221 return isLong ? "milliseconds" : "ms";
222}
223
224template<>
225inline std::string
226getTimeUnit<time::seconds>(bool isLong)
227{
228 return isLong ? "seconds" : "s";
229}
230
231template<>
232inline std::string
233getTimeUnit<time::minutes>(bool isLong)
234{
235 return isLong ? "minutes" : "m";
236}
237
238template<>
239inline std::string
240getTimeUnit<time::hours>(bool isLong)
241{
242 return isLong ? "hours" : "h";
243}
244
245template<>
246inline std::string
247getTimeUnit<time::days>(bool isLong)
248{
249 return isLong ? "days" : "d";
250}
251
252} // namespace detail
253
254template<typename OutputPrecision>
255std::string
256formatDuration(time::nanoseconds d, bool isLong = false)
257{
258 return to_string(time::duration_cast<OutputPrecision>(d).count()) +
259 (isLong ? " " : "") + detail::getTimeUnit<OutputPrecision>(isLong);
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000260}
261
262std::string
Davide Pesavento412c9822021-07-02 00:21:05 -0400263formatTimestamp(time::system_clock::time_point t);
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000264
265} // namespace text
266
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400267} // namespace nfd::tools::nfdc
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000268
Junxiao Shi331ade72016-08-19 14:07:19 +0000269#endif // NFD_TOOLS_NFDC_FORMAT_HELPERS_HPP