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

View File

@ -26,12 +26,7 @@ public:
{ {
if(enabled) if(enabled)
{ {
_log_msg.msg_time = log_clock::now(); _log_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();
} }
} }
@ -58,8 +53,7 @@ public:
//only if enabled and not empty //only if enabled and not empty
if (_enabled && !_empty) if (_enabled && !_empty)
{ {
_oss << os::eol(); _log_msg.raw = _oss.str();
_log_msg.str = _oss.str();
_callback_logger->_log_it(_log_msg); _callback_logger->_log_it(_log_msg);
} }
} }

View File

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

View File

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

View File

@ -2,14 +2,15 @@
#include <string> #include <string>
#include <chrono> #include <chrono>
#include <functional>
#include <iomanip> #include <iomanip>
#include <thread> #include <thread>
#include <cstring> #include <cstring>
#include <sstream> #include <sstream>
#include "common_types.h" #include "common_types.h"
#include "details/os.h" #include "details/os.h"
#include "details/log_msg.h"
namespace c11log namespace c11log
@ -20,7 +21,7 @@ namespace formatters
class formatter class formatter
{ {
public: 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,14 +29,17 @@ class default_formatter: public formatter
{ {
public: public:
// Format: [2013-12-29 01:04:42.900] [logger_name:Info] Message body // 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()) if(!logger_name.empty())
output << " [" << logger_name << ':' << c11log::level::to_str(level) << "] "; oss << " [" << logger_name << ':' << c11log::level::to_str(msg.level) << "] ";
else 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: 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; using namespace std::chrono;
#ifdef _WIN32 //VS2013 doesn't support yet thread_local keyword #ifdef _WIN32 //VS2013 doesn't support yet thread_local keyword
__declspec(thread) static char s_cache_str[64]; __declspec(thread) static char s_cache_timestr[128];
__declspec(thread) static size_t s_cache_size; __declspec(thread) static int s_cache_timesize = 0;
__declspec(thread) static std::time_t s_cache_time_t = 0; __declspec(thread) static std::time_t s_cache_time_t = 0;
#else #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; thread_local static std::time_t s_cache_time_t = 0;
#endif #endif
@ -81,10 +86,9 @@ inline void c11log::formatters::default_formatter::_format_time(const log_clock:
time_oss << tm_now.tm_sec << ']'; time_oss << tm_now.tm_sec << ']';
//Cache the resulted string and its size //Cache the resulted string and its size
s_cache_time_t = tp_time_t; s_cache_time_t = tp_time_t;
//const std::string &s = time_oss.str_ref(); const std::string s = time_oss.str();
s_cache_timestr = 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; sinks_vector_t _sinks;
std::atomic_int _min_level; 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(); 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) for (auto &sink : _sinks)
sink->log(msg); 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) inline void c11log::sinks::async_sink::_sink_it(const details::log_msg& msg)
{ {
if(!_active || msg.str.empty()) if(!_active || msg.formatted.empty())
return; return;
_q.push(msg); _q.push(msg);
} }

View File

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

View File

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