Commit bf75bfd9 authored by gabi's avatar gabi

Removed fast_oss in favour of simple ostringsream

parent 5f4bc308
...@@ -2,7 +2,6 @@ ...@@ -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[]) ...@@ -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;
......
...@@ -10,12 +10,15 @@ public: ...@@ -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;
...@@ -50,12 +53,11 @@ public: ...@@ -50,12 +53,11 @@ public:
~fast_oss() = default; ~fast_oss() = default;
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();
......
...@@ -2,7 +2,7 @@ ...@@ -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 { ...@@ -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
......
...@@ -49,29 +49,29 @@ inline void c11log::formatters::default_formatter::_format_time(const log_clock: ...@@ -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 std::tm last_tm = { 0, 0, 0, 0, 0, 0, 0, 0, 0};
__declspec(thread) static char last_time_str[64]; __declspec(thread) static char last_time_str[64];
#else #else
thread_local static std::tm last_tm = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; 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 char last_time_str[64];
#endif #endif
auto tm_now = details::os::localtime(log_clock::to_time_t(tp)); 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) if(last_tm != tm_now)
{ {
#ifdef _MSC_VER #ifdef _MSC_VER
::sprintf_s ::sprintf_s
#else #else
::snprintf ::snprintf
#endif #endif
(last_time_str, sizeof(last_time_str), "[%d-%02d-%02d %02d:%02d:%02d]", (last_time_str, sizeof(last_time_str), "[%d-%02d-%02d %02d:%02d:%02d]",
tm_now.tm_year + 1900, tm_now.tm_year + 1900,
tm_now.tm_mon + 1, tm_now.tm_mon + 1,
tm_now.tm_mday, tm_now.tm_mday,
tm_now.tm_hour, tm_now.tm_hour,
tm_now.tm_min, tm_now.tm_min,
tm_now.tm_sec); tm_now.tm_sec);
last_tm = tm_now; last_tm = tm_now;
} }
dest << last_time_str; dest << last_time_str;
} }
...@@ -66,7 +66,7 @@ private: ...@@ -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); ...@@ -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 ...@@ -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);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment