Small changes

This commit is contained in:
gabi 2014-10-10 03:36:50 +03:00
parent 2a6ff3b613
commit ae2426e0be
12 changed files with 41 additions and 84 deletions

View File

@ -84,7 +84,7 @@
<Text Include="ReadMe.txt" /> <Text Include="ReadMe.txt" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="..\..\include\c11log\common_types.h" /> <ClInclude Include="..\..\include\c11log\common.h" />
<ClInclude Include="..\..\include\c11log\details\blocking_queue.h" /> <ClInclude Include="..\..\include\c11log\details\blocking_queue.h" />
<ClInclude Include="..\..\include\c11log\details\factory.h" /> <ClInclude Include="..\..\include\c11log\details\factory.h" />
<ClInclude Include="..\..\include\c11log\details\fast_oss.h" /> <ClInclude Include="..\..\include\c11log\details\fast_oss.h" />

View File

@ -33,9 +33,6 @@
<ClInclude Include="targetver.h"> <ClInclude Include="targetver.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\..\include\c11log\common_types.h">
<Filter>Header Files\c11log</Filter>
</ClInclude>
<ClInclude Include="..\..\include\c11log\formatter.h"> <ClInclude Include="..\..\include\c11log\formatter.h">
<Filter>Header Files\c11log</Filter> <Filter>Header Files\c11log</Filter>
</ClInclude> </ClInclude>
@ -90,6 +87,9 @@
<ClInclude Include="..\..\include\c11log\sinks\stdout_sinks.h"> <ClInclude Include="..\..\include\c11log\sinks\stdout_sinks.h">
<Filter>Header Files\c11log\sinks</Filter> <Filter>Header Files\c11log\sinks</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\..\include\c11log\common.h">
<Filter>Header Files\c11log</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="stdafx.cpp"> <ClCompile Include="stdafx.cpp">

View File

@ -20,14 +20,14 @@ int main(int argc, char* argv[])
const unsigned int howmany = argc <= 1 ? 10000 : atoi(argv[1]); const unsigned int howmany = argc <= 1 ? 10000 : atoi(argv[1]);
logger cout_logger("example", std::make_shared<sinks::stderr_sink_st>()); logger cout_logger("example", std::make_shared<sinks::stderr_sink_mt>());
cout_logger.info() << "Hello logger"; cout_logger.info() << "Hello logger";
//auto nullsink = std::make_shared<sinks::null_sink<std::mutex>>(); //auto nullsink = std::make_shared<sinks::null_sink<std::mutex>>();
//auto nullsink = std::make_shared<sinks::null_sink<details::null_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 fsink = std::make_shared<sinks::rotating_file_sink_st>("log", "txt", 1024*1024*50 , 5, 10);
//auto as = std::make_shared<sinks::async_sink>(1000); auto as = std::make_shared<sinks::async_sink>(1000);
//as->add_sink(fsink);
logger my_logger("my_logger", fsink); logger my_logger("my_logger", fsink);
@ -36,7 +36,7 @@ int main(int argc, char* argv[])
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;
//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();

View File

@ -1,30 +0,0 @@
#pragma once
#include <chrono>
namespace c11log
{
typedef std::chrono::system_clock log_clock;
namespace level
{
typedef enum
{
TRACE,
DEBUG,
INFO,
WARNING,
ERROR,
CRITICAL,
FATAL,
NONE = 99
} level_enum;
static const char* level_names[] { "trace", "debug", "info", "warning", "error", "critical", "fatal" };
inline const char* to_str(c11log::level::level_enum l)
{
return level_names[l];
}
} //level
} //c11log

View File

@ -1,7 +1,7 @@
#pragma once #pragma once
#include <sstream> #include <sstream>
#include "../common_types.h" #include "../common.h"
#include "../logger.h" #include "../logger.h"
#include "fast_oss.h" #include "fast_oss.h"

View File

@ -1,6 +1,7 @@
#pragma once #pragma once
#include "../common_types.h" #include <chrono>
#include "../common.h"
namespace c11log namespace c11log
{ {
@ -16,7 +17,6 @@ struct log_msg
raw(), raw(),
formatted() {} formatted() {}
log_msg(const log_msg& other): log_msg(const log_msg& other):
logger_name(other.logger_name), logger_name(other.logger_name),
level(other.level), level(other.level),

View File

@ -8,7 +8,7 @@
#include <cstring> #include <cstring>
#include <sstream> #include <sstream>
#include "common_types.h" #include "common.h"
#include "details/os.h" #include "details/os.h"
#include "details/log_msg.h" #include "details/log_msg.h"
#include "details/fast_oss.h" #include "details/fast_oss.h"

View File

@ -8,8 +8,7 @@
#include <sstream> #include <sstream>
#include "sinks/base_sink.h" #include "sinks/base_sink.h"
#include "common_types.h" #include "common.h"

View File

@ -8,6 +8,7 @@
#include "base_sink.h" #include "base_sink.h"
#include "../logger.h" #include "../logger.h"
#include "../details/blocking_queue.h" #include "../details/blocking_queue.h"
#include "../details/null_mutex.h"
#include "../details/log_msg.h" #include "../details/log_msg.h"
#include<iostream> #include<iostream>
@ -17,8 +18,7 @@ namespace c11log
namespace sinks namespace sinks
{ {
template<class Mutex> class async_sink : public base_sink<details::null_mutex>
class async_sink : public base_sink<Mutex>
{ {
public: public:
using q_type = details::blocking_queue<details::log_msg>; using q_type = details::blocking_queue<details::log_msg>;
@ -26,7 +26,6 @@ public:
explicit async_sink(const q_type::size_type max_queue_size); explicit async_sink(const q_type::size_type max_queue_size);
//Stop logging and join the back thread //Stop logging and join the back thread
// TODO: limit with timeout of the join and kill it afterwards?
~async_sink(); ~async_sink();
void add_sink(logger::sink_ptr sink); void add_sink(logger::sink_ptr sink);
void remove_sink(logger::sink_ptr sink_ptr); void remove_sink(logger::sink_ptr sink_ptr);
@ -46,7 +45,7 @@ private:
std::thread _back_thread; std::thread _back_thread;
//Clear all remaining messages(if any), stop the _back_thread and join it //Clear all remaining messages(if any), stop the _back_thread and join it
void _shutdown(); void _shutdown();
std::mutex _shutdown_mutex; std::mutex _mutex;
}; };
} }
} }
@ -54,30 +53,26 @@ private:
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// async_sink class implementation // async_sink class implementation
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
template<class Mutex> inline c11log::sinks::async_sink::async_sink(const q_type::size_type max_queue_size)
inline c11log::sinks::async_sink<Mutex>::async_sink(const q_type::size_type max_queue_size)
:_sinks(), :_sinks(),
_active(true), _active(true),
_q(max_queue_size), _q(max_queue_size),
_back_thread(&async_sink::_thread_loop, this) _back_thread(&async_sink::_thread_loop, this)
{} {}
template<class Mutex> inline c11log::sinks::async_sink::~async_sink()
inline c11log::sinks::async_sink<Mutex>::~async_sink()
{ {
_shutdown(); _shutdown();
} }
template<class Mutex> inline void c11log::sinks::async_sink::_sink_it(const details::log_msg& msg)
inline void c11log::sinks::async_sink<Mutex>::_sink_it(const details::log_msg& msg)
{ {
if(!_active || msg.formatted.empty()) if(!_active || msg.formatted.empty())
return; return;
_q.push(msg); _q.push(msg);
} }
template<class Mutex> inline void c11log::sinks::async_sink::_thread_loop()
inline void c11log::sinks::async_sink<Mutex>::_thread_loop()
{ {
static std::chrono::seconds pop_timeout { 1 }; static std::chrono::seconds pop_timeout { 1 };
while (_active) while (_active)
@ -95,27 +90,27 @@ inline void c11log::sinks::async_sink<Mutex>::_thread_loop()
} }
} }
template<class Mutex> inline void c11log::sinks::async_sink::add_sink(logger::sink_ptr sink)
inline void c11log::sinks::async_sink<Mutex>::add_sink(logger::sink_ptr sink)
{ {
std::lock_guard<std::mutex> guard(_mutex);
_sinks.push_back(sink); _sinks.push_back(sink);
} }
template<class Mutex>
inline void c11log::sinks::async_sink<Mutex>::remove_sink(logger::sink_ptr sink) inline void c11log::sinks::async_sink::remove_sink(logger::sink_ptr sink)
{ {
std::lock_guard<std::mutex> guard(_mutex);
_sinks.erase(std::remove(_sinks.begin(), _sinks.end(), sink), _sinks.end()); _sinks.erase(std::remove(_sinks.begin(), _sinks.end(), sink), _sinks.end());
} }
/*
template<class Mutex>
inline c11log::sinks::async_sink::q_type& c11log::sinks::async_sink<Mutex>::q() inline c11log::sinks::async_sink::q_type& c11log::sinks::async_sink::q()
{ {
return _q; return _q;
}*/ }
template<class Mutex> inline void c11log::sinks::async_sink::shutdown(const std::chrono::milliseconds& timeout)
inline void c11log::sinks::async_sink<Mutex>::shutdown(const std::chrono::milliseconds& timeout)
{ {
if(timeout > std::chrono::milliseconds::zero()) if(timeout > std::chrono::milliseconds::zero())
{ {
@ -129,10 +124,9 @@ inline void c11log::sinks::async_sink<Mutex>::shutdown(const std::chrono::millis
} }
template<class Mutex> inline void c11log::sinks::async_sink::_shutdown()
inline void c11log::sinks::async_sink<Mutex>::_shutdown()
{ {
std::lock_guard<std::mutex> guard(_shutdown_mutex); std::lock_guard<std::mutex> guard(_mutex);
if(_active) if(_active)
{ {
_active = false; _active = false;

View File

@ -5,7 +5,7 @@
#include<atomic> #include<atomic>
#include "isink.h" #include "isink.h"
#include "../formatter.h" #include "../formatter.h"
#include "../common_types.h" #include "../common.h"
#include "../details/log_msg.h" #include "../details/log_msg.h"
@ -23,7 +23,7 @@ public:
base_sink(const base_sink&) = delete; base_sink(const base_sink&) = delete;
base_sink& operator=(const base_sink&) = delete; base_sink& operator=(const base_sink&) = delete;
void log(const details::log_msg& msg) void log(const details::log_msg& msg) override
{ {
if (_enabled) if (_enabled)
{ {
@ -32,12 +32,12 @@ public:
} }
}; };
void enable(bool enabled) void enable(bool enabled) override
{ {
_enabled = enabled; _enabled = enabled;
} }
bool is_enabled() bool is_enabled() override
{ {
return _enabled.load(); return _enabled.load();
} }
@ -48,8 +48,5 @@ protected:
std::atomic<bool> _enabled; std::atomic<bool> _enabled;
}; };
} }
} }

View File

@ -87,12 +87,10 @@ private:
} }
// Rotate old files: // Rotate files:
// log.txt -> log.1.txt // log.txt -> log.1.txt
// log.n-1.txt -> log.n.txt // ..
// log.n-2.txt -> log.n-1.txt // log.n.txt -> log.n+1.txt
// log.n-3.txt ->..
// log.n.txt -> log.txt
void _rotate() void _rotate()

View File

@ -26,7 +26,6 @@ protected:
{ {
_ostream << msg.formatted; _ostream << msg.formatted;
} }
std::ostream& _ostream; std::ostream& _ostream;
}; };