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
0e77c339
Commit
0e77c339
authored
Nov 11, 2018
by
gabime
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
New compile time API using SPDLOG_ACTIVE_LEVEL
parent
f1e79bde
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
90 additions
and
64 deletions
+90
-64
common.h
include/spdlog/common.h
+19
-7
logger_impl.h
include/spdlog/details/logger_impl.h
+1
-7
pattern_formatter.h
include/spdlog/details/pattern_formatter.h
+3
-2
logger.h
include/spdlog/logger.h
+7
-5
spdlog.h
include/spdlog/spdlog.h
+41
-23
tweakme.h
include/spdlog/tweakme.h
+8
-0
includes.h
tests/includes.h
+1
-2
test_macros.cpp
tests/test_macros.cpp
+10
-18
No files found.
include/spdlog/common.h
View file @
0e77c339
...
...
@@ -69,17 +69,29 @@ using level_t = details::null_atomic_int;
using
level_t
=
std
::
atomic
<
int
>
;
#endif
#define SPDLOG_LEVEL_TRACE 0
#define SPDLOG_LEVEL_DEBUG 1
#define SPDLOG_LEVEL_INFO 2
#define SPDLOG_LEVEL_WARN 3
#define SPDLOG_LEVEL_ERROR 4
#define SPDLOG_LEVEL_CRITICAL 5
#define SPDLOG_LEVEL_OFF 6
#if !defined(SPDLOG_ACTIVE_LEVEL)
#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_INFO
#endif
// Log level enum
namespace
level
{
enum
level_enum
{
trace
=
0
,
debug
=
1
,
info
=
2
,
warn
=
3
,
err
=
4
,
critical
=
5
,
off
=
6
trace
=
SPDLOG_LEVEL_TRACE
,
debug
=
SPDLOG_LEVEL_DEBUG
,
info
=
SPDLOG_LEVEL_INFO
,
warn
=
SPDLOG_LEVEL_WARN
,
err
=
SPDLOG_LEVEL_ERROR
,
critical
=
SPDLOG_LEVEL_CRITICAL
,
off
=
SPDLOG_LEVEL_OFF
,
};
#if !defined(SPDLOG_LEVEL_NAMES)
...
...
include/spdlog/details/logger_impl.h
View file @
0e77c339
...
...
@@ -17,7 +17,7 @@
} \
catch (...) \
{ \
err_handler_("Unknown ex
eption in logger");
\
err_handler_("Unknown ex
ception in logger");
\
}
// create logger with given name, sinks and the default pattern formatter
...
...
@@ -26,13 +26,7 @@ template<typename It>
inline
spdlog
::
logger
::
logger
(
std
::
string
logger_name
,
It
begin
,
It
end
)
:
name_
(
std
::
move
(
logger_name
))
,
sinks_
(
begin
,
end
)
,
level_
(
level
::
info
)
,
flush_level_
(
level
::
off
)
,
last_err_time_
(
0
)
,
msg_counter_
(
1
)
// message counter will start from 1. 0-message id will be
// reserved for control messages
{
err_handler_
=
[
this
](
const
std
::
string
&
msg
)
{
this
->
default_err_handler_
(
msg
);
};
}
// ctor with sinks as init list
...
...
include/spdlog/details/pattern_formatter.h
View file @
0e77c339
...
...
@@ -98,8 +98,9 @@ private:
const
padding_info
&
padinfo_
;
fmt
::
memory_buffer
&
dest_
;
size_t
total_pad_
;
string_view_t
spaces_
{
" "
" "
,
128
};
string_view_t
spaces_
{
" "
" "
,
128
};
};
class
flag_formatter
...
...
include/spdlog/logger.h
View file @
0e77c339
...
...
@@ -158,13 +158,15 @@ protected:
// increment the message count (only if defined(SPDLOG_ENABLE_MESSAGE_COUNTER))
void
incr_msg_counter_
(
details
::
log_msg
&
msg
);
// const spdlog::level_t default_level_{static_cast<level::level_enum>(SPDLOG_ACTIVE_LEVEL)};
const
std
::
string
name_
;
std
::
vector
<
sink_ptr
>
sinks_
;
spdlog
::
level_t
level_
;
spdlog
::
level_t
flush_level_
;
log_err_handler
err_handler_
;
std
::
atomic
<
time_t
>
last_err_time_
;
std
::
atomic
<
size_t
>
msg_counter_
;
spdlog
::
level_t
level_
{
static_cast
<
level
::
level_enum
>
(
SPDLOG_ACTIVE_LEVEL
)}
;
spdlog
::
level_t
flush_level_
{
level
::
off
}
;
log_err_handler
err_handler_
{[
this
](
const
std
::
string
&
msg
)
{
this
->
default_err_handler_
(
msg
);
}}
;
std
::
atomic
<
time_t
>
last_err_time_
{
0
}
;
std
::
atomic
<
size_t
>
msg_counter_
{
1
}
;
};
}
// namespace spdlog
...
...
include/spdlog/spdlog.h
View file @
0e77c339
...
...
@@ -290,36 +290,54 @@ inline void critical(const wchar_t *fmt, const Args &... args)
#endif // SPDLOG_WCHAR_TO_UTF8_SUPPORT
//
// Trace & Debug can be switched on/off at compile time with zero cost.
// Uncomment SPDLOG_DEBUG_ON/SPDLOG_TRACE_ON in tweakme.h to enable.
// SPDLOG_TRACE(..) will also print current file and line.
//
// Example:
// spdlog::set_level(spdlog::level::trace);
// SPDLOG_TRACE(my_logger, "another trace message {} {}", 1, 2);
//
//
compile time level. defaults to info
#if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_TRACE
#define SPDLOG_LOGGER_TRACE(logger, ...) logger->trace(__VA_ARGS__)
#define SPDLOG_TRACE(...) spdlog::trace(__VA_ARGS__)
#else
#define SPDLOG_LOGGER_TRACE(logger, ...) (void)0
#define SPDLOG_TRACE(...) (void)0
#endif
#ifdef SPDLOG_TRACE_ON
#define SPDLOG_STR_H(x) #x
#define SPDLOG_STR_HELPER(x) SPDLOG_STR_H(x)
#ifdef _MSC_VER
#define SPDLOG_TRACE(logger, ...) \
logger->trace("[ "__FILE__ \
"(" SPDLOG_STR_HELPER(__LINE__) ")] " __VA_ARGS__)
#if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_DEBUG
#define SPDLOG_LOGGER_DEBUG(logger, ...) logger->debug(__VA_ARGS__)
#define SPDLOG_DEBUG(...) spdlog::debug(__VA_ARGS__)
#else
#define SPDLOG_TRACE(logger, ...) \
logger->trace("[" __FILE__ ":" SPDLOG_STR_HELPER(__LINE__) "]" \
" " __VA_ARGS__)
#define SPDLOG_LOGGER_DEBUG(logger, ...) (void)0
#define SPDLOG_DEBUG(...) (void)0
#endif
#if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_INFO
#define SPDLOG_LOGGER_INFO(logger, ...) logger->info(__VA_ARGS__)
#define SPDLOG_INFO(...) spdlog::info(__VA_ARGS__)
#else
#define SPDLOG_LOGGER_INFO(logger, ...) (void)0
#define SPDLOG_INFO(...) (void)0
#endif
#if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_WARN
#define SPDLOG_LOGGER_WARN(logger, ...) logger->warn(__VA_ARGS__)
#define SPDLOG_WARN(...) spdlog::warn(__VA_ARGS__)
#else
#define SPDLOG_LOGGER_WARN(logger, ...) (void)0
#define SPDLOG_WARN(...) (void)0
#endif
#if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_ERROR
#define SPDLOG_LOGGER_ERROR(logger, ...) logger->error(__VA_ARGS__)
#define SPDLOG_ERROR(...) spdlog::error(__VA_ARGS__)
#else
#define SPDLOG_TRACE(logger, ...) (void)0
#define SPDLOG_LOGGER_ERROR(logger, ...) (void)0
#define SPDLOG_ERROR(...) (void)0
#endif
#ifdef SPDLOG_DEBUG_ON
#define SPDLOG_DEBUG(logger, ...) logger->debug(__VA_ARGS__)
#if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_CRITICAL
#define SPDLOG_LOGGER_CRITICAL(logger, ...) logger->critical(__VA_ARGS__)
#define SPDLOG_CRITICAL(...) spdlog::critical(__VA_ARGS__)
#else
#define SPDLOG_DEBUG(logger, ...) (void)0
#define SPDLOG_LOGGER_CRITICAL(logger, ...) (void)0
#define SPDLOG_CRITICAL(...) (void)0
#endif
}
// namespace spdlog
...
...
include/spdlog/tweakme.h
View file @
0e77c339
...
...
@@ -128,3 +128,10 @@
//
// #define SPDLOG_DISABLE_DEFAULT_LOGGER
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// Uncomment and set to compile time level with zero cost (default is INFO).
// Macros like SPDLOG_DEBUG(..), SPDLOG_INFO(..) will expand to empty statements if not enabled
//
// #define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_INFO
///////////////////////////////////////////////////////////////////////////////
\ No newline at end of file
tests/includes.h
View file @
0e77c339
...
...
@@ -10,8 +10,7 @@
#include <ostream>
#include <string>
#define SPDLOG_TRACE_ON
#define SPDLOG_DEBUG_ON
#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_DEBUG
#define SPDLOG_ENABLE_MESSAGE_COUNTER
#include "spdlog/spdlog.h"
...
...
tests/test_macros.cpp
View file @
0e77c339
...
...
@@ -4,8 +4,13 @@
#include "includes.h"
#if SPDLOG_ACTIVE_LEVEL != SPDLOG_LEVEL_DEBUG
#error "Invalid SPDLOG_ACTIVE_LEVEL in test. Should be SPDLOG_LEVEL_DEBUG"
#endif
TEST_CASE
(
"debug and trace w/o format string"
,
"[macros]]"
)
{
prepare_logdir
();
std
::
string
filename
=
"logs/simple_log"
;
...
...
@@ -13,28 +18,15 @@ TEST_CASE("debug and trace w/o format string", "[macros]]")
logger
->
set_pattern
(
"%v"
);
logger
->
set_level
(
spdlog
::
level
::
trace
);
SPDLOG_TRACE
(
logger
,
"Test message 1"
);
SPDLOG_DEBUG
(
logger
,
"Test message 2"
);
SPDLOG_
LOGGER_
TRACE
(
logger
,
"Test message 1"
);
SPDLOG_
LOGGER_
DEBUG
(
logger
,
"Test message 2"
);
logger
->
flush
();
REQUIRE
(
ends_with
(
file_contents
(
filename
),
"Test message 2
\n
"
));
REQUIRE
(
count_lines
(
filename
)
==
2
);
REQUIRE
(
count_lines
(
filename
)
==
1
);
}
TEST_CASE
(
"d
ebug and trace with format strings"
,
"[macros]
]"
)
TEST_CASE
(
"d
isable param evaluation"
,
"[macros
]"
)
{
prepare_logdir
();
std
::
string
filename
=
"logs/simple_log"
;
auto
logger
=
spdlog
::
create
<
spdlog
::
sinks
::
basic_file_sink_mt
>
(
"logger"
,
filename
);
logger
->
set_pattern
(
"%v"
);
logger
->
set_level
(
spdlog
::
level
::
trace
);
SPDLOG_TRACE
(
logger
,
"Test message {}"
,
1
);
// SPDLOG_DEBUG(logger, "Test message 2");
SPDLOG_DEBUG
(
logger
,
"Test message {}"
,
222
);
logger
->
flush
();
REQUIRE
(
ends_with
(
file_contents
(
filename
),
"Test message 222
\n
"
));
REQUIRE
(
count_lines
(
filename
)
==
2
);
SPDLOG_TRACE
(
"Test message {}"
,
throw
std
::
runtime_error
(
"Should not be evaluated"
));
}
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