log_msg refactoring and raw message support

This commit is contained in:
gabi 2014-05-09 15:27:06 +03:00
parent bafea6a6b2
commit 497d511746
9 changed files with 106 additions and 102 deletions

View File

@ -18,15 +18,13 @@ using namespace utils;
int main(int argc, char* argv[])
{
const unsigned int howmany = argc <= 1 ? 4000000:atoi(argv[1]);
const unsigned int howmany = argc <= 1 ? 1000000:atoi(argv[1]);
logger cout_logger ("", sinks::stdout_sink());
cout_logger.set_min_level(c11log::level::TRACE);
cout_logger.info() << "Hello " << "man" << 123;
logger cout_logger ("example", sinks::stdout_sink());
cout_logger.info() << "Hello logger";
auto fsink = std::make_shared<sinks::rotating_file_sink>("log", "txt", 1024*1024*50 , 5, 0);
auto nullsink = sinks::null_sink::get();
//auto fsink = std::make_shared<sinks::rotating_file_sink>("log", "txt", 1024*1024*50 , 5, 0);
//auto as = std::make_shared<sinks::async_sink>(1000);
//as->add_sink(fsink);
@ -34,8 +32,8 @@ int main(int argc, char* argv[])
auto start = system_clock::now();
for(unsigned int i = 1; i <= howmany ; ++i)
my_logger.info("Hello logger: ") << 4.5 <<'\t' << i << "\tasdasd:" << 123 << 'f';
for (unsigned int i = 1; i <= howmany; ++i)
my_logger.info("Hello logger: msg #") << i << " äàí æä éòáåã??";
//as->shutdown(std::chrono::milliseconds(15000));
auto delta = system_clock::now() - start;

View File

@ -26,12 +26,7 @@ public:
{
if(enabled)
{
_log_msg.msg_time = log_clock::now();
callback_logger->_formatter->format_header(callback_logger->_logger_name,
_log_msg.msg_level,
_log_msg.msg_time,
_oss);
_log_msg.msg_header_size = _oss.str().size();
_log_msg.time = log_clock::now();
}
}
@ -58,8 +53,7 @@ public:
//only if enabled and not empty
if (_enabled && !_empty)
{
_oss << os::eol();
_log_msg.str = _oss.str();
_log_msg.raw = _oss.str();
_callback_logger->_log_it(_log_msg);
}
}

View File

@ -8,19 +8,19 @@ struct log_msg
{
log_msg() = default;
log_msg(level::level_enum l):
msg_level(l),
msg_time(),
msg_header_size(0),
str() {}
level(l),
time(),
raw(),
formatted() {}
log_msg(const log_msg& other):
msg_level(other.msg_level),
msg_time(other.msg_time),
msg_header_size(other.msg_header_size),
str(other.str) {}
level(other.level),
time(other.time),
raw(other.raw),
formatted(other.formatted) {}
log_msg(log_msg&& other):log_msg()
log_msg(log_msg&& other)
{
swap(*this, other);
}
@ -28,14 +28,13 @@ struct log_msg
friend void swap(log_msg& l, log_msg& r)
{
using std::swap;
swap(l.msg_level, r.msg_level);
swap(l.msg_time, r.msg_time);
swap(l.msg_header_size, r.msg_header_size);
swap(l.str, r.str);
swap(l.level, r.level);
swap(l.time, r.time);
swap(l.raw, r.raw);
swap(l.formatted, r.formatted);
}
log_msg& operator=(log_msg other)
{
swap(*this, other);
@ -45,14 +44,14 @@ struct log_msg
void clear()
{
msg_header_size = 0;
str.clear();
raw.clear();
formatted.clear();
}
level::level_enum msg_level;
log_clock::time_point msg_time;
std::size_t msg_header_size;
std::string str;
level::level_enum level;
log_clock::time_point time;
std::string raw;
std::string formatted;
};
}

View File

@ -44,23 +44,31 @@ inline bool operator!=(const std::tm& tm1, const std::tm& tm2)
return !(tm1==tm2);
}
#ifdef _WIN32
inline const char* eol()
{
return "\r\n";
}
#else
constexpr inline const char* eol()
{
#ifdef _WIN32
return "\r\n";
#else
return "\n";
#endif
}
#endif
#ifdef _WIN32
inline unsigned short eol_size()
{
return 2;
}
#else
constexpr inline unsigned short eol_size()
{
#ifdef _WIN32
return 2;
#else
return 1;
#endif
}
#endif
} //os
} //details
} //c11log

View File

@ -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);
};
@ -53,11 +57,12 @@ inline void c11log::formatters::default_formatter::_format_time(const log_clock:
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 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);
}

View File

@ -75,8 +75,7 @@ private:
sinks_vector_t _sinks;
std::atomic_int _min_level;
void _log_it(const details::log_msg& msg);
void _log_it(details::log_msg& msg);
};
@ -208,8 +207,9 @@ inline bool c11log::logger::should_log(c11log::level::level_enum level) const
return level >= _min_level.load();
}
inline void c11log::logger::_log_it(const details::log_msg& msg)
inline void c11log::logger::_log_it(details::log_msg& msg)
{
_formatter->format(_logger_name, msg);
for (auto &sink : _sinks)
sink->log(msg);
}

View File

@ -70,7 +70,7 @@ inline c11log::sinks::async_sink::~async_sink()
inline void c11log::sinks::async_sink::_sink_it(const details::log_msg& msg)
{
if(!_active || msg.str.empty())
if(!_active || msg.formatted.empty())
return;
_q.push(msg);
}

View File

@ -23,7 +23,7 @@ protected:
virtual void _sink_it(const details::log_msg& msg) override
{
std::lock_guard<std::mutex> lock(_mutex);
_ostream << msg.str;
_ostream << msg.formatted;
}
std::ostream& _ostream;

View File

@ -8,6 +8,7 @@
#include "../details/flush_helper.h"
#include "../details/blocking_queue.h"
namespace c11log
{
namespace sinks
@ -31,7 +32,7 @@ protected:
void _sink_it(const details::log_msg& msg) override
{
std::lock_guard<std::mutex> lock(_mutex);
_flush_helper.write(msg.str, _ofstream);
_flush_helper.write(msg.formatted, _ofstream);
}
private:
std::mutex _mutex;
@ -65,13 +66,13 @@ protected:
{
std::lock_guard<std::mutex> lock(_mutex);
_current_size += msg.str.size();
_current_size += msg.formatted.size();
if (_current_size > _max_size)
{
_rotate();
_current_size = msg.str.size();
_current_size = msg.formatted.size();
}
_flush_helper.write(msg.str, _ofstream);
_flush_helper.write(msg.formatted, _ofstream);
}
@ -104,7 +105,7 @@ private:
std::remove(target.c_str());
std::rename(src.c_str(), target.c_str());
}
_ofstream.open(_calc_filename(_base_filename, 0, _extension));
_ofstream.open(_calc_filename(_base_filename, 0, _extension), std::ofstream::binary);
}
std::string _base_filename;
std::string _extension;
@ -144,7 +145,7 @@ protected:
_ofstream.open(_calc_filename(_base_filename, _extension));
_midnight_tp = _calc_midnight_tp();
}
_flush_helper.write(msg.str, _ofstream);
_flush_helper.write(msg.formatted, _ofstream);
}
private: