Commit 650486a0 authored by gabi's avatar gabi

removed the use of str() in when building the log to improve perf even more

parent 41db9955
...@@ -17,16 +17,16 @@ using namespace utils; ...@@ -17,16 +17,16 @@ using namespace utils;
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
const unsigned int howmany = argc <= 1 ? 1000000 : atoi(argv[1]); const unsigned int howmany = argc <= 1 ? 500000 : atoi(argv[1]);
//std::string pattern = "%Y:%m:%d %H:%M:%S.%e ---> [%n:%l] %t"; //std::string pattern = "%Y:%m:%d %H:%M:%S.%e ---> [%n:%l] %t";
//auto formatter = std::make_shared<details::pattern_formatter>(pattern); //auto formatter = std::make_shared<details::pattern_formatter>(pattern);
logger cout_logger("bench", { std::make_shared<sinks::stderr_sink_mt>() }); logger cout_logger("bench", { std::make_shared<sinks::stderr_sink_mt>() });
cout_logger.info() << "Hello logger " << std::setw(10) << std::setfill('x') << 1234.55; cout_logger.info() << "Hello logger " << 1234;
auto nullsink = std::make_shared<sinks::null_sink_st>(); auto nullsink = std::make_shared<sinks::null_sink_st>();
auto rotating = std::make_shared<sinks::rotating_file_sink_mt>("myrotating", "txt", 1024 * 1024 * 5, 5, 100); //auto rotating = std::make_shared<sinks::rotating_file_sink_mt>("myrotating", "txt", 1024 * 1024 * 5, 5, 100);
logger my_logger("my_logger", { nullsink }); logger my_logger("my_logger", { nullsink });
......
...@@ -34,11 +34,10 @@ public: ...@@ -34,11 +34,10 @@ public:
other.clear(); other.clear();
} }
stackbuf_t::bufpair_t buf() const const stackbuf_t& buf() const
{ {
return _stackbuf.get(); return _stackbuf;
} }
std::size_t size() const std::size_t size() const
{ {
return _stackbuf.size(); return _stackbuf.size();
...@@ -91,7 +90,13 @@ public: ...@@ -91,7 +90,13 @@ public:
std::string str() std::string str()
{ {
auto buf = _dev.buf(); auto buf = _dev.buf();
return std::string(buf.first, buf.second); const char*data = buf.data();
return std::string(data, data+buf.size());
}
const stack_devicebuf::stackbuf_t& buf() const
{
return _dev.buf();
} }
...@@ -111,22 +116,27 @@ public: ...@@ -111,22 +116,27 @@ public:
void putc(char c) void putc(char c)
{ {
_dev.sputc(c); _dev.sputc(c);
this->width(4);
} }
// put int and pad with zeroes if smalled than min_width // put int and pad with zeroes if smalled than min_width
void put_int(int n, int padding) void write_int(int n, int padding)
{ {
std::string s; std::string s;
details::fast_itostr(n, s, padding); details::fast_itostr(n, s, padding);
_dev.sputn(s.data(), s.size()); _dev.sputn(s.data(), s.size());
} }
void put_str(const std::string& s) void write_str(const std::string& s)
{ {
_dev.sputn(s.data(), s.size()); _dev.sputn(s.data(), s.size());
} }
void write_fast_oss(const fast_oss& oss)
{
auto buf = oss.buf();
_dev.sputn(buf.data(), buf.size());
}
private: private:
stack_devicebuf _dev; stack_devicebuf _dev;
......
...@@ -16,9 +16,10 @@ public: ...@@ -16,9 +16,10 @@ public:
file_flush_helper(const file_flush_helper&) = delete; file_flush_helper(const file_flush_helper&) = delete;
void write(const std::string& msg, std::ofstream& ofs) void write(const log_msg msg, std::ofstream& ofs)
{ {
ofs.write(msg.data(), msg.size()); auto buf = msg.formatted.buf();
ofs.write(buf.data(), buf.size());
if(--_flush_countdown == 0) if(--_flush_countdown == 0)
{ {
ofs.flush(); ofs.flush();
......
...@@ -19,7 +19,6 @@ public: ...@@ -19,7 +19,6 @@ public:
line_logger(logger* callback_logger, level::level_enum msg_level, bool enabled): line_logger(logger* callback_logger, level::level_enum msg_level, bool enabled):
_callback_logger(callback_logger), _callback_logger(callback_logger),
_log_msg(msg_level), _log_msg(msg_level),
_oss(),
_enabled(enabled) _enabled(enabled)
{} {}
...@@ -32,7 +31,6 @@ public: ...@@ -32,7 +31,6 @@ public:
line_logger(line_logger&& other) : line_logger(line_logger&& other) :
_callback_logger(other._callback_logger), _callback_logger(other._callback_logger),
_log_msg(std::move(other._log_msg)), _log_msg(std::move(other._log_msg)),
_oss(std::move(other._oss)),
_enabled(other._enabled) _enabled(other._enabled)
{ {
other.disable(); other.disable();
...@@ -46,7 +44,6 @@ public: ...@@ -46,7 +44,6 @@ public:
_log_msg.logger_name = _callback_logger->name(); _log_msg.logger_name = _callback_logger->name();
_log_msg.time = log_clock::now(); _log_msg.time = log_clock::now();
_log_msg.tm_time = details::os::localtime(log_clock::to_time_t(_log_msg.time)); _log_msg.tm_time = details::os::localtime(log_clock::to_time_t(_log_msg.time));
_log_msg.raw = _oss.str();
_callback_logger->_log_msg(_log_msg); _callback_logger->_log_msg(_log_msg);
} }
} }
...@@ -56,7 +53,7 @@ public: ...@@ -56,7 +53,7 @@ public:
{ {
if (_enabled) if (_enabled)
{ {
_oss << what; _log_msg.raw << what;
} }
} }
...@@ -77,7 +74,6 @@ public: ...@@ -77,7 +74,6 @@ public:
private: private:
logger* _callback_logger; logger* _callback_logger;
log_msg _log_msg; log_msg _log_msg;
details::fast_oss _oss;
bool _enabled; bool _enabled;
}; };
} //Namespace details } //Namespace details
......
#pragma once #pragma once
#include <chrono>
#include "../common.h" #include "../common.h"
#include "fast_oss.h"
namespace c11log namespace c11log
{ {
...@@ -38,8 +38,8 @@ struct log_msg ...@@ -38,8 +38,8 @@ struct log_msg
swap(l.level, r.level); swap(l.level, r.level);
swap(l.time, r.time); swap(l.time, r.time);
swap(l.tm_time, r.tm_time); swap(l.tm_time, r.tm_time);
swap(l.raw, r.raw); //swap(l.raw, r.raw);
swap(l.formatted, r.formatted); //swap(l.formatted, r.formatted);
} }
...@@ -60,8 +60,8 @@ struct log_msg ...@@ -60,8 +60,8 @@ struct log_msg
level::level_enum level; level::level_enum level;
log_clock::time_point time; log_clock::time_point time;
std::tm tm_time; std::tm tm_time;
std::string raw; fast_oss raw;
std::string formatted; fast_oss formatted;
}; };
......
This diff is collapsed.
...@@ -15,8 +15,6 @@ template<unsigned short STACK_SIZE> ...@@ -15,8 +15,6 @@ template<unsigned short STACK_SIZE>
class stack_buf class stack_buf
{ {
public: public:
using bufpair_t = std::pair<const char*, std::size_t>;
using iterator = char const*;
static const unsigned short stack_size = STACK_SIZE; static const unsigned short stack_size = STACK_SIZE;
stack_buf() :_v(), _stack_size(0) {} stack_buf() :_v(), _stack_size(0) {}
~stack_buf() = default; ~stack_buf() = default;
...@@ -63,28 +61,28 @@ public: ...@@ -63,28 +61,28 @@ public:
_v.clear(); _v.clear();
} }
bufpair_t get() const /* bufpair_t get() const
{
if (vector_used())
return bufpair_t(_v.data(), _v.size());
else
return bufpair_t(_stack_array.data(), _stack_size);
}*/
const char* data() const
{ {
if (vector_used()) if (vector_used())
return bufpair_t(_v.data(), _v.size()); return _v.data();
else else
return bufpair_t(_stack_array.data(), _stack_size); return _stack_array.data();
}
iterator begin() const
{
return get().first;
}
iterator end() const
{
bufpair_t bpair = get();
return bpair.first + bpair.second;
} }
std::size_t size() const std::size_t size() const
{ {
return get().second; if (vector_used())
return _v.size();
else
return _stack_size;
} }
private: private:
......
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
// Upon each log write the logger: // Upon each log write the logger:
// 1. Checks if its log level is enough to log the message // 1. Checks if its log level is enough to log the message
// 2. Format the message using the formatter function // 2. Format the message using the formatter function
// 3. Pass the formatted message to it sinks to performa the actual logging // 3. Pass the formatted message to its sinks to performa the actual logging
#include<vector> #include<vector>
#include<memory> #include<memory>
...@@ -68,8 +68,6 @@ private: ...@@ -68,8 +68,6 @@ private:
void _log_msg(details::log_msg& msg); void _log_msg(details::log_msg& msg);
formatter_ptr _default_formatter(); formatter_ptr _default_formatter();
const char* _default_pattern = "[%Y:%m:%d %H:%M:%S.%e] [%n:%l] %t"; const char* _default_pattern = "[%Y:%m:%d %H:%M:%S.%e] [%n:%l] %t";
}; };
} }
...@@ -183,7 +181,6 @@ inline c11log::logger::formatter_ptr c11log::logger::formatter() const ...@@ -183,7 +181,6 @@ inline c11log::logger::formatter_ptr c11log::logger::formatter() const
} }
inline void c11log::logger::_variadic_log(c11log::details::line_logger&) {} inline void c11log::logger::_variadic_log(c11log::details::line_logger&) {}
template <typename First, typename... Rest> template <typename First, typename... Rest>
......
...@@ -67,7 +67,7 @@ inline c11log::sinks::async_sink::~async_sink() ...@@ -67,7 +67,7 @@ inline c11log::sinks::async_sink::~async_sink()
inline void c11log::sinks::async_sink::_sink_it(const details::log_msg& msg) inline void c11log::sinks::async_sink::_sink_it(const details::log_msg& msg)
{ {
if(!_active || msg.formatted.empty()) if(!_active)
return; return;
_q.push(msg); _q.push(msg);
} }
......
...@@ -32,7 +32,7 @@ public: ...@@ -32,7 +32,7 @@ public:
protected: protected:
void _sink_it(const details::log_msg& msg) override void _sink_it(const details::log_msg& msg) override
{ {
_flush_helper.write(msg.formatted, _ofstream); _flush_helper.write(msg, _ofstream);
} }
private: private:
std::ofstream _ofstream; std::ofstream _ofstream;
...@@ -71,7 +71,7 @@ protected: ...@@ -71,7 +71,7 @@ protected:
_rotate(); _rotate();
_current_size = msg.formatted.size(); _current_size = msg.formatted.size();
} }
_flush_helper.write(msg.formatted, _ofstream); _flush_helper.write(msg, _ofstream);
} }
...@@ -146,7 +146,7 @@ protected: ...@@ -146,7 +146,7 @@ protected:
_ofstream.open(_calc_filename(_base_filename, _extension)); _ofstream.open(_calc_filename(_base_filename, _extension));
_midnight_tp = _calc_midnight_tp(); _midnight_tp = _calc_midnight_tp();
} }
_flush_helper.write(msg.formatted, _ofstream); _flush_helper.write(msg, _ofstream);
} }
private: private:
......
...@@ -24,7 +24,8 @@ public: ...@@ -24,7 +24,8 @@ public:
protected: protected:
virtual void _sink_it(const details::log_msg& msg) override virtual void _sink_it(const details::log_msg& msg) override
{ {
_ostream << msg.formatted; auto buf = msg.formatted.buf();
_ostream.write(buf.data(), buf.size());
} }
std::ostream& _ostream; std::ostream& _ostream;
}; };
......
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