|
|
@@ -50,55 +50,48 @@ namespace logging {
|
|
|
}
|
|
|
|
|
|
string_generator string_token(std::string str) {
|
|
|
- return [=](logpacket const &){
|
|
|
- return str;
|
|
|
- };
|
|
|
+ return [=](logpacket const &){ return str; };
|
|
|
}
|
|
|
|
|
|
string_generator handle( char const * & next ) {
|
|
|
if (is('c')) {
|
|
|
- return [](logpacket const & lp){
|
|
|
- return lp.logger;
|
|
|
- };
|
|
|
+ return [](logpacket const & lp){ return lp.logger; };
|
|
|
} else if (is('p')) {
|
|
|
- return [](logpacket const & lp){
|
|
|
- return to_string(lp.level, true);
|
|
|
- };
|
|
|
+ return [](logpacket const & lp){ return to_string(lp.level, true); };
|
|
|
} else if (is('m')) {
|
|
|
- return [](logpacket const & lp){
|
|
|
- return lp.message.str();
|
|
|
- };
|
|
|
+ return [](logpacket const & lp){ return lp.message.str(); };
|
|
|
} else {
|
|
|
std::string error_msg{"unknown format character: '"};
|
|
|
throw unknown_format_specifier{error_msg + *next + "'"};
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- format::generator parse_with_bounds( char const * & next ) {
|
|
|
+ int eat(char const * & token) {
|
|
|
+ size_t chars = 0;
|
|
|
+ int rval = std::stoi(token, &chars);
|
|
|
+ token += chars;
|
|
|
+ return rval;
|
|
|
+ }
|
|
|
+
|
|
|
+ std::pair<int, size_t> get_bounds(char const * & next) {
|
|
|
+ int min = *next == '.' ? 0 : eat(next);
|
|
|
+ size_t max = *next == '.' ? eat(++next) : std::string::npos;
|
|
|
+ return std::make_pair(min, max);
|
|
|
+ }
|
|
|
+
|
|
|
+ format::generator parse_with_bounds(char const * & next) {
|
|
|
bool const is_left = *next == '-';
|
|
|
auto align = is_left ? std::left : std::right;
|
|
|
- bool const trunc = *next == '.';
|
|
|
- if ( is_left || trunc ) ++next;
|
|
|
+ if (is_left) ++next;
|
|
|
|
|
|
- string_generator gen;
|
|
|
- size_t chars = 0;
|
|
|
- int min = std::stoi( next, &chars );
|
|
|
- size_t max = 0xFFFFFFFF;
|
|
|
- next += chars;
|
|
|
- if ( trunc ) {
|
|
|
- max = min;
|
|
|
- min = 0;
|
|
|
- } else if ( *next == '.' ) {
|
|
|
- max = std::stoi( next + 1, &chars );
|
|
|
- next += chars + 1;
|
|
|
- }
|
|
|
- gen = handle( next );
|
|
|
+ auto bounds = get_bounds(next);
|
|
|
+ string_generator gen = handle(next);
|
|
|
|
|
|
return [=](logpacket const & lp, std::ostream & out) {
|
|
|
std::string str = gen(lp);
|
|
|
- if (str.length() > max)
|
|
|
- str.erase(str.begin()+max, str.end());
|
|
|
- out << align << std::setw(min) << str;
|
|
|
+ if (str.length() > bounds.second)
|
|
|
+ str.erase(str.begin()+bounds.second, str.end());
|
|
|
+ out << align << std::setw(bounds.first) << str;
|
|
|
};
|
|
|
}
|
|
|
|