|
| 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 | + |
| 15 | +/** |
| 16 | + * @file LogPart.cpp |
| 17 | + */ |
| 18 | + |
| 19 | +#include "LogPart.hpp" |
| 20 | +#include "common/format/StringUtilities.hpp" |
| 21 | +#include <algorithm> |
| 22 | + |
| 23 | +using namespace geos::stringutilities; |
| 24 | +namespace geos |
| 25 | +{ |
| 26 | + |
| 27 | +LogPart::LogPart( string_view logPartTitle, bool enableOutput ) |
| 28 | +{ |
| 29 | + m_formattedStartDescription.m_title = logPartTitle; |
| 30 | + m_formattedEndDescription.m_title = GEOS_FMT( "{}{}", m_prefixEndTitle, logPartTitle ); |
| 31 | + |
| 32 | + m_enableOutput = enableOutput; |
| 33 | +} |
| 34 | + |
| 35 | +void LogPart::addDescription( string_view description ) |
| 36 | +{ |
| 37 | + size_t compareWidth = m_width; |
| 38 | + m_startDescription.m_names.push_back( stringutilities::divideLines< string >( compareWidth, description ) ); |
| 39 | + m_startDescription.m_values.push_back( std::vector< string >() ); |
| 40 | +} |
| 41 | + |
| 42 | +void LogPart::addEndDescription( string_view description ) |
| 43 | +{ |
| 44 | + size_t compareWidth = m_width; |
| 45 | + m_endDescription.m_names.push_back( stringutilities::divideLines< string >( compareWidth, description ) ); |
| 46 | + m_endDescription.m_values.push_back( std::vector< string >() ); |
| 47 | + |
| 48 | +} |
| 49 | + |
| 50 | + |
| 51 | +void LogPart::setMinWidth( size_t const & minWidth ) |
| 52 | +{ |
| 53 | + m_minWidth = minWidth; |
| 54 | +} |
| 55 | + |
| 56 | +void LogPart::setMaxWidth( size_t const & maxWidth ) |
| 57 | +{ |
| 58 | + m_maxWidth = maxWidth; |
| 59 | +} |
| 60 | + |
| 61 | + |
| 62 | +double clamp( double v, double min, double max ) |
| 63 | +{ |
| 64 | + return std::min( max, std::max( min, v )); |
| 65 | +} |
| 66 | + |
| 67 | +void LogPart::formatDescriptions( LogPart::Description & description, |
| 68 | + FormattedDescription & formattedDescription ) |
| 69 | +{ |
| 70 | + std::vector< string > & formattedLines = formattedDescription.m_lines; |
| 71 | + size_t const borderSpaceWidth = m_nbBorderChar * 2 + m_borderMargin * 2; |
| 72 | + |
| 73 | + size_t const formattingCharSize = borderSpaceWidth; |
| 74 | + size_t & maxNameSize = formattedDescription.m_maxNameWidth; |
| 75 | + size_t & maxValueSize = formattedDescription.m_maxValueWidth; |
| 76 | + |
| 77 | + formattedLines.reserve( description.m_names.size() * 2 ); |
| 78 | + |
| 79 | + /// clamp |
| 80 | + m_width = std::min( m_maxWidth, std::max( m_minWidth, m_width )); |
| 81 | + |
| 82 | + for( size_t idxName = 0; idxName < description.m_names.size(); idxName++ ) |
| 83 | + { |
| 84 | + auto const & nonFormattedNames = description.m_names[idxName]; |
| 85 | + auto const & nonFormattedValues = description.m_values[idxName]; |
| 86 | + |
| 87 | + // Format name with no values associated |
| 88 | + if( nonFormattedValues.empty()) |
| 89 | + { |
| 90 | + size_t maxLineLength = m_width - borderSpaceWidth; |
| 91 | + auto wrappedNames = stringutilities::wrapTextToMaxLength( nonFormattedNames, maxLineLength ); |
| 92 | + |
| 93 | + for( auto & name : wrappedNames ) |
| 94 | + { |
| 95 | + auto const currMaxNameSize = std::max( name.size(), maxNameSize ); |
| 96 | + if( currMaxNameSize + formattingCharSize < m_width ) |
| 97 | + { |
| 98 | + // append space at the end of name if needed |
| 99 | + name.reserve( m_width - borderSpaceWidth ); |
| 100 | + name.append( std::string( m_width - currMaxNameSize - formattingCharSize, ' ' )); |
| 101 | + } |
| 102 | + formattedLines.push_back( name ); |
| 103 | + } |
| 104 | + continue; |
| 105 | + } |
| 106 | + |
| 107 | + // Format name with values assiociated |
| 108 | + size_t maxLineLength = m_width - maxNameSize - formattingCharSize - m_delimiter.size(); |
| 109 | + auto wrappedValues = stringutilities::wrapTextToMaxLength( nonFormattedValues, maxLineLength ); |
| 110 | + |
| 111 | + // format name |
| 112 | + std::vector< string > formatNames {nonFormattedNames}; |
| 113 | + for( size_t idxSubName = 0; idxSubName < formatNames.size(); idxSubName++ ) |
| 114 | + { |
| 115 | + size_t const spaces = idxSubName < wrappedValues.size() ? |
| 116 | + maxNameSize - formatNames[idxSubName].size() : |
| 117 | + m_width - formatNames[idxSubName].size() - formattingCharSize; |
| 118 | + // append space at the end of name if needed |
| 119 | + formatNames[idxSubName].reserve( formatNames[idxSubName].size() + spaces ); |
| 120 | + formatNames[idxSubName].append( spaces, ' ' ); |
| 121 | + } |
| 122 | + |
| 123 | + size_t const lineCount = std::max( formatNames.size(), wrappedValues.size()); |
| 124 | + |
| 125 | + // format values |
| 126 | + size_t const minValueSizeRequired = m_width - maxNameSize - formattingCharSize - m_delimiter.size(); |
| 127 | + for( auto & wrappedValue : wrappedValues ) |
| 128 | + { |
| 129 | + wrappedValue.reserve( minValueSizeRequired ); |
| 130 | + wrappedValue.append( minValueSizeRequired - wrappedValue.size(), ' ' ); |
| 131 | + } |
| 132 | + maxValueSize = std::max( maxValueSize, |
| 133 | + (std::max_element( wrappedValues.begin(), wrappedValues.end() ))->size() ); |
| 134 | + |
| 135 | + // add the first line |
| 136 | + string firstLine; |
| 137 | + firstLine.reserve( formatNames.front().size() + m_delimiter.size() + wrappedValues.front().size()); |
| 138 | + firstLine.append( formatNames.front()).append( m_delimiter ).append( wrappedValues.front()); |
| 139 | + formattedLines.push_back( firstLine ); |
| 140 | + |
| 141 | + // combination name + value |
| 142 | + for( size_t idxLine = 1; idxLine < lineCount; ++idxLine ) |
| 143 | + { |
| 144 | + if( idxLine < formatNames.size() && idxLine < wrappedValues.size()) |
| 145 | + { // name + value |
| 146 | + formattedLines.push_back( GEOS_FMT( "{}{}{}", formatNames[idxLine], m_delimiter, wrappedValues[idxLine] )); |
| 147 | + } |
| 148 | + else if( idxLine < formatNames.size()) |
| 149 | + { // subnames remaining |
| 150 | + formattedLines.push_back( formatNames[idxLine] ); |
| 151 | + } |
| 152 | + else if( idxLine < wrappedValues.size()) |
| 153 | + { // subvalues remaining |
| 154 | + size_t const spaceAvailable = maxNameSize + wrappedValues[idxLine].size() + m_delimiter.size(); |
| 155 | + formattedLines.push_back( GEOS_FMT( "{:>{}}", wrappedValues[idxLine], spaceAvailable )); |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +string LogPart::outputDescription( FormattedDescription & formattedDescription ) |
| 162 | +{ |
| 163 | + std::ostringstream oss; |
| 164 | + string const borderCharacters = string( m_nbBorderChar, m_borderCharacter ); |
| 165 | + string const borderSpaces = string( m_borderMargin, ' ' ); |
| 166 | + |
| 167 | + for( auto const & line : formattedDescription.m_lines ) |
| 168 | + { |
| 169 | + oss << borderCharacters; |
| 170 | + oss << borderSpaces << line << borderSpaces; |
| 171 | + oss << borderCharacters << '\n'; |
| 172 | + } |
| 173 | + return oss.str(); |
| 174 | +} |
| 175 | + |
| 176 | +string LogPart::outputTitle( LogPart::FormattedDescription & formattedDescription ) |
| 177 | +{ |
| 178 | + size_t const titleRowLength = m_width; |
| 179 | + string const borderCharacters = string( m_nbBorderChar, m_borderCharacter ); |
| 180 | + |
| 181 | + return GEOS_FMT( "\n{}{:^{}}{}\n", |
| 182 | + borderCharacters, |
| 183 | + formattedDescription.m_title, |
| 184 | + titleRowLength - 4, |
| 185 | + borderCharacters ); |
| 186 | +} |
| 187 | + |
| 188 | +void LogPart::begin( std::ostream & os ) |
| 189 | +{ |
| 190 | + if( !m_enableOutput ) |
| 191 | + return; |
| 192 | + |
| 193 | + |
| 194 | + if( !m_startDescription.m_names.empty()) |
| 195 | + { |
| 196 | + formatDescriptions( m_startDescription, m_formattedStartDescription ); |
| 197 | + } |
| 198 | + |
| 199 | + string const line = string( m_width, m_borderCharacter ); |
| 200 | + os << '\n' << line; |
| 201 | + os << outputTitle( m_formattedStartDescription ); |
| 202 | + os << line << '\n'; |
| 203 | + os << outputDescription( m_formattedStartDescription ) << '\n'; |
| 204 | +} |
| 205 | + |
| 206 | +void LogPart::end( std::ostream & os ) |
| 207 | +{ |
| 208 | + if( !m_enableOutput ) |
| 209 | + return; |
| 210 | + |
| 211 | + formatDescriptions( m_endDescription, m_formattedEndDescription ); |
| 212 | + |
| 213 | + string const line = string( m_width, m_borderCharacter ); |
| 214 | + if( !m_endDescription.m_names.empty() ) |
| 215 | + { |
| 216 | + os << '\n'; |
| 217 | + os << outputDescription( m_formattedEndDescription ); |
| 218 | + os << line; |
| 219 | + } |
| 220 | + os << outputTitle( m_formattedEndDescription ); |
| 221 | + os << line << "\n\n"; |
| 222 | +} |
| 223 | + |
| 224 | +} |
0 commit comments