Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
spdlog
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
spdlog
Commits
d040ab93
Commit
d040ab93
authored
Apr 06, 2018
by
gabime
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
wincolor color formatting support
parent
c8610d9a
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
29 additions
and
9 deletions
+29
-9
example/example.cpp
example/example.cpp
+2
-2
include/spdlog/sinks/ansicolor_sink.h
include/spdlog/sinks/ansicolor_sink.h
+1
-1
include/spdlog/sinks/wincolor_sink.h
include/spdlog/sinks/wincolor_sink.h
+26
-6
No files found.
example/example.cpp
View file @
d040ab93
...
...
@@ -58,8 +58,8 @@ int main(int, char *[])
daily_logger
->
info
(
123.44
);
// Customize msg format for all messages
spd
::
set_pattern
(
"
*** [%H:%M:%S %z] [thread %t] %v ***
"
);
rotating_logger
->
info
(
"This is another message with custom format
"
);
spd
::
set_pattern
(
"
[%^+++%$] [%H:%M:%S %z] [thread %t] %v
"
);
console
->
info
(
"This a 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
...
...
include/spdlog/sinks/ansicolor_sink.h
View file @
d040ab93
...
...
@@ -28,7 +28,7 @@ public:
:
target_file_
(
file
)
{
should_do_colors_
=
details
::
os
::
in_terminal
(
file
)
&&
details
::
os
::
is_color_terminal
();
colors_
[
level
::
trace
]
=
magenta
;
colors_
[
level
::
trace
]
=
white
;
colors_
[
level
::
debug
]
=
cyan
;
colors_
[
level
::
info
]
=
green
;
colors_
[
level
::
warn
]
=
yellow
+
bold
;
...
...
include/spdlog/sinks/wincolor_sink.h
View file @
d040ab93
...
...
@@ -25,6 +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
CYAN
=
FOREGROUND_GREEN
|
FOREGROUND_BLUE
;
const
WORD
WHITE
=
FOREGROUND_RED
|
FOREGROUND_GREEN
|
FOREGROUND_BLUE
;
const
WORD
YELLOW
=
FOREGROUND_RED
|
FOREGROUND_GREEN
;
...
...
@@ -32,9 +33,9 @@ public:
wincolor_sink
(
HANDLE
std_handle
)
:
out_handle_
(
std_handle
)
{
colors_
[
level
::
trace
]
=
CYAN
;
colors_
[
level
::
trace
]
=
WHITE
;
colors_
[
level
::
debug
]
=
CYAN
;
colors_
[
level
::
info
]
=
WHITE
|
BOLD
;
colors_
[
level
::
info
]
=
GREEN
;
colors_
[
level
::
warn
]
=
YELLOW
|
BOLD
;
colors_
[
level
::
err
]
=
RED
|
BOLD
;
// red bold
colors_
[
level
::
critical
]
=
BACKGROUND_RED
|
WHITE
|
BOLD
;
// white bold on red background
...
...
@@ -59,10 +60,22 @@ public:
protected:
void
_sink_it
(
const
details
::
log_msg
&
msg
)
override
{
auto
color
=
colors_
[
msg
.
level
];
auto
orig_attribs
=
set_console_attribs
(
color
);
WriteConsoleA
(
out_handle_
,
msg
.
formatted
.
data
(),
static_cast
<
DWORD
>
(
msg
.
formatted
.
size
()),
nullptr
,
nullptr
);
SetConsoleTextAttribute
(
out_handle_
,
orig_attribs
);
// reset to orig colors
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
...
...
@@ -86,6 +99,13 @@ private:
SetConsoleTextAttribute
(
out_handle_
,
attribs
|
back_color
);
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
);
}
};
//
...
...
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