|
|
|
@ -2,14 +2,15 @@
|
|
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <chrono>
|
|
|
|
|
#include <functional>
|
|
|
|
|
|
|
|
|
|
#include <iomanip>
|
|
|
|
|
#include <thread>
|
|
|
|
|
#include <cstring>
|
|
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
|
|
#include "common_types.h"
|
|
|
|
|
#include "details/os.h"
|
|
|
|
|
#include "details/log_msg.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace c11log
|
|
|
|
@ -20,7 +21,7 @@ namespace formatters
|
|
|
|
|
class formatter
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
virtual void format_header(const std::string& logger_name, level::level_enum level, const log_clock::time_point& tp, std::ostream& output) = 0;
|
|
|
|
|
virtual void format(const std::string& logger_name, details::log_msg& msg) = 0;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -28,17 +29,20 @@ class default_formatter: public formatter
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
// Format: [2013-12-29 01:04:42.900] [logger_name:Info] Message body
|
|
|
|
|
void format_header(const std::string& logger_name, level::level_enum level, const log_clock::time_point& tp, std::ostream& output) override
|
|
|
|
|
void format(const std::string& logger_name, details::log_msg& msg) override
|
|
|
|
|
{
|
|
|
|
|
_format_time(tp, output);
|
|
|
|
|
std::ostringstream oss;
|
|
|
|
|
//Format datetime like this:[2014 - 03 - 14 17:15 : 22]
|
|
|
|
|
_format_time(msg.time, oss);
|
|
|
|
|
if(!logger_name.empty())
|
|
|
|
|
output << " [" << logger_name << ':' << c11log::level::to_str(level) << "] ";
|
|
|
|
|
oss << " [" << logger_name << ':' << c11log::level::to_str(msg.level) << "] ";
|
|
|
|
|
else
|
|
|
|
|
output << " [" << c11log::level::to_str(level) << "] ";
|
|
|
|
|
|
|
|
|
|
oss << " [" << c11log::level::to_str(msg.level) << "] ";
|
|
|
|
|
oss << msg.raw << details::os::eol();
|
|
|
|
|
msg.formatted = oss.str();
|
|
|
|
|
}
|
|
|
|
|
private:
|
|
|
|
|
void _format_time(const log_clock::time_point& tp, std::ostream &output);
|
|
|
|
|
void _format_time(const log_clock::time_point& tp, std::ostream& output);
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
@ -52,12 +56,13 @@ inline void c11log::formatters::default_formatter::_format_time(const log_clock:
|
|
|
|
|
using namespace c11log::details::os;
|
|
|
|
|
using namespace std::chrono;
|
|
|
|
|
|
|
|
|
|
#ifdef _WIN32 //VS2013 doesn't support yet thread_local keyword
|
|
|
|
|
__declspec(thread) static char s_cache_str[64];
|
|
|
|
|
__declspec(thread) static size_t s_cache_size;
|
|
|
|
|
#ifdef _WIN32 //VS2013 doesn't support yet thread_local keyword
|
|
|
|
|
__declspec(thread) static char s_cache_timestr[128];
|
|
|
|
|
__declspec(thread) static int s_cache_timesize = 0;
|
|
|
|
|
__declspec(thread) static std::time_t s_cache_time_t = 0;
|
|
|
|
|
#else
|
|
|
|
|
thread_local static std::string s_cache_timestr;
|
|
|
|
|
thread_local static char s_cache_timestr[128];
|
|
|
|
|
thread_local static int s_cache_timesize = 0;
|
|
|
|
|
thread_local static std::time_t s_cache_time_t = 0;
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
@ -81,10 +86,9 @@ inline void c11log::formatters::default_formatter::_format_time(const log_clock:
|
|
|
|
|
time_oss << tm_now.tm_sec << ']';
|
|
|
|
|
//Cache the resulted string and its size
|
|
|
|
|
s_cache_time_t = tp_time_t;
|
|
|
|
|
//const std::string &s = time_oss.str_ref();
|
|
|
|
|
s_cache_timestr = time_oss.str();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const std::string s = time_oss.str();
|
|
|
|
|
s_cache_timesize = s.size();
|
|
|
|
|
std::memcpy(s_cache_timestr, s.c_str(), s_cache_timesize);
|
|
|
|
|
}
|
|
|
|
|
output << s_cache_timestr;
|
|
|
|
|
output.write(s_cache_timestr, s_cache_timesize);
|
|
|
|
|
}
|
|
|
|
|