Unverified Commit 2f854428 authored by Gabi Melman's avatar Gabi Melman Committed by GitHub

Merge pull request #1188 from gabime/no-exceptions

Support for -fno exceptions 
parents 76aa1059 c1a524a9
...@@ -56,13 +56,15 @@ option(SPDLOG_BUILD_BENCH "Build benchmarks (Requires https://github.com/google/ ...@@ -56,13 +56,15 @@ option(SPDLOG_BUILD_BENCH "Build benchmarks (Requires https://github.com/google/
option(SPDLOG_SANITIZE_ADDRESS "Enable address sanitizer in tests" OFF) option(SPDLOG_SANITIZE_ADDRESS "Enable address sanitizer in tests" OFF)
# install options # 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) option(SPDLOG_FMT_EXTERNAL "Use external fmt library instead of bundled" OFF)
if(WIN32) if(WIN32)
option(SPDLOG_WCHAR_SUPPORT "Support wchar api" OFF) option(SPDLOG_WCHAR_SUPPORT "Support wchar api" OFF)
endif() endif()
option(SPDLOG_NO_EXCEPTIONS "Compile with -fno-exceptions. Call abort() on any spdlog exceptions" OFF)
find_package(Threads REQUIRED) find_package(Threads REQUIRED)
...@@ -119,6 +121,16 @@ if(SPDLOG_WCHAR_SUPPORT) ...@@ -119,6 +121,16 @@ if(SPDLOG_WCHAR_SUPPORT)
target_compile_definitions(spdlog_header_only INTERFACE SPDLOG_WCHAR_TO_UTF8_SUPPORT) target_compile_definitions(spdlog_header_only INTERFACE SPDLOG_WCHAR_TO_UTF8_SUPPORT)
endif() endif()
if(SPDLOG_NO_EXCEPTIONS)
target_compile_definitions(spdlog PUBLIC SPDLOG_NO_EXCEPTIONS)
target_compile_definitions(spdlog_header_only INTERFACE SPDLOG_NO_EXCEPTIONS)
if(NOT MSVC)
target_compile_options(spdlog PRIVATE -fno-exceptions)
endif()
endif()
#--------------------------------------------------------------------------------------- #---------------------------------------------------------------------------------------
# Build binaries # Build binaries
......
...@@ -23,7 +23,7 @@ void clone_example(); ...@@ -23,7 +23,7 @@ void clone_example();
int main(int, char *[]) 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::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::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); spdlog::info("Support for floats {:03.2f}", 1.23456);
...@@ -89,7 +89,7 @@ void stdout_logger_example() ...@@ -89,7 +89,7 @@ void stdout_logger_example()
void basic_example() void basic_example()
{ {
// Create basic file logger (not rotated). // 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" #include "spdlog/sinks/rotating_file_sink.h"
......
...@@ -40,7 +40,8 @@ struct async_factory_impl ...@@ -40,7 +40,8 @@ struct async_factory_impl
auto &registry_inst = details::registry::instance(); auto &registry_inst = details::registry::instance();
// create global thread pool if not already exists.. // 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); std::lock_guard<std::recursive_mutex> tp_lock(mutex);
auto tp = registry_inst.get_tp(); auto tp = registry_inst.get_tp();
if (tp == nullptr) if (tp == nullptr)
......
...@@ -33,7 +33,7 @@ SPDLOG_INLINE void spdlog::async_logger::sink_it_(details::log_msg &msg) ...@@ -33,7 +33,7 @@ SPDLOG_INLINE void spdlog::async_logger::sink_it_(details::log_msg &msg)
} }
else else
{ {
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_() ...@@ -46,7 +46,7 @@ SPDLOG_INLINE void spdlog::async_logger::flush_()
} }
else else
{ {
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_() ...@@ -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) SPDLOG_INLINE void spdlog::async_logger::backend_log_(const details::log_msg &incoming_log_msg)
{ {
try SPDLOG_TRY
{ {
for (auto &s : sinks_) for (auto &s : sinks_)
{ {
...@@ -73,16 +73,10 @@ SPDLOG_INLINE void spdlog::async_logger::backend_log_(const details::log_msg &in ...@@ -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_() SPDLOG_INLINE void spdlog::async_logger::backend_flush_(){SPDLOG_TRY{for (auto &sink : sinks_){sink->flush();
{ }
try }
{ SPDLOG_LOGGER_CATCH()
for (auto &sink : sinks_)
{
sink->flush();
}
}
SPDLOG_LOGGER_CATCH()
} }
SPDLOG_INLINE std::shared_ptr<spdlog::logger> spdlog::async_logger::clone(std::string new_name) SPDLOG_INLINE std::shared_ptr<spdlog::logger> spdlog::async_logger::clone(std::string new_name)
......
...@@ -64,6 +64,21 @@ ...@@ -64,6 +64,21 @@
#define SPDLOG_FUNCTION __FUNCTION__ #define SPDLOG_FUNCTION __FUNCTION__
#endif #endif
#ifdef SPDLOG_NO_EXCEPTIONS
#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_TRY try
#define SPDLOG_THROW(ex) throw(ex)
#define SPDLOG_CATCH_ALL() catch (...)
#endif
namespace spdlog { namespace spdlog {
class formatter; class formatter;
...@@ -97,11 +112,13 @@ using string_view_t = basic_string_view_t<char>; ...@@ -97,11 +112,13 @@ using string_view_t = basic_string_view_t<char>;
using wstring_view_t = basic_string_view_t<wchar_t>; using wstring_view_t = basic_string_view_t<wchar_t>;
template<typename 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 #endif // _WIN32
#else #else
template<typename> 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 #endif // SPDLOG_WCHAR_TO_UTF8_SUPPORT
#if defined(SPDLOG_NO_ATOMIC_LEVELS) #if defined(SPDLOG_NO_ATOMIC_LEVELS)
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#endif #endif
#include "spdlog/details/os.h" #include "spdlog/details/os.h"
#include "spdlog/common.h"
#include <cerrno> #include <cerrno>
#include <chrono> #include <chrono>
...@@ -39,14 +40,14 @@ SPDLOG_INLINE void file_helper::open(const filename_t &fname, bool truncate) ...@@ -39,14 +40,14 @@ SPDLOG_INLINE void file_helper::open(const filename_t &fname, bool truncate)
details::os::sleep_for_millis(open_interval); details::os::sleep_for_millis(open_interval);
} }
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) SPDLOG_INLINE void file_helper::reopen(bool truncate)
{ {
if (_filename.empty()) if (_filename.empty())
{ {
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); open(_filename, truncate);
} }
...@@ -71,7 +72,7 @@ SPDLOG_INLINE void file_helper::write(const fmt::memory_buffer &buf) ...@@ -71,7 +72,7 @@ SPDLOG_INLINE void file_helper::write(const fmt::memory_buffer &buf)
auto data = buf.data(); auto data = buf.data();
if (std::fwrite(data, 1, msg_size, fd_) != msg_size) if (std::fwrite(data, 1, msg_size, fd_) != msg_size)
{ {
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 +80,7 @@ SPDLOG_INLINE size_t file_helper::size() const ...@@ -79,7 +80,7 @@ SPDLOG_INLINE size_t file_helper::size() const
{ {
if (fd_ == nullptr) if (fd_ == nullptr)
{ {
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_); return os::filesize(fd_);
} }
......
...@@ -7,6 +7,8 @@ ...@@ -7,6 +7,8 @@
#include "spdlog/details/os.h" #include "spdlog/details/os.h"
#endif #endif
#include "spdlog/common.h"
#include <algorithm> #include <algorithm>
#include <chrono> #include <chrono>
#include <cstdio> #include <cstdio>
...@@ -120,13 +122,13 @@ SPDLOG_INLINE void prevent_child_fd(FILE *f) ...@@ -120,13 +122,13 @@ SPDLOG_INLINE void prevent_child_fd(FILE *f)
#if !defined(__cplusplus_winrt) #if !defined(__cplusplus_winrt)
auto file_handle = reinterpret_cast<HANDLE>(_get_osfhandle(_fileno(f))); auto file_handle = reinterpret_cast<HANDLE>(_get_osfhandle(_fileno(f)));
if (!::SetHandleInformation(file_handle, HANDLE_FLAG_INHERIT, 0)) if (!::SetHandleInformation(file_handle, HANDLE_FLAG_INHERIT, 0))
throw spdlog_ex("SetHandleInformation failed", errno); SPDLOG_THROW(spdlog_ex("SetHandleInformation failed", errno));
#endif #endif
#else #else
auto fd = fileno(f); auto fd = fileno(f);
if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
{ {
throw spdlog_ex("fcntl with FD_CLOEXEC failed", errno); SPDLOG_THROW(spdlog_ex("fcntl with FD_CLOEXEC failed", errno));
} }
#endif #endif
} }
...@@ -192,7 +194,7 @@ SPDLOG_INLINE size_t filesize(FILE *f) ...@@ -192,7 +194,7 @@ SPDLOG_INLINE size_t filesize(FILE *f)
{ {
if (f == nullptr) if (f == nullptr)
{ {
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__) #if defined(_WIN32) && !defined(__CYGWIN__)
int fd = _fileno(f); int fd = _fileno(f);
...@@ -229,7 +231,7 @@ SPDLOG_INLINE size_t filesize(FILE *f) ...@@ -229,7 +231,7 @@ SPDLOG_INLINE size_t filesize(FILE *f)
} }
#endif #endif
#endif #endif
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 // Return utc offset in minutes or throw spdlog_ex on failure
...@@ -245,7 +247,7 @@ SPDLOG_INLINE int utc_minutes_offset(const std::tm &tm) ...@@ -245,7 +247,7 @@ SPDLOG_INLINE int utc_minutes_offset(const std::tm &tm)
auto rv = GetDynamicTimeZoneInformation(&tzinfo); auto rv = GetDynamicTimeZoneInformation(&tzinfo);
#endif #endif
if (rv == TIME_ZONE_ID_INVALID) 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; int offset = -tzinfo.Bias;
if (tm.tm_isdst) if (tm.tm_isdst)
...@@ -408,7 +410,7 @@ SPDLOG_INLINE void wstr_to_utf8buf(basic_string_view_t<wchar_t> wstr, fmt::memor ...@@ -408,7 +410,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())) 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()); int wstr_size = static_cast<int>(wstr.size());
...@@ -436,7 +438,7 @@ SPDLOG_INLINE void wstr_to_utf8buf(basic_string_view_t<wchar_t> wstr, fmt::memor ...@@ -436,7 +438,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) #endif // (defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT) || defined(SPDLOG_WCHAR_FILENAMES)) && defined(_WIN32)
......
...@@ -32,7 +32,6 @@ namespace details { ...@@ -32,7 +32,6 @@ namespace details {
SPDLOG_INLINE registry::registry() SPDLOG_INLINE registry::registry()
: formatter_(new pattern_formatter()) : formatter_(new pattern_formatter())
, level_(spdlog::level::info)
{ {
#ifndef SPDLOG_DISABLE_DEFAULT_LOGGER #ifndef SPDLOG_DISABLE_DEFAULT_LOGGER
...@@ -245,7 +244,7 @@ SPDLOG_INLINE void registry::throw_if_exists_(const std::string &logger_name) ...@@ -245,7 +244,7 @@ SPDLOG_INLINE void registry::throw_if_exists_(const std::string &logger_name)
{ {
if (loggers_.find(logger_name) != loggers_.end()) if (loggers_.find(logger_name) != loggers_.end())
{ {
throw spdlog_ex("logger with name '" + logger_name + "' already exists"); SPDLOG_THROW(spdlog_ex("logger with name '" + logger_name + "' already exists"));
} }
} }
......
...@@ -17,8 +17,8 @@ SPDLOG_INLINE thread_pool::thread_pool(size_t q_max_items, size_t threads_n, std ...@@ -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) if (threads_n == 0 || threads_n > 1000)
{ {
throw spdlog_ex("spdlog::thread_pool(): invalid threads_n param (valid " SPDLOG_THROW(spdlog_ex("spdlog::thread_pool(): invalid threads_n param (valid "
"range is 1-1000)"); "range is 1-1000)"));
} }
for (size_t i = 0; i < threads_n; i++) 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) ...@@ -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 // message all threads to terminate gracefully join them
SPDLOG_INLINE thread_pool::~thread_pool() SPDLOG_INLINE thread_pool::~thread_pool()
{ {
try SPDLOG_TRY
{ {
for (size_t i = 0; i < threads_.size(); i++) for (size_t i = 0; i < threads_.size(); i++)
{ {
...@@ -48,8 +48,7 @@ SPDLOG_INLINE thread_pool::~thread_pool() ...@@ -48,8 +48,7 @@ SPDLOG_INLINE thread_pool::~thread_pool()
t.join(); 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) void SPDLOG_INLINE thread_pool::post_log(async_logger_ptr &&worker_ptr, details::log_msg &msg, async_overflow_policy overflow_policy)
......
...@@ -31,20 +31,16 @@ enum class async_msg_type ...@@ -31,20 +31,16 @@ enum class async_msg_type
// Movable only. should never be copied // Movable only. should never be copied
struct async_msg struct async_msg
{ {
async_msg_type msg_type; async_msg_type msg_type {async_msg_type::log};
level::level_enum level; level::level_enum level {level::info};
log_clock::time_point time; log_clock::time_point time;
size_t thread_id; size_t thread_id {0};
fmt::basic_memory_buffer<char, 176> raw; fmt::basic_memory_buffer<char, 176> raw;
source_loc source; source_loc source;
async_logger_ptr worker_ptr; async_logger_ptr worker_ptr;
async_msg() async_msg() = default;
:msg_type(async_msg_type::log),
level(level::info),
thread_id(0)
{}
~async_msg() = default; ~async_msg() = default;
// should only be moved in or out of the queue.. // should only be moved in or out of the queue..
...@@ -52,17 +48,18 @@ struct async_msg ...@@ -52,17 +48,18 @@ struct async_msg
// support for vs2013 move // support for vs2013 move
#if defined(_MSC_VER) && _MSC_VER <= 1800 #if defined(_MSC_VER) && _MSC_VER <= 1800
async_msg(async_msg &&other) : msg_type(other.msg_type), async_msg(async_msg &&other)
level(other.level), : msg_type(other.msg_type)
time(other.time), , level(other.level)
thread_id(other.thread_id), , time(other.time)
raw(move(other.raw)), , thread_id(other.thread_id)
msg_id(other.msg_id), , raw(move(other.raw))
source(other.source), , msg_id(other.msg_id)
worker_ptr(std::move(other.worker_ptr)) , 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; msg_type = other.msg_type;
level = other.level; level = other.level;
......
...@@ -23,12 +23,11 @@ SPDLOG_INLINE logger::logger(const logger &other) ...@@ -23,12 +23,11 @@ SPDLOG_INLINE logger::logger(const logger &other)
, custom_err_handler_(other.custom_err_handler_) , custom_err_handler_(other.custom_err_handler_)
{} {}
SPDLOG_INLINE logger::logger(logger &&other) SPDLOG_NOEXCEPT SPDLOG_INLINE logger::logger(logger &&other) SPDLOG_NOEXCEPT : name_(std::move(other.name_)),
: name_(std::move(other.name_)) sinks_(std::move(other.sinks_)),
, sinks_(std::move(other.sinks_)) level_(other.level_.load(std::memory_order_relaxed)),
, level_(other.level_.load(std::memory_order_relaxed)) flush_level_(other.flush_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_))
, custom_err_handler_(std::move(other.custom_err_handler_))
{} {}
SPDLOG_INLINE logger &logger::operator=(logger other) SPDLOG_NOEXCEPT SPDLOG_INLINE logger &logger::operator=(logger other) SPDLOG_NOEXCEPT
...@@ -168,7 +167,7 @@ SPDLOG_INLINE void logger::sink_it_(details::log_msg &msg) ...@@ -168,7 +167,7 @@ SPDLOG_INLINE void logger::sink_it_(details::log_msg &msg)
{ {
if (sink->should_log(msg.level)) if (sink->should_log(msg.level))
{ {
try SPDLOG_TRY
{ {
sink->log(msg); sink->log(msg);
} }
...@@ -186,7 +185,7 @@ SPDLOG_INLINE void logger::flush_() ...@@ -186,7 +185,7 @@ SPDLOG_INLINE void logger::flush_()
{ {
for (auto &sink : sinks_) for (auto &sink : sinks_)
{ {
try SPDLOG_TRY
{ {
sink->flush(); sink->flush();
} }
......
...@@ -23,7 +23,7 @@ ...@@ -23,7 +23,7 @@
#endif #endif
#include <vector> #include <vector>
#ifndef SPDLOG_NO_EXCEPTIONS
#define SPDLOG_LOGGER_CATCH() \ #define SPDLOG_LOGGER_CATCH() \
catch (const std::exception &ex) \ catch (const std::exception &ex) \
{ \ { \
...@@ -33,6 +33,9 @@ ...@@ -33,6 +33,9 @@
{ \ { \
err_handler_("Unknown exception in logger"); \ err_handler_("Unknown exception in logger"); \
} }
#else
#define SPDLOG_LOGGER_CATCH()
#endif
namespace spdlog { namespace spdlog {
class logger class logger
...@@ -63,7 +66,7 @@ public: ...@@ -63,7 +66,7 @@ public:
virtual ~logger() = default; virtual ~logger() = default;
logger(const logger &other) ; logger(const logger &other);
logger(logger &&other) SPDLOG_NOEXCEPT; logger(logger &&other) SPDLOG_NOEXCEPT;
logger &operator=(logger other) SPDLOG_NOEXCEPT; logger &operator=(logger other) SPDLOG_NOEXCEPT;
...@@ -72,7 +75,7 @@ public: ...@@ -72,7 +75,7 @@ public:
template<typename... Args> template<typename... Args>
void force_log(source_loc loc, level::level_enum lvl, string_view_t fmt, const Args &... 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::memory_buffer buf;
fmt::format_to(buf, fmt, args...); fmt::format_to(buf, fmt, args...);
...@@ -156,14 +159,16 @@ public: ...@@ -156,14 +159,16 @@ public:
} }
// T cannot be statically converted to string_view or wstring_view // 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) void log(source_loc loc, level::level_enum lvl, const T &msg)
{ {
if (!should_log(lvl)) if (!should_log(lvl))
{ {
return; return;
} }
try SPDLOG_TRY
{ {
fmt::memory_buffer buf; fmt::memory_buffer buf;
fmt::format_to(buf, "{}", msg); fmt::format_to(buf, "{}", msg);
......
...@@ -64,7 +64,7 @@ protected: ...@@ -64,7 +64,7 @@ protected:
if (ret < 0) if (ret < 0)
{ {
throw spdlog_ex("__android_log_write() failed", ret); SPDLOG_THROW(spdlog_ex("__android_log_write() failed", ret));
} }
} }
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
#pragma once #pragma once
#include "spdlog/common.h"
#include "spdlog/details/file_helper.h" #include "spdlog/details/file_helper.h"
#include "spdlog/details/null_mutex.h" #include "spdlog/details/null_mutex.h"
#include "spdlog/fmt/fmt.h" #include "spdlog/fmt/fmt.h"
...@@ -10,6 +11,7 @@ ...@@ -10,6 +11,7 @@
#include "spdlog/details/os.h" #include "spdlog/details/os.h"
#include "spdlog/details/synchronous_factory.h" #include "spdlog/details/synchronous_factory.h"
#include <chrono> #include <chrono>
#include <cstdio> #include <cstdio>
#include <ctime> #include <ctime>
...@@ -52,7 +54,7 @@ public: ...@@ -52,7 +54,7 @@ public:
{ {
if (rotation_hour < 0 || rotation_hour > 23 || rotation_minute < 0 || rotation_minute > 59) if (rotation_hour < 0 || rotation_hour > 23 || rotation_minute < 0 || rotation_minute > 59)
{ {
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(); auto now = log_clock::now();
file_helper_.open(FileNameCalc::calc_filename(base_filename_, now_tm(now)), truncate_); file_helper_.open(FileNameCalc::calc_filename(base_filename_, now_tm(now)), truncate_);
......
...@@ -112,7 +112,8 @@ SPDLOG_INLINE void rotating_file_sink<Mutex>::rotate_() ...@@ -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! file_helper_.reopen(true); // truncate the log file anyway to prevent it to grow beyond its limit!
current_size_ = 0; current_size_ = 0;
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));
} }
} }
} }
......
...@@ -73,7 +73,8 @@ protected: ...@@ -73,7 +73,8 @@ protected:
bool enable_formatting_ = false; bool enable_formatting_ = false;
private: private:
std::array<int, 7> syslog_levels_; using levels_array = std::array<int, 7>;
levels_array syslog_levels_;
// must store the ident because the man says openlog might use the pointer as // must store the ident because the man says openlog might use the pointer as
// is and not a string copy // is and not a string copy
const std::string ident_; const std::string ident_;
...@@ -83,7 +84,7 @@ private: ...@@ -83,7 +84,7 @@ private:
// //
int syslog_prio_from_level(const details::log_msg &msg) const int syslog_prio_from_level(const details::log_msg &msg) const
{ {
return syslog_levels_.at(static_cast<int>(msg.level)); return syslog_levels_.at(static_cast<levels_array::size_type>(msg.level));
} }
}; };
......
...@@ -38,7 +38,8 @@ public: ...@@ -38,7 +38,8 @@ public:
systemd_sink &operator=(const systemd_sink &) = delete; systemd_sink &operator=(const systemd_sink &) = delete;
protected: protected:
std::array<int, 7> syslog_levels_; using levels_array = std::array<int, 7>;
levels_array syslog_levels_;
void sink_it_(const details::log_msg &msg) override void sink_it_(const details::log_msg &msg) override
{ {
...@@ -66,13 +67,13 @@ protected: ...@@ -66,13 +67,13 @@ protected:
if (err) if (err)
{ {
throw spdlog_ex("Failed writing to systemd", errno); SPDLOG_THROW(spdlog_ex("Failed writing to systemd", errno));
} }
} }
int syslog_level(level::level_enum l) int syslog_level(level::level_enum l)
{ {
return syslog_levels_.at(static_cast<int>(l)); return syslog_levels_.at(static_cast<levels_array::size_type>(l));
} }
void flush_() override {} void flush_() override {}
......
...@@ -153,7 +153,7 @@ void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::write_to_file_(const fmt::memory ...@@ -153,7 +153,7 @@ void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::write_to_file_(const fmt::memory
bool ok = ::WriteFile(out_handle_, formatted.data() + total_written, size - total_written, &bytes_written, nullptr) != 0; bool ok = ::WriteFile(out_handle_, formatted.data() + total_written, size - total_written, &bytes_written, nullptr) != 0;
if (!ok || bytes_written == 0) if (!ok || bytes_written == 0)
{ {
throw spdlog_ex("wincolor_sink: write_to_file_ failed. GetLastError(): " + std::to_string(::GetLastError())); SPDLOG_THROW(spdlog_ex("wincolor_sink: write_to_file_ failed. GetLastError(): " + std::to_string(::GetLastError())));
} }
total_written += bytes_written; total_written += bytes_written;
} while (total_written < size); } while (total_written < size);
......
...@@ -8,7 +8,6 @@ if(PkgConfig_FOUND) ...@@ -8,7 +8,6 @@ if(PkgConfig_FOUND)
endif() endif()
set(SPDLOG_UTESTS_SOURCES set(SPDLOG_UTESTS_SOURCES
test_errors.cpp
test_file_helper.cpp test_file_helper.cpp
test_file_logging.cpp test_file_logging.cpp
test_misc.cpp test_misc.cpp
...@@ -26,6 +25,10 @@ set(SPDLOG_UTESTS_SOURCES ...@@ -26,6 +25,10 @@ set(SPDLOG_UTESTS_SOURCES
test_stdout_api.cpp test_stdout_api.cpp
test_dup_filter.cpp) test_dup_filter.cpp)
if(NOT SPDLOG_NO_EXCEPTIONS)
list(APPEND SPDLOG_UTESTS_SOURCES test_errors.cpp)
endif()
if(systemd_FOUND) if(systemd_FOUND)
list(APPEND SPDLOG_UTESTS_SOURCES test_systemd.cpp) list(APPEND SPDLOG_UTESTS_SOURCES test_systemd.cpp)
endif() endif()
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
static const char *tested_logger_name = "null_logger"; static const char *tested_logger_name = "null_logger";
static const char *tested_logger_name2 = "null_logger2"; static const char *tested_logger_name2 = "null_logger2";
#ifndef SPDLOG_NO_EXCEPTIONS
TEST_CASE("register_drop", "[registry]") TEST_CASE("register_drop", "[registry]")
{ {
spdlog::drop_all(); spdlog::drop_all();
...@@ -21,6 +22,7 @@ TEST_CASE("explicit register", "[registry]") ...@@ -21,6 +22,7 @@ TEST_CASE("explicit register", "[registry]")
// Throw if registring existing name // Throw if registring existing name
REQUIRE_THROWS_AS(spdlog::create<spdlog::sinks::null_sink_mt>(tested_logger_name), spdlog::spdlog_ex); REQUIRE_THROWS_AS(spdlog::create<spdlog::sinks::null_sink_mt>(tested_logger_name), spdlog::spdlog_ex);
} }
#endif
TEST_CASE("apply_all", "[registry]") TEST_CASE("apply_all", "[registry]")
{ {
......
...@@ -1074,7 +1074,7 @@ ...@@ -1074,7 +1074,7 @@
}, },
"invocations": [ "invocations": [
{ {
"commandLine": "\"C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\VC\\Tools\\MSVC\\14.22.27905\\bin\\HostX64\\x64\\c1xx.dll\" -ACf{1F7B090C-16DB-4822-966A-A93D26ED4681} -ACpmspft140.dll -Alint -D_PREFAST_ -D_AST_FE_ -Analyze -zm0x00007FF619B86AB0 -il C:\\Users\\gmelm\\AppData\\Local\\Temp\\_CL_74d41b28ast -typedil -f E:\\devel\\spdlog\\src\\spdlog.cpp -Ze -D_MSC_EXTENSIONS -Zp16 -ZB64 -D_INTEGRAL_MAX_BITS=64 -pc \\:/ -D_MSC_VER=1922 -D_MSC_FULL_VER=192227905 -D_MSC_BUILD=0 -D_M_AMD64=100 -ZILP448 -D_M_X64=100 -D_WIN64 -D_WIN32 -I E:\\devel\\spdlog\\include -nologo -W 4 -WX -diagnostics:column -Ot -DCODE_ANALYSIS -DWIN32 -D_WINDOWS -DSPDLOG_COMPILED_LIB -DCMAKE_INTDIR=\"Debug\" -D_MBCS -EHs -D_CPPUNWIND -EHc -D__MSVC_RUNTIME_CHECKS -RTCs -RTCu -D_DEBUG -D_MT -D_DLL -GS -D_M_FP_PRECISE -Zc:wchar_t -Zc:forScope -GR -D_CPPRTTI -Fospdlog.dir\\Debug\\spdlog.obj -Fdspdlog.dir\\Debug\\spdlog.pdb -Gd -analyze:quiet -analyze:plugin C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\VC\\Tools\\MSVC\\14.22.27905\\bin\\HostX64\\x64\\EspXEngine.dll -errorreport:prompt -analyze:ruleset C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\Team Tools\\Static Analysis Tools\\Rule Sets\\NativeRecommendedRules.ruleset -analyze:projectdirectory E:\\devel\\spdlog\\win64-release -analyze:rulesetdirectory ;C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\Team Tools\\Static Analysis Tools\\\\Rule Sets -I C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\VC\\Tools\\MSVC\\14.22.27905\\include -I C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\VC\\Tools\\MSVC\\14.22.27905\\atlmfc\\include -I C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\VC\\Auxiliary\\VS\\include -I C:\\Program Files (x86)\\Windows Kits\\10\\Include\\10.0.17134.0\\ucrt -I C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\VC\\Auxiliary\\VS\\UnitTest\\include -I C:\\Program Files (x86)\\Windows Kits\\10\\Include\\10.0.17134.0\\um -I C:\\Program Files (x86)\\Windows Kits\\10\\Include\\10.0.17134.0\\shared -I C:\\Program Files (x86)\\Windows Kits\\10\\Include\\10.0.17134.0\\winrt -I C:\\Program Files (x86)\\Windows Kits\\10\\Include\\10.0.17134.0\\cppwinrt -I C:\\Program Files (x86)\\Windows Kits\\NETFXSDK\\4.7.2\\Include\\um" "commandLine": "\"C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\VC\\Tools\\MSVC\\14.22.27905\\bin\\HostX64\\x64\\c1xx.dll\" -ACf{1F7B090C-16DB-4822-966A-A93D26ED4681} -ACpmspft140.dll -Alint -D_PREFAST_ -D_AST_FE_ -Analyze -zm0x00007FF619B86AB0 -il C:\\Users\\gmelm\\AppData\\Local\\Temp\\_CL_9a7db7e3ast -typedil -f E:\\devel\\spdlog\\src\\spdlog.cpp -Ze -D_MSC_EXTENSIONS -Zp16 -ZB64 -D_INTEGRAL_MAX_BITS=64 -pc \\:/ -D_MSC_VER=1922 -D_MSC_FULL_VER=192227905 -D_MSC_BUILD=0 -D_M_AMD64=100 -ZILP448 -D_M_X64=100 -D_WIN64 -D_WIN32 -I E:\\devel\\spdlog\\include -nologo -W 4 -WX -diagnostics:column -Ot -DCODE_ANALYSIS -DWIN32 -D_WINDOWS -DSPDLOG_COMPILED_LIB -DCMAKE_INTDIR=\"Debug\" -D_MBCS -EHs -D_CPPUNWIND -EHc -D__MSVC_RUNTIME_CHECKS -RTCs -RTCu -D_DEBUG -D_MT -D_DLL -GS -D_M_FP_PRECISE -Zc:wchar_t -Zc:forScope -GR -D_CPPRTTI -Fospdlog.dir\\Debug\\spdlog.obj -Fdspdlog.dir\\Debug\\spdlog.pdb -Gd -analyze:quiet -analyze:plugin C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\VC\\Tools\\MSVC\\14.22.27905\\bin\\HostX64\\x64\\EspXEngine.dll -errorreport:prompt -analyze:ruleset C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\Team Tools\\Static Analysis Tools\\Rule Sets\\NativeRecommendedRules.ruleset -analyze:projectdirectory E:\\devel\\spdlog\\win64-release -analyze:rulesetdirectory ;C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\Team Tools\\Static Analysis Tools\\\\Rule Sets -I C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\VC\\Tools\\MSVC\\14.22.27905\\include -I C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\VC\\Tools\\MSVC\\14.22.27905\\atlmfc\\include -I C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\VC\\Auxiliary\\VS\\include -I C:\\Program Files (x86)\\Windows Kits\\10\\Include\\10.0.17134.0\\ucrt -I C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\VC\\Auxiliary\\VS\\UnitTest\\include -I C:\\Program Files (x86)\\Windows Kits\\10\\Include\\10.0.17134.0\\um -I C:\\Program Files (x86)\\Windows Kits\\10\\Include\\10.0.17134.0\\shared -I C:\\Program Files (x86)\\Windows Kits\\10\\Include\\10.0.17134.0\\winrt -I C:\\Program Files (x86)\\Windows Kits\\10\\Include\\10.0.17134.0\\cppwinrt -I C:\\Program Files (x86)\\Windows Kits\\NETFXSDK\\4.7.2\\Include\\um"
} }
], ],
"files": { "files": {
......
This diff is collapsed.
This diff is collapsed.
# Copyright(c) 2019 spdlog authors
# Distributed under the MIT License (http://opensource.org/licenses/MIT)
find_package(Threads REQUIRED)
set(SPDLOG_FMT_EXTERNAL OFF)
set(config_targets_file spdlogConfigTargets.cmake)
if(SPDLOG_FMT_EXTERNAL)
include(CMakeFindDependencyMacro)
find_dependency(fmt CONFIG)
endif()
include("${CMAKE_CURRENT_LIST_DIR}/${config_targets_file}")
# This is a basic version file for the Config-mode of find_package().
# It is used by write_basic_package_version_file() as input file for configure_file()
# to create a version-file which can be installed along a config.cmake file.
#
# The created file sets PACKAGE_VERSION_EXACT if the current version string and
# the requested version string are exactly the same and it sets
# PACKAGE_VERSION_COMPATIBLE if the current version is >= requested version,
# but only if the requested major version is the same as the current one.
# The variable CVF_VERSION must be set before calling configure_file().
set(PACKAGE_VERSION "1.4.0")
if(PACKAGE_VERSION VERSION_LESS PACKAGE_FIND_VERSION)
set(PACKAGE_VERSION_COMPATIBLE FALSE)
else()
if("1.4.0" MATCHES "^([0-9]+)\\.")
set(CVF_VERSION_MAJOR "${CMAKE_MATCH_1}")
else()
set(CVF_VERSION_MAJOR "1.4.0")
endif()
if(PACKAGE_FIND_VERSION_MAJOR STREQUAL CVF_VERSION_MAJOR)
set(PACKAGE_VERSION_COMPATIBLE TRUE)
else()
set(PACKAGE_VERSION_COMPATIBLE FALSE)
endif()
if(PACKAGE_FIND_VERSION STREQUAL PACKAGE_VERSION)
set(PACKAGE_VERSION_EXACT TRUE)
endif()
endif()
# if the installed project requested no architecture check, don't perform the check
if("FALSE")
return()
endif()
# if the installed or the using project don't have CMAKE_SIZEOF_VOID_P set, ignore it:
if("${CMAKE_SIZEOF_VOID_P}" STREQUAL "" OR "8" STREQUAL "")
return()
endif()
# check that the installed version has the same 32/64bit-ness as the one which is currently searching:
if(NOT CMAKE_SIZEOF_VOID_P STREQUAL "8")
math(EXPR installedBits "8 * 8")
set(PACKAGE_VERSION "${PACKAGE_VERSION} (${installedBits}bit)")
set(PACKAGE_VERSION_UNSUITABLE TRUE)
endif()
# CMake generated Testfile for
# Source directory: E:/devel/spdlog/tests
# Build directory: E:/devel/spdlog/win64-release/tests
#
# This file includes the relevant testing commands required for
# testing this directory and lists subdirectories to be tested as well.
if("${CTEST_CONFIGURATION_TYPE}" MATCHES "^([Dd][Ee][Bb][Uu][Gg])$")
add_test(spdlog-utests "E:/devel/spdlog/win64-release/tests/Debug/spdlog-utests.exe")
set_tests_properties(spdlog-utests PROPERTIES _BACKTRACE_TRIPLES "E:/devel/spdlog/tests/CMakeLists.txt;48;add_test;E:/devel/spdlog/tests/CMakeLists.txt;0;")
elseif("${CTEST_CONFIGURATION_TYPE}" MATCHES "^([Rr][Ee][Ll][Ee][Aa][Ss][Ee])$")
add_test(spdlog-utests "E:/devel/spdlog/win64-release/tests/Release/spdlog-utests.exe")
set_tests_properties(spdlog-utests PROPERTIES _BACKTRACE_TRIPLES "E:/devel/spdlog/tests/CMakeLists.txt;48;add_test;E:/devel/spdlog/tests/CMakeLists.txt;0;")
elseif("${CTEST_CONFIGURATION_TYPE}" MATCHES "^([Mm][Ii][Nn][Ss][Ii][Zz][Ee][Rr][Ee][Ll])$")
add_test(spdlog-utests "E:/devel/spdlog/win64-release/tests/MinSizeRel/spdlog-utests.exe")
set_tests_properties(spdlog-utests PROPERTIES _BACKTRACE_TRIPLES "E:/devel/spdlog/tests/CMakeLists.txt;48;add_test;E:/devel/spdlog/tests/CMakeLists.txt;0;")
elseif("${CTEST_CONFIGURATION_TYPE}" MATCHES "^([Rr][Ee][Ll][Ww][Ii][Tt][Hh][Dd][Ee][Bb][Ii][Nn][Ff][Oo])$")
add_test(spdlog-utests "E:/devel/spdlog/win64-release/tests/RelWithDebInfo/spdlog-utests.exe")
set_tests_properties(spdlog-utests PROPERTIES _BACKTRACE_TRIPLES "E:/devel/spdlog/tests/CMakeLists.txt;48;add_test;E:/devel/spdlog/tests/CMakeLists.txt;0;")
else()
add_test(spdlog-utests NOT_AVAILABLE)
endif()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment