CtaAdminTextFormatter.hpp 6.36 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/*!
 * @project        The CERN Tape Archive (CTA)
 * @brief          Text formatter for CTA Admin command tool
 * @description    CTA Admin command using Google Protocol Buffers and XRootD SSI transport
 * @copyright      Copyright 2019 CERN
 * @license        This program is free software: you can redistribute it and/or modify
 *                 it under the terms of the GNU General Public License as published by
 *                 the Free Software Foundation, either version 3 of the License, or
 *                 (at your option) any later version.
 *
 *                 This program is distributed in the hope that it will be useful,
 *                 but WITHOUT ANY WARRANTY; without even the implied warranty of
 *                 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *                 GNU General Public License for more details.
 *
 *                 You should have received a copy of the GNU General Public License
 *                 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#pragma once

#include "CtaFrontendApi.hpp"

namespace cta {
namespace admin {

27
class TextFormatter
28
29
{
public:
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
  /*!
   * Constructor
   *
   * @param[in]  bufLines  Number of text lines to buffer before flushing formatted output
   *                       (Not used for JSON output which does not need to be formatted
   *                        so can be streamed directly)
   */
  TextFormatter(unsigned int bufLines = 1000) :
    m_bufLines(bufLines) {
    m_outputBuffer.reserve(bufLines);
  }

  ~TextFormatter() {
    flush();
  }

  // Output headers
47
48
49
50
51
52
53
  void printAdminLsHeader();
  void printArchiveFileLsHeader();
  void printArchiveFileLsSummaryHeader();
  void printArchiveRouteLsHeader();
  void printDriveLsHeader();
  void printFailedRequestLsHeader();
  void printFailedRequestLsSummaryHeader();
54
  void printGroupMountRuleLsHeader();
55
56
57
58
  void printListPendingArchivesHeader();
  void printListPendingArchivesSummaryHeader();
  void printListPendingRetrievesHeader();
  void printListPendingRetrievesSummaryHeader();
59
60
  void printLogicalLibraryLsHeader();
  void printMountPolicyLsHeader();
61
  void printRepackLsHeader();
62
  void printRequesterMountRuleLsHeader();
63
64
  void printShowQueuesHeader();
  void printStorageClassLsHeader();
65
  void printTapeLsHeader();
66
  void printTapeFileLsHeader();
67
  void printTapePoolLsHeader();
68
  void printDiskSystemLsHeader();
69
  void printVirtualOrganizationLsHeader();
70
  void printVersionHeader();
71
  void printMediaTypeLsHeader();
72
   
73
  // Output records
74
  void print(const AdminLsItem &adls_item);
75
  void print(const ArchiveFileLsItem &afls_item);
76
  void print(const ArchiveFileLsSummary &afls_summary);
77
  void print(const ArchiveRouteLsItem &afls_item);
78
  void print(const DriveLsItem &drls_item);
79
80
  void print(const FailedRequestLsItem &frls_item);
  void print(const FailedRequestLsSummary &frls_summary);
81
  void print(const GroupMountRuleLsItem &gmrls_item);
82
83
84
85
  void print(const ListPendingArchivesItem &lpa_item);
  void print(const ListPendingArchivesSummary &lpa_summary);
  void print(const ListPendingRetrievesItem &lpr_item);
  void print(const ListPendingRetrievesSummary &lpr_summary);
86
87
  void print(const LogicalLibraryLsItem &llls_item);
  void print(const MountPolicyLsItem &mpls_item);
88
  void print(const RepackLsItem &rels_item);
89
  void print(const RequesterMountRuleLsItem &rmrls_item);
90
91
  void print(const ShowQueuesItem &sq_item);
  void print(const StorageClassLsItem &scls_item);
92
  void print(const TapeLsItem &tals_item);
93
  void print(const TapeFileLsItem &tfls_item);
94
  void print(const TapePoolLsItem &tpls_item);
95
  void print(const DiskSystemLsItem &dsls_item);
96
  void print(const VirtualOrganizationLsItem &vols_item);
97
  void print(const VersionItem & version_item);
98
  void print(const MediaTypeLsItem &mtls_item);
99
  
100
101

private:
102
103
104
105
106
107
108
109
110
  //! Add a line to the buffer
  template<typename... Args>
  void push_back(Args... args) {
    std::vector<std::string> line;
    buildVector(line, args...);
    m_outputBuffer.push_back(line);
    if(m_outputBuffer.size() >= m_bufLines) flush();
  }

111
  //! Recursive variadic method to build a log string from an arbitrary number of items of arbitrary type
112
  template<typename T, typename... Args>
113
  static void buildVector(std::vector<std::string> &line, const T &item, Args... args) {
114
115
116
117
    buildVector(line, item);
    buildVector(line, args...);
  }

118
  //! Base case method to add one item to the log
119
  static void buildVector(std::vector<std::string> &line, const std::string &item) {
120
121
122
    line.push_back(item);
  }

123
  //! Base case method to add one item to the log, overloaded for char*
124
  static void buildVector(std::vector<std::string> &line, const char *item) {
125
126
127
    line.push_back(std::string(item));
  }

128
  //! Base case method to add one item to the log, overloaded for bool
129
  static void buildVector(std::vector<std::string> &line, bool item) {
130
131
132
133
134
135
136
137
    line.push_back(item ? "true" : "false");
  }

  /*!
   * Base case method to add one item to the log, with partial specialisation
   * (works for all integer and floating-point types)
   */
  template<typename T>
138
  static void buildVector(std::vector<std::string> &line, const T &item) {
139
140
141
    line.push_back(std::to_string(item));
  }

142
143
144
  //! Convert double to string with one decimal place precision and a suffix
  static std::string doubleToStr(double value, char unit);

145
146
147
  //! Convert UNIX time to string
  static std::string timeToStr(const time_t &unixtime);

148
  //! Convert data size in bytes to abbreviated string with appropriate size suffix (K/M/G/T/P/E)
149
  static std::string dataSizeToStr(uint64_t value);
150

151
152
  //! Flush buffer to stdout
  void flush();
153

154
  std::vector<unsigned int> m_colSize;                              //!< Array of column sizes
155
156
  unsigned int m_bufLines;                                          //!< Number of text lines to buffer before flushing formatted output
  std::vector<std::vector<std::string>> m_outputBuffer;             //!< Buffer for text output (not used for JSON)
157

158
159
  static constexpr const char* const TEXT_RED    = "\x1b[31;1m";    //!< Terminal formatting code for red text
  static constexpr const char* const TEXT_NORMAL = "\x1b[0m";       //!< Terminal formatting code for normal text
160
161
  static constexpr const int NB_CHAR_REASON_DRIVE = 50;             //!< DriveLs reason max length to display in tabular output
  static constexpr const char* const REASON_PREFIX = "REASON_";     //!< DriveLs reason prefix to be spot for left-align tabular output
162
163
164
};

}}