2014-03-22 08:11:17 -04:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <chrono>
|
|
|
|
#include <functional>
|
2014-03-28 05:52:36 -04:00
|
|
|
|
2014-03-22 08:11:17 -04:00
|
|
|
#include <iomanip>
|
|
|
|
#include <thread>
|
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
#include "common_types.h"
|
|
|
|
#include "details/os.h"
|
2014-03-28 09:05:09 -04:00
|
|
|
#include "details/stack_oss.h"
|
2014-03-22 08:11:17 -04:00
|
|
|
|
|
|
|
namespace c11log
|
|
|
|
{
|
|
|
|
namespace formatters
|
|
|
|
{
|
|
|
|
|
|
|
|
class formatter
|
|
|
|
{
|
2014-03-28 09:05:09 -04:00
|
|
|
public:
|
|
|
|
virtual void format_header(const std::string& logger_name, level::level_enum level, const log_clock::time_point& tp, std::ostream& output) = 0;
|
2014-03-22 08:11:17 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class default_formatter: public formatter
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
// Format: [2013-12-29 01:04:42.900] [logger_name:Info] Message body
|
2014-03-28 09:05:09 -04:00
|
|
|
void format_header(const std::string& logger_name, level::level_enum level, const log_clock::time_point& tp, std::ostream& output) override
|
2014-03-22 08:11:17 -04:00
|
|
|
{
|
2014-03-28 09:05:09 -04:00
|
|
|
_format_time(tp, output);
|
2014-03-22 08:11:17 -04:00
|
|
|
if(!logger_name.empty())
|
2014-03-28 09:05:09 -04:00
|
|
|
output << " [" << logger_name << ':' << c11log::level::to_str(level) << "] ";
|
2014-03-22 08:11:17 -04:00
|
|
|
else
|
2014-03-28 09:05:09 -04:00
|
|
|
output << " [" << c11log::level::to_str(level) << "] ";
|
2014-03-22 08:11:17 -04:00
|
|
|
|
|
|
|
}
|
|
|
|
private:
|
2014-03-28 09:05:09 -04:00
|
|
|
void _format_time(const log_clock::time_point& tp, std::ostream &output);
|
2014-03-22 08:11:17 -04:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
} //namespace formatter
|
|
|
|
} //namespace c11log
|
|
|
|
|
|
|
|
// Format datetime like this: [2014-03-14 17:15:22]
|
2014-03-28 09:05:09 -04:00
|
|
|
inline void c11log::formatters::default_formatter::_format_time(const log_clock::time_point& tp, std::ostream &output)
|
2014-03-22 08:11:17 -04:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
__declspec(thread) static std::time_t s_cache_time_t = 0;
|
|
|
|
#else
|
|
|
|
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;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
//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);
|
2014-03-28 09:05:09 -04:00
|
|
|
details::stack_oss time_oss;
|
2014-03-22 08:11:17 -04:00
|
|
|
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;
|
|
|
|
//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-28 09:05:09 -04:00
|
|
|
output.write(s_cache_str, s_cache_size);
|
2014-03-22 08:11:17 -04:00
|
|
|
}
|