| 17 | |
| 18 | |
| 19 | std::string debug_format_message(debug_level::flag level, const std::string& domain, |
| 20 | debug_format::flags& format_flags, int indent_level, bool is_first_line, const std::string& msg) |
| 21 | { |
| 22 | if (msg.empty()) |
| 23 | return msg; |
| 24 | |
| 25 | std::string ret; |
| 26 | ret.reserve(msg.size() + 40); // indentation + domain/level |
| 27 | |
| 28 | // allow prefix only for first line and others when first_line_only is disabled |
| 29 | if (is_first_line || !format_flags.test(debug_format::first_line_only)) { |
| 30 | |
| 31 | if (format_flags.test(debug_format::datetime)) { // print time |
| 32 | ret += hz::format_date("%Y-%m-%d %H:%M:%S: ", true); |
| 33 | } |
| 34 | |
| 35 | if (format_flags.test(debug_format::level)) { // print level name |
| 36 | const bool use_color = format_flags.test(debug_format::color); |
| 37 | if (use_color) |
| 38 | ret += debug_level::get_color_start(level); |
| 39 | |
| 40 | const std::string level_name = std::string("<") + debug_level::get_name(level) + ">"; |
| 41 | ret += level_name + std::string(8 - level_name.size(), ' '); |
| 42 | |
| 43 | if (use_color) |
| 44 | ret += debug_level::get_color_stop(level); |
| 45 | } |
| 46 | |
| 47 | if (format_flags.test(debug_format::domain)) { // print domain name |
| 48 | ret += std::string("[") + domain + "] "; |
| 49 | } |
| 50 | |
| 51 | } |
| 52 | |
| 53 | |
| 54 | if (format_flags.test(debug_format::indent)) { |
| 55 | const std::string spaces(static_cast<std::size_t>(indent_level * 4), ' '); // indentation spaces |
| 56 | |
| 57 | // replace all newlines with \n(indent-spaces) except for the last one. |
| 58 | std::string::size_type oldpos = 0, pos = 0; |
| 59 | while(pos != std::string::npos) { |
| 60 | if (pos == 0) { |
| 61 | ret.insert(0, spaces); |
| 62 | } else { |
| 63 | ret.append(spaces); |
| 64 | } |
| 65 | pos = msg.find('\n', oldpos); |
| 66 | if (pos != std::string::npos) |
| 67 | pos++; |
| 68 | if (pos >= msg.size()) |
| 69 | pos = std::string::npos; |
| 70 | |
| 71 | ret.append(msg, oldpos, pos - oldpos); |
| 72 | oldpos = pos; |
| 73 | } |
| 74 | |
| 75 | } else { |
| 76 | ret += msg; |
no test coverage detected