Added ostream_sink

This commit is contained in:
gabi 2014-10-10 02:46:03 +03:00
parent 6c7793d47b
commit 2a6ff3b613
6 changed files with 78 additions and 29 deletions

View File

@ -91,15 +91,18 @@
<ClInclude Include="..\..\include\c11log\details\flush_helper.h" />
<ClInclude Include="..\..\include\c11log\details\line_logger.h" />
<ClInclude Include="..\..\include\c11log\details\log_msg.h" />
<ClInclude Include="..\..\include\c11log\details\null_mutex.h" />
<ClInclude Include="..\..\include\c11log\details\os.h" />
<ClInclude Include="..\..\include\c11log\details\stack_buf.h" />
<ClInclude Include="..\..\include\c11log\formatter.h" />
<ClInclude Include="..\..\include\c11log\logger.h" />
<ClInclude Include="..\..\include\c11log\sinks\async_sink.h" />
<ClInclude Include="..\..\include\c11log\sinks\base_sink.h" />
<ClInclude Include="..\..\include\c11log\sinks\console_sinks.h" />
<ClInclude Include="..\..\include\c11log\sinks\ostream_sink.h" />
<ClInclude Include="..\..\include\c11log\sinks\file_sinks.h" />
<ClInclude Include="..\..\include\c11log\sinks\isink.h" />
<ClInclude Include="..\..\include\c11log\sinks\null_sink.h" />
<ClInclude Include="..\..\include\c11log\sinks\stdout_sinks.h" />
<ClInclude Include="stdafx.h" />
<ClInclude Include="targetver.h" />
</ItemGroup>

View File

@ -45,9 +45,6 @@
<ClInclude Include="..\..\include\c11log\details\blocking_queue.h">
<Filter>Header Files\c11log\details</Filter>
</ClInclude>
<ClInclude Include="..\..\include\c11log\details\factory.h">
<Filter>Header Files\c11log\details</Filter>
</ClInclude>
<ClInclude Include="..\..\include\c11log\details\fast_oss.h">
<Filter>Header Files\c11log\details</Filter>
</ClInclude>
@ -72,15 +69,27 @@
<ClInclude Include="..\..\include\c11log\sinks\base_sink.h">
<Filter>Header Files\c11log\sinks</Filter>
</ClInclude>
<ClInclude Include="..\..\include\c11log\sinks\console_sinks.h">
<Filter>Header Files\c11log\sinks</Filter>
</ClInclude>
<ClInclude Include="..\..\include\c11log\sinks\file_sinks.h">
<Filter>Header Files\c11log\sinks</Filter>
</ClInclude>
<ClInclude Include="..\..\include\c11log\sinks\null_sink.h">
<ClInclude Include="..\..\include\c11log\details\factory.h">
<Filter>Header Files\c11log\details</Filter>
</ClInclude>
<ClInclude Include="..\..\include\c11log\sinks\isink.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\..\include\c11log\details\null_mutex.h">
<Filter>Header Files\c11log\details</Filter>
</ClInclude>
<ClInclude Include="..\..\include\c11log\sinks\null_sink.h">
<Filter>Header Files\c11log\sinks</Filter>
</ClInclude>
<ClInclude Include="..\..\include\c11log\sinks\ostream_sink.h">
<Filter>Header Files\c11log\sinks</Filter>
</ClInclude>
<ClInclude Include="..\..\include\c11log\sinks\stdout_sinks.h">
<Filter>Header Files\c11log\sinks</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="stdafx.cpp">

View File

@ -1,10 +1,10 @@
// example.cpp : Simple logger example
//
#include <mutex>
#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,25 +14,27 @@ 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());
logger cout_logger("example", std::make_shared<sinks::stderr_sink_st>());
cout_logger.info() << "Hello logger";
auto nullsink = std::make_shared<sinks::null_sink>();
//auto fsink = std::make_shared<sinks::rotating_file_sink>("log", "txt", 1024*1024*50 , 5, 0);
//auto nullsink = std::make_shared<sinks::null_sink<std::mutex>>();
//auto nullsink = std::make_shared<sinks::null_sink<details::null_mutex>>();
auto fsink = std::make_shared<sinks::rotating_file_sink_mt>("log", "txt", 1024*1024*50 , 5, 1);
//auto as = std::make_shared<sinks::async_sink>(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;
@ -41,6 +43,7 @@ int main(int argc, char* argv[])
cout << "Total:" << format(howmany) << endl;
cout << "Delta:" << format(delta_d) << endl;
cout << "Rate:" << format(howmany / delta_d) << "/sec" << endl;
return 0;
}

View File

@ -1,5 +1,7 @@
#pragma once
#include "../common_types.h"
namespace c11log
{
namespace details

View File

@ -4,7 +4,6 @@
// Has log level and vector sinks which do the actual logging
#include<vector>
#include<memory>
#include<mutex>
#include<atomic>
#include <sstream>
@ -25,14 +24,11 @@ class line_logger;
template<std::size_t> class fast_buf;
}
class logger
{
public:
using sink_ptr = std::shared_ptr<sinks::base_sink>;
using sink_ptr = std::shared_ptr<sinks::isink>;
using sinks_vector_t = std::vector<sink_ptr>;
using sinks_init_list = std::initializer_list<sink_ptr>;
@ -84,20 +80,20 @@ private:
};
std::shared_ptr<c11log::logger> create_logger(const std::string& name, logger::sinks_init_list sinks, logger::formatter_ptr formatter = nullptr);
std::shared_ptr<logger> get_logger(const std::string& name);
//std::shared_ptr<c11log::logger> create_logger(const std::string& name, logger::sinks_init_list sinks, logger::formatter_ptr formatter = nullptr);
//std::shared_ptr<logger> 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::logger> 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::logger> c11log::create_logger(const std::string&
inline std::shared_ptr<c11log::logger> 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<formatters::default_formatter>();

View File

@ -0,0 +1,36 @@
#pragma once
#include <iostream>
#include <mutex>
#include <memory>
#include "../details/null_mutex.h"
#include "base_sink.h"
namespace c11log
{
namespace sinks
{
template<class Mutex>
class ostream_sink: public base_sink<Mutex>
{
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<std::mutex> ostream_sink_mt;
typedef ostream_sink<details::null_mutex> ostream_sink_st;
}
}