-
Notifications
You must be signed in to change notification settings - Fork 101
Expand file tree
/
Copy pathLogPart.cpp
More file actions
228 lines (188 loc) · 7.53 KB
/
LogPart.cpp
File metadata and controls
228 lines (188 loc) · 7.53 KB
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
* ------------------------------------------------------------------------------------------------------------
* SPDX-License-Identifier: LGPL-2.1-only
*
* Copyright (c) 2018-2020 Lawrence Livermore National Security LLC
* Copyright (c) 2018-2020 The Board of Trustees of the Leland Stanford Junior University
* Copyright (c) 2018-2020 TotalEnergies
* Copyright (c) 2019- GEOSX Contributors
* All rights reserved
*
* See top level LICENSE, COPYRIGHT, CONTRIBUTORS, NOTICE, and ACKNOWLEDGEMENTS files for details.
* ------------------------------------------------------------------------------------------------------------
*/
/**
* @file LogPart.cpp
*/
#include "LogPart.hpp"
#include "common/format/StringUtilities.hpp"
#include "common/logger/ErrorHandling.hpp"
#include <algorithm>
using namespace geos::stringutilities;
namespace geos
{
LogPart::LogPart( string_view logpartName, bool enableOutput )
{
m_formattedStartDescription.m_title = logpartName;
m_formattedEndDescription.m_title = GEOS_FMT( "{}{}", m_prefixEndTitle, logpartName );
m_enableOutput = enableOutput;
ErrorLogger::global().setCurrentLogPart( std::string( logpartName ) );
}
void LogPart::addDescription( string_view description )
{
size_t compareWidth = m_width;
m_startDescription.m_names.push_back( stringutilities::divideLines< string >( compareWidth, description ) );
m_startDescription.m_values.push_back( stdVector< string >() );
}
void LogPart::addEndDescription( string_view description )
{
size_t compareWidth = m_width;
m_endDescription.m_names.push_back( stringutilities::divideLines< string >( compareWidth, description ) );
m_endDescription.m_values.push_back( stdVector< string >() );
}
void LogPart::setMinWidth( size_t const & minWidth )
{
m_minWidth = minWidth;
}
void LogPart::setMaxWidth( size_t const & maxWidth )
{
m_maxWidth = maxWidth;
}
double clamp( double v, double min, double max )
{
return std::min( max, std::max( min, v ));
}
void LogPart::formatDescriptions( LogPart::Description & description,
FormattedDescription & formattedDescription )
{
stdVector< string > & formattedLines = formattedDescription.m_lines;
size_t const borderSpaceWidth = m_nbBorderChar * 2 + m_borderMargin * 2;
size_t const formattingCharSize = borderSpaceWidth;
size_t & maxNameSize = formattedDescription.m_maxNameWidth;
size_t & maxValueSize = formattedDescription.m_maxValueWidth;
formattedLines.reserve( description.m_names.size() * 2 );
/// clamp
m_width = std::min( m_maxWidth, std::max( m_minWidth, m_width ));
for( size_t idxName = 0; idxName < description.m_names.size(); idxName++ )
{
auto const & nonFormattedNames = description.m_names[idxName];
auto const & nonFormattedValues = description.m_values[idxName];
// Format name with no values associated
if( nonFormattedValues.empty())
{
size_t maxLineLength = m_width - borderSpaceWidth;
auto wrappedNames = stringutilities::wrapTextToMaxLength( nonFormattedNames, maxLineLength );
for( auto & name : wrappedNames )
{
auto const currMaxNameSize = std::max( name.size(), maxNameSize );
if( currMaxNameSize + formattingCharSize < m_width )
{
// append space at the end of name if needed
name.reserve( m_width - borderSpaceWidth );
name.append( std::string( m_width - currMaxNameSize - formattingCharSize, ' ' ));
}
formattedLines.push_back( name );
}
continue;
}
// Format name with values assiociated
size_t maxLineLength = m_width - maxNameSize - formattingCharSize - m_delimiter.size();
auto wrappedValues = stringutilities::wrapTextToMaxLength( nonFormattedValues, maxLineLength );
// format name
stdVector< string > formatNames {nonFormattedNames};
for( size_t idxSubName = 0; idxSubName < formatNames.size(); idxSubName++ )
{
size_t const spaces = idxSubName < wrappedValues.size() ?
maxNameSize - formatNames[idxSubName].size() :
m_width - formatNames[idxSubName].size() - formattingCharSize;
// append space at the end of name if needed
formatNames[idxSubName].reserve( formatNames[idxSubName].size() + spaces );
formatNames[idxSubName].append( spaces, ' ' );
}
size_t const lineCount = std::max( formatNames.size(), wrappedValues.size());
// format values
size_t const minValueSizeRequired = m_width - maxNameSize - formattingCharSize - m_delimiter.size();
for( auto & wrappedValue : wrappedValues )
{
wrappedValue.reserve( minValueSizeRequired );
wrappedValue.append( minValueSizeRequired - wrappedValue.size(), ' ' );
}
maxValueSize = std::max( maxValueSize,
(std::max_element( wrappedValues.begin(), wrappedValues.end() ))->size() );
// add the first line
string firstLine;
firstLine.reserve( formatNames.front().size() + m_delimiter.size() + wrappedValues.front().size());
firstLine.append( formatNames.front()).append( m_delimiter ).append( wrappedValues.front());
formattedLines.push_back( firstLine );
// combination name + value
for( size_t idxLine = 1; idxLine < lineCount; ++idxLine )
{
if( idxLine < formatNames.size() && idxLine < wrappedValues.size())
{ // name + value
formattedLines.push_back( GEOS_FMT( "{}{}{}", formatNames[idxLine], m_delimiter, wrappedValues[idxLine] ));
}
else if( idxLine < formatNames.size())
{ // subnames remaining
formattedLines.push_back( formatNames[idxLine] );
}
else if( idxLine < wrappedValues.size())
{ // subvalues remaining
size_t const spaceAvailable = maxNameSize + wrappedValues[idxLine].size() + m_delimiter.size();
formattedLines.push_back( GEOS_FMT( "{:>{}}", wrappedValues[idxLine], spaceAvailable ));
}
}
}
}
string LogPart::outputDescription( FormattedDescription & formattedDescription )
{
std::ostringstream oss;
string const borderCharacters = string( m_nbBorderChar, m_borderCharacter );
string const borderSpaces = string( m_borderMargin, ' ' );
for( auto const & line : formattedDescription.m_lines )
{
oss << borderCharacters;
oss << borderSpaces << line << borderSpaces;
oss << borderCharacters << '\n';
}
return oss.str();
}
string LogPart::outputTitle( LogPart::FormattedDescription & formattedDescription )
{
size_t const titleRowLength = m_width;
string const borderCharacters = string( m_nbBorderChar, m_borderCharacter );
return GEOS_FMT( "\n{}{:^{}}{}\n",
borderCharacters,
formattedDescription.m_title,
titleRowLength - 4,
borderCharacters );
}
void LogPart::begin( std::ostream & os )
{
if( !m_enableOutput )
return;
if( !m_startDescription.m_names.empty())
{
formatDescriptions( m_startDescription, m_formattedStartDescription );
}
string const line = string( m_width, m_borderCharacter );
os << '\n' << line;
os << outputTitle( m_formattedStartDescription );
os << line << '\n';
os << outputDescription( m_formattedStartDescription ) << '\n';
}
void LogPart::end( std::ostream & os )
{
if( !m_enableOutput )
return;
formatDescriptions( m_endDescription, m_formattedEndDescription );
string const line = string( m_width, m_borderCharacter );
if( !m_endDescription.m_names.empty() )
{
os << '\n';
os << outputDescription( m_formattedEndDescription );
os << line;
}
os << outputTitle( m_formattedEndDescription );
os << line << "\n\n";
}
}