Print error and abort instead of throw if SPDLOG_NO_EXEPTIONS is defined

This commit is contained in:
gabime 2019-08-19 11:31:33 +03:00
parent b0a25f0183
commit 0335e3fcc0
17 changed files with 88 additions and 74 deletions

View File

@ -56,16 +56,14 @@ option(SPDLOG_BUILD_BENCH "Build benchmarks (Requires https://github.com/google/
option(SPDLOG_SANITIZE_ADDRESS "Enable address sanitizer in tests" OFF)
# install options
option(SPDLOG_INSTALL "Generate the install target." ${SPDLOG_MASTER_PROJECT})
option(SPDLOG_INSTALL "Generate the install target" ${SPDLOG_MASTER_PROJECT})
option(SPDLOG_FMT_EXTERNAL "Use external fmt library instead of bundled" OFF)
if(WIN32)
option(SPDLOG_WCHAR_SUPPORT "Support wchar api" OFF)
endif()
option(SPDLOG_NO_EXCEPTIONS "Support for -fno-exceptions. Replace throw with std::abort" OFF)
option(SPDLOG_NO_EXCEPTIONS "Compile with -fno-exceptions. Call abort() on any spdlog exceptions" OFF)
find_package(Threads REQUIRED)
@ -125,6 +123,7 @@ if(SPDLOG_WCHAR_SUPPORT)
if(SPDLOG_NO_EXCEPTIONS)
target_compile_definitions(spdlog PUBLIC SPDLOG_NO_EXCEPTIONS)
target_compile_options(spdlog PRIVATE -fno-exceptions)
target_compile_definitions(spdlog_header_only INTERFACE SPDLOG_NO_EXCEPTIONS)
endif()

View File

@ -23,7 +23,7 @@ void clone_example();
int main(int, char *[])
{
spdlog::info("Welcome to spdlog version {}.{}.{} !", SPDLOG_VER_MAJOR, SPDLOG_VER_MINOR, SPDLOG_VER_PATCH);
spdlog::info("Welcome to spdlog version {}{}.{}.{} !", SPDLOG_VER_MAJOR, SPDLOG_VER_MINOR, SPDLOG_VER_PATCH);
spdlog::warn("Easy padding in numbers like {:08d}", 12);
spdlog::critical("Support for int: {0:d}; hex: {0:x}; oct: {0:o}; bin: {0:b}", 42);
spdlog::info("Support for floats {:03.2f}", 1.23456);
@ -89,7 +89,7 @@ void stdout_logger_example()
void basic_example()
{
// Create basic file logger (not rotated).
auto my_logger = spdlog::basic_logger_mt("file_logger", "logs/basic-log.txt");
auto my_logger = spdlog::basic_logger_mt("file_logger", "lodgs/basic-log.txt");
}
#include "spdlog/sinks/rotating_file_sink.h"

View File

@ -40,7 +40,7 @@ struct async_factory_impl
auto &registry_inst = details::registry::instance();
// create global thread pool if not already exists..
auto& mutex = registry_inst.tp_mutex();
auto &mutex = registry_inst.tp_mutex();
std::lock_guard<std::recursive_mutex> tp_lock(mutex);
auto tp = registry_inst.get_tp();
if (tp == nullptr)

View File

@ -33,7 +33,7 @@ SPDLOG_INLINE void spdlog::async_logger::sink_it_(details::log_msg &msg)
}
else
{
SPDLOG_THROW spdlog_ex("async log: thread pool doesn't exist anymore");
SPDLOG_THROW(spdlog_ex("async log: thread pool doesn't exist anymore"));
}
}
@ -46,7 +46,7 @@ SPDLOG_INLINE void spdlog::async_logger::flush_()
}
else
{
SPDLOG_THROW spdlog_ex("async flush: thread pool doesn't exist anymore");
SPDLOG_THROW(spdlog_ex("async flush: thread pool doesn't exist anymore"));
}
}
@ -55,7 +55,7 @@ SPDLOG_INLINE void spdlog::async_logger::flush_()
//
SPDLOG_INLINE void spdlog::async_logger::backend_log_(const details::log_msg &incoming_log_msg)
{
try
SPDLOG_TRY
{
for (auto &s : sinks_)
{
@ -73,16 +73,10 @@ SPDLOG_INLINE void spdlog::async_logger::backend_log_(const details::log_msg &in
}
}
SPDLOG_INLINE void spdlog::async_logger::backend_flush_()
{
try
{
for (auto &sink : sinks_)
{
sink->flush();
}
}
SPDLOG_LOGGER_CATCH()
SPDLOG_INLINE void spdlog::async_logger::backend_flush_(){SPDLOG_TRY{for (auto &sink : sinks_){sink->flush();
}
}
SPDLOG_LOGGER_CATCH()
}
SPDLOG_INLINE std::shared_ptr<spdlog::logger> spdlog::async_logger::clone(std::string new_name)

View File

@ -65,9 +65,18 @@
#endif
#ifdef SPDLOG_NO_EXCEPTIONS
#define SPDLOG_THROW
#define SPDLOG_TRY
#define SPDLOG_THROW(ex) \
do \
{ \
printf("spdlog fatal error: %s\n", ex.what()); \
std::abort(); \
} while (0)
#define SPDLOG_CATCH_ALL()
#else
#define SPDLOG_THROW throw
#define SPDLOG_TRY try
#define SPDLOG_THROW(ex) throw(ex)
#define SPDLOG_CATCH_ALL() catch (...)
#endif
namespace spdlog {
@ -103,11 +112,13 @@ using string_view_t = basic_string_view_t<char>;
using wstring_view_t = basic_string_view_t<wchar_t>;
template<typename T>
struct is_convertible_to_wstring_view : std::is_convertible<T, wstring_view_t> { };
struct is_convertible_to_wstring_view : std::is_convertible<T, wstring_view_t>
{};
#endif // _WIN32
#else
template<typename>
struct is_convertible_to_wstring_view : std::false_type { };
struct is_convertible_to_wstring_view : std::false_type
{};
#endif // SPDLOG_WCHAR_TO_UTF8_SUPPORT
#if defined(SPDLOG_NO_ATOMIC_LEVELS)

View File

@ -39,14 +39,14 @@ SPDLOG_INLINE void file_helper::open(const filename_t &fname, bool truncate)
details::os::sleep_for_millis(open_interval);
}
SPDLOG_THROW spdlog_ex("Failed opening file " + os::filename_to_str(_filename) + " for writing", errno);
SPDLOG_THROW(spdlog_ex("Failed opening file " + os::filename_to_str(_filename) + " for writing", errno));
}
SPDLOG_INLINE void file_helper::reopen(bool truncate)
{
if (_filename.empty())
{
SPDLOG_THROW spdlog_ex("Failed re opening file - was not opened before");
SPDLOG_THROW(spdlog_ex("Failed re opening file - was not opened before"));
}
open(_filename, truncate);
}
@ -71,7 +71,7 @@ SPDLOG_INLINE void file_helper::write(const fmt::memory_buffer &buf)
auto data = buf.data();
if (std::fwrite(data, 1, msg_size, fd_) != msg_size)
{
SPDLOG_THROW spdlog_ex("Failed writing to file " + os::filename_to_str(_filename), errno);
SPDLOG_THROW(spdlog_ex("Failed writing to file " + os::filename_to_str(_filename), errno));
}
}
@ -79,7 +79,7 @@ SPDLOG_INLINE size_t file_helper::size() const
{
if (fd_ == nullptr)
{
SPDLOG_THROW spdlog_ex("Cannot use size() on closed file " + os::filename_to_str(_filename));
SPDLOG_THROW(spdlog_ex("Cannot use size() on closed file " + os::filename_to_str(_filename)));
}
return os::filesize(fd_);
}

View File

@ -126,7 +126,7 @@ SPDLOG_INLINE void prevent_child_fd(FILE *f)
auto fd = fileno(f);
if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
{
SPDLOG_THROW spdlog_ex("fcntl with FD_CLOEXEC failed", errno);
SPDLOG_THROW(spdlog_ex("fcntl with FD_CLOEXEC failed", errno));
}
#endif
}
@ -192,7 +192,7 @@ SPDLOG_INLINE size_t filesize(FILE *f)
{
if (f == nullptr)
{
SPDLOG_THROW spdlog_ex("Failed getting file size. fd is null");
SPDLOG_THROW(spdlog_ex("Failed getting file size. fd is null"));
}
#if defined(_WIN32) && !defined(__CYGWIN__)
int fd = _fileno(f);
@ -229,7 +229,7 @@ SPDLOG_INLINE size_t filesize(FILE *f)
}
#endif
#endif
SPDLOG_THROW spdlog_ex("Failed getting file size from fd", errno);
SPDLOG_THROW(spdlog_ex("Failed getting file size from fd", errno));
}
// Return utc offset in minutes or throw spdlog_ex on failure
@ -245,7 +245,7 @@ SPDLOG_INLINE int utc_minutes_offset(const std::tm &tm)
auto rv = GetDynamicTimeZoneInformation(&tzinfo);
#endif
if (rv == TIME_ZONE_ID_INVALID)
throw spdlog::spdlog_ex("Failed getting timezone info. ", errno);
SPDLOG_THROW(spdlog::spdlog_ex("Failed getting timezone info. ", errno));
int offset = -tzinfo.Bias;
if (tm.tm_isdst)
@ -408,7 +408,7 @@ SPDLOG_INLINE void wstr_to_utf8buf(basic_string_view_t<wchar_t> wstr, fmt::memor
{
if (wstr.size() > static_cast<size_t>(std::numeric_limits<int>::max()))
{
throw spdlog::spdlog_ex("UTF-16 string is too big to be converted to UTF-8");
SPDLOG_THROW(spdlog::spdlog_ex("UTF-16 string is too big to be converted to UTF-8"));
}
int wstr_size = static_cast<int>(wstr.size());
@ -436,7 +436,7 @@ SPDLOG_INLINE void wstr_to_utf8buf(basic_string_view_t<wchar_t> wstr, fmt::memor
}
}
throw spdlog::spdlog_ex(fmt::format("WideCharToMultiByte failed. Last error: {}", ::GetLastError()));
SPDLOG_THROW(spdlog::spdlog_ex(fmt::format("WideCharToMultiByte failed. Last error: {}", ::GetLastError())));
}
#endif // (defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT) || defined(SPDLOG_WCHAR_FILENAMES)) && defined(_WIN32)

View File

@ -245,7 +245,7 @@ SPDLOG_INLINE void registry::throw_if_exists_(const std::string &logger_name)
{
if (loggers_.find(logger_name) != loggers_.end())
{
SPDLOG_THROW spdlog_ex("logger with name '" + logger_name + "' already exists");
SPDLOG_THROW(spdlog_ex("logger with name '" + logger_name + "' already exists"));
}
}

View File

@ -17,8 +17,8 @@ SPDLOG_INLINE thread_pool::thread_pool(size_t q_max_items, size_t threads_n, std
{
if (threads_n == 0 || threads_n > 1000)
{
SPDLOG_THROW spdlog_ex("spdlog::thread_pool(): invalid threads_n param (valid "
"range is 1-1000)");
SPDLOG_THROW(spdlog_ex("spdlog::thread_pool(): invalid threads_n param (valid "
"range is 1-1000)"));
}
for (size_t i = 0; i < threads_n; i++)
{
@ -36,7 +36,7 @@ SPDLOG_INLINE thread_pool::thread_pool(size_t q_max_items, size_t threads_n)
// message all threads to terminate gracefully join them
SPDLOG_INLINE thread_pool::~thread_pool()
{
try
SPDLOG_TRY
{
for (size_t i = 0; i < threads_.size(); i++)
{
@ -48,8 +48,7 @@ SPDLOG_INLINE thread_pool::~thread_pool()
t.join();
}
}
catch (...)
{}
SPDLOG_CATCH_ALL() {}
}
void SPDLOG_INLINE thread_pool::post_log(async_logger_ptr &&worker_ptr, details::log_msg &msg, async_overflow_policy overflow_policy)

View File

@ -31,20 +31,20 @@ enum class async_msg_type
// Movable only. should never be copied
struct async_msg
{
async_msg_type msg_type;
level::level_enum level;
async_msg_type msg_type;
level::level_enum level;
log_clock::time_point time;
size_t thread_id;
size_t thread_id;
fmt::basic_memory_buffer<char, 176> raw;
source_loc source;
async_logger_ptr worker_ptr;
async_msg()
:msg_type(async_msg_type::log),
level(level::info),
thread_id(0)
{}
async_msg()
: msg_type(async_msg_type::log)
, level(level::info)
, thread_id(0)
{}
~async_msg() = default;
// should only be moved in or out of the queue..
@ -52,17 +52,18 @@ struct async_msg
// support for vs2013 move
#if defined(_MSC_VER) && _MSC_VER <= 1800
async_msg(async_msg &&other) : msg_type(other.msg_type),
level(other.level),
time(other.time),
thread_id(other.thread_id),
raw(move(other.raw)),
msg_id(other.msg_id),
source(other.source),
worker_ptr(std::move(other.worker_ptr))
async_msg(async_msg &&other)
: msg_type(other.msg_type)
, level(other.level)
, time(other.time)
, thread_id(other.thread_id)
, raw(move(other.raw))
, msg_id(other.msg_id)
, source(other.source)
, worker_ptr(std::move(other.worker_ptr))
{}
async_msg &operator=(async_msg &&other)
async_msg &operator=(async_msg &&other)
{
msg_type = other.msg_type;
level = other.level;

View File

@ -23,12 +23,11 @@ SPDLOG_INLINE logger::logger(const logger &other)
, custom_err_handler_(other.custom_err_handler_)
{}
SPDLOG_INLINE logger::logger(logger &&other) SPDLOG_NOEXCEPT
: name_(std::move(other.name_))
, sinks_(std::move(other.sinks_))
, level_(other.level_.load(std::memory_order_relaxed))
, flush_level_(other.flush_level_.load(std::memory_order_relaxed))
, custom_err_handler_(std::move(other.custom_err_handler_))
SPDLOG_INLINE logger::logger(logger &&other) SPDLOG_NOEXCEPT : name_(std::move(other.name_)),
sinks_(std::move(other.sinks_)),
level_(other.level_.load(std::memory_order_relaxed)),
flush_level_(other.flush_level_.load(std::memory_order_relaxed)),
custom_err_handler_(std::move(other.custom_err_handler_))
{}
SPDLOG_INLINE logger &logger::operator=(logger other) SPDLOG_NOEXCEPT
@ -168,7 +167,7 @@ SPDLOG_INLINE void logger::sink_it_(details::log_msg &msg)
{
if (sink->should_log(msg.level))
{
try
SPDLOG_TRY
{
sink->log(msg);
}
@ -186,7 +185,7 @@ SPDLOG_INLINE void logger::flush_()
{
for (auto &sink : sinks_)
{
try
SPDLOG_TRY
{
sink->flush();
}

View File

@ -23,7 +23,7 @@
#endif
#include <vector>
#ifndef SPDLOG_NO_EXCEPTIONS
#define SPDLOG_LOGGER_CATCH() \
catch (const std::exception &ex) \
{ \
@ -33,6 +33,9 @@
{ \
err_handler_("Unknown exception in logger"); \
}
#else
#define SPDLOG_LOGGER_CATCH()
#endif
namespace spdlog {
class logger
@ -63,7 +66,7 @@ public:
virtual ~logger() = default;
logger(const logger &other) ;
logger(const logger &other);
logger(logger &&other) SPDLOG_NOEXCEPT;
logger &operator=(logger other) SPDLOG_NOEXCEPT;
@ -72,7 +75,7 @@ public:
template<typename... Args>
void force_log(source_loc loc, level::level_enum lvl, string_view_t fmt, const Args &... args)
{
try
SPDLOG_TRY
{
fmt::memory_buffer buf;
fmt::format_to(buf, fmt, args...);
@ -156,14 +159,16 @@ public:
}
// T cannot be statically converted to string_view or wstring_view
template<class T, typename std::enable_if<!std::is_convertible<const T &, spdlog::string_view_t>::value && !is_convertible_to_wstring_view<const T &>::value, T>::type * = nullptr>
template<class T, typename std::enable_if<!std::is_convertible<const T &, spdlog::string_view_t>::value &&
!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))
{
return;
}
try
SPDLOG_TRY
{
fmt::memory_buffer buf;
fmt::format_to(buf, "{}", msg);

View File

@ -52,7 +52,7 @@ public:
{
if (rotation_hour < 0 || rotation_hour > 23 || rotation_minute < 0 || rotation_minute > 59)
{
SPDLOG_THROW spdlog_ex("daily_file_sink: Invalid rotation time in ctor");
SPDLOG_THROW(spdlog_ex("daily_file_sink: Invalid rotation time in ctor"));
}
auto now = log_clock::now();
file_helper_.open(FileNameCalc::calc_filename(base_filename_, now_tm(now)), truncate_);

View File

@ -112,7 +112,8 @@ SPDLOG_INLINE void rotating_file_sink<Mutex>::rotate_()
{
file_helper_.reopen(true); // truncate the log file anyway to prevent it to grow beyond its limit!
current_size_ = 0;
SPDLOG_THROW spdlog_ex("rotating_file_sink: failed renaming " + filename_to_str(src) + " to " + filename_to_str(target), errno);
SPDLOG_THROW(
spdlog_ex("rotating_file_sink: failed renaming " + filename_to_str(src) + " to " + filename_to_str(target), errno));
}
}
}

View File

@ -66,7 +66,7 @@ protected:
if (err)
{
SPDLOG_THROW spdlog_ex("Failed writing to systemd", errno);
SPDLOG_THROW(spdlog_ex("Failed writing to systemd", errno));
}
}

View File

@ -8,7 +8,6 @@ if(PkgConfig_FOUND)
endif()
set(SPDLOG_UTESTS_SOURCES
test_errors.cpp
test_file_helper.cpp
test_file_logging.cpp
test_misc.cpp
@ -26,6 +25,10 @@ set(SPDLOG_UTESTS_SOURCES
test_stdout_api.cpp
test_dup_filter.cpp)
if(NOT SPDLOG_NO_EXCEPTIONS)
list(APPEND SPDLOG_UTESTS_SOURCES test_errors.cpp)
endif()
if(systemd_FOUND)
list(APPEND SPDLOG_UTESTS_SOURCES test_systemd.cpp)
endif()

View File

@ -3,6 +3,7 @@
static const char *tested_logger_name = "null_logger";
static const char *tested_logger_name2 = "null_logger2";
#ifndef SPDLOG_NO_EXCEPTIONS
TEST_CASE("register_drop", "[registry]")
{
spdlog::drop_all();
@ -21,6 +22,7 @@ TEST_CASE("explicit register", "[registry]")
// Throw if registring existing name
REQUIRE_THROWS_AS(spdlog::create<spdlog::sinks::null_sink_mt>(tested_logger_name), spdlog::spdlog_ex);
}
#endif
TEST_CASE("apply_all", "[registry]")
{