mirror of
https://github.com/gabime/spdlog.git
synced 2024-12-26 02:21:34 +08:00
code formatting
This commit is contained in:
parent
4abe403544
commit
1dea46e1ab
@ -60,7 +60,7 @@ int main(int, char *[])
|
|||||||
// Customize msg format for all messages
|
// Customize msg format for all messages
|
||||||
spd::set_pattern("[%^+++%$] [%H:%M:%S %z] [thread %t] %v");
|
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->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
|
// Runtime log levels
|
||||||
spd::set_level(spd::level::info); // Set global log level to info
|
spd::set_level(spd::level::info); // Set global log level to info
|
||||||
|
@ -25,7 +25,7 @@ class wincolor_sink : public base_sink<Mutex>
|
|||||||
public:
|
public:
|
||||||
const WORD BOLD = FOREGROUND_INTENSITY;
|
const WORD BOLD = FOREGROUND_INTENSITY;
|
||||||
const WORD RED = FOREGROUND_RED;
|
const WORD RED = FOREGROUND_RED;
|
||||||
const WORD GREEN = FOREGROUND_GREEN;
|
const WORD GREEN = FOREGROUND_GREEN;
|
||||||
const WORD CYAN = FOREGROUND_GREEN | FOREGROUND_BLUE;
|
const WORD CYAN = FOREGROUND_GREEN | FOREGROUND_BLUE;
|
||||||
const WORD WHITE = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
|
const WORD WHITE = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
|
||||||
const WORD YELLOW = FOREGROUND_RED | FOREGROUND_GREEN;
|
const WORD YELLOW = FOREGROUND_RED | FOREGROUND_GREEN;
|
||||||
@ -60,22 +60,22 @@ public:
|
|||||||
protected:
|
protected:
|
||||||
void _sink_it(const details::log_msg &msg) override
|
void _sink_it(const details::log_msg &msg) override
|
||||||
{
|
{
|
||||||
if (msg.color_range_end > msg.color_range_start)
|
if (msg.color_range_end > msg.color_range_start)
|
||||||
{
|
{
|
||||||
// before color range
|
// before color range
|
||||||
_print_range(msg, 0, msg.color_range_start);
|
_print_range(msg, 0, msg.color_range_start);
|
||||||
|
|
||||||
// in color range
|
// in color range
|
||||||
auto orig_attribs = set_console_attribs(colors_[msg.level]);
|
auto orig_attribs = set_console_attribs(colors_[msg.level]);
|
||||||
_print_range(msg, msg.color_range_start, msg.color_range_end);
|
_print_range(msg, msg.color_range_start, msg.color_range_end);
|
||||||
::SetConsoleTextAttribute(out_handle_, orig_attribs); // reset to orig colors
|
::SetConsoleTextAttribute(out_handle_, orig_attribs); // reset to orig colors
|
||||||
// after color range
|
// after color range
|
||||||
_print_range(msg, msg.color_range_end, msg.formatted.size());
|
_print_range(msg, msg.color_range_end, msg.formatted.size());
|
||||||
}
|
}
|
||||||
else // print without colors if color range is invalid
|
else // print without colors if color range is invalid
|
||||||
{
|
{
|
||||||
_print_range(msg, 0, msg.formatted.size());
|
_print_range(msg, 0, msg.formatted.size());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void _flush() override
|
void _flush() override
|
||||||
@ -100,12 +100,12 @@ private:
|
|||||||
return orig_buffer_info.wAttributes; // return orig attribs
|
return orig_buffer_info.wAttributes; // return orig attribs
|
||||||
}
|
}
|
||||||
|
|
||||||
// print a range of formatted message to console
|
// print a range of formatted message to console
|
||||||
void _print_range(const details::log_msg &msg, size_t start, size_t end)
|
void _print_range(const details::log_msg &msg, size_t start, size_t end)
|
||||||
{
|
{
|
||||||
DWORD size = static_cast<DWORD>(end - start);
|
DWORD size = static_cast<DWORD>(end - start);
|
||||||
WriteConsoleA(out_handle_, msg.formatted.data() + start, size, nullptr, nullptr);
|
WriteConsoleA(out_handle_, msg.formatted.data() + start, size, nullptr, nullptr);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
//
|
//
|
||||||
|
@ -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);
|
auto oss_sink = std::make_shared<spdlog::sinks::ostream_sink_mt>(oss);
|
||||||
spdlog::logger oss_logger("pattern_tester", oss_sink);
|
spdlog::logger oss_logger("pattern_tester", oss_sink);
|
||||||
oss_logger.set_level(spdlog::level::info);
|
oss_logger.set_level(spdlog::level::info);
|
||||||
if (formatter)
|
if (formatter)
|
||||||
{
|
{
|
||||||
oss_logger.set_formatter(formatter);
|
oss_logger.set_formatter(formatter);
|
||||||
}
|
}
|
||||||
oss_logger.info(msg);
|
oss_logger.info(msg);
|
||||||
return oss.str();
|
return oss.str();
|
||||||
}
|
}
|
||||||
@ -66,59 +66,59 @@ TEST_CASE("date MM/DD/YY ", "[pattern_formatter]")
|
|||||||
|
|
||||||
TEST_CASE("color range test1", "[pattern_formatter]")
|
TEST_CASE("color range test1", "[pattern_formatter]")
|
||||||
{
|
{
|
||||||
auto formatter = std::make_shared<spdlog::pattern_formatter>("%^%v%$", spdlog::pattern_time_type::local, "\n");
|
auto formatter = std::make_shared<spdlog::pattern_formatter>("%^%v%$", spdlog::pattern_time_type::local, "\n");
|
||||||
spdlog::details::log_msg msg;
|
spdlog::details::log_msg msg;
|
||||||
msg.raw << "Hello";
|
msg.raw << "Hello";
|
||||||
formatter->format(msg);
|
formatter->format(msg);
|
||||||
REQUIRE(msg.color_range_start == 0);
|
REQUIRE(msg.color_range_start == 0);
|
||||||
REQUIRE(msg.color_range_end == 5);
|
REQUIRE(msg.color_range_end == 5);
|
||||||
REQUIRE(log_to_str("hello", formatter) == "hello\n");
|
REQUIRE(log_to_str("hello", formatter) == "hello\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("color range test2", "[pattern_formatter]")
|
TEST_CASE("color range test2", "[pattern_formatter]")
|
||||||
{
|
{
|
||||||
auto formatter = std::make_shared<spdlog::pattern_formatter>("%^%$", spdlog::pattern_time_type::local, "\n");
|
auto formatter = std::make_shared<spdlog::pattern_formatter>("%^%$", spdlog::pattern_time_type::local, "\n");
|
||||||
spdlog::details::log_msg msg;
|
spdlog::details::log_msg msg;
|
||||||
formatter->format(msg);
|
formatter->format(msg);
|
||||||
REQUIRE(msg.color_range_start == 0);
|
REQUIRE(msg.color_range_start == 0);
|
||||||
REQUIRE(msg.color_range_end == 0);
|
REQUIRE(msg.color_range_end == 0);
|
||||||
REQUIRE(log_to_str("", formatter) == "\n");
|
REQUIRE(log_to_str("", formatter) == "\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("color range test3", "[pattern_formatter]")
|
TEST_CASE("color range test3", "[pattern_formatter]")
|
||||||
{
|
{
|
||||||
auto formatter = std::make_shared<spdlog::pattern_formatter>("%^***%$");
|
auto formatter = std::make_shared<spdlog::pattern_formatter>("%^***%$");
|
||||||
spdlog::details::log_msg msg;
|
spdlog::details::log_msg msg;
|
||||||
formatter->format(msg);
|
formatter->format(msg);
|
||||||
REQUIRE(msg.color_range_start == 0);
|
REQUIRE(msg.color_range_start == 0);
|
||||||
REQUIRE(msg.color_range_end == 3);
|
REQUIRE(msg.color_range_end == 3);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("color range test4", "[pattern_formatter]")
|
TEST_CASE("color range test4", "[pattern_formatter]")
|
||||||
{
|
{
|
||||||
auto formatter = std::make_shared<spdlog::pattern_formatter>("XX%^YYY%$", spdlog::pattern_time_type::local, "\n");
|
auto formatter = std::make_shared<spdlog::pattern_formatter>("XX%^YYY%$", spdlog::pattern_time_type::local, "\n");
|
||||||
spdlog::details::log_msg msg;
|
spdlog::details::log_msg msg;
|
||||||
msg.raw << "ignored";
|
msg.raw << "ignored";
|
||||||
formatter->format(msg);
|
formatter->format(msg);
|
||||||
REQUIRE(msg.color_range_start == 2);
|
REQUIRE(msg.color_range_start == 2);
|
||||||
REQUIRE(msg.color_range_end == 5);
|
REQUIRE(msg.color_range_end == 5);
|
||||||
REQUIRE(log_to_str("ignored", formatter) == "XXYYY\n");
|
REQUIRE(log_to_str("ignored", formatter) == "XXYYY\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("color range test5", "[pattern_formatter]")
|
TEST_CASE("color range test5", "[pattern_formatter]")
|
||||||
{
|
{
|
||||||
auto formatter = std::make_shared<spdlog::pattern_formatter>("**%^");
|
auto formatter = std::make_shared<spdlog::pattern_formatter>("**%^");
|
||||||
spdlog::details::log_msg msg;
|
spdlog::details::log_msg msg;
|
||||||
formatter->format(msg);
|
formatter->format(msg);
|
||||||
REQUIRE(msg.color_range_start == 2);
|
REQUIRE(msg.color_range_start == 2);
|
||||||
REQUIRE(msg.color_range_end == 0);
|
REQUIRE(msg.color_range_end == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("color range test6", "[pattern_formatter]")
|
TEST_CASE("color range test6", "[pattern_formatter]")
|
||||||
{
|
{
|
||||||
auto formatter = std::make_shared<spdlog::pattern_formatter>("**%$");
|
auto formatter = std::make_shared<spdlog::pattern_formatter>("**%$");
|
||||||
spdlog::details::log_msg msg;
|
spdlog::details::log_msg msg;
|
||||||
formatter->format(msg);
|
formatter->format(msg);
|
||||||
REQUIRE(msg.color_range_start == 0);
|
REQUIRE(msg.color_range_start == 0);
|
||||||
REQUIRE(msg.color_range_end == 2);
|
REQUIRE(msg.color_range_end == 2);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user