3 #include <boost/algorithm/string/classification.hpp>
4 #include <boost/algorithm/string/split.hpp>
6 #include "casm/external/MersenneTwister/MersenneTwister.h"
22 Log::Log(std::ostream &_ostream,
int _verbosity,
bool _show_clock,
24 : m_verbosity(_verbosity),
25 m_show_clock(_show_clock),
26 m_indent_space(_indent_space),
29 m_paragraph_width(100),
31 m_ostream(&_ostream) {
42 using namespace boost::chrono;
43 auto curr_time = steady_clock::now();
50 using namespace boost::chrono;
51 auto curr_time = steady_clock::now();
63 std::vector<int> index;
64 for (
int i = 0; i < n; ++i) {
71 for (
int i = 0; i < c; ++i) {
72 choice = mtrand.randInt(
s - 1);
73 res.push_back(index[choice]);
74 swap(index[
s - 1], index[choice]);
85 curr_width + line.size() - 1 >=
width()) {
94 throw std::runtime_error(
"Log print justification error");
101 for (
int i = 0; i < line.size(); ++i) {
113 std::stringstream ss;
114 for (
int i = 0; i < line.size(); ++i) {
121 *
this << std::string(
width() -
indent_str().size() - ss.str().size(),
' ')
122 << ss.str() << std::endl;
129 std::stringstream ss;
130 for (
int i = 0; i < line.size(); ++i) {
136 std::string str = ss.str();
137 std::string before = std::string((
width() - str.size()) / 2,
' ');
138 std::string after = std::string(
width() - before.size() - str.size(),
' ');
139 *
this << before << str << after << std::endl;
146 while (
width() - curr_width >= line.size() - 1) {
147 for (
int i = 0; i < line.size() - 1; ++i) {
154 MTRand mtrand(curr_width);
155 std::vector<int> index =
157 for (
int i = 0; i < index.size(); ++i) {
161 for (
auto &word : line) {
169 std::vector<std::string> words;
170 boost::split(words, text, boost::is_any_of(
" "), boost::token_compress_on);
174 std::vector<std::string> line;
175 for (
int i = 0; i < words.size(); ++i) {
176 if (line.size() == 0 ||
177 curr_width + line.size() + words[i].size() <=
width()) {
178 line.push_back(words[i]);
179 curr_width += words[i].size();
186 line.push_back(words[i]);
187 curr_width =
indent_str().size() + words[i].size();
202 std::istringstream input;
204 std::string first_line;
205 if (std::getline(input, first_line)) {
206 if (indent_first_line) {
209 *
this << first_line << std::endl;
210 for (std::string line; std::getline(input, line);) {
211 indent() << line << std::endl;
217 Log::operator std::ostream &() {
return ostream(); }
220 return std::string(
"Error: Received '") +
s +
221 "', expected one of 'none', 'quiet', 'standard', 'verbose', 'debug', "
222 "or an int in range [0, 100]";
232 auto is_int = [](std::string
s) {
234 if (
s.empty() || !isdigit(
s[0])) {
235 return std::make_pair(
false, val);
238 val = strtol(
s.c_str(), &p, 10);
239 return std::make_pair(*p == 0 && val >= 0 && val <= 100, val);
242 auto res = is_int(
s);
245 }
else if (
s ==
"none") {
246 return std::make_pair(
true, 0);
247 }
else if (
s ==
"quiet") {
248 return std::make_pair(
true, 5);
249 }
else if (
s ==
"standard") {
250 return std::make_pair(
true, 10);
251 }
else if (
s ==
"verbose") {
252 return std::make_pair(
true, 20);
253 }
else if (
s ==
"debug") {
254 return std::make_pair(
true, 100);
256 return std::make_pair(
false, 0);
272 fptr(
static_cast<std::ostream &
>(
log));
std::set< std::string > & s
FixedLog(std::ostream &_ostream)
boost::chrono::steady_clock::time_point m_lap_start_time
bool m_print
Whether to print.
static std::string invalid_verbosity_msg(std::string s)
void _print_justified_line(std::vector< std::string > &line, int curr_width)
std::string indent_str() const
Log(std::ostream &_ostream=std::cout, int _verbosity=standard, bool _show_clock=false, int _indent_space=2)
Construct a Log.
void set_verbosity(int _verbosity)
void _print_center_justified_line(std::vector< std::string > &line, int curr_width)
boost::chrono::steady_clock::time_point m_start_time
JustificationType justification()
void begin_section()
Begin a section, without header.
friend Log & operator<<(Log &log, const T &msg_details)
int m_verbosity
If m_verbosity >= required verbosity, then print.
Log & verbatim(std::string text, bool indent_first_line=true)
Print verbatim, but with indentation (optional on first line)
void reset(std::ostream &_ostream=std::cout)
Log & paragraph(std::string text)
Print indented paragraph with wrapping at Log::width()
void _print_full_justified_line(std::vector< std::string > &line, int curr_width)
static const int standard
static std::pair< bool, int > verbosity_level(std::string s)
Read verbosity level from a string.
void _print_right_justified_line(std::vector< std::string > &line, int curr_width)
void _print_left_justified_line(std::vector< std::string > &line, int curr_width)
void swap(ConfigDoF &A, ConfigDoF &B)
std::vector< int > rand_unique(int n, int c, MTRand &mtrand)
Choose c random unique numbers in [0,n)
virtual void print(Log &log) const override
virtual void print(Log &log) const override