Skip to content

Commit

Permalink
Add support for stderr sinks (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
nextsilicon-itay-bookstein authored Dec 27, 2020
1 parent a5dc6d0 commit a991861
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,12 @@ The header files should be installed in `build/install/include`.

- `stdout_sink_st`
- `stdout_sink_mt`
- `stderr_sink_st`
- `stderr_sink_mt`
- `color_stdout_sink_st`
- `color_stdout_sink_mt`
- `color_stderr_sink_st`
- `color_stderr_sink_mt`
- `basic_file_sink_st`
- `basic_file_sink_mt`
- `rotating_file_sink_st`
Expand Down
40 changes: 40 additions & 0 deletions include/spdlog_setup/details/conf_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ enum class sink_type {
/** Represents stdout_sink_mt */
StdoutSinkMt,

/** Represents stderr_sink_st */
StderrSinkSt,

/** Represents stderr_sink_mt */
StderrSinkMt,

/**
* Represents either wincolor_stdout_sink_st (Windows) or
* ansicolor_stdout_sink_st (Linux)
Expand All @@ -90,6 +96,18 @@ enum class sink_type {
*/
ColorStdoutSinkMt,

/**
* Represents either wincolor_stderr_sink_st (Windows) or
* ansicolor_stderr_sink_st (Linux)
*/
ColorStderrSinkSt,

/**
* Represents either wincolor_stderr_sink_mt (Windows) or
* ansicolor_stderr_sink_mt (Linux)
*/
ColorStderrSinkMt,

/** Represents basic_file_sink_st */
BasicFileSinkSt,

Expand Down Expand Up @@ -597,8 +615,12 @@ inline auto sink_type_from_str(const std::string &type) -> sink_type {
static const unordered_map<string, sink_type> MAPPING{
{"stdout_sink_st", sink_type::StdoutSinkSt},
{"stdout_sink_mt", sink_type::StdoutSinkMt},
{"stderr_sink_st", sink_type::StderrSinkSt},
{"stderr_sink_mt", sink_type::StderrSinkMt},
{"color_stdout_sink_st", sink_type::ColorStdoutSinkSt},
{"color_stdout_sink_mt", sink_type::ColorStdoutSinkMt},
{"color_stderr_sink_st", sink_type::ColorStderrSinkSt},
{"color_stderr_sink_mt", sink_type::ColorStderrSinkMt},
{"basic_file_sink_st", sink_type::BasicFileSinkSt},
{"basic_file_sink_mt", sink_type::BasicFileSinkMt},
{"rotating_file_sink_st", sink_type::RotatingFileSinkSt},
Expand Down Expand Up @@ -881,6 +903,8 @@ inline auto sink_from_sink_type(
using spdlog::sinks::rotating_file_sink_mt;
using spdlog::sinks::rotating_file_sink_st;
using spdlog::sinks::sink;
using spdlog::sinks::stderr_sink_mt;
using spdlog::sinks::stderr_sink_st;
using spdlog::sinks::stdout_sink_mt;
using spdlog::sinks::stdout_sink_st;

Expand All @@ -890,11 +914,15 @@ inline auto sink_from_sink_type(
#ifdef _WIN32
using color_stdout_sink_st = spdlog::sinks::wincolor_stdout_sink_st;
using color_stdout_sink_mt = spdlog::sinks::wincolor_stdout_sink_mt;
using color_stderr_sink_st = spdlog::sinks::wincolor_stderr_sink_st;
using color_stderr_sink_mt = spdlog::sinks::wincolor_stderr_sink_mt;
using msvc_sink_st = spdlog::sinks::msvc_sink_st;
using msvc_sink_mt = spdlog::sinks::msvc_sink_mt;
#else
using color_stdout_sink_st = spdlog::sinks::ansicolor_stdout_sink_st;
using color_stdout_sink_mt = spdlog::sinks::ansicolor_stdout_sink_mt;
using color_stderr_sink_st = spdlog::sinks::ansicolor_stderr_sink_st;
using color_stderr_sink_mt = spdlog::sinks::ansicolor_stderr_sink_mt;

using spdlog::sinks::syslog_sink_mt;
using spdlog::sinks::syslog_sink_st;
Expand All @@ -907,12 +935,24 @@ inline auto sink_from_sink_type(
case sink_type::StdoutSinkMt:
return make_shared<stdout_sink_mt>();

case sink_type::StderrSinkSt:
return make_shared<stderr_sink_st>();

case sink_type::StderrSinkMt:
return make_shared<stderr_sink_mt>();

case sink_type::ColorStdoutSinkSt:
return make_shared<color_stdout_sink_st>();

case sink_type::ColorStdoutSinkMt:
return make_shared<color_stdout_sink_mt>();

case sink_type::ColorStderrSinkSt:
return make_shared<color_stderr_sink_st>();

case sink_type::ColorStderrSinkMt:
return make_shared<color_stderr_sink_mt>();

case sink_type::BasicFileSinkSt:
return setup_basic_file_sink<basic_file_sink_st>(sink_table);

Expand Down
18 changes: 18 additions & 0 deletions src/unit_test/sinks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,21 @@ TEST_CASE("Parse stdout sink st", "[parse_generate_stdout_sink_st]") {
spdlog_setup::details::setup_sink(generate_stdout_sink_st());
REQUIRE(typeid(*sink) == typeid(const spdlog::sinks::stdout_sink_st &));
}

TEST_CASE("Parse stderr sink st", "[parse_generate_stderr_sink_st]") {
const auto sink =
spdlog_setup::details::setup_sink(generate_stderr_sink_st());
REQUIRE(typeid(*sink) == typeid(const spdlog::sinks::stderr_sink_st &));
}

TEST_CASE("Parse stdout sink mt", "[parse_generate_stdout_sink_mt]") {
const auto sink =
spdlog_setup::details::setup_sink(generate_stdout_sink_mt());
REQUIRE(typeid(*sink) == typeid(const spdlog::sinks::stdout_sink_mt &));
}

TEST_CASE("Parse stderr sink mt", "[parse_generate_stderr_sink_mt]") {
const auto sink =
spdlog_setup::details::setup_sink(generate_stderr_sink_mt());
REQUIRE(typeid(*sink) == typeid(const spdlog::sinks::stderr_sink_mt &));
}
24 changes: 24 additions & 0 deletions src/unit_test/sinks.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,27 @@ inline auto generate_stdout_sink_st() -> std::shared_ptr<cpptoml::table> {
sink_table->insert(names::TYPE, std::string("stdout_sink_st"));
return std::move(sink_table);
}

inline auto generate_stderr_sink_st() -> std::shared_ptr<cpptoml::table> {
namespace names = spdlog_setup::details::names;

auto sink_table = cpptoml::make_table();
sink_table->insert(names::TYPE, std::string("stderr_sink_st"));
return std::move(sink_table);
}

inline auto generate_stdout_sink_mt() -> std::shared_ptr<cpptoml::table> {
namespace names = spdlog_setup::details::names;

auto sink_table = cpptoml::make_table();
sink_table->insert(names::TYPE, std::string("stdout_sink_mt"));
return std::move(sink_table);
}

inline auto generate_stderr_sink_mt() -> std::shared_ptr<cpptoml::table> {
namespace names = spdlog_setup::details::names;

auto sink_table = cpptoml::make_table();
sink_table->insert(names::TYPE, std::string("stderr_sink_mt"));
return std::move(sink_table);
}

0 comments on commit a991861

Please sign in to comment.