GEOS
LogPart.hpp
Go to the documentation of this file.
1 /*
2  * ------------------------------------------------------------------------------------------------------------
3  * SPDX-License-Identifier: LGPL-2.1-only
4  *
5  * Copyright (c) 2018-2020 Lawrence Livermore National Security LLC
6  * Copyright (c) 2018-2020 The Board of Trustees of the Leland Stanford Junior University
7  * Copyright (c) 2018-2020 TotalEnergies
8  * Copyright (c) 2019- GEOSX Contributors
9  * All rights reserved
10  *
11  * See top level LICENSE, COPYRIGHT, CONTRIBUTORS, NOTICE, and ACKNOWLEDGEMENTS files for details.
12  * ------------------------------------------------------------------------------------------------------------
13  */
14 
19 #ifndef GEOS_COMMON_FORMAT_LOGPART_HPP
20 #define GEOS_COMMON_FORMAT_LOGPART_HPP
21 
22 #include "common/DataTypes.hpp"
23 #include "common/format/Format.hpp"
25 
26 namespace geos
27 {
28 
29 using namespace stringutilities;
33 class LogPart
34 {
35 public:
36 
42  LogPart( string_view logPartTitle, bool enableOutput );
43 
50  template< typename ... Args >
51  void addDescription( string_view name, Args const & ... args );
52 
57  void addDescription( string_view description );
58 
65  template< typename ... Args >
66  void addEndDescription( string_view name, Args const & ... args );
67 
72  void addEndDescription( string_view description );
73 
78  void setMinWidth( size_t const & minWidth );
79 
84  void setMaxWidth( size_t const & maxWidth );
85 
90  void begin( std::ostream & os = std::cout );
91 
96  void end( std::ostream & oss = std::cout );
97 
102  void enableOutput( bool enabled )
103  { m_enableOutput = enabled; }
104 
105 private:
106 
111  struct Description
112  {
114  std::vector< std::vector< string > > m_names;
117  std::vector< std::vector< string > > m_values;
118  };
119 
123  struct FormattedDescription
124  {
126  string m_title;
128  std::vector< string > m_lines;
130  size_t m_maxNameWidth;
132  size_t m_maxValueWidth;
133  };
134 
135  Description m_startDescription = { {}, {} };
136  Description m_endDescription = { {}, {} };
137 
138  FormattedDescription m_formattedStartDescription = { "", {}, 0, 0 };
139  FormattedDescription m_formattedEndDescription = { "", {}, 0, 0 };
140 
142  size_t m_width = 100;
144  size_t m_minWidth = 100;
146  size_t m_maxWidth = SIZE_MAX;
148  static constexpr size_t m_borderMargin = 2;
150  static constexpr size_t m_nbBorderChar = 2;
152  char const m_borderCharacter = '#';
154  static constexpr string_view m_prefixEndTitle = "End of ";
156  static constexpr string_view m_delimiter = " : ";
158  bool m_enableOutput = true;
159 
167  template< typename ... Args >
168  void addDescriptionBySection( Description & description, FormattedDescription & formattedDescription,
169  string_view name, Args const &... args );
170 
176  void formatDescriptions( LogPart::Description & description,
177  FormattedDescription & formattedDescription );
178 
183  string outputTitle( FormattedDescription & formattedDescription );
184 
189  string outputDescription( FormattedDescription & formattedDescription );
190 };
191 
192 template< typename ... Args >
193 void LogPart::addDescriptionBySection( Description & description, FormattedDescription & formattedDescription,
194  string_view name, Args const &... args )
195 {
196  std::vector< string > values;
197  size_t & maxValueSize = formattedDescription.m_maxValueWidth;
198  size_t & maxNameSize = formattedDescription.m_maxNameWidth;
199  ( [&] {
200  static_assert( has_formatter_v< decltype(args) >,
201  "Argument passed cannot be converted to string" );
202  string const value = GEOS_FMT( "{}", args );
203 
204  std::vector< string_view > splitValues = divideLines< string_view >( maxValueSize, value );
205  values.insert( values.end(), splitValues.begin(), splitValues.end() );
206  } (), ...);
207 
208  description.m_values.push_back( values );
209 
210  size_t lineWidth = 0;
211  std::vector< string > nameDivided = divideLines< string >( lineWidth, name );
212  if( lineWidth == 0 )
213  lineWidth = name.size();
214  maxNameSize = std::max( maxNameSize, lineWidth );
215 
216  description.m_names.push_back( nameDivided );
217 
218  size_t const formattingCharSize = m_nbBorderChar * 2 + m_borderMargin * 2;
219  size_t const currentTotalWidth = maxNameSize + maxValueSize + formattingCharSize;
220  m_width = std::max( m_width, currentTotalWidth );
221  m_width = std::max( m_width, formattedDescription.m_title.size());
222 }
223 
224 template< typename ... Args >
225 void LogPart::addDescription( string_view name, Args const &... args )
226 {
227  addDescriptionBySection( m_startDescription, m_formattedStartDescription, name, args ... );
228 }
229 
230 template< typename ... Args >
231 void LogPart::addEndDescription( string_view name, Args const &... args )
232 {
233  addDescriptionBySection( m_endDescription, m_formattedEndDescription, name, args ... );
234 }
235 
236 }
237 
238 #endif
Class for displaying section for different steps of simulation.
Definition: LogPart.hpp:34
void addDescription(string_view description)
Add a description to the top logPart. No specific format will be apply the this description.
void setMinWidth(size_t const &minWidth)
Set the minimal width of the LogPart.
void addEndDescription(string_view name, Args const &... args)
Add a description to the bottom logPart by concatening a description name and descriptions values.
Definition: LogPart.hpp:231
void addDescription(string_view name, Args const &... args)
Add a description to the top LogPart.
Definition: LogPart.hpp:225
void enableOutput(bool enabled)
Toggles the CSV output feature.
Definition: LogPart.hpp:102
void end(std::ostream &oss=std::cout)
Draw the last part of the logPart. It include the title and optionnaly the end description(s).
void begin(std::ostream &os=std::cout)
Draw the first part of the logPart. It include the title and optionnaly the description(s).
LogPart(string_view logPartTitle, bool enableOutput)
Initialize a LogPart given a title.
void setMaxWidth(size_t const &maxWidth)
Set the maximal width of the LogPart.
void addEndDescription(string_view description)
Add a description to the top logPart. No specific format will be apply the this description.
std::string_view string_view
String type.
Definition: DataTypes.hpp:93