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

View File

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

View File

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

View File

@ -66,7 +66,7 @@ private:
std::mutex _mutex; std::mutex _mutex;
std::atomic_int _atomic_level; 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" #include "details/line_logger.h"
inline c11log::details::line_logger c11log::logger::log(c11log::level::level_enum msg_level) inline c11log::details::line_logger c11log::logger::log(c11log::level::level_enum msg_level)
{ {
return details::line_logger(this, msg_level, msg_level >= _atomic_level);
if (msg_level >= _atomic_level)
return details::line_logger(this, msg_level);
else
return details::line_logger(nullptr);
} }
inline c11log::details::line_logger c11log::logger::debug() 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(); 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); std::lock_guard<std::mutex> lock(_mutex);
for (auto &sink : _sinks) for (auto &sink : _sinks)
sink->log(msg, level); sink->log(msg, level);