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
9bffa921
Commit
9bffa921
authored
Apr 15, 2018
by
gabime
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
global mutex stdout stderr sinks
parent
c50ba696
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
83 additions
and
41 deletions
+83
-41
os.h
include/spdlog/details/os.h
+14
-0
wincolor_sink.h
include/spdlog/sinks/wincolor_sink.h
+69
-41
No files found.
include/spdlog/details/os.h
View file @
9bffa921
...
@@ -17,6 +17,7 @@
...
@@ -17,6 +17,7 @@
#include <sys/stat.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/types.h>
#include <thread>
#include <thread>
#include <mutex>
#ifdef _WIN32
#ifdef _WIN32
...
@@ -474,6 +475,19 @@ inline bool in_terminal(FILE *file)
...
@@ -474,6 +475,19 @@ inline bool in_terminal(FILE *file)
return
isatty
(
fileno
(
file
))
!=
0
;
return
isatty
(
fileno
(
file
))
!=
0
;
#endif
#endif
}
}
// stdout/stderr global mutexes
inline
std
::
mutex
&
stdout_mutex
()
{
static
std
::
mutex
&
mutex
=
std
::
mutex
{};
return
mutex
;
}
inline
std
::
mutex
&
stderr_mutex
()
{
static
std
::
mutex
&
mutex
=
std
::
mutex
{};
return
mutex
;
}
}
// namespace os
}
// namespace os
}
// namespace details
}
// namespace details
}
// namespace spdlog
}
// namespace spdlog
include/spdlog/sinks/wincolor_sink.h
View file @
9bffa921
...
@@ -10,6 +10,7 @@
...
@@ -10,6 +10,7 @@
#include "base_sink.h"
#include "base_sink.h"
#include <mutex>
#include <mutex>
#include <memory>
#include <string>
#include <string>
#include <unordered_map>
#include <unordered_map>
#include <wincon.h>
#include <wincon.h>
...
@@ -20,7 +21,7 @@ namespace sinks {
...
@@ -20,7 +21,7 @@ namespace sinks {
* Windows color console sink. Uses WriteConsoleA to write to the console with colors
* Windows color console sink. Uses WriteConsoleA to write to the console with colors
*/
*/
template
<
class
Mutex
>
template
<
class
Mutex
>
class
wincolor_sink
:
public
base_sink
<
Mutex
>
class
wincolor_sink
:
public
sink
{
{
public
:
public
:
const
WORD
BOLD
=
FOREGROUND_INTENSITY
;
const
WORD
BOLD
=
FOREGROUND_INTENSITY
;
...
@@ -30,8 +31,8 @@ public:
...
@@ -30,8 +31,8 @@ public:
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
;
wincolor_sink
(
HANDLE
std_handle
)
wincolor_sink
(
HANDLE
std_handle
,
Mutex
&
stdout_mutex
)
:
out_handle_
(
std_handle
)
:
out_handle_
(
std_handle
)
,
_mutex
(
stdout_mutex
)
{
{
colors_
[
level
::
trace
]
=
WHITE
;
colors_
[
level
::
trace
]
=
WHITE
;
colors_
[
level
::
debug
]
=
CYAN
;
colors_
[
level
::
debug
]
=
CYAN
;
...
@@ -42,6 +43,7 @@ public:
...
@@ -42,6 +43,7 @@ public:
colors_
[
level
::
off
]
=
0
;
colors_
[
level
::
off
]
=
0
;
}
}
~
wincolor_sink
()
override
~
wincolor_sink
()
override
{
{
this
->
flush
();
this
->
flush
();
...
@@ -57,55 +59,58 @@ public:
...
@@ -57,55 +59,58 @@ public:
colors_
[
level
]
=
color
;
colors_
[
level
]
=
color
;
}
}
protected
:
void
log
(
const
details
::
log_msg
&
msg
)
SPDLOG_FINAL
override
void
_sink_it
(
const
details
::
log_msg
&
msg
)
override
{
{
std
::
lock_guard
<
Mutex
>
lock
(
_mutex
);
if
(
msg
.
color_range_end
>
msg
.
color_range_start
)
{
if
(
msg
.
color_range_end
>
msg
.
color_range_start
)
// before color range
{
_print_range
(
msg
,
0
,
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
]);
// in color range
_print_range
(
msg
,
msg
.
color_range_start
,
msg
.
color_range_end
);
auto
orig_attribs
=
set_console_attribs
(
colors_
[
msg
.
level
]);
::
SetConsoleTextAttribute
(
out_handle_
,
orig_attribs
);
// reset to orig colors
_print_range
(
msg
,
msg
.
color_range_start
,
msg
.
color_range_end
);
// after color range
::
SetConsoleTextAttribute
(
out_handle_
,
orig_attribs
);
// reset to orig colors
_print_range
(
msg
,
msg
.
color_range_end
,
msg
.
formatted
.
size
());
// after color range
}
_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
()
SPDLOG_FINAL
override
// windows console always flushed?
{
}
// windows console always flushed?
}
private
:
HANDLE
out_handle_
;
std
::
unordered_map
<
level
::
level_enum
,
WORD
,
level
::
level_hasher
>
colors_
;
private
:
// set color and return the orig console attributes (for resetting later)
// set color and return the orig console attributes (for resetting later)
WORD
set_console_attribs
(
WORD
attribs
)
WORD
set_console_attribs
(
WORD
attribs
)
{
{
CONSOLE_SCREEN_BUFFER_INFO
orig_buffer_info
;
CONSOLE_SCREEN_BUFFER_INFO
orig_buffer_info
;
GetConsoleScreenBufferInfo
(
out_handle_
,
&
orig_buffer_info
);
::
GetConsoleScreenBufferInfo
(
out_handle_
,
&
orig_buffer_info
);
WORD
back_color
=
orig_buffer_info
.
wAttributes
;
WORD
back_color
=
orig_buffer_info
.
wAttributes
;
// retrieve the current background color
// retrieve the current background color
back_color
&=
static_cast
<
WORD
>
(
~
(
FOREGROUND_RED
|
FOREGROUND_GREEN
|
FOREGROUND_BLUE
|
FOREGROUND_INTENSITY
));
back_color
&=
static_cast
<
WORD
>
(
~
(
FOREGROUND_RED
|
FOREGROUND_GREEN
|
FOREGROUND_BLUE
|
FOREGROUND_INTENSITY
));
// keep the background color unchanged
// keep the background color unchanged
SetConsoleTextAttribute
(
out_handle_
,
attribs
|
back_color
);
::
SetConsoleTextAttribute
(
out_handle_
,
attribs
|
back_color
);
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
);
auto
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
);
}
}
HANDLE
out_handle_
;
Mutex
&
_mutex
;
std
::
unordered_map
<
level
::
level_enum
,
WORD
,
level
::
level_hasher
>
colors_
;
};
};
//
//
...
@@ -113,14 +118,26 @@ private:
...
@@ -113,14 +118,26 @@ private:
//
//
template
<
class
Mutex
>
template
<
class
Mutex
>
class
wincolor_stdout_sink
:
public
wincolor_sink
<
Mutex
>
class
wincolor_stdout_sink
:
public
wincolor_sink
<
Mutex
>
{
{
public
:
public
:
wincolor_stdout_sink
()
wincolor_stdout_sink
()
:
wincolor_sink
<
Mutex
>
(
GetStdHandle
(
STD_OUTPUT_HANDLE
))
:
wincolor_sink
<
Mutex
>
(
GetStdHandle
(
STD_OUTPUT_HANDLE
),
details
::
os
::
stdout_mutex
(
))
{
{
}
}
};
};
template
<>
class
wincolor_stdout_sink
<
details
::
null_mutex
>
:
public
wincolor_sink
<
details
::
null_mutex
>
{
details
::
null_mutex
_null_mutex
;
public
:
wincolor_stdout_sink
()
:
wincolor_sink
<
details
::
null_mutex
>
(
GetStdHandle
(
STD_OUTPUT_HANDLE
),
_null_mutex
)
{
}
};
using
wincolor_stdout_sink_mt
=
wincolor_stdout_sink
<
std
::
mutex
>
;
using
wincolor_stdout_sink_mt
=
wincolor_stdout_sink
<
std
::
mutex
>
;
using
wincolor_stdout_sink_st
=
wincolor_stdout_sink
<
details
::
null_mutex
>
;
using
wincolor_stdout_sink_st
=
wincolor_stdout_sink
<
details
::
null_mutex
>
;
...
@@ -132,11 +149,22 @@ class wincolor_stderr_sink : public wincolor_sink<Mutex>
...
@@ -132,11 +149,22 @@ class wincolor_stderr_sink : public wincolor_sink<Mutex>
{
{
public
:
public
:
wincolor_stderr_sink
()
wincolor_stderr_sink
()
:
wincolor_sink
<
Mutex
>
(
GetStdHandle
(
STD_ERROR_HANDLE
))
:
wincolor_sink
<
Mutex
>
(
GetStdHandle
(
STD_ERROR_HANDLE
)
,
details
::
os
::
stderr_mutex
()
)
{
{
}
}
};
};
template
<>
class
wincolor_stderr_sink
<
details
::
null_mutex
>
:
public
wincolor_sink
<
details
::
null_mutex
>
{
details
::
null_mutex
_null_mutex
;
public
:
wincolor_stderr_sink
()
:
wincolor_sink
<
details
::
null_mutex
>
(
GetStdHandle
(
STD_ERROR_HANDLE
),
_null_mutex
)
{
}
};
using
wincolor_stderr_sink_mt
=
wincolor_stderr_sink
<
std
::
mutex
>
;
using
wincolor_stderr_sink_mt
=
wincolor_stderr_sink
<
std
::
mutex
>
;
using
wincolor_stderr_sink_st
=
wincolor_stderr_sink
<
details
::
null_mutex
>
;
using
wincolor_stderr_sink_st
=
wincolor_stderr_sink
<
details
::
null_mutex
>
;
...
...
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