Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
S
spdlog
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
spdlog
Commits
1dea46e1
Commit
1dea46e1
authored
Apr 06, 2018
by
gabime
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
code formatting
parent
4abe4035
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
65 additions
and
65 deletions
+65
-65
example.cpp
example/example.cpp
+2
-2
wincolor_sink.h
include/spdlog/sinks/wincolor_sink.h
+23
-23
test_pattern_formatter.cpp
tests/test_pattern_formatter.cpp
+40
-40
No files found.
example/example.cpp
View file @
1dea46e1
...
...
@@ -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!"
);
...
...
include/spdlog/sinks/wincolor_sink.h
View file @
1dea46e1
...
...
@@ -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
);
}
};
//
...
...
tests/test_pattern_formatter.cpp
View file @
1dea46e1
...
...
@@ -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
);
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment