mirror of
https://github.com/gabime/spdlog.git
synced 2025-09-29 01:29:35 +08:00
Compare commits
34 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
dea6bb1085 | ||
![]() |
b960d5a062 | ||
![]() |
c6d0487474 | ||
![]() |
7d22b689b6 | ||
![]() |
a51b485637 | ||
![]() |
3a13e76bb1 | ||
![]() |
139c0d135f | ||
![]() |
cee913d76e | ||
![]() |
7088644d3f | ||
![]() |
9ca4e3d8bc | ||
![]() |
f1a60eb6b1 | ||
![]() |
96961e14ad | ||
![]() |
c639483db0 | ||
![]() |
b3881b9c15 | ||
![]() |
032035e72f | ||
![]() |
66b5772b61 | ||
![]() |
faea32c9f9 | ||
![]() |
d78ddcee24 | ||
![]() |
7bc598c9b5 | ||
![]() |
aa9012fa07 | ||
![]() |
3fbac8e2b7 | ||
![]() |
d033ccc0c7 | ||
![]() |
1fcc896486 | ||
![]() |
4640c5cb50 | ||
![]() |
4131347079 | ||
![]() |
98b9cd38ec | ||
![]() |
877a618400 | ||
![]() |
eb2118c181 | ||
![]() |
5c1951acec | ||
![]() |
67a6eaf23e | ||
![]() |
f4db1c510c | ||
![]() |
2aa25109ef | ||
![]() |
bd3f8a3b92 | ||
![]() |
d6b700eaf2 |
6
INSTALL
6
INSTALL
@@ -2,9 +2,9 @@ spdlog is header only library.
|
||||
Just copy the files to your build tree and use a C++11 compiler
|
||||
|
||||
Tested on:
|
||||
gcc 4.8.1 and above
|
||||
clang 3.5
|
||||
Visual Studio 2013
|
||||
gcc 4.8.1 or above
|
||||
clang 3.5 or above
|
||||
Visual Studio 2013 or above
|
||||
|
||||
gcc 4.8 flags: --std==c++11 -pthread -O3 -flto -Wl,--no-as-needed
|
||||
gcc 4.9 flags: --std=c++11 -pthread -O3 -flto
|
||||
|
@@ -20,7 +20,7 @@ Very fast, header only, C++ logging library. [
|
||||
* Mac OSX (clang 3.5+)
|
||||
* Android
|
||||
|
@@ -143,11 +143,6 @@ enum class pattern_time_type
|
||||
//
|
||||
// Log exception
|
||||
//
|
||||
namespace details {
|
||||
namespace os {
|
||||
std::string errno_str(int err_num);
|
||||
}
|
||||
} // namespace details
|
||||
class spdlog_ex : public std::exception
|
||||
{
|
||||
public:
|
||||
@@ -158,7 +153,9 @@ public:
|
||||
|
||||
spdlog_ex(const std::string &msg, int last_errno)
|
||||
{
|
||||
_msg = msg + ": " + details::os::errno_str(last_errno);
|
||||
fmt::MemoryWriter writer;
|
||||
fmt::format_system_error(writer, last_errno, msg);
|
||||
_msg = writer.str();
|
||||
}
|
||||
|
||||
const char *what() const SPDLOG_NOEXCEPT override
|
||||
@@ -179,4 +176,13 @@ using filename_t = std::wstring;
|
||||
using filename_t = std::string;
|
||||
#endif
|
||||
|
||||
#define SPDLOG_CATCH_AND_HANDLE \
|
||||
catch (const std::exception &ex) \
|
||||
{ \
|
||||
_err_handler(ex.what()); \
|
||||
} \
|
||||
catch (...) \
|
||||
{ \
|
||||
_err_handler("Unknown exeption in logger"); \
|
||||
}
|
||||
} // namespace spdlog
|
||||
|
@@ -18,22 +18,26 @@
|
||||
// Create a file logger which creates new files with a specified time step and fixed file size:
|
||||
//
|
||||
// std::shared_ptr<logger> step_logger_mt(const std::string &logger_name, const filename_t &filename, unsigned seconds = 60, const
|
||||
// filename_t &tmp_ext = ".tmp", unsigned max_file_size = std::numeric_limits<unsigned>::max()); std::shared_ptr<logger>
|
||||
// step_logger_st(const std::string &logger_name, const filename_t &filename, unsigned seconds = 60, const filename_t &tmp_ext = ".tmp",
|
||||
// unsigned max_file_size = std::numeric_limits<unsigned>::max());
|
||||
// filename_t &tmp_ext = ".tmp", unsigned max_file_size = std::numeric_limits<unsigned>::max(), bool delete_empty_files = true, const
|
||||
// filename_t &file_header = ""); std::shared_ptr<logger> step_logger_st(const std::string &logger_name, const filename_t &filename,
|
||||
// unsigned seconds = 60, const filename_t &tmp_ext = ".tmp", unsigned max_file_size = std::numeric_limits<unsigned>::max());
|
||||
|
||||
// Example for spdlog_impl.h
|
||||
// Create a file logger that creates new files with a specified increment
|
||||
// inline std::shared_ptr<spdlog::logger> spdlog::step_logger_mt(
|
||||
// const std::string &logger_name, const filename_t &filename_fmt, unsigned seconds, const filename_t &tmp_ext, unsigned max_file_size)
|
||||
// const std::string &logger_name, const filename_t &filename_fmt, unsigned seconds, const filename_t &tmp_ext, unsigned max_file_size,
|
||||
// bool delete_empty_files, const filename_t &file_header)
|
||||
// {
|
||||
// return create<spdlog::sinks::step_file_sink_mt>(logger_name, filename_fmt, seconds, tmp_ext, max_file_size);
|
||||
// return create<spdlog::sinks::step_file_sink_mt>(logger_name, filename_fmt, seconds, tmp_ext, max_file_size, delete_empty_files,
|
||||
// file_header);
|
||||
// }
|
||||
|
||||
// inline std::shared_ptr<spdlog::logger> spdlog::step_logger_st(
|
||||
// const std::string &logger_name, const filename_t &filename_fmt, unsigned seconds, const filename_t &tmp_ext, unsigned max_file_size)
|
||||
// const std::string &logger_name, const filename_t &filename_fmt, unsigned seconds, const filename_t &tmp_ext, unsigned max_file_size,
|
||||
// bool delete_empty_files, const filename_t &file_header)
|
||||
// {
|
||||
// return create<spdlog::sinks::step_file_sink_st>(logger_name, filename_fmt, seconds, tmp_ext, max_file_size);
|
||||
// return create<spdlog::sinks::step_file_sink_st>(logger_name, filename_fmt, seconds, tmp_ext, max_file_size, delete_empty_files,
|
||||
// file_header);
|
||||
// }
|
||||
|
||||
namespace spdlog {
|
||||
@@ -64,17 +68,27 @@ template<class Mutex, class FileNameCalc = default_step_file_name_calculator>
|
||||
class step_file_sink SPDLOG_FINAL : public base_sink<Mutex>
|
||||
{
|
||||
public:
|
||||
step_file_sink(filename_t base_filename, unsigned step_seconds, filename_t tmp_ext, unsigned max_size)
|
||||
step_file_sink(filename_t base_filename, unsigned step_seconds, filename_t tmp_ext, unsigned max_size, bool delete_empty_files,
|
||||
filename_t file_header)
|
||||
: _base_filename(std::move(base_filename))
|
||||
, _tmp_ext(std::move(tmp_ext))
|
||||
, _step_seconds(step_seconds)
|
||||
, _max_size(max_size)
|
||||
, _delete_empty_files(delete_empty_files)
|
||||
{
|
||||
if (step_seconds == 0)
|
||||
{
|
||||
throw spdlog_ex("step_file_sink: Invalid time step in ctor");
|
||||
}
|
||||
if (max_size == 0)
|
||||
|
||||
if (!file_header.empty())
|
||||
{
|
||||
pattern_formatter formatter_for_file_header("%v");
|
||||
_file_header.raw << file_header;
|
||||
formatter_for_file_header.format(_file_header);
|
||||
}
|
||||
|
||||
if (max_size <= _file_header.formatted.size())
|
||||
{
|
||||
throw spdlog_ex("step_file_sink: Invalid max log size in ctor");
|
||||
}
|
||||
@@ -89,6 +103,13 @@ public:
|
||||
|
||||
_file_helper.open(_current_filename);
|
||||
_current_size = _file_helper.size(); // expensive. called only once
|
||||
|
||||
if (!_current_size)
|
||||
{
|
||||
_current_size += _file_header.formatted.size();
|
||||
if (_current_size)
|
||||
_file_helper.write(_file_header);
|
||||
}
|
||||
}
|
||||
|
||||
~step_file_sink()
|
||||
@@ -105,16 +126,34 @@ public:
|
||||
protected:
|
||||
void _sink_it(const details::log_msg &msg) override
|
||||
{
|
||||
_current_size += msg.formatted.size();
|
||||
if (std::chrono::system_clock::now() >= _tp || _current_size > _max_size)
|
||||
auto msg_size = msg.formatted.size();
|
||||
|
||||
if (std::chrono::system_clock::now() >= _tp || _current_size + msg_size > _max_size)
|
||||
{
|
||||
filename_t new_filename;
|
||||
std::tie(new_filename, std::ignore) = FileNameCalc::calc_filename(_base_filename, _tmp_ext);
|
||||
|
||||
bool change_occured = !details::file_helper::file_exists(new_filename);
|
||||
if (change_occured)
|
||||
{
|
||||
close_current_file();
|
||||
|
||||
std::tie(_current_filename, std::ignore) = FileNameCalc::calc_filename(_base_filename, _tmp_ext);
|
||||
_current_filename = std::move(new_filename);
|
||||
|
||||
_file_helper.open(_current_filename);
|
||||
_tp = _next_tp();
|
||||
_current_size = msg.formatted.size();
|
||||
}
|
||||
|
||||
_tp = _next_tp();
|
||||
|
||||
if (change_occured)
|
||||
{
|
||||
_current_size = _file_header.formatted.size();
|
||||
if (_current_size)
|
||||
_file_helper.write(_file_header);
|
||||
}
|
||||
}
|
||||
|
||||
_current_size += msg_size;
|
||||
_file_helper.write(msg);
|
||||
}
|
||||
|
||||
@@ -133,13 +172,25 @@ private:
|
||||
{
|
||||
using details::os::filename_to_str;
|
||||
|
||||
filename_t src = _current_filename, target;
|
||||
std::tie(target, std::ignore) = details::file_helper::split_by_extenstion(src);
|
||||
// Delete empty files, if required
|
||||
if (_delete_empty_files && _current_size <= _file_header.formatted.size())
|
||||
{
|
||||
if (details::os::remove(_current_filename) != 0)
|
||||
{
|
||||
throw spdlog_ex("step_file_sink: not remove " + filename_to_str(_current_filename), errno);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
filename_t target;
|
||||
std::tie(target, std::ignore) = details::file_helper::split_by_extenstion(_current_filename);
|
||||
target += _ext;
|
||||
|
||||
if (details::file_helper::file_exists(src) && details::os::rename(src, target) != 0)
|
||||
if (details::file_helper::file_exists(_current_filename) && details::os::rename(_current_filename, target) != 0)
|
||||
{
|
||||
throw spdlog_ex("step_file_sink: failed renaming " + filename_to_str(src) + " to " + filename_to_str(target), errno);
|
||||
throw spdlog_ex(
|
||||
"step_file_sink: failed renaming " + filename_to_str(_current_filename) + " to " + filename_to_str(target), errno);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -147,6 +198,7 @@ private:
|
||||
const filename_t _tmp_ext;
|
||||
const std::chrono::seconds _step_seconds;
|
||||
const unsigned _max_size;
|
||||
const bool _delete_empty_files;
|
||||
|
||||
std::chrono::system_clock::time_point _tp;
|
||||
filename_t _current_filename;
|
||||
@@ -154,6 +206,7 @@ private:
|
||||
unsigned _current_size;
|
||||
|
||||
details::file_helper _file_helper;
|
||||
details::log_msg _file_header;
|
||||
};
|
||||
|
||||
using step_file_sink_mt = step_file_sink<std::mutex>;
|
||||
|
@@ -23,7 +23,6 @@
|
||||
#include <condition_variable>
|
||||
#include <exception>
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
@@ -83,7 +82,7 @@ class async_log_helper
|
||||
}
|
||||
|
||||
// copy into log_msg
|
||||
void fill_log_msg(log_msg &msg, std::string* logger_name)
|
||||
void fill_log_msg(log_msg &msg, std::string *logger_name)
|
||||
{
|
||||
msg.logger_name = logger_name;
|
||||
msg.level = level;
|
||||
@@ -101,12 +100,8 @@ public:
|
||||
|
||||
using clock = std::chrono::steady_clock;
|
||||
|
||||
async_log_helper(std::string logger_name,
|
||||
formatter_ptr formatter,
|
||||
std::vector<sink_ptr> sinks,
|
||||
size_t queue_size,
|
||||
const log_err_handler err_handler,
|
||||
const async_overflow_policy overflow_policy = async_overflow_policy::block_retry,
|
||||
async_log_helper(std::string logger_name, formatter_ptr formatter, std::vector<sink_ptr> sinks, size_t queue_size,
|
||||
const log_err_handler err_handler, const async_overflow_policy overflow_policy = async_overflow_policy::block_retry,
|
||||
std::function<void()> worker_warmup_cb = nullptr,
|
||||
const std::chrono::milliseconds &flush_interval_ms = std::chrono::milliseconds::zero(),
|
||||
std::function<void()> worker_teardown_cb = nullptr);
|
||||
@@ -176,15 +171,9 @@ private:
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// async_sink class implementation
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
inline spdlog::details::async_log_helper::async_log_helper(std::string logger_name,
|
||||
formatter_ptr formatter,
|
||||
std::vector<sink_ptr> sinks,
|
||||
size_t queue_size,
|
||||
log_err_handler err_handler,
|
||||
const async_overflow_policy overflow_policy,
|
||||
std::function<void()> worker_warmup_cb,
|
||||
const std::chrono::milliseconds &flush_interval_ms,
|
||||
std::function<void()> worker_teardown_cb)
|
||||
inline spdlog::details::async_log_helper::async_log_helper(std::string logger_name, formatter_ptr formatter, std::vector<sink_ptr> sinks,
|
||||
size_t queue_size, log_err_handler err_handler, const async_overflow_policy overflow_policy, std::function<void()> worker_warmup_cb,
|
||||
const std::chrono::milliseconds &flush_interval_ms, std::function<void()> worker_teardown_cb)
|
||||
: _logger_name(std::move(logger_name))
|
||||
, _formatter(std::move(formatter))
|
||||
, _sinks(std::move(sinks))
|
||||
@@ -251,14 +240,7 @@ inline void spdlog::details::async_log_helper::worker_loop()
|
||||
{
|
||||
active = process_next_msg();
|
||||
}
|
||||
catch (const std::exception &ex)
|
||||
{
|
||||
_err_handler(ex.what());
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
_err_handler("Unknown exeption in async logger worker loop.");
|
||||
}
|
||||
SPDLOG_CATCH_AND_HANDLE
|
||||
}
|
||||
if (_worker_teardown_cb)
|
||||
{
|
||||
@@ -295,9 +277,13 @@ inline bool spdlog::details::async_log_helper::process_next_msg()
|
||||
for (auto &s : _sinks)
|
||||
{
|
||||
if (s->should_log(incoming_log_msg.level))
|
||||
{
|
||||
try
|
||||
{
|
||||
s->log(incoming_log_msg);
|
||||
}
|
||||
SPDLOG_CATCH_AND_HANDLE
|
||||
}
|
||||
}
|
||||
handle_flush_interval();
|
||||
return true;
|
||||
@@ -334,9 +320,14 @@ inline void spdlog::details::async_log_helper::handle_flush_interval()
|
||||
// flush all sinks if _flush_interval_ms has expired. only called if queue is empty
|
||||
inline void spdlog::details::async_log_helper::flush_sinks()
|
||||
{
|
||||
|
||||
for (auto &s : _sinks)
|
||||
{
|
||||
try
|
||||
{
|
||||
s->flush();
|
||||
}
|
||||
SPDLOG_CATCH_AND_HANDLE
|
||||
}
|
||||
_last_flush = os::now();
|
||||
}
|
||||
|
@@ -21,8 +21,8 @@ inline spdlog::async_logger::async_logger(const std::string &logger_name, const
|
||||
const async_overflow_policy overflow_policy, const std::function<void()> &worker_warmup_cb,
|
||||
const std::chrono::milliseconds &flush_interval_ms, const std::function<void()> &worker_teardown_cb)
|
||||
: logger(logger_name, begin, end)
|
||||
, _async_log_helper(new details::async_log_helper(
|
||||
logger_name, _formatter, _sinks, queue_size, _err_handler, overflow_policy, worker_warmup_cb, flush_interval_ms, worker_teardown_cb))
|
||||
, _async_log_helper(new details::async_log_helper(logger_name, _formatter, _sinks, queue_size, _err_handler, overflow_policy,
|
||||
worker_warmup_cb, flush_interval_ms, worker_teardown_cb))
|
||||
{
|
||||
}
|
||||
|
||||
|
@@ -68,15 +68,7 @@ inline void spdlog::logger::log(level::level_enum lvl, const char *fmt, const Ar
|
||||
#endif
|
||||
_sink_it(log_msg);
|
||||
}
|
||||
catch (const std::exception &ex)
|
||||
{
|
||||
_err_handler(ex.what());
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
_err_handler("Unknown exception in logger " + _name);
|
||||
throw;
|
||||
}
|
||||
SPDLOG_CATCH_AND_HANDLE
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
@@ -92,15 +84,7 @@ inline void spdlog::logger::log(level::level_enum lvl, const char *msg)
|
||||
log_msg.raw << msg;
|
||||
_sink_it(log_msg);
|
||||
}
|
||||
catch (const std::exception &ex)
|
||||
{
|
||||
_err_handler(ex.what());
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
_err_handler("Unknown exception in logger " + _name);
|
||||
throw;
|
||||
}
|
||||
SPDLOG_CATCH_AND_HANDLE
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
@@ -116,15 +100,7 @@ inline void spdlog::logger::log(level::level_enum lvl, const T &msg)
|
||||
log_msg.raw << msg;
|
||||
_sink_it(log_msg);
|
||||
}
|
||||
catch (const std::exception &ex)
|
||||
{
|
||||
_err_handler(ex.what());
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
_err_handler("Unknown exception in logger " + _name);
|
||||
throw;
|
||||
}
|
||||
SPDLOG_CATCH_AND_HANDLE
|
||||
}
|
||||
|
||||
template<typename Arg1, typename... Args>
|
||||
@@ -331,10 +307,14 @@ inline void spdlog::logger::_set_formatter(formatter_ptr msg_formatter)
|
||||
|
||||
inline void spdlog::logger::flush()
|
||||
{
|
||||
try
|
||||
{
|
||||
for (auto &sink : _sinks)
|
||||
{
|
||||
sink->flush();
|
||||
}
|
||||
}
|
||||
SPDLOG_CATCH_AND_HANDLE
|
||||
}
|
||||
|
||||
inline void spdlog::logger::_default_err_handler(const std::string &msg)
|
||||
|
@@ -237,7 +237,7 @@ inline size_t filesize(FILE *f)
|
||||
#else // unix
|
||||
int fd = fileno(f);
|
||||
// 64 bits(but not in osx or cygwin, where fstat64 is deprecated)
|
||||
#if !defined(__FreeBSD__) && !defined(__APPLE__) && (defined(__x86_64__) || defined(__ppc64__)) && !defined(__CYGWIN__)
|
||||
#if !defined(__OpenBSD__) && !defined(__FreeBSD__) && !defined(__APPLE__) && !defined(__HAIKU__) && (defined(__x86_64__) || defined(__ppc64__)) && !defined(__CYGWIN__)
|
||||
struct stat64 st;
|
||||
if (fstat64(fd, &st) == 0)
|
||||
{
|
||||
@@ -282,7 +282,7 @@ inline int utc_minutes_offset(const std::tm &tm = details::os::localtime())
|
||||
return offset;
|
||||
#else
|
||||
|
||||
#if defined(sun) || defined(__sun)
|
||||
#if defined(sun) || defined(__sun) || defined(_AIX)
|
||||
// 'tm_gmtoff' field is BSD extension and it's missing on SunOS/Solaris
|
||||
struct helper
|
||||
{
|
||||
@@ -383,54 +383,6 @@ inline std::string filename_to_str(const filename_t &filename)
|
||||
}
|
||||
#endif
|
||||
|
||||
inline std::string errno_to_string(char[256], char *res)
|
||||
{
|
||||
return std::string(res);
|
||||
}
|
||||
|
||||
inline std::string errno_to_string(char buf[256], int res)
|
||||
{
|
||||
if (res == 0)
|
||||
{
|
||||
return std::string(buf);
|
||||
}
|
||||
return "Unknown error";
|
||||
}
|
||||
|
||||
// Return errno string (thread safe)
|
||||
inline std::string errno_str(int err_num)
|
||||
{
|
||||
char buf[256];
|
||||
SPDLOG_CONSTEXPR auto buf_size = sizeof(buf);
|
||||
|
||||
#ifdef _WIN32
|
||||
if (strerror_s(buf, buf_size, err_num) == 0)
|
||||
{
|
||||
return std::string(buf);
|
||||
}
|
||||
else
|
||||
{
|
||||
return "Unknown error";
|
||||
}
|
||||
|
||||
#elif defined(__FreeBSD__) || defined(__APPLE__) || defined(ANDROID) || defined(__SUNPRO_CC) || \
|
||||
((_POSIX_C_SOURCE >= 200112L) && !defined(_GNU_SOURCE)) // posix version
|
||||
|
||||
if (strerror_r(err_num, buf, buf_size) == 0)
|
||||
{
|
||||
return std::string(buf);
|
||||
}
|
||||
else
|
||||
{
|
||||
return "Unknown error";
|
||||
}
|
||||
|
||||
#else // gnu version (might not use the given buf, so its retval pointer must be used)
|
||||
auto err = strerror_r(err_num, buf, buf_size); // let compiler choose type
|
||||
return errno_to_string(buf, err); // use overloading to select correct stringify function
|
||||
#endif
|
||||
}
|
||||
|
||||
inline int pid()
|
||||
{
|
||||
|
||||
|
@@ -62,6 +62,12 @@ public:
|
||||
std::lock_guard<Mutex> lock(base_sink<Mutex>::_mutex);
|
||||
_sinks.erase(std::remove(_sinks.begin(), _sinks.end(), sink), _sinks.end());
|
||||
}
|
||||
|
||||
void remove_all_sinks()
|
||||
{
|
||||
std::lock_guard<Mutex> lock(base_sink<Mutex>::_mutex);
|
||||
_sinks.clear();
|
||||
}
|
||||
};
|
||||
|
||||
using dist_sink_mt = dist_sink<std::mutex>;
|
||||
|
@@ -104,7 +104,7 @@ private:
|
||||
void _print_range(const details::log_msg &msg, size_t start, size_t end)
|
||||
{
|
||||
DWORD size = static_cast<DWORD>(end - start);
|
||||
WriteConsoleA(out_handle_, msg.formatted.data() + start, size, nullptr, nullptr);
|
||||
WriteFile(out_handle_, msg.formatted.data() + start, size, nullptr, nullptr);
|
||||
}
|
||||
};
|
||||
|
||||
|
@@ -7,14 +7,17 @@
|
||||
|
||||
class failing_sink : public spdlog::sinks::sink
|
||||
{
|
||||
void log(const spdlog::details::log_msg &msg) override
|
||||
void log(const spdlog::details::log_msg &) override
|
||||
{
|
||||
throw std::runtime_error("some error happened during log");
|
||||
}
|
||||
|
||||
void flush() override {}
|
||||
void flush() override
|
||||
{
|
||||
throw std::runtime_error("some error happened during flush");
|
||||
}
|
||||
};
|
||||
|
||||
using namespace std;
|
||||
TEST_CASE("default_error_handler", "[errors]]")
|
||||
{
|
||||
prepare_logdir();
|
||||
@@ -44,7 +47,7 @@ TEST_CASE("custom_error_handler", "[errors]]")
|
||||
std::string filename = "logs/simple_log.txt";
|
||||
auto logger = spdlog::create<spdlog::sinks::simple_file_sink_mt>("logger", filename, true);
|
||||
logger->flush_on(spdlog::level::info);
|
||||
logger->set_error_handler([=](const std::string &msg) { throw custom_ex(); });
|
||||
logger->set_error_handler([=](const std::string &) { throw custom_ex(); });
|
||||
logger->info("Good message #1");
|
||||
#if !defined(SPDLOG_FMT_PRINTF)
|
||||
REQUIRE_THROWS_AS(logger->info("Bad format msg {} {}", "xxx"), custom_ex);
|
||||
@@ -57,12 +60,19 @@ TEST_CASE("custom_error_handler", "[errors]]")
|
||||
|
||||
TEST_CASE("default_error_handler2", "[errors]]")
|
||||
{
|
||||
|
||||
auto logger = spdlog::create<failing_sink>("failed_logger");
|
||||
logger->set_error_handler([=](const std::string &msg) { throw custom_ex(); });
|
||||
logger->set_error_handler([=](const std::string &) { throw custom_ex(); });
|
||||
REQUIRE_THROWS_AS(logger->info("Some message"), custom_ex);
|
||||
}
|
||||
|
||||
TEST_CASE("flush_error_handler", "[errors]]")
|
||||
{
|
||||
spdlog::drop_all();
|
||||
auto logger = spdlog::create<failing_sink>("failed_logger");
|
||||
logger->set_error_handler([=](const std::string &) { throw custom_ex(); });
|
||||
REQUIRE_THROWS_AS(logger->flush(), custom_ex);
|
||||
}
|
||||
|
||||
TEST_CASE("async_error_handler", "[errors]]")
|
||||
{
|
||||
prepare_logdir();
|
||||
@@ -71,7 +81,7 @@ TEST_CASE("async_error_handler", "[errors]]")
|
||||
std::string filename = "logs/simple_async_log.txt";
|
||||
{
|
||||
auto logger = spdlog::create<spdlog::sinks::simple_file_sink_mt>("logger", filename, true);
|
||||
logger->set_error_handler([=](const std::string &msg) {
|
||||
logger->set_error_handler([=](const std::string &) {
|
||||
std::ofstream ofs("logs/custom_err.txt");
|
||||
if (!ofs)
|
||||
throw std::runtime_error("Failed open logs/custom_err.txt");
|
||||
@@ -99,7 +109,7 @@ TEST_CASE("async_error_handler2", "[errors]]")
|
||||
spdlog::set_async_mode(128);
|
||||
{
|
||||
auto logger = spdlog::create<failing_sink>("failed_logger");
|
||||
logger->set_error_handler([=](const std::string &msg) {
|
||||
logger->set_error_handler([=](const std::string &) {
|
||||
std::ofstream ofs("logs/custom_err2.txt");
|
||||
if (!ofs)
|
||||
throw std::runtime_error("Failed open logs/custom_err2.txt");
|
||||
@@ -108,6 +118,7 @@ TEST_CASE("async_error_handler2", "[errors]]")
|
||||
logger->info("Hello failure");
|
||||
spdlog::drop("failed_logger"); // force logger to drain the queue and shutdown
|
||||
spdlog::set_sync_mode();
|
||||
logger.reset();
|
||||
}
|
||||
|
||||
REQUIRE(file_contents("logs/custom_err2.txt") == err_msg);
|
||||
|
@@ -50,7 +50,7 @@ TEST_CASE("flush_on", "[flush_on]]")
|
||||
TEST_CASE("rotating_file_logger1", "[rotating_logger]]")
|
||||
{
|
||||
prepare_logdir();
|
||||
std::string basename = "logs/rotating_log";
|
||||
std::string basename = "logs/rotating_log_A";
|
||||
auto logger = spdlog::rotating_logger_mt("logger", basename, 1024, 0);
|
||||
|
||||
for (int i = 0; i < 10; ++i)
|
||||
@@ -70,8 +70,9 @@ TEST_CASE("rotating_file_logger1", "[rotating_logger]]")
|
||||
TEST_CASE("rotating_file_logger2", "[rotating_logger]]")
|
||||
{
|
||||
prepare_logdir();
|
||||
std::string basename = "logs/rotating_log";
|
||||
auto logger = spdlog::rotating_logger_mt("logger", basename, 1024, 1);
|
||||
size_t max_size = 10 * 1024;
|
||||
std::string basename = "logs/rotating_log.txt";
|
||||
auto logger = spdlog::rotating_logger_mt("logger", basename, max_size, 1);
|
||||
for (int i = 0; i < 10; ++i)
|
||||
logger->info("Test message {}", i);
|
||||
|
||||
@@ -88,9 +89,9 @@ TEST_CASE("rotating_file_logger2", "[rotating_logger]]")
|
||||
}
|
||||
|
||||
logger->flush();
|
||||
REQUIRE(get_filesize(filename) <= 1024);
|
||||
auto filename1 = basename + ".1";
|
||||
REQUIRE(get_filesize(filename1) <= 1024);
|
||||
REQUIRE(get_filesize(filename) <= max_size);
|
||||
auto filename1 = "logs/rotating_log.1.txt";
|
||||
REQUIRE(get_filesize(filename1) <= max_size);
|
||||
}
|
||||
|
||||
TEST_CASE("daily_logger", "[daily_logger]]")
|
||||
|
@@ -21,7 +21,7 @@ TEST_CASE("basic async test ", "[async]")
|
||||
auto test_sink = std::make_shared<spdlog::sinks::test_sink_mt>();
|
||||
size_t queue_size = 128;
|
||||
size_t messages = 256;
|
||||
auto logger = spdlog::create_async("as", test_sink, 128, spdlog::async_overflow_policy::block_retry);
|
||||
auto logger = spdlog::create_async("as", test_sink, queue_size, spdlog::async_overflow_policy::block_retry);
|
||||
for (size_t i = 0; i < messages; i++)
|
||||
{
|
||||
logger->info("Hello message #{}", i);
|
||||
|
@@ -1,10 +1,83 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio 2015
|
||||
VisualStudioVersion = 14.0
|
||||
# Visual Studio 15
|
||||
VisualStudioVersion = 15.0.27428.2037
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "tests", "tests.vcxproj", "{59A07559-5F38-4DD6-A7FA-DB4153690B42}"
|
||||
EndProject
|
||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "spdlog", "spdlog", "{7EFD7EC9-512F-4B35-ADFE-49863B2C04A5}"
|
||||
ProjectSection(SolutionItems) = preProject
|
||||
..\include\spdlog\async_logger.h = ..\include\spdlog\async_logger.h
|
||||
..\include\spdlog\common.h = ..\include\spdlog\common.h
|
||||
..\include\spdlog\formatter.h = ..\include\spdlog\formatter.h
|
||||
..\include\spdlog\logger.h = ..\include\spdlog\logger.h
|
||||
..\include\spdlog\spdlog.h = ..\include\spdlog\spdlog.h
|
||||
..\include\spdlog\tweakme.h = ..\include\spdlog\tweakme.h
|
||||
EndProjectSection
|
||||
EndProject
|
||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "contrib", "contrib", "{AAD3C108-4E24-4AA8-BA09-4C9A75A881B3}"
|
||||
ProjectSection(SolutionItems) = preProject
|
||||
..\include\spdlog\contrib\README.md = ..\include\spdlog\contrib\README.md
|
||||
EndProjectSection
|
||||
EndProject
|
||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "sinks", "sinks", "{3FB7E4DF-2397-463E-BDAD-32854AB66DFD}"
|
||||
ProjectSection(SolutionItems) = preProject
|
||||
..\include\spdlog\contrib\sinks\.gitignore = ..\include\spdlog\contrib\sinks\.gitignore
|
||||
..\include\spdlog\contrib\sinks\step_file_sink.h = ..\include\spdlog\contrib\sinks\step_file_sink.h
|
||||
EndProjectSection
|
||||
EndProject
|
||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "details", "details", "{97DED9BF-821E-4A7A-8D13-ED9A739E1F55}"
|
||||
ProjectSection(SolutionItems) = preProject
|
||||
..\include\spdlog\details\async_log_helper.h = ..\include\spdlog\details\async_log_helper.h
|
||||
..\include\spdlog\details\async_logger_impl.h = ..\include\spdlog\details\async_logger_impl.h
|
||||
..\include\spdlog\details\file_helper.h = ..\include\spdlog\details\file_helper.h
|
||||
..\include\spdlog\details\log_msg.h = ..\include\spdlog\details\log_msg.h
|
||||
..\include\spdlog\details\logger_impl.h = ..\include\spdlog\details\logger_impl.h
|
||||
..\include\spdlog\details\mpmc_blocking_q.h = ..\include\spdlog\details\mpmc_blocking_q.h
|
||||
..\include\spdlog\details\null_mutex.h = ..\include\spdlog\details\null_mutex.h
|
||||
..\include\spdlog\details\os.h = ..\include\spdlog\details\os.h
|
||||
..\include\spdlog\details\pattern_formatter_impl.h = ..\include\spdlog\details\pattern_formatter_impl.h
|
||||
..\include\spdlog\details\registry.h = ..\include\spdlog\details\registry.h
|
||||
..\include\spdlog\details\spdlog_impl.h = ..\include\spdlog\details\spdlog_impl.h
|
||||
EndProjectSection
|
||||
EndProject
|
||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "fmt", "fmt", "{0B649723-CF78-47C0-B1CA-1F173DDBFED4}"
|
||||
ProjectSection(SolutionItems) = preProject
|
||||
..\include\spdlog\fmt\fmt.h = ..\include\spdlog\fmt\fmt.h
|
||||
..\include\spdlog\fmt\ostr.h = ..\include\spdlog\fmt\ostr.h
|
||||
EndProjectSection
|
||||
EndProject
|
||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "bundled", "bundled", "{1FBA69C4-7EAA-4D60-BCF9-3D59D5A88D32}"
|
||||
ProjectSection(SolutionItems) = preProject
|
||||
..\include\spdlog\fmt\bundled\format.cc = ..\include\spdlog\fmt\bundled\format.cc
|
||||
..\include\spdlog\fmt\bundled\format.h = ..\include\spdlog\fmt\bundled\format.h
|
||||
..\include\spdlog\fmt\bundled\LICENSE.rst = ..\include\spdlog\fmt\bundled\LICENSE.rst
|
||||
..\include\spdlog\fmt\bundled\ostream.cc = ..\include\spdlog\fmt\bundled\ostream.cc
|
||||
..\include\spdlog\fmt\bundled\ostream.h = ..\include\spdlog\fmt\bundled\ostream.h
|
||||
..\include\spdlog\fmt\bundled\posix.cc = ..\include\spdlog\fmt\bundled\posix.cc
|
||||
..\include\spdlog\fmt\bundled\posix.h = ..\include\spdlog\fmt\bundled\posix.h
|
||||
..\include\spdlog\fmt\bundled\printf.cc = ..\include\spdlog\fmt\bundled\printf.cc
|
||||
..\include\spdlog\fmt\bundled\printf.h = ..\include\spdlog\fmt\bundled\printf.h
|
||||
..\include\spdlog\fmt\bundled\time.h = ..\include\spdlog\fmt\bundled\time.h
|
||||
EndProjectSection
|
||||
EndProject
|
||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "sinks", "sinks", "{278CDF3C-6E6D-4FAF-AF79-C1806101B4CB}"
|
||||
ProjectSection(SolutionItems) = preProject
|
||||
..\include\spdlog\sinks\android_sink.h = ..\include\spdlog\sinks\android_sink.h
|
||||
..\include\spdlog\sinks\ansicolor_sink.h = ..\include\spdlog\sinks\ansicolor_sink.h
|
||||
..\include\spdlog\sinks\base_sink.h = ..\include\spdlog\sinks\base_sink.h
|
||||
..\include\spdlog\sinks\dist_sink.h = ..\include\spdlog\sinks\dist_sink.h
|
||||
..\include\spdlog\sinks\file_sinks.h = ..\include\spdlog\sinks\file_sinks.h
|
||||
..\include\spdlog\sinks\msvc_sink.h = ..\include\spdlog\sinks\msvc_sink.h
|
||||
..\include\spdlog\sinks\null_sink.h = ..\include\spdlog\sinks\null_sink.h
|
||||
..\include\spdlog\sinks\ostream_sink.h = ..\include\spdlog\sinks\ostream_sink.h
|
||||
..\include\spdlog\sinks\sink.h = ..\include\spdlog\sinks\sink.h
|
||||
..\include\spdlog\sinks\stdout_sinks.h = ..\include\spdlog\sinks\stdout_sinks.h
|
||||
..\include\spdlog\sinks\syslog_sink.h = ..\include\spdlog\sinks\syslog_sink.h
|
||||
..\include\spdlog\sinks\wincolor_sink.h = ..\include\spdlog\sinks\wincolor_sink.h
|
||||
..\include\spdlog\sinks\windebug_sink.h = ..\include\spdlog\sinks\windebug_sink.h
|
||||
EndProjectSection
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Win32 = Debug|Win32
|
||||
@@ -25,4 +98,15 @@ Global
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(NestedProjects) = preSolution
|
||||
{AAD3C108-4E24-4AA8-BA09-4C9A75A881B3} = {7EFD7EC9-512F-4B35-ADFE-49863B2C04A5}
|
||||
{3FB7E4DF-2397-463E-BDAD-32854AB66DFD} = {AAD3C108-4E24-4AA8-BA09-4C9A75A881B3}
|
||||
{97DED9BF-821E-4A7A-8D13-ED9A739E1F55} = {7EFD7EC9-512F-4B35-ADFE-49863B2C04A5}
|
||||
{0B649723-CF78-47C0-B1CA-1F173DDBFED4} = {7EFD7EC9-512F-4B35-ADFE-49863B2C04A5}
|
||||
{1FBA69C4-7EAA-4D60-BCF9-3D59D5A88D32} = {0B649723-CF78-47C0-B1CA-1F173DDBFED4}
|
||||
{278CDF3C-6E6D-4FAF-AF79-C1806101B4CB} = {7EFD7EC9-512F-4B35-ADFE-49863B2C04A5}
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {25A6A4D3-9B25-4071-81B4-99DFDD066255}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
|
@@ -30,9 +30,6 @@
|
||||
<ClCompile Include="utils.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="errors.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="test_macros.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
@@ -42,6 +39,9 @@
|
||||
<ClCompile Include="test_misc.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="errors.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="test_async.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
|
Reference in New Issue
Block a user