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  {
118  };
119 
123  struct FormattedDescription
124  {
126  string m_title;
128  stdVector< 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 = 100;
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  stdVector< string > values;
197  size_t & formattedDescriptionMaxWidth = formattedDescription.m_maxValueWidth;
198  size_t & formattedDescriptionNameWidth = 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  stdVector< string_view > dividedDescriptionValues =
205  divideLines< string_view >( formattedDescriptionMaxWidth, value );
206  values.insert( values.end(), dividedDescriptionValues.begin(), dividedDescriptionValues.end() );
207  } (), ...);
208 
209  description.m_values.push_back( values );
210 
211  size_t nameWidth = 0;
212  stdVector< string > nameLines = divideLines< string >( nameWidth, name );
213  if( nameWidth == 0 )
214  nameWidth = name.size();
215  formattedDescriptionNameWidth = std::max( formattedDescriptionNameWidth, nameWidth );
216 
217  description.m_names.push_back( nameLines );
218 
219  size_t const totalDecorationWidth = m_nbBorderChar * 2 + m_borderMargin * 2;
220  size_t const logPartTotalWidth = formattedDescriptionNameWidth +
221  formattedDescriptionMaxWidth +
222  totalDecorationWidth;
223  m_width = std::max( m_width, logPartTotalWidth );
224  m_width = std::max( m_width, formattedDescription.m_title.size());
225 }
226 
227 template< typename ... Args >
228 void LogPart::addDescription( string_view name, Args const &... args )
229 {
230  addDescriptionBySection( m_startDescription, m_formattedStartDescription, name, args ... );
231 }
232 
233 template< typename ... Args >
234 void LogPart::addEndDescription( string_view name, Args const &... args )
235 {
236  addDescriptionBySection( m_endDescription, m_formattedEndDescription, name, args ... );
237 }
238 
239 }
240 
241 #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:234
void addDescription(string_view name, Args const &... args)
Add a description to the top LogPart.
Definition: LogPart.hpp:228
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
internal::StdVectorWrapper< T, Allocator, USE_STD_CONTAINER_BOUNDS_CHECKING > stdVector