mirror of
https://github.com/gabime/spdlog.git
synced 2025-09-28 17:19:34 +08:00
Merge remote-tracking branch 'origin/v1.x' into pwm1234/rotate_on_open
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
project(spdlog-utests CXX)
|
||||
enable_testing()
|
||||
|
||||
find_package(Threads REQUIRED)
|
||||
|
||||
set(SPDLOG_UTESTS_SOURCES
|
||||
@@ -7,18 +7,23 @@ set(SPDLOG_UTESTS_SOURCES
|
||||
file_helper.cpp
|
||||
file_log.cpp
|
||||
test_misc.cpp
|
||||
test_pattern_formatter.cpp
|
||||
test_pattern_formatter.cpp
|
||||
test_async.cpp
|
||||
includes.h
|
||||
registry.cpp
|
||||
test_macros.cpp
|
||||
utils.cpp
|
||||
utils.h
|
||||
main.cpp)
|
||||
main.cpp
|
||||
test_mpmc_q.cpp
|
||||
test_sink.h
|
||||
)
|
||||
|
||||
add_executable(${PROJECT_NAME} ${SPDLOG_UTESTS_SOURCES})
|
||||
target_link_libraries(${PROJECT_NAME} PRIVATE Threads::Threads)
|
||||
target_link_libraries(${PROJECT_NAME} PRIVATE spdlog)
|
||||
target_link_libraries(${PROJECT_NAME} PRIVATE spdlog::spdlog)
|
||||
|
||||
add_test(NAME ${PROJECT_NAME} COMMAND ${PROJECT_NAME})
|
||||
file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/logs")
|
||||
|
||||
enable_testing()
|
||||
add_test(NAME ${PROJECT_NAME} COMMAND ${PROJECT_NAME})
|
||||
|
@@ -1,5 +1,5 @@
|
||||
CXX ?= g++
|
||||
CXXFLAGS = -Wall -pedantic -std=c++11 -pthread -O3 -I../include -fmax-errors=1
|
||||
CXXFLAGS = -Wall -pedantic -std=c++11 -pthread -Wconversion -O3 -I../include -fmax-errors=1
|
||||
LDPFALGS = -pthread
|
||||
|
||||
CPP_FILES := $(wildcard *.cpp)
|
||||
|
@@ -9,15 +9,15 @@ class failing_sink : public spdlog::sinks::base_sink<std::mutex>
|
||||
{
|
||||
public:
|
||||
failing_sink() = default;
|
||||
~failing_sink() = default;
|
||||
~failing_sink() final = default;
|
||||
|
||||
protected:
|
||||
void sink_it_(const spdlog::details::log_msg &) override
|
||||
void sink_it_(const spdlog::details::log_msg &) final
|
||||
{
|
||||
throw std::runtime_error("some error happened during log");
|
||||
}
|
||||
|
||||
void flush_() override
|
||||
void flush_() final
|
||||
{
|
||||
throw std::runtime_error("some error happened during flush");
|
||||
}
|
||||
@@ -25,7 +25,6 @@ protected:
|
||||
|
||||
TEST_CASE("default_error_handler", "[errors]]")
|
||||
{
|
||||
|
||||
prepare_logdir();
|
||||
std::string filename = "logs/simple_log.txt";
|
||||
|
||||
|
@@ -33,7 +33,7 @@ TEST_CASE("flush_on", "[flush_on]]")
|
||||
|
||||
logger->info("Test message {}", 1);
|
||||
logger->info("Test message {}", 2);
|
||||
logger->flush();
|
||||
|
||||
REQUIRE(file_contents(filename) == std::string("Should not be flushed\nTest message 1\nTest message 2\n"));
|
||||
REQUIRE(count_lines(filename) == 3);
|
||||
}
|
||||
|
@@ -6,6 +6,7 @@
|
||||
#include <cstdio>
|
||||
#include <exception>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <ostream>
|
||||
#include <string>
|
||||
|
||||
|
@@ -1,12 +0,0 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
# Install libc++ under travis
|
||||
|
||||
svn --quiet co http://llvm.org/svn/llvm-project/libcxx/trunk libcxx
|
||||
mkdir libcxx/build
|
||||
(cd libcxx/build && cmake .. -DLIBCXX_CXX_ABI=libstdc++ -DLIBCXX_CXX_ABI_INCLUDE_PATHS="/usr/include/c++/4.6;/usr/include/c++/4.6/x86_64-linux-gnu")
|
||||
make -C libcxx/build cxx -j2
|
||||
sudo cp libcxx/build/lib/libc++.so.1.0 /usr/lib/
|
||||
sudo cp -r libcxx/build/include/c++/v1 /usr/include/c++/v1/
|
||||
sudo ln -sf /usr/lib/libc++.so.1.0 /usr/lib/libc++.so
|
||||
sudo ln -sf /usr/lib/libc++.so.1.0 /usr/lib/libc++.so.1
|
@@ -9,7 +9,7 @@ TEST_CASE("register_drop", "[registry]")
|
||||
spdlog::create<spdlog::sinks::null_sink_mt>(tested_logger_name);
|
||||
REQUIRE(spdlog::get(tested_logger_name) != nullptr);
|
||||
// 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), const spdlog::spdlog_ex &);
|
||||
}
|
||||
|
||||
TEST_CASE("explicit register"
|
||||
@@ -20,7 +20,7 @@ TEST_CASE("explicit register"
|
||||
spdlog::register_logger(logger);
|
||||
REQUIRE(spdlog::get(tested_logger_name) != nullptr);
|
||||
// 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), const spdlog::spdlog_ex &);
|
||||
}
|
||||
|
||||
TEST_CASE("apply_all"
|
||||
@@ -33,7 +33,7 @@ TEST_CASE("apply_all"
|
||||
spdlog::register_logger(logger2);
|
||||
|
||||
int counter = 0;
|
||||
spdlog::apply_all([&counter](std::shared_ptr<spdlog::logger> l) { counter++; });
|
||||
spdlog::apply_all([&counter](std::shared_ptr<spdlog::logger>) { counter++; });
|
||||
REQUIRE(counter == 2);
|
||||
|
||||
counter = 0;
|
||||
@@ -62,7 +62,7 @@ TEST_CASE("drop_all"
|
||||
spdlog::create<spdlog::sinks::null_sink_mt>(tested_logger_name2);
|
||||
spdlog::drop_all();
|
||||
REQUIRE_FALSE(spdlog::get(tested_logger_name));
|
||||
REQUIRE_FALSE(spdlog::get(tested_logger_name));
|
||||
REQUIRE_FALSE(spdlog::get(tested_logger_name2));
|
||||
}
|
||||
|
||||
TEST_CASE("drop non existing"
|
||||
|
@@ -7,6 +7,7 @@ TEST_CASE("basic async test ", "[async]")
|
||||
{
|
||||
using namespace spdlog;
|
||||
auto test_sink = std::make_shared<sinks::test_sink_mt>();
|
||||
size_t overrun_counter = 0;
|
||||
size_t queue_size = 128;
|
||||
size_t messages = 256;
|
||||
{
|
||||
@@ -17,17 +18,20 @@ TEST_CASE("basic async test ", "[async]")
|
||||
logger->info("Hello message #{}", i);
|
||||
}
|
||||
logger->flush();
|
||||
overrun_counter = tp->overrun_counter();
|
||||
}
|
||||
REQUIRE(test_sink->msg_counter() == messages);
|
||||
REQUIRE(test_sink->flush_counter() == 1);
|
||||
REQUIRE(overrun_counter == 0);
|
||||
}
|
||||
|
||||
TEST_CASE("discard policy ", "[async]")
|
||||
{
|
||||
using namespace spdlog;
|
||||
auto test_sink = std::make_shared<sinks::test_sink_mt>();
|
||||
size_t queue_size = 2;
|
||||
size_t messages = 10240;
|
||||
test_sink->set_delay(std::chrono::milliseconds(1));
|
||||
size_t queue_size = 4;
|
||||
size_t messages = 1024;
|
||||
|
||||
auto tp = std::make_shared<details::thread_pool>(queue_size, 1);
|
||||
auto logger = std::make_shared<async_logger>("as", test_sink, tp, async_overflow_policy::overrun_oldest);
|
||||
@@ -36,22 +40,26 @@ TEST_CASE("discard policy ", "[async]")
|
||||
logger->info("Hello message");
|
||||
}
|
||||
REQUIRE(test_sink->msg_counter() < messages);
|
||||
REQUIRE(tp->overrun_counter() > 0);
|
||||
}
|
||||
|
||||
TEST_CASE("discard policy using factory ", "[async]")
|
||||
{
|
||||
using namespace spdlog;
|
||||
size_t queue_size = 2;
|
||||
size_t messages = 10240;
|
||||
size_t queue_size = 4;
|
||||
size_t messages = 1024;
|
||||
spdlog::init_thread_pool(queue_size, 1);
|
||||
|
||||
auto logger = spdlog::create_async_nb<sinks::test_sink_mt>("as2");
|
||||
auto test_sink = std::static_pointer_cast<sinks::test_sink_mt>(logger->sinks()[0]);
|
||||
test_sink->set_delay(std::chrono::milliseconds(1));
|
||||
|
||||
for (size_t i = 0; i < messages; i++)
|
||||
{
|
||||
logger->info("Hello message");
|
||||
}
|
||||
auto sink = std::static_pointer_cast<sinks::test_sink_mt>(logger->sinks()[0]);
|
||||
REQUIRE(sink->msg_counter() < messages);
|
||||
|
||||
REQUIRE(test_sink->msg_counter() < messages);
|
||||
spdlog::drop_all();
|
||||
}
|
||||
|
||||
|
@@ -91,3 +91,46 @@ TEST_CASE("periodic flush", "[periodic_flush]")
|
||||
spdlog::flush_every(std::chrono::seconds(0));
|
||||
spdlog::drop_all();
|
||||
}
|
||||
|
||||
TEST_CASE("clone", "[clone]")
|
||||
{
|
||||
using namespace spdlog;
|
||||
|
||||
auto logger = spdlog::create<sinks::test_sink_mt>("orig");
|
||||
auto cloned = logger->clone("clone");
|
||||
|
||||
REQUIRE(cloned->name() == "clone");
|
||||
REQUIRE(logger->sinks() == cloned->sinks());
|
||||
REQUIRE(logger->level() == cloned->level());
|
||||
REQUIRE(logger->flush_level() == cloned->flush_level());
|
||||
logger->info("Some message 1");
|
||||
cloned->info("Some message 2");
|
||||
|
||||
auto test_sink = std::static_pointer_cast<sinks::test_sink_mt>(cloned->sinks()[0]);
|
||||
REQUIRE(test_sink->msg_counter() == 2);
|
||||
|
||||
spdlog::drop_all();
|
||||
}
|
||||
|
||||
TEST_CASE("clone async", "[clone]")
|
||||
{
|
||||
using namespace spdlog;
|
||||
|
||||
auto logger = spdlog::create_async<sinks::test_sink_mt>("orig");
|
||||
auto cloned = logger->clone("clone");
|
||||
|
||||
REQUIRE(cloned->name() == "clone");
|
||||
REQUIRE(logger->sinks() == cloned->sinks());
|
||||
REQUIRE(logger->level() == cloned->level());
|
||||
REQUIRE(logger->flush_level() == cloned->flush_level());
|
||||
|
||||
logger->info("Some message 1");
|
||||
cloned->info("Some message 2");
|
||||
|
||||
spdlog::details::os::sleep_for_millis(10);
|
||||
|
||||
auto test_sink = std::static_pointer_cast<sinks::test_sink_mt>(cloned->sinks()[0]);
|
||||
REQUIRE(test_sink->msg_counter() == 2);
|
||||
|
||||
spdlog::drop_all();
|
||||
}
|
||||
|
107
tests/test_mpmc_q.cpp
Normal file
107
tests/test_mpmc_q.cpp
Normal file
@@ -0,0 +1,107 @@
|
||||
#include "includes.h"
|
||||
|
||||
using namespace std::chrono;
|
||||
using std::chrono::milliseconds;
|
||||
using test_clock = std::chrono::high_resolution_clock;
|
||||
|
||||
static milliseconds millis_from(const test_clock::time_point &tp0)
|
||||
{
|
||||
return std::chrono::duration_cast<milliseconds>(test_clock::now() - tp0);
|
||||
}
|
||||
TEST_CASE("dequeue-empty-nowait", "[mpmc_blocking_q]")
|
||||
{
|
||||
size_t q_size = 100;
|
||||
milliseconds tolerance_wait(10);
|
||||
spdlog::details::mpmc_blocking_queue<int> q(q_size);
|
||||
int popped_item;
|
||||
|
||||
auto start = test_clock::now();
|
||||
auto rv = q.dequeue_for(popped_item, milliseconds::zero());
|
||||
auto delta_ms = millis_from(start);
|
||||
|
||||
REQUIRE(rv == false);
|
||||
INFO("Delta " << delta_ms.count() << " millis");
|
||||
REQUIRE(delta_ms <= tolerance_wait);
|
||||
}
|
||||
|
||||
TEST_CASE("dequeue-empty-wait", "[mpmc_blocking_q]")
|
||||
{
|
||||
|
||||
size_t q_size = 100;
|
||||
milliseconds wait_ms(250);
|
||||
milliseconds tolerance_wait(100);
|
||||
|
||||
spdlog::details::mpmc_blocking_queue<int> q(q_size);
|
||||
int popped_item;
|
||||
auto start = test_clock::now();
|
||||
auto rv = q.dequeue_for(popped_item, wait_ms);
|
||||
auto delta_ms = millis_from(start);
|
||||
|
||||
REQUIRE(rv == false);
|
||||
|
||||
INFO("Delta " << delta_ms.count() << " millis");
|
||||
REQUIRE(delta_ms >= wait_ms);
|
||||
REQUIRE(delta_ms <= wait_ms + tolerance_wait);
|
||||
}
|
||||
|
||||
TEST_CASE("enqueue_nowait", "[mpmc_blocking_q]")
|
||||
{
|
||||
|
||||
size_t q_size = 1;
|
||||
spdlog::details::mpmc_blocking_queue<int> q(q_size);
|
||||
milliseconds tolerance_wait(10);
|
||||
|
||||
q.enqueue(1);
|
||||
REQUIRE(q.overrun_counter() == 0);
|
||||
|
||||
auto start = test_clock::now();
|
||||
q.enqueue_nowait(2);
|
||||
auto delta_ms = millis_from(start);
|
||||
|
||||
INFO("Delta " << delta_ms.count() << " millis");
|
||||
REQUIRE(delta_ms <= tolerance_wait);
|
||||
REQUIRE(q.overrun_counter() == 1);
|
||||
}
|
||||
|
||||
TEST_CASE("bad_queue", "[mpmc_blocking_q]")
|
||||
{
|
||||
size_t q_size = 0;
|
||||
spdlog::details::mpmc_blocking_queue<int> q(q_size);
|
||||
q.enqueue_nowait(1);
|
||||
REQUIRE(q.overrun_counter() == 1);
|
||||
int i;
|
||||
REQUIRE(q.dequeue_for(i, milliseconds(0)) == false);
|
||||
}
|
||||
|
||||
TEST_CASE("empty_queue", "[mpmc_blocking_q]")
|
||||
{
|
||||
size_t q_size = 10;
|
||||
spdlog::details::mpmc_blocking_queue<int> q(q_size);
|
||||
int i;
|
||||
REQUIRE(q.dequeue_for(i, milliseconds(10)) == false);
|
||||
}
|
||||
|
||||
TEST_CASE("full_queue", "[mpmc_blocking_q]")
|
||||
{
|
||||
size_t q_size = 100;
|
||||
spdlog::details::mpmc_blocking_queue<int> q(q_size);
|
||||
for (int i = 0; i < static_cast<int>(q_size); i++)
|
||||
{
|
||||
q.enqueue(std::move(i));
|
||||
}
|
||||
|
||||
q.enqueue_nowait(123456);
|
||||
REQUIRE(q.overrun_counter() == 1);
|
||||
|
||||
for (int i = 1; i < static_cast<int>(q_size); i++)
|
||||
{
|
||||
int item = -1;
|
||||
q.dequeue_for(item, milliseconds(0));
|
||||
REQUIRE(item == i);
|
||||
}
|
||||
|
||||
// last item pushed has overridden the oldest.
|
||||
int item = -1;
|
||||
q.dequeue_for(item, milliseconds(0));
|
||||
REQUIRE(item == 123456);
|
||||
}
|
@@ -21,11 +21,13 @@ class test_sink : public base_sink<Mutex>
|
||||
public:
|
||||
size_t msg_counter()
|
||||
{
|
||||
std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
|
||||
return msg_counter_;
|
||||
}
|
||||
|
||||
size_t flush_counter()
|
||||
{
|
||||
std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
|
||||
return flush_counter_;
|
||||
}
|
||||
|
||||
|
@@ -56,7 +56,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "sinks", "sinks", "{093AE34A
|
||||
..\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
|
||||
@@ -65,7 +64,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "sinks", "sinks", "{093AE34A
|
||||
..\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
|
||||
|
Reference in New Issue
Block a user