mirror of
https://github.com/gabime/spdlog.git
synced 2025-01-16 01:37:58 +00:00
Compare commits
5 Commits
34244656a6
...
854abdf5e6
Author | SHA1 | Date | |
---|---|---|---|
|
854abdf5e6 | ||
|
d0fc8a572c | ||
|
8bc1ca0e44 | ||
|
d38bd138cd | ||
|
7766bc25d1 |
@ -59,6 +59,8 @@ if (NOT DEFINED SPDLOG_MASTER_PROJECT)
|
||||
endif()
|
||||
endif ()
|
||||
|
||||
option(SPDLOG_BUILD_ALL "Build all artifacts" OFF)
|
||||
|
||||
# build shared option
|
||||
option(SPDLOG_BUILD_SHARED "Build shared library" OFF)
|
||||
|
||||
@ -255,7 +257,7 @@ endif()
|
||||
#---------------------------------------------------------------------------------------
|
||||
# Build binaries
|
||||
#---------------------------------------------------------------------------------------
|
||||
if(SPDLOG_BUILD_EXAMPLE OR SPDLOG_BUILD_EXAMPLE_HO)
|
||||
if(SPDLOG_BUILD_EXAMPLE OR SPDLOG_BUILD_EXAMPLE_HO OR SPDLOG_BUILD_ALL)
|
||||
message(STATUS "Generating example(s)")
|
||||
add_subdirectory(example)
|
||||
spdlog_enable_warnings(example)
|
||||
@ -264,13 +266,13 @@ if(SPDLOG_BUILD_EXAMPLE OR SPDLOG_BUILD_EXAMPLE_HO)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
if(SPDLOG_BUILD_TESTS OR SPDLOG_BUILD_TESTS_HO)
|
||||
if(SPDLOG_BUILD_TESTS OR SPDLOG_BUILD_TESTS_HO OR SPDLOG_BUILD_ALL)
|
||||
message(STATUS "Generating tests")
|
||||
enable_testing()
|
||||
add_subdirectory(tests)
|
||||
endif()
|
||||
|
||||
if(SPDLOG_BUILD_BENCH)
|
||||
if(SPDLOG_BUILD_BENCH OR SPDLOG_BUILD_ALL)
|
||||
message(STATUS "Generating benchmarks")
|
||||
add_subdirectory(bench)
|
||||
endif()
|
||||
|
@ -20,7 +20,7 @@
|
||||
#include <thread>
|
||||
|
||||
void bench(int howmany, std::shared_ptr<spdlog::logger> log);
|
||||
void bench_mt(int howmany, std::shared_ptr<spdlog::logger> log, int thread_count);
|
||||
void bench_mt(int howmany, std::shared_ptr<spdlog::logger> log, size_t thread_count);
|
||||
|
||||
// void bench_default_api(int howmany, std::shared_ptr<spdlog::logger> log);
|
||||
// void bench_c_string(int howmany, std::shared_ptr<spdlog::logger> log);
|
||||
@ -29,7 +29,7 @@ static const size_t file_size = 30 * 1024 * 1024;
|
||||
static const size_t rotating_files = 5;
|
||||
static const int max_threads = 1000;
|
||||
|
||||
void bench_threaded_logging(int threads, int iters)
|
||||
void bench_threaded_logging(size_t threads, int iters)
|
||||
{
|
||||
spdlog::info("**************************************************************");
|
||||
spdlog::info("Multi threaded: {:n} threads, {:n} messages", threads, iters);
|
||||
@ -107,7 +107,7 @@ int main(int argc, char *argv[])
|
||||
spdlog::set_automatic_registration(false);
|
||||
spdlog::default_logger()->set_pattern("[%^%l%$] %v");
|
||||
int iters = 250000;
|
||||
int threads = 4;
|
||||
size_t threads = 4;
|
||||
try
|
||||
{
|
||||
|
||||
@ -117,7 +117,7 @@ int main(int argc, char *argv[])
|
||||
}
|
||||
if (argc > 2)
|
||||
{
|
||||
threads = std::stoi(argv[2]);
|
||||
threads = std::stoul(argv[2]);
|
||||
}
|
||||
|
||||
if (threads > max_threads)
|
||||
@ -156,7 +156,7 @@ void bench(int howmany, std::shared_ptr<spdlog::logger> log)
|
||||
spdlog::drop(log->name());
|
||||
}
|
||||
|
||||
void bench_mt(int howmany, std::shared_ptr<spdlog::logger> log, int thread_count)
|
||||
void bench_mt(int howmany, std::shared_ptr<spdlog::logger> log, size_t thread_count)
|
||||
{
|
||||
using std::chrono::duration;
|
||||
using std::chrono::duration_cast;
|
||||
@ -165,10 +165,10 @@ void bench_mt(int howmany, std::shared_ptr<spdlog::logger> log, int thread_count
|
||||
std::vector<std::thread> threads;
|
||||
threads.reserve(thread_count);
|
||||
auto start = high_resolution_clock::now();
|
||||
for (int t = 0; t < thread_count; ++t)
|
||||
for (size_t t = 0; t < thread_count; ++t)
|
||||
{
|
||||
threads.emplace_back([&]() {
|
||||
for (int j = 0; j < howmany / thread_count; j++)
|
||||
for (int j = 0; j < howmany / static_cast<int>(thread_count); j++)
|
||||
{
|
||||
log->info("Hello logger: msg number {}", j);
|
||||
}
|
||||
|
@ -39,21 +39,12 @@ inline unsigned int count_digits(T n)
|
||||
|
||||
inline void pad2(int n, memory_buf_t &dest)
|
||||
{
|
||||
if (n > 99)
|
||||
{
|
||||
append_int(n, dest);
|
||||
}
|
||||
else if (n > 9) // 10-99
|
||||
if (n >= 0 && n < 100) // 0-99
|
||||
{
|
||||
dest.push_back(static_cast<char>('0' + n / 10));
|
||||
dest.push_back(static_cast<char>('0' + n % 10));
|
||||
}
|
||||
else if (n >= 0) // 0-9
|
||||
{
|
||||
dest.push_back('0');
|
||||
dest.push_back(static_cast<char>('0' + n));
|
||||
}
|
||||
else // negatives (unlikely, but just in case, let fmt deal with it)
|
||||
else // unlikely, but just in case, let fmt deal with it
|
||||
{
|
||||
fmt::format_to(dest, "{:02}", n);
|
||||
}
|
||||
@ -74,7 +65,7 @@ template<typename T>
|
||||
inline void pad3(T n, memory_buf_t &dest)
|
||||
{
|
||||
static_assert(std::is_unsigned<T>::value, "pad3 must get unsigned T");
|
||||
if(n < 1000)
|
||||
if (n < 1000)
|
||||
{
|
||||
dest.push_back(static_cast<char>(n / 100 + '0'));
|
||||
n = n % 100;
|
||||
|
@ -644,6 +644,21 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
// If padding is not needed, there is no need to count the digits of the thread id
|
||||
template<>
|
||||
class t_formatter<null_scoped_padder> final : public flag_formatter
|
||||
{
|
||||
public:
|
||||
explicit t_formatter(padding_info padinfo)
|
||||
: flag_formatter(padinfo)
|
||||
{}
|
||||
|
||||
void format(const details::log_msg &msg, const std::tm &, memory_buf_t &dest) override
|
||||
{
|
||||
fmt_helper::append_int(msg.thread_id, dest);
|
||||
}
|
||||
};
|
||||
|
||||
// Current pid
|
||||
template<typename ScopedPadder>
|
||||
class pid_formatter final : public flag_formatter
|
||||
@ -662,6 +677,22 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
// If padding is not needed, there is no need to count the digits of the pid
|
||||
template<>
|
||||
class pid_formatter<null_scoped_padder> final : public flag_formatter
|
||||
{
|
||||
public:
|
||||
explicit pid_formatter(padding_info padinfo)
|
||||
: flag_formatter(padinfo)
|
||||
{}
|
||||
|
||||
void format(const details::log_msg &, const std::tm &, memory_buf_t &dest) override
|
||||
{
|
||||
const auto pid = static_cast<uint32_t>(details::os::pid());
|
||||
fmt_helper::append_int(pid, dest);
|
||||
}
|
||||
};
|
||||
|
||||
template<typename ScopedPadder>
|
||||
class v_formatter final : public flag_formatter
|
||||
{
|
||||
@ -857,7 +888,6 @@ public:
|
||||
|
||||
// print elapsed time since last message
|
||||
template<typename ScopedPadder, typename Units>
|
||||
|
||||
class elapsed_formatter final : public flag_formatter
|
||||
{
|
||||
public:
|
||||
@ -883,6 +913,31 @@ private:
|
||||
log_clock::time_point last_message_time_;
|
||||
};
|
||||
|
||||
// If padding is not needed, there is no need to count the digits of the value
|
||||
template<typename Units>
|
||||
class elapsed_formatter<null_scoped_padder, Units> final : public flag_formatter
|
||||
{
|
||||
public:
|
||||
using DurationUnits = Units;
|
||||
|
||||
explicit elapsed_formatter(padding_info padinfo)
|
||||
: flag_formatter(padinfo)
|
||||
, last_message_time_(log_clock::now())
|
||||
{}
|
||||
|
||||
void format(const details::log_msg &msg, const std::tm &, memory_buf_t &dest) override
|
||||
{
|
||||
auto delta = (std::max)(msg.time - last_message_time_, log_clock::duration::zero());
|
||||
auto delta_units = std::chrono::duration_cast<DurationUnits>(delta);
|
||||
last_message_time_ = msg.time;
|
||||
auto delta_count = static_cast<size_t>(delta_units.count());
|
||||
fmt_helper::append_int(delta_count, dest);
|
||||
}
|
||||
|
||||
private:
|
||||
log_clock::time_point last_message_time_;
|
||||
};
|
||||
|
||||
// Full info formatter
|
||||
// pattern: [%Y-%m-%d %H:%M:%S.%e] [%n] [%l] %v
|
||||
class full_formatter final : public flag_formatter
|
||||
|
@ -60,11 +60,11 @@ function(spdlog_prepare_test test_target spdlog_lib)
|
||||
endfunction()
|
||||
|
||||
# The compiled library tests
|
||||
if(SPDLOG_BUILD_TESTS)
|
||||
if(SPDLOG_BUILD_TESTS OR SPDLOG_BUILD_ALL)
|
||||
spdlog_prepare_test(spdlog-utests spdlog::spdlog)
|
||||
endif()
|
||||
|
||||
# The header-only library version tests
|
||||
if(SPDLOG_BUILD_TESTS_HO)
|
||||
if(SPDLOG_BUILD_TESTS_HO OR SPDLOG_BUILD_ALL)
|
||||
spdlog_prepare_test(spdlog-utests-ho spdlog::spdlog_header_only)
|
||||
endif()
|
||||
|
Loading…
Reference in New Issue
Block a user