code formatting

This commit is contained in:
gabime 2018-04-06 04:06:02 +03:00
parent 4abe403544
commit 1dea46e1ab
3 changed files with 65 additions and 65 deletions

View File

@ -60,8 +60,8 @@ int main(int, char *[])
// Customize msg format for all messages
spd::set_pattern("[%^+++%$] [%H:%M:%S %z] [thread %t] %v");
console->info("This an info message with custom format (and custom color range between the '%^' and '%$')");
console->error("This an error message with custom format (and custom color range between the '%^' and '%$')");
console->error("This an error message with custom format (and custom color range between the '%^' and '%$')");
// Runtime log levels
spd::set_level(spd::level::info); // Set global log level to info
console->debug("This message should not be displayed!");

View File

@ -25,7 +25,7 @@ class wincolor_sink : public base_sink<Mutex>
public:
const WORD BOLD = FOREGROUND_INTENSITY;
const WORD RED = FOREGROUND_RED;
const WORD GREEN = FOREGROUND_GREEN;
const WORD GREEN = FOREGROUND_GREEN;
const WORD CYAN = FOREGROUND_GREEN | FOREGROUND_BLUE;
const WORD WHITE = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
const WORD YELLOW = FOREGROUND_RED | FOREGROUND_GREEN;
@ -60,22 +60,22 @@ public:
protected:
void _sink_it(const details::log_msg &msg) override
{
if (msg.color_range_end > msg.color_range_start)
{
// before color range
_print_range(msg, 0, msg.color_range_start);
// in color range
auto orig_attribs = set_console_attribs(colors_[msg.level]);
_print_range(msg, msg.color_range_start, msg.color_range_end);
::SetConsoleTextAttribute(out_handle_, orig_attribs); // reset to orig colors
// after color range
_print_range(msg, msg.color_range_end, msg.formatted.size());
}
else // print without colors if color range is invalid
{
_print_range(msg, 0, msg.formatted.size());
}
if (msg.color_range_end > msg.color_range_start)
{
// before color range
_print_range(msg, 0, msg.color_range_start);
// in color range
auto orig_attribs = set_console_attribs(colors_[msg.level]);
_print_range(msg, msg.color_range_start, msg.color_range_end);
::SetConsoleTextAttribute(out_handle_, orig_attribs); // reset to orig colors
// after color range
_print_range(msg, msg.color_range_end, msg.formatted.size());
}
else // print without colors if color range is invalid
{
_print_range(msg, 0, msg.formatted.size());
}
}
void _flush() override
@ -100,12 +100,12 @@ private:
return orig_buffer_info.wAttributes; // return orig attribs
}
// print a range of formatted message to console
void _print_range(const details::log_msg &msg, size_t start, size_t end)
{
DWORD size = static_cast<DWORD>(end - start);
WriteConsoleA(out_handle_, msg.formatted.data() + start, size, nullptr, nullptr);
}
// print a range of formatted message to console
void _print_range(const details::log_msg &msg, size_t start, size_t end)
{
DWORD size = static_cast<DWORD>(end - start);
WriteConsoleA(out_handle_, msg.formatted.data() + start, size, nullptr, nullptr);
}
};
//

View File

@ -7,10 +7,10 @@ static std::string log_to_str(const std::string &msg, const std::shared_ptr<spdl
auto oss_sink = std::make_shared<spdlog::sinks::ostream_sink_mt>(oss);
spdlog::logger oss_logger("pattern_tester", oss_sink);
oss_logger.set_level(spdlog::level::info);
if (formatter)
{
oss_logger.set_formatter(formatter);
}
if (formatter)
{
oss_logger.set_formatter(formatter);
}
oss_logger.info(msg);
return oss.str();
}
@ -65,60 +65,60 @@ TEST_CASE("date MM/DD/YY ", "[pattern_formatter]")
}
TEST_CASE("color range test1", "[pattern_formatter]")
{
auto formatter = std::make_shared<spdlog::pattern_formatter>("%^%v%$", spdlog::pattern_time_type::local, "\n");
spdlog::details::log_msg msg;
msg.raw << "Hello";
formatter->format(msg);
REQUIRE(msg.color_range_start == 0);
REQUIRE(msg.color_range_end == 5);
REQUIRE(log_to_str("hello", formatter) == "hello\n");
{
auto formatter = std::make_shared<spdlog::pattern_formatter>("%^%v%$", spdlog::pattern_time_type::local, "\n");
spdlog::details::log_msg msg;
msg.raw << "Hello";
formatter->format(msg);
REQUIRE(msg.color_range_start == 0);
REQUIRE(msg.color_range_end == 5);
REQUIRE(log_to_str("hello", formatter) == "hello\n");
}
TEST_CASE("color range test2", "[pattern_formatter]")
{
auto formatter = std::make_shared<spdlog::pattern_formatter>("%^%$", spdlog::pattern_time_type::local, "\n");
spdlog::details::log_msg msg;
formatter->format(msg);
REQUIRE(msg.color_range_start == 0);
REQUIRE(msg.color_range_end == 0);
REQUIRE(log_to_str("", formatter) == "\n");
auto formatter = std::make_shared<spdlog::pattern_formatter>("%^%$", spdlog::pattern_time_type::local, "\n");
spdlog::details::log_msg msg;
formatter->format(msg);
REQUIRE(msg.color_range_start == 0);
REQUIRE(msg.color_range_end == 0);
REQUIRE(log_to_str("", formatter) == "\n");
}
TEST_CASE("color range test3", "[pattern_formatter]")
{
auto formatter = std::make_shared<spdlog::pattern_formatter>("%^***%$");
spdlog::details::log_msg msg;
formatter->format(msg);
REQUIRE(msg.color_range_start == 0);
REQUIRE(msg.color_range_end == 3);
auto formatter = std::make_shared<spdlog::pattern_formatter>("%^***%$");
spdlog::details::log_msg msg;
formatter->format(msg);
REQUIRE(msg.color_range_start == 0);
REQUIRE(msg.color_range_end == 3);
}
TEST_CASE("color range test4", "[pattern_formatter]")
{
auto formatter = std::make_shared<spdlog::pattern_formatter>("XX%^YYY%$", spdlog::pattern_time_type::local, "\n");
spdlog::details::log_msg msg;
msg.raw << "ignored";
formatter->format(msg);
REQUIRE(msg.color_range_start == 2);
REQUIRE(msg.color_range_end == 5);
REQUIRE(log_to_str("ignored", formatter) == "XXYYY\n");
auto formatter = std::make_shared<spdlog::pattern_formatter>("XX%^YYY%$", spdlog::pattern_time_type::local, "\n");
spdlog::details::log_msg msg;
msg.raw << "ignored";
formatter->format(msg);
REQUIRE(msg.color_range_start == 2);
REQUIRE(msg.color_range_end == 5);
REQUIRE(log_to_str("ignored", formatter) == "XXYYY\n");
}
TEST_CASE("color range test5", "[pattern_formatter]")
{
auto formatter = std::make_shared<spdlog::pattern_formatter>("**%^");
spdlog::details::log_msg msg;
formatter->format(msg);
REQUIRE(msg.color_range_start == 2);
REQUIRE(msg.color_range_end == 0);
auto formatter = std::make_shared<spdlog::pattern_formatter>("**%^");
spdlog::details::log_msg msg;
formatter->format(msg);
REQUIRE(msg.color_range_start == 2);
REQUIRE(msg.color_range_end == 0);
}
TEST_CASE("color range test6", "[pattern_formatter]")
{
auto formatter = std::make_shared<spdlog::pattern_formatter>("**%$");
spdlog::details::log_msg msg;
formatter->format(msg);
REQUIRE(msg.color_range_start == 0);
REQUIRE(msg.color_range_end == 2);
auto formatter = std::make_shared<spdlog::pattern_formatter>("**%$");
spdlog::details::log_msg msg;
formatter->format(msg);
REQUIRE(msg.color_range_start == 0);
REQUIRE(msg.color_range_end == 2);
}