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
cf63bcb8
Commit
cf63bcb8
authored
May 22, 2018
by
gabime
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
catch exceptions during flush and cal handler
parent
d0b8ecb6
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
34 additions
and
8 deletions
+34
-8
logger_impl.h
include/spdlog/details/logger_impl.h
+12
-1
errors.cpp
tests/errors.cpp
+22
-7
No files found.
include/spdlog/details/logger_impl.h
View file @
cf63bcb8
...
...
@@ -278,7 +278,18 @@ inline spdlog::log_err_handler spdlog::logger::error_handler()
inline
void
spdlog
::
logger
::
flush
()
{
_flush
();
try
{
_flush
();
}
catch
(
const
std
::
exception
&
ex
)
{
_err_handler
(
ex
.
what
());
}
catch
(...)
{
_err_handler
(
"Unknown exception in logger during flush: "
+
_name
);
}
}
inline
void
spdlog
::
logger
::
flush_on
(
level
::
level_enum
log_level
)
...
...
tests/errors.cpp
View file @
cf63bcb8
...
...
@@ -7,16 +7,20 @@
class
failing_sink
:
public
spdlog
::
sinks
::
sink
{
void
log
(
const
spdlog
::
details
::
log_msg
&
msg
)
override
void
log
(
const
spdlog
::
details
::
log_msg
&
)
override
{
throw
std
::
runtime_error
(
"some error happened during log"
);
}
void
flush
()
override
{}
void
flush
()
override
{
throw
std
::
runtime_error
(
"some error happened during flush"
);
}
};
TEST_CASE
(
"default_error_handler"
,
"[errors]]"
)
{
spdlog
::
drop_all
();
prepare_logdir
();
std
::
string
filename
=
"logs/simple_log.txt"
;
...
...
@@ -40,11 +44,12 @@ struct custom_ex
};
TEST_CASE
(
"custom_error_handler"
,
"[errors]]"
)
{
spdlog
::
drop_all
();
prepare_logdir
();
std
::
string
filename
=
"logs/simple_log.txt"
;
auto
logger
=
spdlog
::
create
<
spdlog
::
sinks
::
simple_file_sink_mt
>
(
"logger"
,
filename
,
true
);
logger
->
flush_on
(
spdlog
::
level
::
info
);
logger
->
set_error_handler
([
=
](
const
std
::
string
&
msg
)
{
throw
custom_ex
();
});
logger
->
set_error_handler
([
=
](
const
std
::
string
&
)
{
throw
custom_ex
();
});
logger
->
info
(
"Good message #1"
);
#if !defined(SPDLOG_FMT_PRINTF)
REQUIRE_THROWS_AS
(
logger
->
info
(
"Bad format msg {} {}"
,
"xxx"
),
custom_ex
);
...
...
@@ -57,14 +62,23 @@ TEST_CASE("custom_error_handler", "[errors]]")
TEST_CASE
(
"default_error_handler2"
,
"[errors]]"
)
{
spdlog
::
drop_all
();
auto
logger
=
spdlog
::
create
<
failing_sink
>
(
"failed_logger"
);
logger
->
set_error_handler
([
=
](
const
std
::
string
&
msg
)
{
throw
custom_ex
();
});
logger
->
set_error_handler
([
=
](
const
std
::
string
&
)
{
throw
custom_ex
();
});
REQUIRE_THROWS_AS
(
logger
->
info
(
"Some message"
),
custom_ex
);
}
TEST_CASE
(
"flush_error_handler"
,
"[errors]]"
)
{
spdlog
::
drop_all
();
auto
logger
=
spdlog
::
create
<
failing_sink
>
(
"failed_logger"
);
logger
->
set_error_handler
([
=
](
const
std
::
string
&
)
{
throw
custom_ex
();
});
REQUIRE_THROWS_AS
(
logger
->
flush
(),
custom_ex
);
}
TEST_CASE
(
"async_error_handler"
,
"[errors]]"
)
{
spdlog
::
drop_all
();
prepare_logdir
();
std
::
string
err_msg
(
"log failed with some msg"
);
...
...
@@ -72,7 +86,7 @@ TEST_CASE("async_error_handler", "[errors]]")
{
spdlog
::
init_thread_pool
(
128
,
1
);
auto
logger
=
spdlog
::
create_async_logger
<
spdlog
::
sinks
::
simple_file_sink_mt
>
(
"logger"
,
filename
,
true
);
logger
->
set_error_handler
([
=
](
const
std
::
string
&
msg
)
{
logger
->
set_error_handler
([
=
](
const
std
::
string
&
)
{
std
::
ofstream
ofs
(
"logs/custom_err.txt"
);
if
(
!
ofs
)
throw
std
::
runtime_error
(
"Failed open logs/custom_err.txt"
);
...
...
@@ -95,12 +109,13 @@ TEST_CASE("async_error_handler", "[errors]]")
// Make sure async error handler is executed
TEST_CASE
(
"async_error_handler2"
,
"[errors]]"
)
{
spdlog
::
drop_all
();
prepare_logdir
();
std
::
string
err_msg
(
"This is async handler error message"
);
{
spdlog
::
init_thread_pool
(
128
,
1
);
auto
logger
=
spdlog
::
create_async_logger
<
failing_sink
>
(
"failed_logger"
);
logger
->
set_error_handler
([
=
](
const
std
::
string
&
msg
)
{
logger
->
set_error_handler
([
=
](
const
std
::
string
&
)
{
std
::
ofstream
ofs
(
"logs/custom_err2.txt"
);
if
(
!
ofs
)
throw
std
::
runtime_error
(
"Failed open logs/custom_err2.txt"
);
...
...
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