mirror of
https://github.com/gabime/spdlog.git
synced 2025-09-29 01:29:35 +08:00
removed the use of str() in when building the log to improve perf even more
This commit is contained in:
@@ -14,27 +14,27 @@
|
||||
namespace c11log
|
||||
{
|
||||
namespace details {
|
||||
class pattern_compiler
|
||||
class flag_formatter
|
||||
{
|
||||
public:
|
||||
virtual void append(const details::log_msg& msg, details::fast_oss& oss) = 0;
|
||||
virtual void format(details::log_msg& msg) = 0;
|
||||
};
|
||||
|
||||
// log name appender
|
||||
class name_compiler :public pattern_compiler
|
||||
class name_formatter :public flag_formatter
|
||||
{
|
||||
void append(const details::log_msg& msg, details::fast_oss& oss) override
|
||||
void format(details::log_msg& msg) override
|
||||
{
|
||||
oss << msg.logger_name;
|
||||
msg.formatted << msg.logger_name;
|
||||
}
|
||||
};
|
||||
|
||||
// log level appender
|
||||
class level_compiler :public pattern_compiler
|
||||
class level_formatter :public flag_formatter
|
||||
{
|
||||
void append(const details::log_msg& msg, details::fast_oss& oss) override
|
||||
void format(details::log_msg& msg) override
|
||||
{
|
||||
oss << level::to_str(msg.level);
|
||||
msg.formatted << level::to_str(msg.level);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -43,122 +43,122 @@ class level_compiler :public pattern_compiler
|
||||
///////////////////////////////////////////////////////////////////////
|
||||
|
||||
// year - 4 digit
|
||||
class Y_compiler :public pattern_compiler
|
||||
class Y_formatter :public flag_formatter
|
||||
{
|
||||
void append(const details::log_msg& msg, details::fast_oss& oss) override
|
||||
void format(details::log_msg& msg) override
|
||||
{
|
||||
oss.put_int(msg.tm_time.tm_year + 1900, 4);
|
||||
msg.formatted.write_int(msg.tm_time.tm_year + 1900, 4);
|
||||
}
|
||||
};
|
||||
|
||||
// year - 2 digit
|
||||
class y_compiler :public pattern_compiler
|
||||
class y_formatter :public flag_formatter
|
||||
{
|
||||
void append(const details::log_msg& msg, details::fast_oss& oss) override
|
||||
void format(details::log_msg& msg) override
|
||||
{
|
||||
oss.put_int(msg.tm_time.tm_year, 2);
|
||||
msg.formatted.write_int(msg.tm_time.tm_year, 2);
|
||||
}
|
||||
};
|
||||
// month 1-12
|
||||
class m_compiler :public pattern_compiler
|
||||
class m_formatter :public flag_formatter
|
||||
{
|
||||
void append(const details::log_msg& msg, details::fast_oss& oss) override
|
||||
void format(details::log_msg& msg) override
|
||||
{
|
||||
oss.put_int(msg.tm_time.tm_mon + 1, 2);
|
||||
msg.formatted.write_int(msg.tm_time.tm_mon + 1, 2);
|
||||
}
|
||||
};
|
||||
|
||||
// day of month 1-31
|
||||
class d_compiler :public pattern_compiler
|
||||
class d_formatter :public flag_formatter
|
||||
{
|
||||
void append(const details::log_msg& msg, details::fast_oss& oss) override
|
||||
void format(details::log_msg& msg) override
|
||||
{
|
||||
oss.put_int(msg.tm_time.tm_mday, 2);
|
||||
msg.formatted.write_int(msg.tm_time.tm_mday, 2);
|
||||
}
|
||||
};
|
||||
|
||||
// hours in 24 format 0-23
|
||||
class H_compiler :public pattern_compiler
|
||||
class H_formatter :public flag_formatter
|
||||
{
|
||||
void append(const details::log_msg& msg, details::fast_oss& oss) override
|
||||
void format(details::log_msg& msg) override
|
||||
{
|
||||
oss.put_int(msg.tm_time.tm_hour, 2);
|
||||
msg.formatted.write_int(msg.tm_time.tm_hour, 2);
|
||||
}
|
||||
};
|
||||
|
||||
// hours in 12 format 1-12
|
||||
class I_compiler :public pattern_compiler
|
||||
class I_formatter :public flag_formatter
|
||||
{
|
||||
void append(const details::log_msg& msg, details::fast_oss& oss) override
|
||||
void format(details::log_msg& msg) override
|
||||
{
|
||||
oss.put_int((msg.tm_time.tm_hour + 1) % 1, 2);
|
||||
msg.formatted.write_int((msg.tm_time.tm_hour + 1) % 1, 2);
|
||||
}
|
||||
};
|
||||
|
||||
// ninutes 0-59
|
||||
class M_compiler :public pattern_compiler
|
||||
class M_formatter :public flag_formatter
|
||||
{
|
||||
void append(const details::log_msg& msg, details::fast_oss& oss) override
|
||||
void format(details::log_msg& msg) override
|
||||
{
|
||||
oss.put_int(msg.tm_time.tm_min, 2);
|
||||
msg.formatted.write_int(msg.tm_time.tm_min, 2);
|
||||
}
|
||||
};
|
||||
|
||||
// seconds 0-59
|
||||
class S_compiler :public pattern_compiler
|
||||
class S_formatter :public flag_formatter
|
||||
{
|
||||
void append(const details::log_msg& msg, details::fast_oss& oss) override
|
||||
void format(details::log_msg& msg) override
|
||||
{
|
||||
oss.put_int(msg.tm_time.tm_sec, 2);
|
||||
msg.formatted.write_int(msg.tm_time.tm_sec, 2);
|
||||
}
|
||||
};
|
||||
|
||||
// milliseconds
|
||||
class e_compiler :public pattern_compiler
|
||||
class e_formatter :public flag_formatter
|
||||
{
|
||||
void append(const details::log_msg& msg, details::fast_oss& oss) override
|
||||
void format(details::log_msg& msg) override
|
||||
{
|
||||
auto duration = msg.time.time_since_epoch();
|
||||
int millis = static_cast<int>(std::chrono::duration_cast<std::chrono::milliseconds>(duration).count() % 1000);
|
||||
oss.put_int(millis, 3);
|
||||
auto millis = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count() % 1000;
|
||||
msg.formatted.write_int(static_cast<int>(millis), 3);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
class t_compiler :public pattern_compiler
|
||||
class t_formatter :public flag_formatter
|
||||
{
|
||||
void append(const details::log_msg& msg, details::fast_oss& oss) override
|
||||
void format(details::log_msg& msg) override
|
||||
{
|
||||
oss.put_str(msg.raw);
|
||||
msg.formatted.write_fast_oss(msg.raw);
|
||||
}
|
||||
};
|
||||
|
||||
class ch_compiler :public pattern_compiler
|
||||
class ch_formatter :public flag_formatter
|
||||
{
|
||||
public:
|
||||
explicit ch_compiler(char ch) : _ch(ch)
|
||||
explicit ch_formatter(char ch) : _ch(ch)
|
||||
{}
|
||||
void append(const details::log_msg&, details::fast_oss& oss) override
|
||||
void format(details::log_msg& msg) override
|
||||
{
|
||||
oss.putc(_ch);
|
||||
msg.formatted.putc(_ch);
|
||||
}
|
||||
private:
|
||||
char _ch;
|
||||
};
|
||||
|
||||
|
||||
class str_compiler :public pattern_compiler
|
||||
class str_formatter :public flag_formatter
|
||||
{
|
||||
public:
|
||||
str_compiler()
|
||||
str_formatter()
|
||||
{}
|
||||
void add_ch(char ch)
|
||||
{
|
||||
_str += ch;
|
||||
}
|
||||
void append(const details::log_msg&, details::fast_oss& oss) override
|
||||
void format(details::log_msg& msg) override
|
||||
{
|
||||
oss << _str;
|
||||
msg.formatted << _str;
|
||||
}
|
||||
private:
|
||||
std::string _str;
|
||||
@@ -175,7 +175,7 @@ public:
|
||||
void format(details::log_msg& msg) override;
|
||||
private:
|
||||
const std::string _pattern;
|
||||
std::vector<std::unique_ptr<details::pattern_compiler>> _compilers;
|
||||
std::vector<std::unique_ptr<details::flag_formatter>> _formatters;
|
||||
void handle_flag(char flag);
|
||||
void compile_pattern(const std::string& pattern);
|
||||
};
|
||||
@@ -204,7 +204,7 @@ inline void c11log::details::pattern_formatter::compile_pattern(const std::strin
|
||||
else
|
||||
{
|
||||
// chars not following the % sign should be displayed as is
|
||||
_compilers.push_back(std::unique_ptr<details::pattern_compiler>(new details::ch_compiler(*it)));
|
||||
_formatters.push_back(std::unique_ptr<details::flag_formatter>(new details::ch_formatter(*it)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -215,56 +215,56 @@ inline void c11log::details::pattern_formatter::handle_flag(char flag)
|
||||
{
|
||||
// logger name
|
||||
case 'n':
|
||||
_compilers.push_back(std::unique_ptr<details::pattern_compiler>(new details::name_compiler()));
|
||||
_formatters.push_back(std::unique_ptr<details::flag_formatter>(new details::name_formatter()));
|
||||
break;
|
||||
// message log level
|
||||
case 'l':
|
||||
_compilers.push_back(std::unique_ptr<details::pattern_compiler>(new details::level_compiler()));
|
||||
_formatters.push_back(std::unique_ptr<details::flag_formatter>(new details::level_formatter()));
|
||||
break;
|
||||
// message text
|
||||
case('t') :
|
||||
_compilers.push_back(std::unique_ptr<details::pattern_compiler>(new details::t_compiler()));
|
||||
_formatters.push_back(std::unique_ptr<details::flag_formatter>(new details::t_formatter()));
|
||||
break;
|
||||
// year
|
||||
case('Y') :
|
||||
_compilers.push_back(std::unique_ptr<details::pattern_compiler>(new details::Y_compiler()));
|
||||
_formatters.push_back(std::unique_ptr<details::flag_formatter>(new details::Y_formatter()));
|
||||
break;
|
||||
// year 2 digits
|
||||
case('y') :
|
||||
_compilers.push_back(std::unique_ptr<details::pattern_compiler>(new details::y_compiler()));
|
||||
_formatters.push_back(std::unique_ptr<details::flag_formatter>(new details::y_formatter()));
|
||||
break;
|
||||
// month
|
||||
case('m') :
|
||||
// minute
|
||||
_compilers.push_back(std::unique_ptr<details::pattern_compiler>(new details::m_compiler()));
|
||||
_formatters.push_back(std::unique_ptr<details::flag_formatter>(new details::m_formatter()));
|
||||
break;
|
||||
// day in month
|
||||
case('d') :
|
||||
_compilers.push_back(std::unique_ptr<details::pattern_compiler>(new details::d_compiler()));
|
||||
_formatters.push_back(std::unique_ptr<details::flag_formatter>(new details::d_formatter()));
|
||||
break;
|
||||
// hour (24)
|
||||
case('H') :
|
||||
_compilers.push_back(std::unique_ptr<details::pattern_compiler>(new details::H_compiler()));
|
||||
_formatters.push_back(std::unique_ptr<details::flag_formatter>(new details::H_formatter()));
|
||||
break;
|
||||
// hour (12)
|
||||
case('I') :
|
||||
_compilers.push_back(std::unique_ptr<details::pattern_compiler>(new details::I_compiler()));
|
||||
_formatters.push_back(std::unique_ptr<details::flag_formatter>(new details::I_formatter()));
|
||||
break;
|
||||
// minutes
|
||||
case('M') :
|
||||
_compilers.push_back(std::unique_ptr<details::pattern_compiler>(new details::M_compiler()));
|
||||
_formatters.push_back(std::unique_ptr<details::flag_formatter>(new details::M_formatter()));
|
||||
break;
|
||||
// seconds
|
||||
case('S') :
|
||||
_compilers.push_back(std::unique_ptr<details::pattern_compiler>(new details::S_compiler()));
|
||||
_formatters.push_back(std::unique_ptr<details::flag_formatter>(new details::S_formatter()));
|
||||
break;
|
||||
// milliseconds part
|
||||
case('e'):
|
||||
_compilers.push_back(std::unique_ptr<details::pattern_compiler>(new details::e_compiler()));
|
||||
_formatters.push_back(std::unique_ptr<details::flag_formatter>(new details::e_formatter()));
|
||||
break;
|
||||
// % sign
|
||||
case('%') :
|
||||
_compilers.push_back(std::unique_ptr<details::pattern_compiler>(new details::ch_compiler('%')));
|
||||
_formatters.push_back(std::unique_ptr<details::flag_formatter>(new details::ch_formatter('%')));
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -272,11 +272,10 @@ inline void c11log::details::pattern_formatter::handle_flag(char flag)
|
||||
|
||||
inline void c11log::details::pattern_formatter::format(details::log_msg& msg)
|
||||
{
|
||||
details::fast_oss oss;
|
||||
for (auto &appender : _compilers)
|
||||
for (auto &f : _formatters)
|
||||
{
|
||||
appender->append(msg, oss);
|
||||
f->format(msg);
|
||||
}
|
||||
oss.write(details::os::eol(), details::os::eol_size());
|
||||
msg.formatted = oss.str();
|
||||
//write eol
|
||||
msg.formatted.write(details::os::eol(), details::os::eol_size());
|
||||
}
|
||||
|
Reference in New Issue
Block a user