From 2a6ff3b61336c65053a3678eb86188657f679dba Mon Sep 17 00:00:00 2001 From: gabi Date: Fri, 10 Oct 2014 02:46:03 +0300 Subject: [PATCH] Added ostream_sink --- c11logtest/c11logtest/c11logtest.vcxproj | 5 ++- .../c11logtest/c11logtest.vcxproj.filters | 23 ++++++++---- example/example.cpp | 23 ++++++------ include/c11log/details/log_msg.h | 2 ++ include/c11log/logger.h | 18 ++++------ include/c11log/sinks/ostream_sink.h | 36 +++++++++++++++++++ 6 files changed, 78 insertions(+), 29 deletions(-) create mode 100644 include/c11log/sinks/ostream_sink.h diff --git a/c11logtest/c11logtest/c11logtest.vcxproj b/c11logtest/c11logtest/c11logtest.vcxproj index 6b357b8a..a28d09cf 100644 --- a/c11logtest/c11logtest/c11logtest.vcxproj +++ b/c11logtest/c11logtest/c11logtest.vcxproj @@ -91,15 +91,18 @@ + - + + + diff --git a/c11logtest/c11logtest/c11logtest.vcxproj.filters b/c11logtest/c11logtest/c11logtest.vcxproj.filters index ef86bb8c..9ee71f3d 100644 --- a/c11logtest/c11logtest/c11logtest.vcxproj.filters +++ b/c11logtest/c11logtest/c11logtest.vcxproj.filters @@ -45,9 +45,6 @@ Header Files\c11log\details - - Header Files\c11log\details - Header Files\c11log\details @@ -72,15 +69,27 @@ Header Files\c11log\sinks - - Header Files\c11log\sinks - Header Files\c11log\sinks - + Header Files\c11log\details + + Header Files + + + Header Files\c11log\details + + + Header Files\c11log\sinks + + + Header Files\c11log\sinks + + + Header Files\c11log\sinks + diff --git a/example/example.cpp b/example/example.cpp index 7bc9ebe9..6401c5d7 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -1,10 +1,10 @@ // example.cpp : Simple logger example // - +#include #include "c11log/logger.h" #include "c11log/sinks/async_sink.h" #include "c11log/sinks/file_sinks.h" -#include "c11log/sinks/console_sinks.h" +#include "c11log/sinks/stdout_sinks.h" #include "c11log/sinks/null_sink.h" #include "utils.h" @@ -14,33 +14,36 @@ using namespace std::chrono; using namespace c11log; using namespace utils; + int main(int argc, char* argv[]) { - const unsigned int howmany = argc <= 1 ? 10000:atoi(argv[1]); - logger cout_logger ("example", sinks::stdout_sink()); + const unsigned int howmany = argc <= 1 ? 10000 : atoi(argv[1]); + + logger cout_logger("example", std::make_shared()); cout_logger.info() << "Hello logger"; - auto nullsink = std::make_shared(); - //auto fsink = std::make_shared("log", "txt", 1024*1024*50 , 5, 0); + //auto nullsink = std::make_shared>(); + //auto nullsink = std::make_shared>(); + auto fsink = std::make_shared("log", "txt", 1024*1024*50 , 5, 1); //auto as = std::make_shared(1000); //as->add_sink(fsink); - logger my_logger ("my_logger", nullsink); + logger my_logger("my_logger", fsink); auto start = system_clock::now(); for (unsigned int i = 1; i <= howmany; ++i) my_logger.info() << "Hello logger: msg #" << i; - my_logger.info("FFF"); //as->shutdown(std::chrono::milliseconds(15000)); auto delta = system_clock::now() - start; auto delta_d = duration_cast> (delta).count(); cout << "Total:" << format(howmany) << endl; - cout<< "Delta:" << format(delta_d) << endl; - cout << "Rate:" << format(howmany/delta_d) << "/sec" << endl; + cout << "Delta:" << format(delta_d) << endl; + cout << "Rate:" << format(howmany / delta_d) << "/sec" << endl; + return 0; } diff --git a/include/c11log/details/log_msg.h b/include/c11log/details/log_msg.h index 42c0e2ab..bed7f414 100644 --- a/include/c11log/details/log_msg.h +++ b/include/c11log/details/log_msg.h @@ -1,5 +1,7 @@ #pragma once +#include "../common_types.h" + namespace c11log { namespace details diff --git a/include/c11log/logger.h b/include/c11log/logger.h index 1248de26..ac0179a9 100644 --- a/include/c11log/logger.h +++ b/include/c11log/logger.h @@ -4,7 +4,6 @@ // Has log level and vector sinks which do the actual logging #include #include -#include #include #include @@ -25,14 +24,11 @@ class line_logger; template class fast_buf; } - - - class logger { public: - using sink_ptr = std::shared_ptr; + using sink_ptr = std::shared_ptr; using sinks_vector_t = std::vector; using sinks_init_list = std::initializer_list; @@ -84,20 +80,20 @@ private: }; -std::shared_ptr create_logger(const std::string& name, logger::sinks_init_list sinks, logger::formatter_ptr formatter = nullptr); -std::shared_ptr get_logger(const std::string& name); +//std::shared_ptr create_logger(const std::string& name, logger::sinks_init_list sinks, logger::formatter_ptr formatter = nullptr); +//std::shared_ptr get_logger(const std::string& name); } // -// Logger inline implementation +// Logger implementation // #include "details/line_logger.h" #include "details/factory.h" - +/* inline std::shared_ptr c11log::create_logger(const std::string& name, logger::sinks_init_list sinks, logger::formatter_ptr formatter) { return details::factory::instance().create_logger(name, sinks, std::move(formatter)); @@ -105,7 +101,7 @@ inline std::shared_ptr c11log::create_logger(const std::string& inline std::shared_ptr c11log::get_logger(const std::string& name) { return details::factory::instance().get_logger(name); -} +}*/ inline c11log::logger::logger(const std::string& name, sinks_init_list sinks_list, formatter_ptr f) : @@ -113,7 +109,7 @@ inline c11log::logger::logger(const std::string& name, sinks_init_list sinks_lis _formatter(std::move(f)), _sinks(sinks_list) { - //Seems that vs2013 doesnt support std::atomic member initialization, so its done here + //Seems that vs2013 doesn't support std::atomic member initialization yet _min_level = level::INFO; if(!_formatter) _formatter = std::make_unique(); diff --git a/include/c11log/sinks/ostream_sink.h b/include/c11log/sinks/ostream_sink.h new file mode 100644 index 00000000..9f603d69 --- /dev/null +++ b/include/c11log/sinks/ostream_sink.h @@ -0,0 +1,36 @@ +#pragma once + +#include +#include +#include + +#include "../details/null_mutex.h" +#include "base_sink.h" + +namespace c11log +{ +namespace sinks +{ +template +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 details::log_msg& msg) override + { + _ostream << msg.formatted; + } + + std::ostream& _ostream; +}; + +typedef ostream_sink ostream_sink_mt; +typedef ostream_sink ostream_sink_st; +} +}