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