Removed fast_oss in favour of simple ostringsream

This commit is contained in:
gabi 2014-03-07 00:06:34 +02:00
parent 5f4bc3086a
commit bf75bfd9fb
5 changed files with 53 additions and 47 deletions

View File

@ -2,7 +2,6 @@
//
#include <string>
#include <functional>
#include <iomanip>
#include "c11log/logger.h"
#include "c11log/sinks/async_sink.h"
#include "c11log/sinks/file_sinks.h"
@ -60,10 +59,12 @@ int main(int argc, char* argv[])
auto null_sink = std::make_shared<sinks::null_sink>();
//auto async = std::make_shared<sinks::async_sink>(1000);
//async->add_sink(fsink);
my_logger.add_sink(fsink);
my_logger.add_sink(null_sink);
auto start = system_clock::now();
const unsigned int howmany = 10000000;
const unsigned int howmany = 5000000;
for(unsigned int i = 0; i < howmany ; i++)
my_logger.info() << "Hello logger " << i;

View File

@ -10,12 +10,15 @@ public:
str_devicebuf() = default;
~str_devicebuf() = default;
str_devicebuf(const str_devicebuf& other):std::streambuf(),_str(other._str) {}
str_devicebuf& operator=(const str_devicebuf other) {
if(this != &other)
_str = other._str;
return *this;
str_devicebuf(str_devicebuf&& other) :std::streambuf(), _str(std::move(other._str)) {
other._str.clear();
}
str_devicebuf& operator=(const str_devicebuf&) = delete;
str_devicebuf& operator=(str_devicebuf&&) = delete;
const std::string& str_ref() const {
return _str;
std::ostringstream oss;
@ -50,12 +53,11 @@ public:
~fast_oss() = default;
fast_oss(const fast_oss& other) :std::basic_ios<char>(), std::ostream(&_dev), _dev(other._dev) {}
fast_oss& operator=(const fast_oss& other) {
if(&other != this)
_dev = other._dev;
return *this;
}
fast_oss(fast_oss&& other) :std::basic_ios<char>(), std::ostream(&_dev), _dev(std::move(other._dev)) {}
fast_oss& operator=(const fast_oss& other) = delete;
const std::string& str_ref() const {
return _dev.str_ref();

View File

@ -2,7 +2,7 @@
#include "../common_types.h"
#include "../logger.h"
#include "fast_oss.h"
#include <iostream>
namespace c11log {
class logger;
@ -10,41 +10,49 @@ namespace details {
class line_logger {
public:
line_logger(logger* callback_logger, level::level_enum msg_level):
line_logger(logger* callback_logger, level::level_enum msg_level, bool enabled):
_callback_logger(callback_logger),
_oss(),
_level(msg_level) {
_level(msg_level),
_enabled(enabled) {
callback_logger->_formatter->format_header(callback_logger->_logger_name,
msg_level,
log_clock::now(),
_oss);
}
line_logger(logger*):_callback_logger(nullptr) {};
line_logger(const line_logger& other):
// No copy intended. Only move
line_logger(const line_logger& other) = delete;
line_logger(line_logger&& other) :
_callback_logger(other._callback_logger),
_oss(other._oss),
_level(other._level) {};
_oss(std::move(other._oss)),
_level(other._level) {
};
line_logger& operator=(const line_logger&) = delete;
line_logger& operator=(line_logger&&) = delete;
~line_logger() {
if (_callback_logger) {
if (_enabled) {
_oss << '\n';
_callback_logger->_log_it(_oss.str_ref());
_callback_logger->_log_it(_oss.str(), _level);
}
}
template<typename T>
line_logger& operator<<(const T& msg) {
if (_callback_logger)
if (_enabled)
_oss << msg;
return *this;
}
private:
logger* _callback_logger;
details::fast_oss _oss;
std::ostringstream _oss;
level::level_enum _level;
bool _enabled;
};
} //Namespace details

View File

@ -49,29 +49,29 @@ inline void c11log::formatters::default_formatter::_format_time(const log_clock:
__declspec(thread) static std::tm last_tm = { 0, 0, 0, 0, 0, 0, 0, 0, 0};
__declspec(thread) static char last_time_str[64];
#else
thread_local static std::tm last_tm = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
thread_local static char last_time_str[64];
thread_local static std::tm last_tm = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
thread_local static char last_time_str[64];
#endif
auto tm_now = details::os::localtime(log_clock::to_time_t(tp));
using namespace c11log::details::os;
using namespace c11log::details::os;
if(last_tm != tm_now)
{
#ifdef _MSC_VER
::sprintf_s
#else
::snprintf
::sprintf_s
#else
::snprintf
#endif
(last_time_str, sizeof(last_time_str), "[%d-%02d-%02d %02d:%02d:%02d]",
tm_now.tm_year + 1900,
tm_now.tm_mon + 1,
tm_now.tm_mday,
tm_now.tm_hour,
tm_now.tm_min,
tm_now.tm_sec);
(last_time_str, sizeof(last_time_str), "[%d-%02d-%02d %02d:%02d:%02d]",
tm_now.tm_year + 1900,
tm_now.tm_mon + 1,
tm_now.tm_mday,
tm_now.tm_hour,
tm_now.tm_min,
tm_now.tm_sec);
last_tm = tm_now;
}
dest << last_time_str;
}

View File

@ -66,7 +66,7 @@ private:
std::mutex _mutex;
std::atomic_int _atomic_level;
void _log_it(const std::string& msg);
void _log_it(const std::string& msg, const level::level_enum level);
};
@ -85,11 +85,7 @@ logger& get_logger(const std::string& name);
#include "details/line_logger.h"
inline c11log::details::line_logger c11log::logger::log(c11log::level::level_enum msg_level)
{
if (msg_level >= _atomic_level)
return details::line_logger(this, msg_level);
else
return details::line_logger(nullptr);
return details::line_logger(this, msg_level, msg_level >= _atomic_level);
}
inline c11log::details::line_logger c11log::logger::debug()
@ -157,9 +153,8 @@ inline bool c11log::logger::should_log(c11log::level::level_enum level) const
{
return level >= _atomic_level.load();
}
inline void c11log::logger::_log_it(const std::string& msg)
inline void c11log::logger::_log_it(const std::string& msg, const level::level_enum level)
{
level::level_enum level = static_cast<level::level_enum>(_atomic_level.load());
std::lock_guard<std::mutex> lock(_mutex);
for (auto &sink : _sinks)
sink->log(msg, level);