This commit is contained in:
gabime 2014-03-31 02:31:26 +03:00
parent 4696132cd5
commit bde2d24abb
6 changed files with 83 additions and 85 deletions

View File

@ -21,24 +21,24 @@ int main(int argc, char* argv[])
const unsigned int howmany = argc <= 1 ? 1000000:atoi(argv[1]);
logger cout_logger ("", sinks::stdout_sink());
cout_logger.set_min_level(c11log::level::TRACE);
cout_logger.set_min_level(c11log::level::TRACE);
cout_logger.info() << "Hello " << "man" << 123;
cout_logger.trace("This is very nice! ") << "Yes gabi.." << ":)";
cout_logger.trace("This is very nice! ") << "Yes gabi.." << ":)";
auto fsink = std::make_shared<sinks::rotating_file_sink>("log", "txt", 1024*1024*50 , 5, 0);
auto nullsink = sinks::null_sink::get();
auto fsink = std::make_shared<sinks::rotating_file_sink>("log", "txt", 1024*1024*50 , 5, 0);
auto nullsink = sinks::null_sink::get();
//auto as = std::make_shared<sinks::async_sink>(1000);
//as->add_sink(sinks::null_sink::get());
//as->add_sink(sinks::null_sink::get());
logger my_logger ("my_logger", nullsink);
logger my_logger ("my_logger", nullsink);
auto start = system_clock::now();
for(unsigned int i = 1; i <= howmany ; ++i)
my_logger.info("Hello logger: ");
//auto s = howmany - as->q().size();
auto s = howmany;
//auto s = howmany - as->q().size();
auto s = howmany;
auto delta = system_clock::now() - start;
auto delta_d = duration_cast<duration<double>> (delta).count();

View File

@ -12,7 +12,7 @@ namespace level
{
typedef enum
{
TRACE,
TRACE,
DEBUG,
INFO,
WARNING,

View File

@ -20,7 +20,7 @@ class blocking_queue
{
public:
using queue_type = std::queue<T>;
using item_type = T;
using item_type = T;
using size_type = typename queue_type::size_type;
using clock = std::chrono::system_clock;

View File

@ -21,7 +21,7 @@ public:
_log_msg(msg_level),
_oss(),
_enabled(enabled),
_empty(true)
_empty(true)
{
if(enabled)
{
@ -49,7 +49,7 @@ public:
~line_logger()
{
//only if enabled and not empty
//only if enabled and not empty
if (!_empty)
{
_oss << os::eol();
@ -58,14 +58,14 @@ public:
}
}
template<typename T>
template<typename T>
void write(const T& what)
{
if (_enabled)
{
{
_oss << what;
_empty = false;
}
_empty = false;
}
}
template<typename T>
@ -82,7 +82,7 @@ private:
log_msg _log_msg;
details::stack_oss _oss;
bool _enabled;
bool _empty;
bool _empty;
};
} //Namespace details
} // Namespace c11log

View File

@ -23,7 +23,6 @@ class line_logger;
template<std::size_t> class fast_buf;
}
class logger
{
public:
@ -50,22 +49,21 @@ public:
const std::string& get_name() const;
bool should_log(c11log::level::level_enum) const;
template<typename T> details::line_logger trace(const T&);
template<typename T> details::line_logger debug(const T&);
template<typename T> details::line_logger info(const T&);
template<typename T> details::line_logger warn(const T&);
template<typename T> details::line_logger error(const T&);
template<typename T> details::line_logger critical(const T&);
template<typename T> details::line_logger fatal(const T&);
template<typename T> details::line_logger trace(const T&);
template<typename T> details::line_logger debug(const T&);
template<typename T> details::line_logger info(const T&);
template<typename T> details::line_logger warn(const T&);
template<typename T> details::line_logger error(const T&);
template<typename T> details::line_logger critical(const T&);
template<typename T> details::line_logger fatal(const T&);
details::line_logger trace();
details::line_logger debug();
details::line_logger info();
details::line_logger warn();
details::line_logger error();
details::line_logger critical();
details::line_logger fatal();
details::line_logger trace();
details::line_logger debug();
details::line_logger info();
details::line_logger warn();
details::line_logger error();
details::line_logger critical();
details::line_logger fatal();
private:
@ -112,80 +110,80 @@ inline c11log::logger::logger(const std::string& name, sink_ptr sink, formatter_
template<typename T>
inline c11log::details::line_logger c11log::logger::trace(const T& msg)
{
details::line_logger l(this, level::TRACE, should_log(level::TRACE));
l.write(msg);
return l;
details::line_logger l(this, level::TRACE, should_log(level::TRACE));
l.write(msg);
return l;
}
template<typename T>
inline c11log::details::line_logger c11log::logger::debug(const T& msg)
{
details::line_logger l(this, level::DEBUG, should_log(level::DEBUG));
l.write(msg);
return l;
details::line_logger l(this, level::DEBUG, should_log(level::DEBUG));
l.write(msg);
return l;
}
template<typename T>
inline c11log::details::line_logger c11log::logger::info(const T& msg)
{
details::line_logger l(this, level::INFO, should_log(level::INFO));
l.write(msg);
return l;
details::line_logger l(this, level::INFO, should_log(level::INFO));
l.write(msg);
return l;
}
template<typename T>
inline c11log::details::line_logger c11log::logger::warn(const T& msg)
{
details::line_logger l(this, level::WARNING, should_log(level::WARNING));
l.write(msg);
return l;
details::line_logger l(this, level::WARNING, should_log(level::WARNING));
l.write(msg);
return l;
}
template<typename T>
inline c11log::details::line_logger c11log::logger::critical(const T& msg)
{
details::line_logger l(this, level::CRITICAL, should_log(level::CRITICAL));
l.write(msg);
return l;
details::line_logger l(this, level::CRITICAL, should_log(level::CRITICAL));
l.write(msg);
return l;
}
template<typename T>
inline c11log::details::line_logger c11log::logger::fatal(const T& msg)
{
details::line_logger l(this, level::FATAL, should_log(level::FATAL));
l.write(msg);
return l;
details::line_logger l(this, level::FATAL, should_log(level::FATAL));
l.write(msg);
return l;
}
inline c11log::details::line_logger c11log::logger::trace()
{
return details::line_logger(this, level::TRACE, should_log(level::TRACE));
return details::line_logger(this, level::TRACE, should_log(level::TRACE));
}
inline c11log::details::line_logger c11log::logger::debug()
{
return details::line_logger(this, level::DEBUG, should_log(level::DEBUG));
return details::line_logger(this, level::DEBUG, should_log(level::DEBUG));
}
inline c11log::details::line_logger c11log::logger::info()
{
return details::line_logger(this, level::INFO, should_log(level::INFO));
return details::line_logger(this, level::INFO, should_log(level::INFO));
}
inline c11log::details::line_logger c11log::logger::warn()
{
return details::line_logger(this, level::WARNING, should_log(level::WARNING));
return details::line_logger(this, level::WARNING, should_log(level::WARNING));
}
inline c11log::details::line_logger c11log::logger::critical()
{
return details::line_logger(this, level::CRITICAL, should_log(level::CRITICAL));
return details::line_logger(this, level::CRITICAL, should_log(level::CRITICAL));
}
inline c11log::details::line_logger c11log::logger::fatal()
{
return details::line_logger(this, level::FATAL, should_log(level::FATAL));
return details::line_logger(this, level::FATAL, should_log(level::FATAL));
}

View File

@ -20,8 +20,8 @@ namespace sinks
static void msg_deleter(details::log_msg* msg_to_delete)
{
delete []msg_to_delete->msg_buf.first;
delete msg_to_delete;
delete []msg_to_delete->msg_buf.first;
delete msg_to_delete;
}
class async_sink : public base_sink
@ -30,15 +30,15 @@ public:
using queue_type = c11log::details::blocking_queue<std::unique_ptr<details::log_msg, std::function<void(details::log_msg*)>>>;
explicit async_sink(const queue_type::size_type max_queue_size);
//Stop logging and join the back thread
// TODO: limit with timeout of the join and kill it afterwards?
//Stop logging and join the back thread
// TODO: limit with timeout of the join and kill it afterwards?
~async_sink();
void add_sink(logger::sink_ptr sink);
void remove_sink(logger::sink_ptr sink_ptr);
queue_type& q();
queue_type& q();
//Wait to remaining items (if any) in the queue to be written and shutdown
void shutdown(const std::chrono::milliseconds& timeout);
@ -54,7 +54,7 @@ private:
std::thread _back_thread;
//Clear all remaining messages(if any), stop the _back_thread and join it
void _shutdown();
std::mutex _shutdown_mutex;
std::mutex _shutdown_mutex;
};
}
}
@ -78,17 +78,17 @@ inline c11log::sinks::async_sink::~async_sink()
inline void c11log::sinks::async_sink::_sink_it(const details::log_msg& msg)
{
auto msg_size = msg.msg_buf.second;
if(!_active || !msg_size)
return;
auto msg_size = msg.msg_buf.second;
if(!_active || !msg_size)
return;
//re allocate on the heap the (stack based) message
auto new_msg = new details::log_msg(msg);
char *buf = new char[msg_size];
std::memcpy(buf, msg.msg_buf.first, msg_size);
new_msg->msg_buf = bufpair_t(buf, msg_size);
// Create unique_ptr with custom deleter and push it
queue_type::item_type new_shared_msg(new_msg, msg_deleter);
char *buf = new char[msg_size];
std::memcpy(buf, msg.msg_buf.first, msg_size);
new_msg->msg_buf = bufpair_t(buf, msg_size);
// Create unique_ptr with custom deleter and push it
queue_type::item_type new_shared_msg(new_msg, msg_deleter);
_q.push(std::move(new_shared_msg));
}
@ -103,8 +103,8 @@ inline void c11log::sinks::async_sink::_thread_loop()
for (auto &sink : _sinks)
{
sink->log(*msg);
if(!_active)
break;
if(!_active)
break;
}
}
}
@ -122,31 +122,31 @@ inline void c11log::sinks::async_sink::remove_sink(logger::sink_ptr sink_ptr)
inline c11log::sinks::async_sink::queue_type& c11log::sinks::async_sink::q()
{
return _q;
return _q;
}
inline void c11log::sinks::async_sink::shutdown(const std::chrono::milliseconds& timeout)
{
if(timeout > std::chrono::milliseconds::zero())
{
auto until = log_clock::now() + timeout;
while (_q.size() > 0 && log_clock::now() < until)
{
std::this_thread::sleep_for(std::chrono::milliseconds(2));
}
}
if(timeout > std::chrono::milliseconds::zero())
{
auto until = log_clock::now() + timeout;
while (_q.size() > 0 && log_clock::now() < until)
{
std::this_thread::sleep_for(std::chrono::milliseconds(2));
}
}
_shutdown();
}
inline void c11log::sinks::async_sink::_shutdown()
{
std::lock_guard<std::mutex> guard(_shutdown_mutex);
std::lock_guard<std::mutex> guard(_shutdown_mutex);
if(_active)
{
_active = false;
if (_back_thread.joinable())
_back_thread.join();
_back_thread.join();
}
}