2014-03-22 08:11:17 -04:00
|
|
|
#pragma once
|
|
|
|
|
2014-05-07 19:23:07 -04:00
|
|
|
#include <sstream>
|
2014-10-09 20:36:50 -04:00
|
|
|
#include "../common.h"
|
2014-03-22 08:11:17 -04:00
|
|
|
#include "../logger.h"
|
2014-05-09 09:33:55 -04:00
|
|
|
#include "fast_oss.h"
|
|
|
|
|
2014-05-07 19:23:07 -04:00
|
|
|
|
2014-10-10 14:32:10 -04:00
|
|
|
// Line logger class - aggregates operator<< calls to fast ostream
|
|
|
|
// and logs upon destruction
|
2014-03-22 08:11:17 -04:00
|
|
|
|
|
|
|
namespace c11log
|
|
|
|
{
|
|
|
|
namespace details
|
|
|
|
{
|
|
|
|
class line_logger
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
line_logger(logger* callback_logger, level::level_enum msg_level, bool enabled):
|
|
|
|
_callback_logger(callback_logger),
|
2014-03-28 17:46:45 -04:00
|
|
|
_log_msg(msg_level),
|
2014-03-22 08:11:17 -04:00
|
|
|
_oss(),
|
2014-05-09 09:33:55 -04:00
|
|
|
_enabled(enabled)
|
2014-05-09 10:11:50 -04:00
|
|
|
{}
|
2014-03-22 08:11:17 -04:00
|
|
|
|
|
|
|
// No copy intended. Only move
|
|
|
|
line_logger(const line_logger& other) = delete;
|
|
|
|
line_logger& operator=(const line_logger&) = delete;
|
|
|
|
line_logger& operator=(line_logger&&) = delete;
|
|
|
|
|
2014-05-06 10:38:11 -04:00
|
|
|
|
2014-03-22 08:11:17 -04:00
|
|
|
line_logger(line_logger&& other) :
|
2014-03-22 10:37:48 -04:00
|
|
|
_callback_logger(other._callback_logger),
|
2014-05-06 10:38:11 -04:00
|
|
|
_log_msg(std::move(other._log_msg)),
|
2014-05-09 09:33:55 -04:00
|
|
|
_oss(std::move(other._oss)),
|
|
|
|
_enabled(other._enabled)
|
2014-05-09 08:27:06 -04:00
|
|
|
{
|
|
|
|
other.disable();
|
|
|
|
}
|
2014-05-06 10:38:11 -04:00
|
|
|
|
2014-05-09 09:33:55 -04:00
|
|
|
//Log the log message using the callback logger
|
2014-03-22 08:11:17 -04:00
|
|
|
~line_logger()
|
2014-03-30 18:12:49 -04:00
|
|
|
{
|
2014-05-09 09:33:55 -04:00
|
|
|
if (_enabled)
|
2014-03-22 08:11:17 -04:00
|
|
|
{
|
2014-10-10 14:17:26 -04:00
|
|
|
_log_msg.logger_name = _callback_logger->name();
|
2014-05-09 11:00:10 -04:00
|
|
|
_log_msg.time = log_clock::now();
|
2014-10-13 20:44:40 -04:00
|
|
|
_log_msg.tm_time = details::os::localtime(log_clock::to_time_t(_log_msg.time));
|
2014-05-09 08:27:06 -04:00
|
|
|
_log_msg.raw = _oss.str();
|
2014-10-11 21:38:06 -04:00
|
|
|
_callback_logger->_log_msg(_log_msg);
|
2014-03-22 08:11:17 -04:00
|
|
|
}
|
|
|
|
}
|
2014-03-28 18:38:05 -04:00
|
|
|
|
2014-03-30 19:31:26 -04:00
|
|
|
template<typename T>
|
2014-03-30 19:09:13 -04:00
|
|
|
void write(const T& what)
|
2014-03-22 08:11:17 -04:00
|
|
|
{
|
|
|
|
if (_enabled)
|
2014-03-30 19:31:26 -04:00
|
|
|
{
|
2014-03-28 09:05:09 -04:00
|
|
|
_oss << what;
|
2014-03-30 19:31:26 -04:00
|
|
|
}
|
2014-03-30 19:09:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
line_logger& operator<<(const T& what)
|
|
|
|
{
|
|
|
|
write(what);
|
2014-03-28 17:27:13 -04:00
|
|
|
return *this;
|
2014-03-22 08:11:17 -04:00
|
|
|
}
|
|
|
|
|
2014-05-09 08:27:06 -04:00
|
|
|
void disable()
|
|
|
|
{
|
|
|
|
_enabled = false;
|
|
|
|
}
|
2014-05-06 10:38:11 -04:00
|
|
|
|
2014-03-30 19:09:13 -04:00
|
|
|
|
|
|
|
|
2014-03-22 08:11:17 -04:00
|
|
|
private:
|
|
|
|
logger* _callback_logger;
|
2014-03-28 12:03:24 -04:00
|
|
|
log_msg _log_msg;
|
2014-05-09 09:33:55 -04:00
|
|
|
details::fast_oss _oss;
|
2014-03-22 08:11:17 -04:00
|
|
|
bool _enabled;
|
|
|
|
};
|
|
|
|
} //Namespace details
|
|
|
|
} // Namespace c11log
|