Support for source file/line logging

This commit is contained in:
gabime 2018-11-22 18:47:50 +02:00
parent 216cd6935f
commit f2305fe5bf
6 changed files with 176 additions and 32 deletions

View File

@ -185,6 +185,15 @@ using filename_t = std::wstring;
using filename_t = std::string;
#endif
struct source_loc
{
SPDLOG_CONSTEXPR source_loc(): filename(""), line(0) {}
SPDLOG_CONSTEXPR source_loc(const char *filename, int line) : filename(filename), line(line) {}
const char* const filename ;
const uint32_t line;
};
namespace details {
// make_unique support for pre c++14

View File

@ -16,7 +16,7 @@ namespace details {
struct log_msg
{
log_msg(const std::string *loggers_name, level::level_enum lvl, string_view_t view)
log_msg(source_loc loc, const std::string *loggers_name, level::level_enum lvl, string_view_t view)
: logger_name(loggers_name)
, level(lvl)
#ifndef SPDLOG_NO_DATETIME
@ -25,11 +25,15 @@ struct log_msg
#ifndef SPDLOG_NO_THREAD_ID
, thread_id(os::thread_id())
, source(loc)
, payload(view)
#endif
{
}
log_msg(const std::string *loggers_name, level::level_enum lvl, string_view_t view):
log_msg(source_loc{}, loggers_name, lvl, view){}
log_msg(const log_msg &other) = default;
log_msg &operator=(const log_msg &other) = default;
@ -43,6 +47,7 @@ struct log_msg
mutable size_t color_range_start{0};
mutable size_t color_range_end{0};
source_loc source;
const string_view_t payload;
};
} // namespace details

View File

@ -57,8 +57,9 @@ inline void spdlog::logger::set_pattern(std::string pattern, pattern_time_type t
set_formatter(std::move(new_formatter));
}
template<typename... Args>
inline void spdlog::logger::log(level::level_enum lvl, const char *fmt, const Args &... args)
inline void spdlog::logger::log(source_loc source, level::level_enum lvl, const char *fmt, const Args &... args)
{
if (!should_log(lvl))
{
@ -70,13 +71,20 @@ inline void spdlog::logger::log(level::level_enum lvl, const char *fmt, const Ar
using details::fmt_helper::to_string_view;
fmt::memory_buffer buf;
fmt::format_to(buf, fmt, args...);
details::log_msg log_msg(&name_, lvl, to_string_view(buf));
details::log_msg log_msg(source, &name_, lvl, to_string_view(buf));
sink_it_(log_msg);
}
SPDLOG_CATCH_AND_HANDLE
}
inline void spdlog::logger::log(level::level_enum lvl, const char *msg)
template<typename... Args>
inline void spdlog::logger::log(level::level_enum lvl, const char *fmt, const Args &... args)
{
log(source_loc{}, lvl, fmt, args...);
}
inline void spdlog::logger::log(source_loc source, level::level_enum lvl, const char *msg)
{
if (!should_log(lvl))
{
@ -85,7 +93,27 @@ inline void spdlog::logger::log(level::level_enum lvl, const char *msg)
try
{
details::log_msg log_msg(&name_, lvl, spdlog::string_view_t(msg));
details::log_msg log_msg(source, &name_, lvl, spdlog::string_view_t(msg));
sink_it_(log_msg);
}
SPDLOG_CATCH_AND_HANDLE
}
inline void spdlog::logger::log(level::level_enum lvl, const char *msg)
{
log(source_loc{}, lvl, msg);
}
template<class T, typename std::enable_if<std::is_convertible<T, spdlog::string_view_t>::value, T>::type *>
inline void spdlog::logger::log(source_loc source, level::level_enum lvl, const T &msg)
{
if (!should_log(lvl))
{
return;
}
try
{
details::log_msg log_msg(source, &name_, lvl, msg);
sink_it_(log_msg);
}
SPDLOG_CATCH_AND_HANDLE
@ -94,20 +122,11 @@ inline void spdlog::logger::log(level::level_enum lvl, const char *msg)
template<class T, typename std::enable_if<std::is_convertible<T, spdlog::string_view_t>::value, T>::type *>
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, msg);
sink_it_(log_msg);
}
SPDLOG_CATCH_AND_HANDLE
log(source_loc{}, lvl, msg);
}
template<class T, typename std::enable_if<!std::is_convertible<T, spdlog::string_view_t>::value, T>::type *>
inline void spdlog::logger::log(level::level_enum lvl, const T &msg)
inline void spdlog::logger::log(source_loc source, level::level_enum lvl, const T &msg)
{
if (!should_log(lvl))
{
@ -118,12 +137,18 @@ inline void spdlog::logger::log(level::level_enum lvl, const T &msg)
using details::fmt_helper::to_string_view;
fmt::memory_buffer buf;
fmt::format_to(buf, "{}", msg);
details::log_msg log_msg(&name_, lvl, to_string_view(buf));
details::log_msg log_msg(source, &name_, lvl, to_string_view(buf));
sink_it_(log_msg);
}
SPDLOG_CATCH_AND_HANDLE
}
template<class T, typename std::enable_if<!std::is_convertible<T, spdlog::string_view_t>::value, T>::type *>
inline void spdlog::logger::log(level::level_enum lvl, const T &msg)
{
log(source_loc{}, lvl, msg);
}
template<typename... Args>
inline void spdlog::logger::trace(const char *fmt, const Args &... args)
{
@ -220,7 +245,7 @@ inline void wbuf_to_utf8buf(const fmt::wmemory_buffer &wbuf, fmt::memory_buffer
}
template<typename... Args>
inline void spdlog::logger::log(level::level_enum lvl, const wchar_t *fmt, const Args &... args)
inline void spdlog::logger::log(source_location source, level::level_enum lvl, const wchar_t *fmt, const Args &... args)
{
if (!should_log(lvl))
{
@ -235,12 +260,18 @@ inline void spdlog::logger::log(level::level_enum lvl, const wchar_t *fmt, const
fmt::format_to(wbuf, fmt, args...);
fmt::memory_buffer buf;
wbuf_to_utf8buf(wbuf, buf);
details::log_msg log_msg(&name_, lvl, to_string_view(buf));
details::log_msg log_msg(source, &name_, lvl, to_string_view(buf));
sink_it_(log_msg);
}
SPDLOG_CATCH_AND_HANDLE
}
template<typename... Args>
inline void spdlog::logger::log(level::level_enum lvl, const wchar_t *fmt, const Args &... args)
{
log(source_location{}, lvl, fmt, args...);
}
template<typename... Args>
inline void spdlog::logger::trace(const wchar_t *fmt, const Args &... args)
{

View File

@ -808,6 +808,68 @@ public:
}
};
// print soruce location
class source_location_formatter final : public flag_formatter
{
public:
explicit source_location_formatter(padding_info padinfo)
: flag_formatter(padinfo){};
void format(const details::log_msg &msg, const std::tm &, fmt::memory_buffer &dest) override
{
if(padinfo_.width_)
{
const auto text_size = std::char_traits<char>::length(msg.source.filename)
+ fmt_helper::count_digits(msg.source.line)
+1;
scoped_pad p(text_size, padinfo_, dest);
fmt_helper::append_string_view(msg.source.filename, dest);
dest.push_back(':');
fmt_helper::append_int(msg.source.line, dest);
}
else
{
fmt_helper::append_string_view(msg.source.filename, dest);
dest.push_back(':');
fmt_helper::append_int(msg.source.line, dest);
}
}
};
// print soruce filename
class source_filename_formatter final : public flag_formatter
{
public:
explicit source_filename_formatter(padding_info padinfo)
: flag_formatter(padinfo){};
void format(const details::log_msg &msg, const std::tm &, fmt::memory_buffer &dest) override
{
scoped_pad p(msg.source.filename, padinfo_, dest);
fmt_helper::append_string_view(msg.source.filename, dest);
}
};
class source_linenum_formatter final : public flag_formatter
{
public:
explicit source_linenum_formatter(padding_info padinfo)
: flag_formatter(padinfo){};
void format(const details::log_msg &msg, const std::tm &, fmt::memory_buffer &dest) override
{
if(padinfo_.width_) {
const size_t field_size = fmt::internal::count_digits(msg.source.line);
scoped_pad p(field_size, padinfo_, dest);
fmt_helper::append_int(msg.source.line, dest);
}
else
{
fmt_helper::append_int(msg.source.line, dest);
}
}
};
// Full info formatter
// pattern: [%Y-%m-%d %H:%M:%S.%e] [%n] [%l] %v
class full_formatter final : public flag_formatter
@ -1102,6 +1164,18 @@ private:
formatters_.push_back(details::make_unique<details::color_stop_formatter>(padding));
break;
case ('@'):
formatters_.push_back(details::make_unique<details::source_location_formatter>(padding));
break;
case ('s'):
formatters_.push_back(details::make_unique<details::source_filename_formatter>(padding));
break;
case ('#'):
formatters_.push_back(details::make_unique<details::source_linenum_formatter>(padding));
break;
case ('%'):
formatters_.push_back(details::make_unique<details::ch_formatter>('%'));
break;

View File

@ -46,8 +46,13 @@ public:
template<typename... Args>
void log(level::level_enum lvl, const char *fmt, const Args &... args);
template<typename... Args>
void log(source_loc loc, level::level_enum lvl, const char *fmt, const Args &... args);
void log(level::level_enum lvl, const char *msg);
void log(source_loc loc, level::level_enum lvl, const char *msg);
template<typename... Args>
void trace(const char *fmt, const Args &... args);
@ -73,6 +78,9 @@ public:
template<typename... Args>
void log(level::level_enum lvl, const wchar_t *fmt, const Args &... args);
template<typename... Args>
void log(source_location soruce, level::level_enum lvl, const wchar_t *fmt, const Args &... args);
template<typename... Args>
void trace(const wchar_t *fmt, const Args &... args);
@ -97,10 +105,18 @@ public:
template<class T, typename std::enable_if<std::is_convertible<T, spdlog::string_view_t>::value, T>::type * = nullptr>
void log(level::level_enum lvl, const T &);
// T can be statically converted to string_view
template<class T, typename std::enable_if<std::is_convertible<T, spdlog::string_view_t>::value, T>::type * = nullptr>
void log(source_loc loc, level::level_enum lvl, const T &);
// T cannot be statically converted to string_view
template<class T, typename std::enable_if<!std::is_convertible<T, spdlog::string_view_t>::value, T>::type * = nullptr>
void log(level::level_enum lvl, const T &);
// T cannot be statically converted to string_view
template<class T, typename std::enable_if<!std::is_convertible<T, spdlog::string_view_t>::value, T>::type * = nullptr>
void log(source_loc loc, level::level_enum lvl, const T &);
template<typename T>
void trace(const T &msg);

View File

@ -161,10 +161,16 @@ inline void set_default_logger(std::shared_ptr<spdlog::logger> default_logger)
details::registry::instance().set_default_logger(std::move(default_logger));
}
template<typename... Args>
inline void log(source_loc source, level::level_enum lvl, const char *fmt, const Args &... args)
{
default_logger_raw()->log(source, lvl, fmt, args...);
}
template<typename... Args>
inline void log(level::level_enum lvl, const char *fmt, const Args &... args)
{
default_logger_raw()->log(lvl, fmt, args...);
default_logger_raw()->log(source_loc{}, lvl, fmt, args...);
}
template<typename... Args>
@ -303,49 +309,52 @@ inline void critical(const wchar_t *fmt, const Args &... args)
// SPDLOG_LEVEL_OFF
//
#define SPDLOG_LOGGER_LOG(logger, level, ...) logger->log(spdlog::source_loc{__FILE__, __LINE__}, level, __VA_ARGS__)
#if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_TRACE
#define SPDLOG_LOGGER_TRACE(logger, ...) logger->trace(__VA_ARGS__)
#define SPDLOG_TRACE(...) spdlog::trace(__VA_ARGS__)
#define SPDLOG_LOGGER_TRACE(logger, ...) SPDLOG_LOGGER_LOG(logger, spdlog::level::trace, __VA_ARGS__)
#define SPDLOG_TRACE(...) SPDLOG_LOGGER_LOG(spdlog::default_logger_raw(), spdlog::level::trace, __VA_ARGS__)
#else
#define SPDLOG_LOGGER_TRACE(logger, ...) (void)0
#define SPDLOG_TRACE(...) (void)0
#endif
#if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_DEBUG
#define SPDLOG_LOGGER_DEBUG(logger, ...) logger->debug(__VA_ARGS__)
#define SPDLOG_DEBUG(...) spdlog::debug(__VA_ARGS__)
#define SPDLOG_LOGGER_DEBUG(logger, ...) SPDLOG_LOGGER_LOG(logger, spdlog::level::debug, __VA_ARGS__)
#define SPDLOG_DEBUG(...) SPDLOG_LOGGER_LOG(spdlog::default_logger_raw(), spdlog::level::debug, __VA_ARGS__)
#else
#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__)
#define SPDLOG_LOGGER_INFO(logger, ...) SPDLOG_LOGGER_LOG(logger, spdlog::level::info, __VA_ARGS__)
#define SPDLOG_INFO(...) SPDLOG_LOGGER_LOG(spdlog::default_logger_raw(), spdlog::level::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__)
#define SPDLOG_LOGGER_WARN(logger, ...) SPDLOG_LOGGER_LOG(logger, spdlog::level::warn, __VA_ARGS__)
#define SPDLOG_WARN(...) SPDLOG_LOGGER_LOG(spdlog::default_logger_raw(), spdlog::level::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__)
#define SPDLOG_LOGGER_ERROR(logger, ...) SPDLOG_LOGGER_LOG(logger, spdlog::level::err, __VA_ARGS__)
#define SPDLOG_ERROR(...) SPDLOG_LOGGER_LOG(spdlog::default_logger_raw(), spdlog::level::err, __VA_ARGS__)
#else
#define SPDLOG_LOGGER_ERROR(logger, ...) (void)0
#define SPDLOG_ERROR(...) (void)0
#endif
#if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_CRITICAL
#define SPDLOG_LOGGER_CRITICAL(logger, ...) logger->critical(__VA_ARGS__)
#define SPDLOG_CRITICAL(...) spdlog::critical(__VA_ARGS__)
#define SPDLOG_LOGGER_CRITICAL(logger, ...) SPDLOG_LOGGER_LOG(logger, spdlog::level::critical, __VA_ARGS__)
#define SPDLOG_CRITICAL(...) SPDLOG_LOGGER_LOG(spdlog::default_logger_raw(), spdlog::level::critical, __VA_ARGS__)
#else
#define SPDLOG_LOGGER_CRITICAL(logger, ...) (void)0
#define SPDLOG_CRITICAL(...) (void)0