2014-10-14 08:44:40 +08:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <chrono>
|
|
|
|
#include <memory>
|
|
|
|
#include <vector>
|
|
|
|
|
2014-10-14 09:29:52 +08:00
|
|
|
#include "../formatter.h"
|
|
|
|
#include "log_msg.h"
|
|
|
|
#include "fast_oss.h"
|
2014-10-14 13:00:39 +08:00
|
|
|
#include "os.h"
|
2014-10-14 08:44:40 +08:00
|
|
|
|
|
|
|
|
|
|
|
namespace c11log
|
|
|
|
{
|
|
|
|
namespace details {
|
2014-10-14 13:00:39 +08:00
|
|
|
class pattern_compiler
|
2014-10-14 08:44:40 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual void append(const details::log_msg& msg, details::fast_oss& oss) = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
// log name appender
|
2014-10-14 13:00:39 +08:00
|
|
|
class name_compiler :public pattern_compiler
|
2014-10-14 08:44:40 +08:00
|
|
|
{
|
|
|
|
void append(const details::log_msg& msg, details::fast_oss& oss) override
|
|
|
|
{
|
|
|
|
oss << msg.logger_name;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// log level appender
|
2014-10-14 13:00:39 +08:00
|
|
|
class level_compiler :public pattern_compiler
|
2014-10-14 08:44:40 +08:00
|
|
|
{
|
|
|
|
void append(const details::log_msg& msg, details::fast_oss& oss) override
|
|
|
|
{
|
|
|
|
oss << level::to_str(msg.level);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////
|
|
|
|
// Date time pattern appenders
|
|
|
|
///////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// year - 4 digit
|
2014-10-14 13:00:39 +08:00
|
|
|
class Y_compiler :public pattern_compiler
|
2014-10-14 08:44:40 +08:00
|
|
|
{
|
|
|
|
void append(const details::log_msg& msg, details::fast_oss& oss) override
|
|
|
|
{
|
2014-10-14 09:29:52 +08:00
|
|
|
oss.put_int(msg.tm_time.tm_year + 1900, 4);
|
2014-10-14 08:44:40 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// year - 2 digit
|
2014-10-14 13:00:39 +08:00
|
|
|
class y_compiler :public pattern_compiler
|
2014-10-14 08:44:40 +08:00
|
|
|
{
|
|
|
|
void append(const details::log_msg& msg, details::fast_oss& oss) override
|
|
|
|
{
|
|
|
|
oss.put_int(msg.tm_time.tm_year, 2);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// month 1-12
|
2014-10-14 13:00:39 +08:00
|
|
|
class m_compiler :public pattern_compiler
|
2014-10-14 08:44:40 +08:00
|
|
|
{
|
|
|
|
void append(const details::log_msg& msg, details::fast_oss& oss) override
|
|
|
|
{
|
|
|
|
oss.put_int(msg.tm_time.tm_mon + 1, 2);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// day of month 1-31
|
2014-10-14 13:00:39 +08:00
|
|
|
class d_compiler :public pattern_compiler
|
2014-10-14 08:44:40 +08:00
|
|
|
{
|
|
|
|
void append(const details::log_msg& msg, details::fast_oss& oss) override
|
|
|
|
{
|
|
|
|
oss.put_int(msg.tm_time.tm_mday, 2);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// hours in 24 format 0-23
|
2014-10-14 13:00:39 +08:00
|
|
|
class H_compiler :public pattern_compiler
|
2014-10-14 08:44:40 +08:00
|
|
|
{
|
|
|
|
void append(const details::log_msg& msg, details::fast_oss& oss) override
|
|
|
|
{
|
|
|
|
oss.put_int(msg.tm_time.tm_hour, 2);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// hours in 12 format 1-12
|
2014-10-14 13:00:39 +08:00
|
|
|
class I_compiler :public pattern_compiler
|
2014-10-14 08:44:40 +08:00
|
|
|
{
|
|
|
|
void append(const details::log_msg& msg, details::fast_oss& oss) override
|
|
|
|
{
|
|
|
|
oss.put_int((msg.tm_time.tm_hour + 1) % 1, 2);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// ninutes 0-59
|
2014-10-14 13:00:39 +08:00
|
|
|
class M_compiler :public pattern_compiler
|
2014-10-14 08:44:40 +08:00
|
|
|
{
|
|
|
|
void append(const details::log_msg& msg, details::fast_oss& oss) override
|
|
|
|
{
|
|
|
|
oss.put_int(msg.tm_time.tm_min, 2);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// seconds 0-59
|
2014-10-14 13:00:39 +08:00
|
|
|
class S_compiler :public pattern_compiler
|
2014-10-14 08:44:40 +08:00
|
|
|
{
|
|
|
|
void append(const details::log_msg& msg, details::fast_oss& oss) override
|
|
|
|
{
|
|
|
|
oss.put_int(msg.tm_time.tm_sec, 2);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// milliseconds
|
2014-10-14 13:00:39 +08:00
|
|
|
class e_compiler :public pattern_compiler
|
2014-10-14 08:44:40 +08:00
|
|
|
{
|
|
|
|
void append(const details::log_msg& msg, details::fast_oss& oss) override
|
|
|
|
{
|
|
|
|
auto duration = msg.time.time_since_epoch();
|
2014-10-14 09:00:56 +08:00
|
|
|
int millis = static_cast<int>(std::chrono::duration_cast<std::chrono::milliseconds>(duration).count() % 1000);
|
2014-10-14 08:44:40 +08:00
|
|
|
oss.put_int(millis, 3);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-10-14 13:00:39 +08:00
|
|
|
class t_compiler :public pattern_compiler
|
2014-10-14 08:44:40 +08:00
|
|
|
{
|
|
|
|
void append(const details::log_msg& msg, details::fast_oss& oss) override
|
|
|
|
{
|
|
|
|
oss.put_str(msg.raw);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-10-14 13:00:39 +08:00
|
|
|
class ch_compiler :public pattern_compiler
|
2014-10-14 08:44:40 +08:00
|
|
|
{
|
|
|
|
public:
|
2014-10-14 13:00:39 +08:00
|
|
|
explicit ch_compiler(char ch) : _ch(ch)
|
2014-10-14 08:44:40 +08:00
|
|
|
{}
|
2014-10-14 09:56:10 +08:00
|
|
|
void append(const details::log_msg&, details::fast_oss& oss) override
|
2014-10-14 08:44:40 +08:00
|
|
|
{
|
2014-10-14 09:29:52 +08:00
|
|
|
oss.putc(_ch);
|
2014-10-14 08:44:40 +08:00
|
|
|
}
|
|
|
|
private:
|
|
|
|
char _ch;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-10-14 13:00:39 +08:00
|
|
|
class str_compiler :public pattern_compiler
|
2014-10-14 08:44:40 +08:00
|
|
|
{
|
|
|
|
public:
|
2014-10-14 13:00:39 +08:00
|
|
|
str_compiler()
|
2014-10-14 08:44:40 +08:00
|
|
|
{}
|
|
|
|
void add_ch(char ch)
|
|
|
|
{
|
|
|
|
_str += ch;
|
|
|
|
}
|
2014-10-14 09:56:10 +08:00
|
|
|
void append(const details::log_msg&, details::fast_oss& oss) override
|
2014-10-14 08:44:40 +08:00
|
|
|
{
|
|
|
|
oss << _str;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
std::string _str;
|
|
|
|
};
|
2014-10-14 09:29:52 +08:00
|
|
|
|
2014-10-14 08:44:40 +08:00
|
|
|
|
|
|
|
|
|
|
|
class pattern_formatter : public formatter
|
|
|
|
{
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit pattern_formatter(const std::string& pattern);
|
|
|
|
pattern_formatter(const pattern_formatter&) = delete;
|
|
|
|
void format(details::log_msg& msg) override;
|
|
|
|
private:
|
|
|
|
const std::string _pattern;
|
2014-10-14 13:00:39 +08:00
|
|
|
std::vector<std::unique_ptr<details::pattern_compiler>> _compilers;
|
2014-10-14 08:44:40 +08:00
|
|
|
void handle_flag(char flag);
|
|
|
|
void compile_pattern(const std::string& pattern);
|
|
|
|
};
|
|
|
|
}
|
2014-10-14 09:29:52 +08:00
|
|
|
}
|
2014-10-14 08:44:40 +08:00
|
|
|
|
|
|
|
|
2014-10-14 09:29:52 +08:00
|
|
|
inline c11log::details::pattern_formatter::pattern_formatter(const std::string& pattern)
|
2014-10-14 08:44:40 +08:00
|
|
|
{
|
|
|
|
compile_pattern(pattern);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-10-14 09:29:52 +08:00
|
|
|
inline void c11log::details::pattern_formatter::compile_pattern(const std::string& pattern)
|
2014-10-14 08:44:40 +08:00
|
|
|
{
|
|
|
|
auto end = pattern.end();
|
|
|
|
for (auto it = pattern.begin(); it != end; ++it)
|
|
|
|
{
|
|
|
|
if (*it == '%')
|
|
|
|
{
|
|
|
|
if (++it != end)
|
|
|
|
handle_flag(*it);
|
|
|
|
else
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// chars not following the % sign should be displayed as is
|
2014-10-14 13:00:39 +08:00
|
|
|
_compilers.push_back(std::unique_ptr<details::pattern_compiler>(new details::ch_compiler(*it)));
|
2014-10-14 08:44:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2014-10-14 09:29:52 +08:00
|
|
|
inline void c11log::details::pattern_formatter::handle_flag(char flag)
|
2014-10-14 08:44:40 +08:00
|
|
|
{
|
|
|
|
switch (flag)
|
|
|
|
{
|
|
|
|
// logger name
|
|
|
|
case 'n':
|
2014-10-14 13:00:39 +08:00
|
|
|
_compilers.push_back(std::unique_ptr<details::pattern_compiler>(new details::name_compiler()));
|
2014-10-14 08:44:40 +08:00
|
|
|
break;
|
|
|
|
// message log level
|
|
|
|
case 'l':
|
2014-10-14 13:00:39 +08:00
|
|
|
_compilers.push_back(std::unique_ptr<details::pattern_compiler>(new details::level_compiler()));
|
2014-10-14 08:44:40 +08:00
|
|
|
break;
|
|
|
|
// message text
|
|
|
|
case('t') :
|
2014-10-14 13:00:39 +08:00
|
|
|
_compilers.push_back(std::unique_ptr<details::pattern_compiler>(new details::t_compiler()));
|
2014-10-14 08:44:40 +08:00
|
|
|
break;
|
|
|
|
// year
|
|
|
|
case('Y') :
|
2014-10-14 13:00:39 +08:00
|
|
|
_compilers.push_back(std::unique_ptr<details::pattern_compiler>(new details::Y_compiler()));
|
2014-10-14 08:44:40 +08:00
|
|
|
break;
|
|
|
|
// year 2 digits
|
|
|
|
case('y') :
|
2014-10-14 13:00:39 +08:00
|
|
|
_compilers.push_back(std::unique_ptr<details::pattern_compiler>(new details::y_compiler()));
|
2014-10-14 08:44:40 +08:00
|
|
|
break;
|
|
|
|
// month
|
|
|
|
case('m') :
|
|
|
|
// minute
|
2014-10-14 13:00:39 +08:00
|
|
|
_compilers.push_back(std::unique_ptr<details::pattern_compiler>(new details::m_compiler()));
|
2014-10-14 08:44:40 +08:00
|
|
|
break;
|
|
|
|
// day in month
|
|
|
|
case('d') :
|
2014-10-14 13:00:39 +08:00
|
|
|
_compilers.push_back(std::unique_ptr<details::pattern_compiler>(new details::d_compiler()));
|
2014-10-14 08:44:40 +08:00
|
|
|
break;
|
|
|
|
// hour (24)
|
|
|
|
case('H') :
|
2014-10-14 13:00:39 +08:00
|
|
|
_compilers.push_back(std::unique_ptr<details::pattern_compiler>(new details::H_compiler()));
|
2014-10-14 08:44:40 +08:00
|
|
|
break;
|
|
|
|
// hour (12)
|
|
|
|
case('I') :
|
2014-10-14 13:00:39 +08:00
|
|
|
_compilers.push_back(std::unique_ptr<details::pattern_compiler>(new details::I_compiler()));
|
2014-10-14 08:44:40 +08:00
|
|
|
break;
|
|
|
|
// minutes
|
|
|
|
case('M') :
|
2014-10-14 13:00:39 +08:00
|
|
|
_compilers.push_back(std::unique_ptr<details::pattern_compiler>(new details::M_compiler()));
|
2014-10-14 08:44:40 +08:00
|
|
|
break;
|
|
|
|
// seconds
|
|
|
|
case('S') :
|
2014-10-14 13:00:39 +08:00
|
|
|
_compilers.push_back(std::unique_ptr<details::pattern_compiler>(new details::S_compiler()));
|
2014-10-14 08:44:40 +08:00
|
|
|
break;
|
|
|
|
// milliseconds part
|
|
|
|
case('e'):
|
2014-10-14 13:00:39 +08:00
|
|
|
_compilers.push_back(std::unique_ptr<details::pattern_compiler>(new details::e_compiler()));
|
2014-10-14 08:44:40 +08:00
|
|
|
break;
|
|
|
|
// % sign
|
|
|
|
case('%') :
|
2014-10-14 13:00:39 +08:00
|
|
|
_compilers.push_back(std::unique_ptr<details::pattern_compiler>(new details::ch_compiler('%')));
|
2014-10-14 08:44:40 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-10-14 09:29:52 +08:00
|
|
|
inline void c11log::details::pattern_formatter::format(details::log_msg& msg)
|
2014-10-14 08:44:40 +08:00
|
|
|
{
|
|
|
|
details::fast_oss oss;
|
2014-10-14 13:00:39 +08:00
|
|
|
for (auto &appender : _compilers)
|
2014-10-14 08:44:40 +08:00
|
|
|
{
|
|
|
|
appender->append(msg, oss);
|
|
|
|
}
|
|
|
|
oss.write(details::os::eol(), details::os::eol_size());
|
|
|
|
msg.formatted = oss.str();
|
2014-10-14 09:56:10 +08:00
|
|
|
}
|