1
0
mirror of https://github.com/gabime/spdlog.git synced 2025-01-15 17:27:57 +00:00

Compare commits

...

7 Commits

Author SHA1 Message Date
gabime
e3699070a4 clang-format 2019-11-03 17:07:37 +02:00
gabime
bf40855825 Micro-optimize level checking by inlining 2019-11-03 17:04:34 +02:00
gabime
3ee4f2810d Micro-optimze log_it_ 2019-11-03 17:01:58 +02:00
gabime
79468cf676 Micro-optimze log_it_ 2019-11-03 17:00:12 +02:00
gabime
4037942a26 suppress modernize-use-trailing-return-type fron clang-tidy 2019-11-03 15:22:54 +02:00
gabime
cae6c9ab36 Removed lazy argument evaluation from macros 2019-11-03 15:19:59 +02:00
gabime
15b393193a Replaced a forgotten try with SPDLOG_TRY 2019-11-03 15:16:03 +02:00
9 changed files with 50 additions and 56 deletions

View File

@ -30,7 +30,7 @@ SPDLOG_INLINE void file_helper::open(const filename_t &fname, bool truncate)
close();
filename_ = fname;
auto *mode = truncate ? SPDLOG_FILENAME_T("wb") : SPDLOG_FILENAME_T("ab");
for (int tries = 0; tries < open_tries_; ++tries)
{
// create containing folder if not exists already.

View File

@ -485,7 +485,7 @@ SPDLOG_INLINE bool create_dir(filename_t path)
return true;
}
if(path.empty())
if (path.empty())
{
return false;
}

View File

@ -98,24 +98,20 @@ bool SPDLOG_INLINE thread_pool::process_next_msg_()
switch (incoming_async_msg.msg_type)
{
case async_msg_type::log:
{
case async_msg_type::log: {
incoming_async_msg.worker_ptr->backend_sink_it_(incoming_async_msg);
return true;
}
case async_msg_type::flush:
{
case async_msg_type::flush: {
incoming_async_msg.worker_ptr->backend_flush_();
return true;
}
case async_msg_type::terminate:
{
case async_msg_type::terminate: {
return false;
}
default:
{
default: {
assert(false && "Unexpected async_msg_type");
}
}

View File

@ -64,16 +64,6 @@ SPDLOG_INLINE void swap(logger &a, logger &b)
a.swap(b);
}
SPDLOG_INLINE bool logger::should_log(level::level_enum msg_level) const
{
return msg_level >= level_.load(std::memory_order_relaxed);
}
SPDLOG_INLINE bool logger::should_backtrace() const
{
return tracer_.enabled();
}
SPDLOG_INLINE void logger::set_level(level::level_enum log_level)
{
level_.store(log_level);
@ -172,13 +162,13 @@ SPDLOG_INLINE std::shared_ptr<logger> logger::clone(std::string logger_name)
}
// protected methods
SPDLOG_INLINE void logger::log_it_(const details::log_msg &log_msg)
SPDLOG_INLINE void logger::log_it_(const spdlog::details::log_msg &log_msg, bool log_enabled, bool traceback_enabled)
{
if (should_log(log_msg.level))
if (log_enabled)
{
sink_it_(log_msg);
}
if (tracer_.enabled())
if (traceback_enabled)
{
tracer_.push_back(log_msg);
}

View File

@ -76,7 +76,9 @@ public:
template<typename... Args>
void log(source_loc loc, level::level_enum lvl, string_view_t fmt, const Args &... args)
{
if (!should_log(lvl) && !tracer_.enabled())
bool log_enabled = should_log(lvl);
bool traceback_enabled = tracer_.enabled();
if (!log_enabled && !traceback_enabled)
{
return;
}
@ -85,7 +87,7 @@ public:
memory_buf_t buf;
fmt::format_to(buf, fmt, args...);
details::log_msg log_msg(loc, name_, lvl, string_view_t(buf.data(), buf.size()));
log_it_(log_msg);
log_it_(log_msg, log_enabled, traceback_enabled);
}
SPDLOG_LOGGER_CATCH()
}
@ -142,13 +144,15 @@ public:
template<class T, typename std::enable_if<std::is_convertible<const T &, spdlog::string_view_t>::value, T>::type * = nullptr>
void log(source_loc loc, level::level_enum lvl, const T &msg)
{
if (!should_log(lvl) && !tracer_.enabled())
bool log_enabled = should_log(lvl);
bool traceback_enabled = tracer_.enabled();
if (!log_enabled && !traceback_enabled)
{
return;
}
details::log_msg log_msg(loc, name_, lvl, msg);
log_it_(log_msg);
log_it_(log_msg, log_enabled, traceback_enabled);
}
void log(level::level_enum lvl, string_view_t msg)
@ -209,7 +213,9 @@ public:
template<typename... Args>
void log(source_loc loc, level::level_enum lvl, wstring_view_t fmt, const Args &... args)
{
if (!should_log(lvl) && !tracer_.enabled())
bool log_enabled = should_log(lvl);
bool traceback_enabled = tracer_.enabled();
if (!log_enabled && !traceback_enabled)
{
return;
}
@ -222,7 +228,7 @@ public:
memory_buf_t buf;
details::os::wstr_to_utf8buf(wstring_view_t(wbuf.data(), wbuf.size()), buf);
details::log_msg log_msg(loc, name_, lvl, string_view_t(buf.data(), buf.size()));
log_it_(log_msg);
log_it_(log_msg, log_enabled, traceback_enabled);
}
SPDLOG_LOGGER_CATCH()
}
@ -273,17 +279,19 @@ public:
template<class T, typename std::enable_if<is_convertible_to_wstring_view<const T &>::value, T>::type * = nullptr>
void log(source_loc loc, level::level_enum lvl, const T &msg)
{
if (!should_log(lvl) && !tracer_.enabled())
bool log_enabled = should_log(lvl);
bool traceback_enabled = tracer_.enabled();
if (!log_enabled && !traceback_enabled)
{
return;
}
try
SPDLOG_TRY
{
memory_buf_t buf;
details::os::wstr_to_utf8buf(msg, buf);
details::log_msg log_msg(loc, name_, lvl, string_view_t(buf.data(), buf.size()));
log_it_(log_msg);
log_it_(log_msg, log_enabled, traceback_enabled);
}
SPDLOG_LOGGER_CATCH()
}
@ -291,10 +299,16 @@ public:
#endif // SPDLOG_WCHAR_TO_UTF8_SUPPORT
// return true logging is enabled for the given level.
bool should_log(level::level_enum msg_level) const;
bool should_log(level::level_enum msg_level) const
{
return msg_level >= level_.load(std::memory_order_relaxed);
}
// return true if backtrace logging is enabled.
bool should_backtrace() const;
bool should_backtrace() const
{
return tracer_.enabled();
}
void set_level(level::level_enum log_level);
@ -340,7 +354,7 @@ protected:
// log the given message (if the given log level is high enough),
// and save backtrace (if backtrace is enabled).
void log_it_(const details::log_msg &log_msg);
void log_it_(const details::log_msg &log_msg, bool log_enabled, bool traceback_enabled);
virtual void sink_it_(const details::log_msg &msg);
virtual void flush_();
void dump_backtrace_();

View File

@ -60,15 +60,14 @@ protected:
if (msg.source.empty())
{
// Note: function call inside '()' to avoid macro expansion
err = (sd_journal_send)(
"MESSAGE=%.*s", static_cast<int>(length), msg.payload.data(), "PRIORITY=%d", syslog_level(msg.level),
err = (sd_journal_send)("MESSAGE=%.*s", static_cast<int>(length), msg.payload.data(), "PRIORITY=%d", syslog_level(msg.level),
"SYSLOG_IDENTIFIER=%.*s", static_cast<int>(msg.logger_name.size()), msg.logger_name.data(), nullptr);
}
else
{
err = (sd_journal_send)("MESSAGE=%.*s", static_cast<int>(length), msg.payload.data(), "PRIORITY=%d", syslog_level(msg.level),
"SYSLOG_IDENTIFIER=%.*s", static_cast<int>(msg.logger_name.size()), msg.logger_name.data(),
"CODE_FILE=%s", msg.source.filename, "CODE_LINE=%d", msg.source.line, "CODE_FUNC=%s", msg.source.funcname, nullptr);
"SYSLOG_IDENTIFIER=%.*s", static_cast<int>(msg.logger_name.size()), msg.logger_name.data(), "CODE_FILE=%s",
msg.source.filename, "CODE_LINE=%d", msg.source.line, "CODE_FUNC=%s", msg.source.funcname, nullptr);
}
if (err)

View File

@ -285,12 +285,7 @@ inline void critical(wstring_view_t fmt, const Args &... args)
// SPDLOG_LEVEL_OFF
//
#define SPDLOG_LOGGER_CALL(logger, level, ...) \
do \
{ \
if ((logger)->should_log(level) || (logger)->should_backtrace()) \
(logger)->log(spdlog::source_loc{__FILE__, __LINE__, SPDLOG_FUNCTION}, level, __VA_ARGS__); \
} while (0)
#define SPDLOG_LOGGER_CALL(logger, level, ...) (logger)->log(spdlog::source_loc{__FILE__, __LINE__, SPDLOG_FUNCTION}, level, __VA_ARGS__);
#if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_TRACE
#define SPDLOG_LOGGER_TRACE(logger, ...) SPDLOG_LOGGER_CALL(logger, spdlog::level::trace, __VA_ARGS__)

View File

@ -1,4 +1,4 @@
Checks: 'modernize-*,modernize-use-override,google-*,-google-runtime-references,misc-*,clang-analyzer-*,-misc-non-private-member-variables-in-classes'
Checks: 'modernize-*,modernize-use-override,google-*,-google-runtime-references,misc-*,clang-analyzer-*,-misc-non-private-member-variables-in-classes, -modernize-use-trailing-return-type'
WarningsAsErrors: ''
HeaderFilterRegex: 'async.h|async_logger.h|common.h|details|formatter.h|logger.h|sinks|spdlog.h|tweakme.h|version.h'
AnalyzeTemporaryDtors: false

View File

@ -40,20 +40,20 @@ TEST_CASE("disable param evaluation", "[macros]")
SPDLOG_TRACE("Test message {}", throw std::runtime_error("Should not be evaluated"));
}
TEST_CASE("compile with reference to logger", "[macros]")
TEST_CASE("pass logger pointer", "[macros]")
{
auto logger = spdlog::create<spdlog::sinks::null_sink_mt>("refmacro");
auto& ref = *logger;
auto &ref = *logger;
SPDLOG_LOGGER_TRACE(&ref, "Test message 1");
SPDLOG_LOGGER_DEBUG(&ref, "Test message 2");
}
// ensure that even if right macro level is on- don't evaluate if the logger's level is not high enough
TEST_CASE("disable param evaluation2", "[macros]")
{
auto logger = std::make_shared<spdlog::logger>("test-macro");
logger->set_level(spdlog::level::off);
int x = 0;
SPDLOG_LOGGER_DEBUG(logger, "Test message {}", ++x);
REQUIRE(x == 0);
}
// TEST_CASE("disable param evaluation2", "[macros]")
//{
// auto logger = std::make_shared<spdlog::logger>("test-macro");
// logger->set_level(spdlog::level::off);
// int x = 0;
// SPDLOG_LOGGER_DEBUG(logger, "Test message {}", ++x);
// REQUIRE(x == 0);
//}