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
e7debaac
Commit
e7debaac
authored
Aug 05, 2016
by
gabime
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
astyle
parent
bdbe9086
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
229 additions
and
210 deletions
+229
-210
example.cpp
example/example.cpp
+13
-12
async_log_helper.h
include/spdlog/details/async_log_helper.h
+16
-14
async_logger_impl.h
include/spdlog/details/async_logger_impl.h
+12
-10
file_helper.h
include/spdlog/details/file_helper.h
+1
-1
logger_impl.h
include/spdlog/details/logger_impl.h
+103
-91
os.h
include/spdlog/details/os.h
+23
-23
pattern_formatter_impl.h
include/spdlog/details/pattern_formatter_impl.h
+0
-0
registry.h
include/spdlog/details/registry.h
+9
-9
spdlog_impl.h
include/spdlog/details/spdlog_impl.h
+1
-1
logger.h
include/spdlog/logger.h
+10
-10
errors.cpp
tests/errors.cpp
+41
-39
No files found.
example/example.cpp
View file @
e7debaac
...
...
@@ -65,7 +65,7 @@ int main(int, char*[])
// Enabled #ifdef SPDLOG_DEBUG_ON or #ifdef SPDLOG_TRACE_ON
SPDLOG_TRACE
(
console
,
"Enabled only #ifdef SPDLOG_TRACE_ON..{} ,{}"
,
1
,
3.23
);
SPDLOG_DEBUG
(
console
,
"Enabled only #ifdef SPDLOG_DEBUG_ON.. {} ,{}"
,
1
,
3.23
);
// Asynchronous logging is very fast..
// Just call spdlog::set_async_mode(q_size) and all created loggers from now on will be asynchronous..
async_example
();
...
...
@@ -76,20 +76,20 @@ int main(int, char*[])
// log user-defined types example..
user_defined_example
();
// Change default log error handler
err_handler_example
();
// Change default log error handler
err_handler_example
();
console
->
info
(
"End of example. bye.."
);
console
->
info
(
"End of example. bye.."
);
// Release and close all loggers
spdlog
::
drop_all
();
}
// Exceptions will only be thrown upon failed logger or sink construction (not during logging)
// Exceptions will only be thrown upon failed logger or sink construction (not during logging)
catch
(
const
spd
::
spdlog_ex
&
ex
)
{
std
::
cout
<<
"Log init failed: "
<<
ex
.
what
()
<<
std
::
endl
;
return
1
;
}
}
}
void
async_example
()
...
...
@@ -132,11 +132,12 @@ void user_defined_example()
//custom error handler
//
void
err_handler_example
()
{
//can be set globaly or per logger(logger->set_error_handler(..))
spdlog
::
set_error_handler
([](
const
std
::
string
&
msg
)
{
std
::
cerr
<<
"my err handler: "
<<
msg
<<
std
::
endl
;
});
spd
::
get
(
"console"
)
->
info
(
"some invalid message to trigger an error {}{}{}{}"
,
3
);
{
//can be set globaly or per logger(logger->set_error_handler(..))
spdlog
::
set_error_handler
([](
const
std
::
string
&
msg
)
{
std
::
cerr
<<
"my err handler: "
<<
msg
<<
std
::
endl
;
});
spd
::
get
(
"console"
)
->
info
(
"some invalid message to trigger an error {}{}{}{}"
,
3
);
}
include/spdlog/details/async_log_helper.h
View file @
e7debaac
...
...
@@ -120,7 +120,7 @@ public:
async_log_helper
(
formatter_ptr
formatter
,
const
std
::
vector
<
sink_ptr
>&
sinks
,
size_t
queue_size
,
const
log_err_handler
err_handler
,
const
log_err_handler
err_handler
,
const
async_overflow_policy
overflow_policy
=
async_overflow_policy
::
block_retry
,
const
std
::
function
<
void
()
>&
worker_warmup_cb
=
nullptr
,
const
std
::
chrono
::
milliseconds
&
flush_interval_ms
=
std
::
chrono
::
milliseconds
::
zero
(),
...
...
@@ -143,13 +143,13 @@ private:
// queue of messages to log
q_type
_q
;
log_err_handler
_err_handler
;
log_err_handler
_err_handler
;
bool
_flush_requested
;
bool
_terminate_requested
;
// overflow policy
const
async_overflow_policy
_overflow_policy
;
...
...
@@ -166,7 +166,7 @@ private:
std
::
thread
_worker_thread
;
void
push_msg
(
async_msg
&&
new_msg
);
// worker thread main loop
void
worker_loop
();
...
...
@@ -178,7 +178,7 @@ private:
// sleep,yield or return immediatly using the time passed since last message as a hint
static
void
sleep_or_yield
(
const
spdlog
::
log_clock
::
time_point
&
now
,
const
log_clock
::
time_point
&
last_op_time
);
};
}
}
...
...
@@ -190,7 +190,7 @@ inline spdlog::details::async_log_helper::async_log_helper(
formatter_ptr
formatter
,
const
std
::
vector
<
sink_ptr
>&
sinks
,
size_t
queue_size
,
log_err_handler
err_handler
,
log_err_handler
err_handler
,
const
async_overflow_policy
overflow_policy
,
const
std
::
function
<
void
()
>&
worker_warmup_cb
,
const
std
::
chrono
::
milliseconds
&
flush_interval_ms
,
...
...
@@ -198,7 +198,7 @@ inline spdlog::details::async_log_helper::async_log_helper(
_formatter
(
formatter
),
_sinks
(
sinks
),
_q
(
queue_size
),
_err_handler
(
err_handler
),
_err_handler
(
err_handler
),
_flush_requested
(
false
),
_terminate_requested
(
false
),
_overflow_policy
(
overflow_policy
),
...
...
@@ -231,7 +231,7 @@ inline void spdlog::details::async_log_helper::log(const details::log_msg& msg)
}
inline
void
spdlog
::
details
::
async_log_helper
::
push_msg
(
details
::
async_log_helper
::
async_msg
&&
new_msg
)
{
{
if
(
!
_q
.
enqueue
(
std
::
move
(
new_msg
))
&&
_overflow_policy
!=
async_overflow_policy
::
discard_log_msg
)
{
auto
last_op_time
=
details
::
os
::
now
();
...
...
@@ -261,12 +261,14 @@ inline void spdlog::details::async_log_helper::worker_loop()
while
(
process_next_msg
(
last_pop
,
last_flush
));
if
(
_worker_teardown_cb
)
_worker_teardown_cb
();
}
catch
(
const
std
::
exception
&
ex
)
{
_err_handler
(
ex
.
what
());
}
catch
(...)
{
_err_handler
(
"Unknown exception"
);
}
catch
(
const
std
::
exception
&
ex
)
{
_err_handler
(
ex
.
what
());
}
catch
(...)
{
_err_handler
(
"Unknown exception"
);
}
}
// process next message in the queue
...
...
include/spdlog/details/async_logger_impl.h
View file @
e7debaac
...
...
@@ -73,14 +73,16 @@ inline void spdlog::async_logger::_set_pattern(const std::string& pattern)
inline
void
spdlog
::
async_logger
::
_sink_it
(
details
::
log_msg
&
msg
)
{
try
{
_async_log_helper
->
log
(
msg
);
}
catch
(
const
std
::
exception
&
ex
)
{
_err_handler
(
ex
.
what
());
}
catch
(...)
{
_err_handler
(
"Unknown exception"
);
}
try
{
_async_log_helper
->
log
(
msg
);
}
catch
(
const
std
::
exception
&
ex
)
{
_err_handler
(
ex
.
what
());
}
catch
(...)
{
_err_handler
(
"Unknown exception"
);
}
}
include/spdlog/details/file_helper.h
View file @
e7debaac
...
...
@@ -100,7 +100,7 @@ public:
{
if
(
!
_fd
)
throw
spdlog_ex
(
"Cannot use size() on closed file "
+
os
::
filename_to_str
(
_filename
));
return
os
::
filesize
(
_fd
);
return
os
::
filesize
(
_fd
);
}
const
filename_t
&
filename
()
const
...
...
include/spdlog/details/logger_impl.h
View file @
e7debaac
...
...
@@ -16,28 +16,31 @@
// all other ctors will call this one
template
<
class
It
>
inline
spdlog
::
logger
::
logger
(
const
std
::
string
&
logger_name
,
const
It
&
begin
,
const
It
&
end
)
:
_name
(
logger_name
),
_sinks
(
begin
,
end
),
_formatter
(
std
::
make_shared
<
pattern_formatter
>
(
"%+"
))
{
_level
=
level
::
info
;
_flush_level
=
level
::
off
;
_last_err_time
=
0
;
_err_handler
=
[
this
](
const
std
::
string
&
msg
)
{
this
->
_default_err_handler
(
msg
);};
_name
(
logger_name
),
_sinks
(
begin
,
end
),
_formatter
(
std
::
make_shared
<
pattern_formatter
>
(
"%+"
))
{
_level
=
level
::
info
;
_flush_level
=
level
::
off
;
_last_err_time
=
0
;
_err_handler
=
[
this
](
const
std
::
string
&
msg
)
{
this
->
_default_err_handler
(
msg
);
};
}
// ctor with sinks as init list
inline
spdlog
::
logger
::
logger
(
const
std
::
string
&
logger_name
,
sinks_init_list
sinks_list
)
:
logger
(
logger_name
,
sinks_list
.
begin
(),
sinks_list
.
end
())
logger
(
logger_name
,
sinks_list
.
begin
(),
sinks_list
.
end
())
{}
// ctor with single sink
inline
spdlog
::
logger
::
logger
(
const
std
::
string
&
logger_name
,
spdlog
::
sink_ptr
single_sink
)
:
logger
(
logger_name
,
{
single_sink
})
logger
(
logger_name
,
{
single_sink
})
{}
...
...
@@ -46,143 +49,152 @@ inline spdlog::logger::~logger() = default;
inline
void
spdlog
::
logger
::
set_formatter
(
spdlog
::
formatter_ptr
msg_formatter
)
{
_set_formatter
(
msg_formatter
);
_set_formatter
(
msg_formatter
);
}
inline
void
spdlog
::
logger
::
set_pattern
(
const
std
::
string
&
pattern
)
{
_set_pattern
(
pattern
);
_set_pattern
(
pattern
);
}
template
<
typename
...
Args
>
inline
void
spdlog
::
logger
::
log
(
level
::
level_enum
lvl
,
const
char
*
fmt
,
const
Args
&
...
args
)
{
if
(
!
should_log
(
lvl
))
return
;
try
{
details
::
log_msg
log_msg
(
&
_name
,
lvl
);
log_msg
.
raw
.
write
(
fmt
,
args
...);
_sink_it
(
log_msg
);
}
catch
(
const
std
::
exception
&
ex
)
{
_err_handler
(
ex
.
what
());
}
catch
(...)
{
_err_handler
(
"Unknown exception"
);
}
if
(
!
should_log
(
lvl
))
return
;
try
{
details
::
log_msg
log_msg
(
&
_name
,
lvl
);
log_msg
.
raw
.
write
(
fmt
,
args
...);
_sink_it
(
log_msg
);
}
catch
(
const
std
::
exception
&
ex
)
{
_err_handler
(
ex
.
what
());
}
catch
(...)
{
_err_handler
(
"Unknown exception"
);
}
}
template
<
typename
...
Args
>
inline
void
spdlog
::
logger
::
log
(
level
::
level_enum
lvl
,
const
char
*
msg
)
{
if
(
!
should_log
(
lvl
))
return
;
try
{
details
::
log_msg
log_msg
(
&
_name
,
lvl
);
log_msg
.
raw
<<
msg
;
_sink_it
(
log_msg
);
}
catch
(
const
std
::
exception
&
ex
)
{
_err_handler
(
ex
.
what
());
}
catch
(...)
{
_err_handler
(
"Unknown exception"
);
}
if
(
!
should_log
(
lvl
))
return
;
try
{
details
::
log_msg
log_msg
(
&
_name
,
lvl
);
log_msg
.
raw
<<
msg
;
_sink_it
(
log_msg
);
}
catch
(
const
std
::
exception
&
ex
)
{
_err_handler
(
ex
.
what
());
}
catch
(...)
{
_err_handler
(
"Unknown exception"
);
}
}
template
<
typename
T
>
inline
void
spdlog
::
logger
::
log
(
level
::
level_enum
lvl
,
const
T
&
msg
)
{
if
(
!
should_log
(
lvl
))
return
;
try
{
details
::
log_msg
log_msg
(
&
_name
,
lvl
);
log_msg
.
raw
<<
msg
;
_sink_it
(
log_msg
);
}
catch
(
const
std
::
exception
&
ex
)
{
_err_handler
(
ex
.
what
());
}
catch
(...)
{
_err_handler
(
"Unknown exception"
);
}
if
(
!
should_log
(
lvl
))
return
;
try
{
details
::
log_msg
log_msg
(
&
_name
,
lvl
);
log_msg
.
raw
<<
msg
;
_sink_it
(
log_msg
);
}
catch
(
const
std
::
exception
&
ex
)
{
_err_handler
(
ex
.
what
());
}
catch
(...)
{
_err_handler
(
"Unknown exception"
);
}
}
template
<
typename
...
Args
>
inline
void
spdlog
::
logger
::
trace
(
const
char
*
fmt
,
const
Args
&
...
args
)
{
log
(
level
::
trace
,
fmt
,
args
...);
log
(
level
::
trace
,
fmt
,
args
...);
}
template
<
typename
...
Args
>
inline
void
spdlog
::
logger
::
debug
(
const
char
*
fmt
,
const
Args
&
...
args
)
{
log
(
level
::
debug
,
fmt
,
args
...);
log
(
level
::
debug
,
fmt
,
args
...);
}
template
<
typename
...
Args
>
inline
void
spdlog
::
logger
::
info
(
const
char
*
fmt
,
const
Args
&
...
args
)
{
log
(
level
::
info
,
fmt
,
args
...);
log
(
level
::
info
,
fmt
,
args
...);
}
template
<
typename
...
Args
>
inline
void
spdlog
::
logger
::
warn
(
const
char
*
fmt
,
const
Args
&
...
args
)
{
log
(
level
::
warn
,
fmt
,
args
...);
log
(
level
::
warn
,
fmt
,
args
...);
}
template
<
typename
...
Args
>
inline
void
spdlog
::
logger
::
error
(
const
char
*
fmt
,
const
Args
&
...
args
)
{
log
(
level
::
err
,
fmt
,
args
...);
log
(
level
::
err
,
fmt
,
args
...);
}
template
<
typename
...
Args
>
inline
void
spdlog
::
logger
::
critical
(
const
char
*
fmt
,
const
Args
&
...
args
)
{
log
(
level
::
critical
,
fmt
,
args
...);
log
(
level
::
critical
,
fmt
,
args
...);
}
template
<
typename
T
>
inline
void
spdlog
::
logger
::
trace
(
const
T
&
msg
)
{
log
(
level
::
trace
,
msg
);
log
(
level
::
trace
,
msg
);
}
template
<
typename
T
>
inline
void
spdlog
::
logger
::
debug
(
const
T
&
msg
)
{
log
(
level
::
debug
,
msg
);
log
(
level
::
debug
,
msg
);
}
template
<
typename
T
>
inline
void
spdlog
::
logger
::
info
(
const
T
&
msg
)
{
log
(
level
::
info
,
msg
);
log
(
level
::
info
,
msg
);
}
template
<
typename
T
>
inline
void
spdlog
::
logger
::
warn
(
const
T
&
msg
)
{
log
(
level
::
warn
,
msg
);
log
(
level
::
warn
,
msg
);
}
template
<
typename
T
>
inline
void
spdlog
::
logger
::
error
(
const
T
&
msg
)
{
log
(
level
::
err
,
msg
);
log
(
level
::
err
,
msg
);
}
template
<
typename
T
>
inline
void
spdlog
::
logger
::
critical
(
const
T
&
msg
)
{
log
(
level
::
critical
,
msg
);
log
(
level
::
critical
,
msg
);
}
...
...
@@ -193,38 +205,38 @@ inline void spdlog::logger::critical(const T& msg)
//
inline
const
std
::
string
&
spdlog
::
logger
::
name
()
const
{
return
_name
;
return
_name
;
}
inline
void
spdlog
::
logger
::
set_level
(
spdlog
::
level
::
level_enum
log_level
)
{
_level
.
store
(
log_level
);
_level
.
store
(
log_level
);
}
inline
void
spdlog
::
logger
::
set_error_handler
(
spdlog
::
log_err_handler
err_handler
)
{
_err_handler
=
err_handler
;
_err_handler
=
err_handler
;
}
inline
spdlog
::
log_err_handler
spdlog
::
logger
::
error_handler
()
{
return
_err_handler
;
return
_err_handler
;
}
inline
void
spdlog
::
logger
::
flush_on
(
level
::
level_enum
log_level
)
{
_flush_level
.
store
(
log_level
);
_flush_level
.
store
(
log_level
);
}
inline
spdlog
::
level
::
level_enum
spdlog
::
logger
::
level
()
const
{
return
static_cast
<
spdlog
::
level
::
level_enum
>
(
_level
.
load
(
std
::
memory_order_relaxed
));
return
static_cast
<
spdlog
::
level
::
level_enum
>
(
_level
.
load
(
std
::
memory_order_relaxed
));
}
inline
bool
spdlog
::
logger
::
should_log
(
spdlog
::
level
::
level_enum
msg_level
)
const
{
return
msg_level
>=
_level
.
load
(
std
::
memory_order_relaxed
);
return
msg_level
>=
_level
.
load
(
std
::
memory_order_relaxed
);
}
//
...
...
@@ -233,40 +245,40 @@ inline bool spdlog::logger::should_log(spdlog::level::level_enum msg_level) cons
inline
void
spdlog
::
logger
::
_sink_it
(
details
::
log_msg
&
msg
)
{
_formatter
->
format
(
msg
);
for
(
auto
&
sink
:
_sinks
)
sink
->
log
(
msg
);
_formatter
->
format
(
msg
);
for
(
auto
&
sink
:
_sinks
)
sink
->
log
(
msg
);
const
auto
flush_level
=
_flush_level
.
load
(
std
::
memory_order_relaxed
);
if
(
msg
.
level
>=
flush_level
)
flush
();
const
auto
flush_level
=
_flush_level
.
load
(
std
::
memory_order_relaxed
);
if
(
msg
.
level
>=
flush_level
)
flush
();
}
inline
void
spdlog
::
logger
::
_set_pattern
(
const
std
::
string
&
pattern
)
{
_formatter
=
std
::
make_shared
<
pattern_formatter
>
(
pattern
);
_formatter
=
std
::
make_shared
<
pattern_formatter
>
(
pattern
);
}
inline
void
spdlog
::
logger
::
_set_formatter
(
formatter_ptr
msg_formatter
)
{
_formatter
=
msg_formatter
;
_formatter
=
msg_formatter
;
}
inline
void
spdlog
::
logger
::
flush
()
{
for
(
auto
&
sink
:
_sinks
)
sink
->
flush
();
for
(
auto
&
sink
:
_sinks
)
sink
->
flush
();
}
inline
void
spdlog
::
logger
::
_default_err_handler
(
const
std
::
string
&
msg
)
{
auto
now
=
time
(
nullptr
);
if
(
now
-
_last_err_time
<
60
)
return
;
auto
tm_time
=
details
::
os
::
localtime
(
now
);
char
date_buf
[
100
];
std
::
strftime
(
date_buf
,
sizeof
(
date_buf
),
"%Y-%m-%d %H:%M:%S"
,
&
tm_time
);
details
::
log_msg
err_msg
;
err_msg
.
formatted
.
write
(
"[*** LOG ERROR ***] [{}] [{}] [{}]{}"
,
name
(),
msg
,
date_buf
,
details
::
os
::
eol
);
sinks
::
stderr_sink_mt
::
instance
()
->
log
(
err_msg
);
_last_err_time
=
now
;
auto
now
=
time
(
nullptr
);
if
(
now
-
_last_err_time
<
60
)
return
;
auto
tm_time
=
details
::
os
::
localtime
(
now
);
char
date_buf
[
100
];
std
::
strftime
(
date_buf
,
sizeof
(
date_buf
),
"%Y-%m-%d %H:%M:%S"
,
&
tm_time
);
details
::
log_msg
err_msg
;
err_msg
.
formatted
.
write
(
"[*** LOG ERROR ***] [{}] [{}] [{}]{}"
,
name
(),
msg
,
date_buf
,
details
::
os
::
eol
);
sinks
::
stderr_sink_mt
::
instance
()
->
log
(
err_msg
);
_last_err_time
=
now
;
}
include/spdlog/details/os.h
View file @
e7debaac
...
...
@@ -192,35 +192,35 @@ inline bool file_exists(const filename_t& filename)
//Return file size according to open FILE* object
inline
size_t
filesize
(
FILE
*
f
)
{
if
(
f
==
nullptr
)
throw
spdlog_ex
(
"Failed getting file size. fd is null"
);
if
(
f
==
nullptr
)
throw
spdlog_ex
(
"Failed getting file size. fd is null"
);
#ifdef _WIN32
int
fd
=
_fileno
(
f
);
#if _WIN64 //64 bits
struct
_stat64
st
;
if
(
_fstat64
(
fd
,
&
st
)
==
0
)
return
st
.
st_size
;
#else //windows 32 bits
struct
_stat
st
;
if
(
_fstat
(
fd
,
&
st
)
==
0
)
return
st
.
st_size
;
int
fd
=
_fileno
(
f
);
#if _WIN64 //64 bits
struct
_stat64
st
;
if
(
_fstat64
(
fd
,
&
st
)
==
0
)
return
st
.
st_size
;
#else //windows 32 bits
struct
_stat
st
;
if
(
_fstat
(
fd
,
&
st
)
==
0
)
return
st
.
st_size
;
#endif
#else // unix
int
fd
=
fileno
(
f
);
//64 bits(but not in osx, where fstat64 is deprecated)
#if !defined(__FreeBSD__) && !defined(__APPLE__) && (defined(__x86_64__) || defined(__ppc64__))
struct
stat64
st
;
if
(
fstat64
(
fd
,
&
st
)
==
0
)
return
st
.
st_size
;
#else // unix 32 bits or osx
struct
stat
st
;
if
(
fstat
(
fd
,
&
st
)
==
0
)
return
st
.
st_size
;
int
fd
=
fileno
(
f
);
//64 bits(but not in osx, where fstat64 is deprecated)
#if !defined(__FreeBSD__) && !defined(__APPLE__) && (defined(__x86_64__) || defined(__ppc64__))
struct
stat64
st
;
if
(
fstat64
(
fd
,
&
st
)
==
0
)
return
st
.
st_size
;
#else // unix 32 bits or osx
struct
stat
st
;
if
(
fstat
(
fd
,
&
st
)
==
0
)
return
st
.
st_size
;
#endif
#endif
throw
spdlog_ex
(
"Failed getting file size from fd"
,
errno
);
throw
spdlog_ex
(
"Failed getting file size from fd"
,
errno
);
}
...
...
include/spdlog/details/pattern_formatter_impl.h
View file @
e7debaac
This diff is collapsed.
Click to expand it.
include/spdlog/details/registry.h
View file @
e7debaac
...
...
@@ -60,8 +60,8 @@ public:
if
(
_formatter
)
new_logger
->
set_formatter
(
_formatter
);
if
(
_err_handler
)
new_logger
->
set_error_handler
(
_err_handler
);
if
(
_err_handler
)
new_logger
->
set_error_handler
(
_err_handler
);
new_logger
->
set_level
(
_level
);
...
...
@@ -117,12 +117,12 @@ public:
_level
=
log_level
;
}
void
set_error_handler
(
log_err_handler
handler
)
{
for
(
auto
&
l
:
_loggers
)
l
.
second
->
set_error_handler
(
handler
);
_err_handler
=
handler
;
}
void
set_error_handler
(
log_err_handler
handler
)
{
for
(
auto
&
l
:
_loggers
)
l
.
second
->
set_error_handler
(
handler
);
_err_handler
=
handler
;
}
void
set_async_mode
(
size_t
q_size
,
const
async_overflow_policy
overflow_policy
,
const
std
::
function
<
void
()
>&
worker_warmup_cb
,
const
std
::
chrono
::
milliseconds
&
flush_interval_ms
,
const
std
::
function
<
void
()
>&
worker_teardown_cb
)
{
...
...
@@ -161,7 +161,7 @@ private:
std
::
unordered_map
<
std
::
string
,
std
::
shared_ptr
<
logger
>>
_loggers
;
formatter_ptr
_formatter
;
level
::
level_enum
_level
=
level
::
info
;
log_err_handler
_err_handler
;
log_err_handler
_err_handler
;
bool
_async_mode
=
false
;
size_t
_async_q_size
=
0
;
async_overflow_policy
_overflow_policy
=
async_overflow_policy
::
block_retry
;
...
...
include/spdlog/details/spdlog_impl.h
View file @
e7debaac
...
...
@@ -149,7 +149,7 @@ inline void spdlog::set_level(level::level_enum log_level)
inline
void
spdlog
::
set_error_handler
(
log_err_handler
handler
)
{
return
details
::
registry
::
instance
().
set_error_handler
(
handler
);
return
details
::
registry
::
instance
().
set_error_handler
(
handler
);
}
...
...
include/spdlog/logger.h
View file @
e7debaac
...
...
@@ -51,17 +51,17 @@ public:
template
<
typename
T
>
void
warn
(
const
T
&
);
template
<
typename
T
>
void
error
(
const
T
&
);
template
<
typename
T
>
void
critical
(
const
T
&
);
bool
should_log
(
level
::
level_enum
)
const
;
void
set_level
(
level
::
level_enum
);
level
::
level_enum
level
()
const
;
const
std
::
string
&
name
()
const
;
void
set_pattern
(
const
std
::
string
&
);
void
set_formatter
(
formatter_ptr
);
void
set_formatter
(
formatter_ptr
);
// error handler
void
set_error_handler
(
log_err_handler
);
log_err_handler
error_handler
();
// error handler
void
set_error_handler
(
log_err_handler
);
log_err_handler
error_handler
();
// automatically call flush() if message level >= log_level
void
flush_on
(
level
::
level_enum
log_level
);
...
...
@@ -72,16 +72,16 @@ protected:
virtual
void
_set_pattern
(
const
std
::
string
&
);
virtual
void
_set_formatter
(
formatter_ptr
);
// default error handler: print the error to stderr with the max rate of 1 message/minute
virtual
void
_default_err_handler
(
const
std
::
string
&
msg
);
// default error handler: print the error to stderr with the max rate of 1 message/minute
virtual
void
_default_err_handler
(
const
std
::
string
&
msg
);
const
std
::
string
_name
;
std
::
vector
<
sink_ptr
>
_sinks
;
formatter_ptr
_formatter
;
spdlog
::
level_t
_level
;
spdlog
::
level_t
_flush_level
;
log_err_handler
_err_handler
;
std
::
atomic
<
time_t
>
_last_err_time
;
log_err_handler
_err_handler
;
std
::
atomic
<
time_t
>
_last_err_time
;
};
}
...
...
tests/errors.cpp
View file @
e7debaac
...
...
@@ -8,54 +8,56 @@
TEST_CASE
(
"default_error_handler"
,
"[errors]]"
)
{
prepare_logdir
();
std
::
string
filename
=
"logs/simple_log.txt"
;
prepare_logdir
();
std
::
string
filename
=
"logs/simple_log.txt"
;
auto
logger
=
spdlog
::
create
<
spdlog
::
sinks
::
simple_file_sink_mt
>
(
"logger"
,
filename
,
true
);
logger
->
set_pattern
(
"%v"
);
logger
->
info
(
"Test message {} {}"
,
1
);
logger
->
info
(
"Test message {}"
,
2
);
logger
->
flush
();
auto
logger
=
spdlog
::
create
<
spdlog
::
sinks
::
simple_file_sink_mt
>
(
"logger"
,
filename
,
true
);
logger
->
set_pattern
(
"%v"
);
logger
->
info
(
"Test message {} {}"
,
1
);
logger
->
info
(
"Test message {}"
,
2
);
logger
->
flush
();
REQUIRE
(
file_contents
(
filename
)
==
std
::
string
(
"Test message 2
\n
"
));
REQUIRE
(
count_lines
(
filename
)
==
1
);
REQUIRE
(
file_contents
(
filename
)
==
std
::
string
(
"Test message 2
\n
"
));
REQUIRE
(
count_lines
(
filename
)
==
1
);
}
struct
custom_ex
{};
struct
custom_ex
{};
TEST_CASE
(
"custom_error_handler"
,
"[errors]]"
)
{
prepare_logdir
();
std
::
string
filename
=
"logs/simple_log.txt"
;
auto
logger
=
spdlog
::
create
<
spdlog
::
sinks
::
simple_file_sink_mt
>
(
"logger"
,
filename
,
true
);
logger
->
set_error_handler
([
=
](
const
std
::
string
&
msg
)
{
throw
custom_ex
();
});
logger
->
info
(
"Good message #1"
);
REQUIRE_THROWS_AS
(
logger
->
info
(
"Bad format msg {} {}"
,
"xxx"
),
custom_ex
);
logger
->
info
(
"Good message #2"
);
REQUIRE
(
count_lines
(
filename
)
==
2
);
prepare_logdir
();
std
::
string
filename
=
"logs/simple_log.txt"
;
auto
logger
=
spdlog
::
create
<
spdlog
::
sinks
::
simple_file_sink_mt
>
(
"logger"
,
filename
,
true
);
logger
->
set_error_handler
([
=
](
const
std
::
string
&
msg
)
{
throw
custom_ex
();
});
logger
->
info
(
"Good message #1"
);
REQUIRE_THROWS_AS
(
logger
->
info
(
"Bad format msg {} {}"
,
"xxx"
),
custom_ex
);
logger
->
info
(
"Good message #2"
);
REQUIRE
(
count_lines
(
filename
)
==
2
);
}
TEST_CASE
(
"async_error_handler"
,
"[errors]]"
)
{
prepare_logdir
();
std
::
string
err_msg
(
"log failed with some msg"
);
spdlog
::
set_async_mode
(
128
);
std
::
string
filename
=
"logs/simple_async_log.txt"
;
{
auto
logger
=
spdlog
::
create
<
spdlog
::
sinks
::
simple_file_sink_mt
>
(
"logger"
,
filename
,
true
);
logger
->
set_error_handler
([
=
](
const
std
::
string
&
msg
)
{
std
::
ofstream
ofs
(
"logs/custom_err.txt"
);
if
(
!
ofs
)
throw
std
::
runtime_error
(
"Failed open logs/custom_err.txt"
);
ofs
<<
err_msg
;
});
logger
->
info
(
"Good message #1"
);
logger
->
info
(
"Bad format msg {} {}"
,
"xxx"
);
logger
->
info
(
"Good message #2"
);
spdlog
::
drop
(
"logger"
);
//force logger to drain the queue and shutdown
spdlog
::
set_sync_mode
();
}
REQUIRE
(
count_lines
(
filename
)
==
2
);
REQUIRE
(
file_contents
(
"logs/custom_err.txt"
)
==
err_msg
);
prepare_logdir
();
std
::
string
err_msg
(
"log failed with some msg"
);
spdlog
::
set_async_mode
(
128
);
std
::
string
filename
=
"logs/simple_async_log.txt"
;
{
auto
logger
=
spdlog
::
create
<
spdlog
::
sinks
::
simple_file_sink_mt
>
(
"logger"
,
filename
,
true
);
logger
->
set_error_handler
([
=
](
const
std
::
string
&
msg
)
{
std
::
ofstream
ofs
(
"logs/custom_err.txt"
);
if
(
!
ofs
)
throw
std
::
runtime_error
(
"Failed open logs/custom_err.txt"
);
ofs
<<
err_msg
;
});
logger
->
info
(
"Good message #1"
);
logger
->
info
(
"Bad format msg {} {}"
,
"xxx"
);
logger
->
info
(
"Good message #2"
);
spdlog
::
drop
(
"logger"
);
//force logger to drain the queue and shutdown
spdlog
::
set_sync_mode
();
}
REQUIRE
(
count_lines
(
filename
)
==
2
);
REQUIRE
(
file_contents
(
"logs/custom_err.txt"
)
==
err_msg
);
}
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