spdlog/include/spitlog/details/line_logger.h

80 lines
1.6 KiB
C
Raw Normal View History

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