diff --git a/example/example.cpp b/example/example.cpp index 44c14582..b2bfefdc 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -5,7 +5,7 @@ #include "c11log/logger.h" #include "c11log/sinks/async_sink.h" #include "c11log/sinks/file_sinks.h" -#include "c11log/sinks/stdout_sinks.h" +#include "c11log/sinks/console_sinks.h" #include "utils.h" @@ -58,10 +58,13 @@ int main(int argc, char* argv[]) auto &my_logger = get_logger("example"); auto null_sink = std::make_shared(); //auto async = std::make_shared(1000); - my_logger.add_sink(fsink); - //my_logger.add_sink(null_sink); - + //my_logger.add_sink(fsink); + my_logger.add_sink(null_sink); + auto &cout_logger = get_logger("cout"); + cout_logger.add_sink(sinks::stdout_sink()); + cout_logger.info() << "Hello cout logger!"; + auto start = system_clock::now(); const unsigned int howmany = 5000000; diff --git a/include/c11log/sinks/console_sinks.h b/include/c11log/sinks/console_sinks.h new file mode 100644 index 00000000..c93e753d --- /dev/null +++ b/include/c11log/sinks/console_sinks.h @@ -0,0 +1,46 @@ +#pragma once + +#include +#include +#include + +#include "base_sink.h" + +namespace c11log +{ +namespace sinks +{ +class console_sink: public base_sink +{ +public: + explicit console_sink(std::ostream& os):_ostream(os) {} + console_sink(const console_sink&) = delete; + console_sink& operator=(const console_sink&) = delete; + virtual ~console_sink() = default; + +protected: + virtual void _sink_it(const std::string& msg) override { + std::lock_guard lock(_mutex); + _ostream << msg; + } + + std::ostream& _ostream; + std::mutex _mutex; +}; + + +inline std::shared_ptr& stdout_sink () +{ + static auto inst = std::make_shared(std::cout); + return inst; +} + +inline std::shared_ptr& stderr_sink () +{ + static auto inst = std::make_shared(std::cerr); + return inst; +} + + +} +} diff --git a/include/c11log/sinks/stdout_sinks.h b/include/c11log/sinks/stdout_sinks.h deleted file mode 100644 index 3461b617..00000000 --- a/include/c11log/sinks/stdout_sinks.h +++ /dev/null @@ -1,34 +0,0 @@ -#pragma once - -#include -#include -#include - -#include "base_sink.h" - -namespace c11log -{ -namespace sinks -{ -class ostream_sink: public base_sink -{ -public: - explicit ostream_sink(std::ostream& os):_ostream(os) {} - ostream_sink(const ostream_sink&) = delete; - ostream_sink& operator=(const ostream_sink&) = delete; - virtual ~ostream_sink() = default; - -protected: - virtual void _sink_it(const std::string& msg) override { - std::lock_guard lock(_mutex); - _ostream << msg; - } - - std::ostream& _ostream; - std::mutex _mutex; -}; - - - -} -}