2014-01-25 17:09:04 +08:00
|
|
|
#pragma once
|
|
|
|
|
2014-03-01 20:06:58 +08:00
|
|
|
#include <string>
|
|
|
|
#include <chrono>
|
|
|
|
#include <functional>
|
2014-01-25 17:09:04 +08:00
|
|
|
#include <sstream>
|
2014-03-01 20:06:58 +08:00
|
|
|
#include <iomanip>
|
2014-03-05 06:31:13 +08:00
|
|
|
#include <thread>
|
2014-03-16 03:05:11 +08:00
|
|
|
#include <cstdlib>
|
2014-03-19 01:23:24 +08:00
|
|
|
#include <cstring>
|
2014-01-25 17:09:04 +08:00
|
|
|
|
2014-03-01 20:06:58 +08:00
|
|
|
#include "common_types.h"
|
2014-02-22 07:16:59 +08:00
|
|
|
#include "details/os.h"
|
2014-03-16 03:05:11 +08:00
|
|
|
#include "details/fast_oss.h"
|
2014-01-25 17:09:04 +08:00
|
|
|
|
2014-03-07 06:52:50 +08:00
|
|
|
namespace c11log
|
|
|
|
{
|
|
|
|
namespace formatters
|
|
|
|
{
|
2014-03-01 20:06:58 +08:00
|
|
|
|
|
|
|
typedef std::function<std::string(const std::string& logger_name, const std::string&, level::level_enum, const c11log::log_clock::time_point&)> format_fn;
|
2014-02-12 03:03:57 +08:00
|
|
|
|
|
|
|
|
2014-03-07 06:52:50 +08:00
|
|
|
class formatter
|
|
|
|
{
|
2014-01-25 17:09:04 +08:00
|
|
|
public:
|
|
|
|
formatter() {}
|
|
|
|
virtual ~formatter() {}
|
2014-03-01 20:06:58 +08:00
|
|
|
virtual void format_header(const std::string& logger_name, level::level_enum level, const log_clock::time_point& tp, std::ostream& dest) = 0;
|
2014-01-25 17:09:04 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-03-07 06:52:50 +08:00
|
|
|
class default_formatter: public formatter
|
|
|
|
{
|
2014-01-25 17:09:04 +08:00
|
|
|
public:
|
|
|
|
// Format: [2013-12-29 01:04:42.900] [logger_name:Info] Message body
|
2014-03-14 20:35:46 +08:00
|
|
|
void format_header(const std::string& logger_name, level::level_enum level, const log_clock::time_point& tp, std::ostream& dest) override
|
|
|
|
{
|
2014-02-12 03:03:57 +08:00
|
|
|
_format_time(tp, dest);
|
2014-03-08 22:40:47 +08:00
|
|
|
if(!logger_name.empty())
|
2014-03-16 03:05:11 +08:00
|
|
|
dest << " [" << logger_name << ':' << c11log::level::to_str(level) << "] ";
|
2014-03-08 22:40:47 +08:00
|
|
|
else
|
|
|
|
dest << " [" << c11log::level::to_str(level) << "] ";
|
2014-03-08 22:18:57 +08:00
|
|
|
|
2014-01-25 17:09:04 +08:00
|
|
|
}
|
2014-02-12 03:03:57 +08:00
|
|
|
private:
|
2014-03-01 20:06:58 +08:00
|
|
|
void _format_time(const log_clock::time_point& tp, std::ostream &dest);
|
2014-01-25 17:09:04 +08:00
|
|
|
|
|
|
|
};
|
2014-03-19 01:23:24 +08:00
|
|
|
|
|
|
|
|
2014-01-25 17:09:04 +08:00
|
|
|
} //namespace formatter
|
|
|
|
} //namespace c11log
|
2014-02-21 03:39:58 +08:00
|
|
|
|
2014-03-16 03:05:11 +08:00
|
|
|
// Format datetime like this: [2014-03-14 17:15:22]
|
2014-03-01 20:06:58 +08:00
|
|
|
inline void c11log::formatters::default_formatter::_format_time(const log_clock::time_point& tp, std::ostream &dest)
|
2014-03-02 22:31:13 +08:00
|
|
|
{
|
2014-03-19 01:23:24 +08:00
|
|
|
using namespace c11log::details::os;
|
|
|
|
using namespace std::chrono;
|
2014-03-05 06:31:13 +08:00
|
|
|
|
2014-03-19 01:23:24 +08:00
|
|
|
#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;
|
|
|
|
__declspec(thread) static std::time_t s_cache_time_t = 0;
|
2014-03-05 06:31:13 +08:00
|
|
|
#else
|
2014-03-19 01:23:24 +08:00
|
|
|
thread_local static char s_cache_str[64];
|
|
|
|
thread_local static size_t s_cache_size;
|
|
|
|
thread_local static std::time_t s_cache_time_t = 0;
|
2014-03-05 06:31:13 +08:00
|
|
|
#endif
|
2014-03-16 03:05:11 +08:00
|
|
|
|
2014-03-19 01:23:24 +08:00
|
|
|
//Cache every second
|
|
|
|
std::time_t tp_time_t = log_clock::to_time_t(tp);
|
|
|
|
if(tp_time_t != s_cache_time_t)
|
|
|
|
{
|
|
|
|
auto tm_now = details::os::localtime(tp_time_t);
|
|
|
|
details::fast_oss time_oss;
|
|
|
|
time_oss.fill('0');
|
|
|
|
time_oss << '[' << tm_now.tm_year + 1900 << '-';
|
|
|
|
time_oss.width(2);
|
|
|
|
time_oss << tm_now.tm_mon + 1 << '-';
|
|
|
|
time_oss.width(2);
|
|
|
|
time_oss << tm_now.tm_mday << ' ';
|
|
|
|
time_oss.width(2);
|
|
|
|
time_oss << tm_now.tm_hour << ':';
|
|
|
|
time_oss.width(2);
|
|
|
|
time_oss << tm_now.tm_min << ':';
|
|
|
|
time_oss.width(2);
|
|
|
|
time_oss << tm_now.tm_sec << ']';
|
|
|
|
//Cache the resulted string and its size
|
|
|
|
s_cache_time_t = tp_time_t;
|
2014-03-20 09:47:57 +08:00
|
|
|
//const std::string &s = time_oss.str_ref();
|
|
|
|
bufpair_t buf = time_oss.buf();
|
|
|
|
std::memcpy(s_cache_str, buf.first, buf.second);
|
|
|
|
s_cache_size = buf.second;
|
2014-03-05 06:31:13 +08:00
|
|
|
}
|
2014-03-19 01:23:24 +08:00
|
|
|
dest.write(s_cache_str, s_cache_size);
|
2014-02-21 03:39:58 +08:00
|
|
|
}
|