mirror of
https://github.com/gabime/spdlog.git
synced 2024-12-25 01:51:38 +08:00
Added ostream_sink
This commit is contained in:
parent
6c7793d47b
commit
2a6ff3b613
@ -91,15 +91,18 @@
|
|||||||
<ClInclude Include="..\..\include\c11log\details\flush_helper.h" />
|
<ClInclude Include="..\..\include\c11log\details\flush_helper.h" />
|
||||||
<ClInclude Include="..\..\include\c11log\details\line_logger.h" />
|
<ClInclude Include="..\..\include\c11log\details\line_logger.h" />
|
||||||
<ClInclude Include="..\..\include\c11log\details\log_msg.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\os.h" />
|
||||||
<ClInclude Include="..\..\include\c11log\details\stack_buf.h" />
|
<ClInclude Include="..\..\include\c11log\details\stack_buf.h" />
|
||||||
<ClInclude Include="..\..\include\c11log\formatter.h" />
|
<ClInclude Include="..\..\include\c11log\formatter.h" />
|
||||||
<ClInclude Include="..\..\include\c11log\logger.h" />
|
<ClInclude Include="..\..\include\c11log\logger.h" />
|
||||||
<ClInclude Include="..\..\include\c11log\sinks\async_sink.h" />
|
<ClInclude Include="..\..\include\c11log\sinks\async_sink.h" />
|
||||||
<ClInclude Include="..\..\include\c11log\sinks\base_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\file_sinks.h" />
|
||||||
|
<ClInclude Include="..\..\include\c11log\sinks\isink.h" />
|
||||||
<ClInclude Include="..\..\include\c11log\sinks\null_sink.h" />
|
<ClInclude Include="..\..\include\c11log\sinks\null_sink.h" />
|
||||||
|
<ClInclude Include="..\..\include\c11log\sinks\stdout_sinks.h" />
|
||||||
<ClInclude Include="stdafx.h" />
|
<ClInclude Include="stdafx.h" />
|
||||||
<ClInclude Include="targetver.h" />
|
<ClInclude Include="targetver.h" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
@ -45,9 +45,6 @@
|
|||||||
<ClInclude Include="..\..\include\c11log\details\blocking_queue.h">
|
<ClInclude Include="..\..\include\c11log\details\blocking_queue.h">
|
||||||
<Filter>Header Files\c11log\details</Filter>
|
<Filter>Header Files\c11log\details</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
<ClInclude Include="..\..\include\c11log\details\factory.h">
|
|
||||||
<Filter>Header Files\c11log\details</Filter>
|
|
||||||
</ClInclude>
|
|
||||||
<ClInclude Include="..\..\include\c11log\details\fast_oss.h">
|
<ClInclude Include="..\..\include\c11log\details\fast_oss.h">
|
||||||
<Filter>Header Files\c11log\details</Filter>
|
<Filter>Header Files\c11log\details</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
@ -72,15 +69,27 @@
|
|||||||
<ClInclude Include="..\..\include\c11log\sinks\base_sink.h">
|
<ClInclude Include="..\..\include\c11log\sinks\base_sink.h">
|
||||||
<Filter>Header Files\c11log\sinks</Filter>
|
<Filter>Header Files\c11log\sinks</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
<ClInclude Include="..\..\include\c11log\sinks\console_sinks.h">
|
|
||||||
<Filter>Header Files\c11log\sinks</Filter>
|
|
||||||
</ClInclude>
|
|
||||||
<ClInclude Include="..\..\include\c11log\sinks\file_sinks.h">
|
<ClInclude Include="..\..\include\c11log\sinks\file_sinks.h">
|
||||||
<Filter>Header Files\c11log\sinks</Filter>
|
<Filter>Header Files\c11log\sinks</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
<ClInclude Include="..\..\include\c11log\sinks\null_sink.h">
|
<ClInclude Include="..\..\include\c11log\details\factory.h">
|
||||||
<Filter>Header Files\c11log\details</Filter>
|
<Filter>Header Files\c11log\details</Filter>
|
||||||
</ClInclude>
|
</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>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClCompile Include="stdafx.cpp">
|
<ClCompile Include="stdafx.cpp">
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
// example.cpp : Simple logger example
|
// example.cpp : Simple logger example
|
||||||
//
|
//
|
||||||
|
#include <mutex>
|
||||||
#include "c11log/logger.h"
|
#include "c11log/logger.h"
|
||||||
#include "c11log/sinks/async_sink.h"
|
#include "c11log/sinks/async_sink.h"
|
||||||
#include "c11log/sinks/file_sinks.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 "c11log/sinks/null_sink.h"
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
|
|
||||||
@ -14,33 +14,36 @@ using namespace std::chrono;
|
|||||||
using namespace c11log;
|
using namespace c11log;
|
||||||
using namespace utils;
|
using namespace utils;
|
||||||
|
|
||||||
|
|
||||||
int main(int argc, char* argv[])
|
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<sinks::stderr_sink_st>());
|
||||||
cout_logger.info() << "Hello logger";
|
cout_logger.info() << "Hello logger";
|
||||||
|
|
||||||
auto nullsink = std::make_shared<sinks::null_sink>();
|
//auto nullsink = std::make_shared<sinks::null_sink<std::mutex>>();
|
||||||
//auto fsink = std::make_shared<sinks::rotating_file_sink>("log", "txt", 1024*1024*50 , 5, 0);
|
//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);
|
//auto as = std::make_shared<sinks::async_sink>(1000);
|
||||||
//as->add_sink(fsink);
|
//as->add_sink(fsink);
|
||||||
|
|
||||||
logger my_logger ("my_logger", nullsink);
|
logger my_logger("my_logger", fsink);
|
||||||
|
|
||||||
|
|
||||||
auto start = system_clock::now();
|
auto start = system_clock::now();
|
||||||
for (unsigned int i = 1; i <= howmany; ++i)
|
for (unsigned int i = 1; i <= howmany; ++i)
|
||||||
my_logger.info() << "Hello logger: msg #" << i;
|
my_logger.info() << "Hello logger: msg #" << i;
|
||||||
my_logger.info("FFF");
|
|
||||||
|
|
||||||
//as->shutdown(std::chrono::milliseconds(15000));
|
//as->shutdown(std::chrono::milliseconds(15000));
|
||||||
auto delta = system_clock::now() - start;
|
auto delta = system_clock::now() - start;
|
||||||
auto delta_d = duration_cast<duration<double>> (delta).count();
|
auto delta_d = duration_cast<duration<double>> (delta).count();
|
||||||
|
|
||||||
cout << "Total:" << format(howmany) << endl;
|
cout << "Total:" << format(howmany) << endl;
|
||||||
cout<< "Delta:" << format(delta_d) << endl;
|
cout << "Delta:" << format(delta_d) << endl;
|
||||||
cout << "Rate:" << format(howmany/delta_d) << "/sec" << endl;
|
cout << "Rate:" << format(howmany / delta_d) << "/sec" << endl;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include "../common_types.h"
|
||||||
|
|
||||||
namespace c11log
|
namespace c11log
|
||||||
{
|
{
|
||||||
namespace details
|
namespace details
|
||||||
|
@ -4,7 +4,6 @@
|
|||||||
// Has log level and vector sinks which do the actual logging
|
// Has log level and vector sinks which do the actual logging
|
||||||
#include<vector>
|
#include<vector>
|
||||||
#include<memory>
|
#include<memory>
|
||||||
#include<mutex>
|
|
||||||
#include<atomic>
|
#include<atomic>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
|
||||||
@ -25,14 +24,11 @@ class line_logger;
|
|||||||
template<std::size_t> class fast_buf;
|
template<std::size_t> class fast_buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class logger
|
class logger
|
||||||
{
|
{
|
||||||
public:
|
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_vector_t = std::vector<sink_ptr>;
|
||||||
using sinks_init_list = std::initializer_list<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<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<logger> get_logger(const std::string& name);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//
|
//
|
||||||
// Logger inline implementation
|
// Logger implementation
|
||||||
//
|
//
|
||||||
|
|
||||||
#include "details/line_logger.h"
|
#include "details/line_logger.h"
|
||||||
#include "details/factory.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)
|
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));
|
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)
|
inline std::shared_ptr<c11log::logger> c11log::get_logger(const std::string& name)
|
||||||
{
|
{
|
||||||
return details::factory::instance().get_logger(name);
|
return details::factory::instance().get_logger(name);
|
||||||
}
|
}*/
|
||||||
|
|
||||||
|
|
||||||
inline c11log::logger::logger(const std::string& name, sinks_init_list sinks_list, formatter_ptr f) :
|
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)),
|
_formatter(std::move(f)),
|
||||||
_sinks(sinks_list)
|
_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;
|
_min_level = level::INFO;
|
||||||
if(!_formatter)
|
if(!_formatter)
|
||||||
_formatter = std::make_unique<formatters::default_formatter>();
|
_formatter = std::make_unique<formatters::default_formatter>();
|
||||||
|
36
include/c11log/sinks/ostream_sink.h
Normal file
36
include/c11log/sinks/ostream_sink.h
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user