Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame^] | 2 | /* |
| 3 | * Copyright (c) 2014-2018, Regents of the University of California, |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 4 | * 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 Shi | 331ade7 | 2016-08-19 14:07:19 +0000 | [diff] [blame] | 26 | #ifndef NFD_TOOLS_NFDC_FORMAT_HELPERS_HPP |
| 27 | #define NFD_TOOLS_NFDC_FORMAT_HELPERS_HPP |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 28 | |
Junxiao Shi | 9f5b01d | 2016-08-05 03:54:28 +0000 | [diff] [blame] | 29 | #include "core/common.hpp" |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 30 | |
| 31 | namespace nfd { |
| 32 | namespace tools { |
Junxiao Shi | 331ade7 | 2016-08-19 14:07:19 +0000 | [diff] [blame] | 33 | namespace nfdc { |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 34 | |
| 35 | namespace xml { |
| 36 | |
| 37 | void |
| 38 | printHeader(std::ostream& os); |
| 39 | |
| 40 | void |
| 41 | printFooter(std::ostream& os); |
| 42 | |
| 43 | struct Text |
| 44 | { |
| 45 | const std::string& s; |
| 46 | }; |
| 47 | |
| 48 | /** \brief print XML text with special character represented as predefined entities |
| 49 | */ |
| 50 | std::ostream& |
| 51 | operator<<(std::ostream& os, const Text& text); |
| 52 | |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame^] | 53 | /** \return duration in XML duration format |
| 54 | * |
| 55 | * Definition of this format: https://www.w3.org/TR/xmlschema11-2/#duration |
| 56 | */ |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 57 | std::string |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame^] | 58 | formatDuration(time::nanoseconds d); |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 59 | |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame^] | 60 | /** \return timestamp in XML dateTime format |
| 61 | * |
| 62 | * Definition of this format: https://www.w3.org/TR/xmlschema11-2/#dateTime |
| 63 | */ |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 64 | std::string |
| 65 | formatTimestamp(time::system_clock::TimePoint t); |
| 66 | |
| 67 | } // namespace xml |
| 68 | |
| 69 | namespace text { |
| 70 | |
Junxiao Shi | 6c13562 | 2016-11-21 14:30:33 +0000 | [diff] [blame] | 71 | /** \brief print a number of whitespaces |
| 72 | */ |
| 73 | struct Spaces |
| 74 | { |
| 75 | int nSpaces; ///< number of spaces; print nothing if negative |
| 76 | }; |
| 77 | |
| 78 | std::ostream& |
| 79 | operator<<(std::ostream& os, const Spaces& spaces); |
| 80 | |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 81 | /** \brief print different string on first and subsequent usage |
| 82 | * |
| 83 | * \code |
| 84 | * Separator sep(","); |
| 85 | * for (int i = 1; i <= 3; ++i) { |
| 86 | * os << sep << i; |
| 87 | * } |
| 88 | * // prints: 1,2,3 |
| 89 | * \endcode |
| 90 | */ |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 91 | class Separator : noncopyable |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 92 | { |
| 93 | public: |
| 94 | Separator(const std::string& first, const std::string& subsequent); |
| 95 | |
| 96 | explicit |
| 97 | Separator(const std::string& subsequent); |
| 98 | |
Junxiao Shi | c143afe | 2016-09-20 13:04:51 +0000 | [diff] [blame] | 99 | int |
| 100 | getCount() const |
| 101 | { |
| 102 | return m_count; |
| 103 | } |
| 104 | |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 105 | private: |
| 106 | std::string m_first; |
| 107 | std::string m_subsequent; |
| 108 | int m_count; |
| 109 | |
| 110 | friend std::ostream& operator<<(std::ostream& os, Separator& sep); |
| 111 | }; |
| 112 | |
| 113 | std::ostream& |
| 114 | operator<<(std::ostream& os, Separator& sep); |
| 115 | |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 116 | /** \brief print attributes of an item |
| 117 | * |
| 118 | * \code |
| 119 | * ItemAttributes ia(wantMultiLine, 3); |
| 120 | * os << ia("id") << 500 |
| 121 | * << ia("uri") << "udp4://192.0.2.1:6363" |
| 122 | * << ia.end(); |
| 123 | * |
| 124 | * // prints in single-line style (wantMultiLine==false): |
| 125 | * // id=500 uri=udp4://192.0.2.1:6363 [no-newline] |
| 126 | * |
| 127 | * // prints in multi-line style (wantMultiLine==true): |
| 128 | * // id=500 |
| 129 | * // uri=udp4://192.0.2.1:6363 [newline] |
| 130 | * \endcode |
| 131 | */ |
| 132 | class ItemAttributes : noncopyable |
| 133 | { |
| 134 | public: |
| 135 | /** \brief constructor |
| 136 | * \param wantMultiLine true to select multi-line style, false to use single-line style |
| 137 | * \param maxAttributeWidth maximum width of attribute names, for alignment in multi-line style |
| 138 | */ |
| 139 | explicit |
| 140 | ItemAttributes(bool wantMultiLine = false, int maxAttributeWidth = 0); |
| 141 | |
| 142 | struct Attribute |
| 143 | { |
Junxiao Shi | 056815e | 2017-01-29 16:39:19 +0000 | [diff] [blame] | 144 | ItemAttributes& ia; |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 145 | std::string attribute; |
| 146 | }; |
| 147 | |
Junxiao Shi | 056815e | 2017-01-29 16:39:19 +0000 | [diff] [blame] | 148 | /** \note Caller must ensure ItemAttributes object is alive until after all Attribute objects are |
| 149 | * destructed. |
| 150 | */ |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 151 | Attribute |
| 152 | operator()(const std::string& attribute); |
| 153 | |
| 154 | std::string |
| 155 | end() const; |
| 156 | |
| 157 | private: |
| 158 | bool m_wantMultiLine; |
| 159 | int m_maxAttributeWidth; |
| 160 | int m_count; |
Junxiao Shi | 056815e | 2017-01-29 16:39:19 +0000 | [diff] [blame] | 161 | |
| 162 | friend std::ostream& operator<<(std::ostream& os, const ItemAttributes::Attribute& attr); |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 163 | }; |
| 164 | |
| 165 | std::ostream& |
| 166 | operator<<(std::ostream& os, const ItemAttributes::Attribute& attr); |
| 167 | |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame^] | 168 | namespace detail { |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 169 | |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame^] | 170 | template<typename DurationT> |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 171 | std::string |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame^] | 172 | getTimeUnit(bool isLong); |
| 173 | |
| 174 | template<> |
| 175 | inline std::string |
| 176 | getTimeUnit<time::nanoseconds>(bool isLong) |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 177 | { |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame^] | 178 | return isLong ? "nanoseconds" : "ns"; |
| 179 | } |
| 180 | |
| 181 | template<> |
| 182 | inline std::string |
| 183 | getTimeUnit<time::microseconds>(bool isLong) |
| 184 | { |
| 185 | return isLong ? "microseconds" : "us"; |
| 186 | } |
| 187 | |
| 188 | template<> |
| 189 | inline std::string |
| 190 | getTimeUnit<time::milliseconds>(bool isLong) |
| 191 | { |
| 192 | return isLong ? "milliseconds" : "ms"; |
| 193 | } |
| 194 | |
| 195 | template<> |
| 196 | inline std::string |
| 197 | getTimeUnit<time::seconds>(bool isLong) |
| 198 | { |
| 199 | return isLong ? "seconds" : "s"; |
| 200 | } |
| 201 | |
| 202 | template<> |
| 203 | inline std::string |
| 204 | getTimeUnit<time::minutes>(bool isLong) |
| 205 | { |
| 206 | return isLong ? "minutes" : "m"; |
| 207 | } |
| 208 | |
| 209 | template<> |
| 210 | inline std::string |
| 211 | getTimeUnit<time::hours>(bool isLong) |
| 212 | { |
| 213 | return isLong ? "hours" : "h"; |
| 214 | } |
| 215 | |
| 216 | template<> |
| 217 | inline std::string |
| 218 | getTimeUnit<time::days>(bool isLong) |
| 219 | { |
| 220 | return isLong ? "days" : "d"; |
| 221 | } |
| 222 | |
| 223 | } // namespace detail |
| 224 | |
| 225 | template<typename OutputPrecision> |
| 226 | std::string |
| 227 | formatDuration(time::nanoseconds d, bool isLong = false) |
| 228 | { |
| 229 | return to_string(time::duration_cast<OutputPrecision>(d).count()) + |
| 230 | (isLong ? " " : "") + detail::getTimeUnit<OutputPrecision>(isLong); |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | std::string |
| 234 | formatTimestamp(time::system_clock::TimePoint t); |
| 235 | |
| 236 | } // namespace text |
| 237 | |
Junxiao Shi | 331ade7 | 2016-08-19 14:07:19 +0000 | [diff] [blame] | 238 | } // namespace nfdc |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 239 | } // namespace tools |
| 240 | } // namespace nfd |
| 241 | |
Junxiao Shi | 331ade7 | 2016-08-19 14:07:19 +0000 | [diff] [blame] | 242 | #endif // NFD_TOOLS_NFDC_FORMAT_HELPERS_HPP |