ostream_logger.hpp
Go to the documentation of this file.
/*
restinio
*/
#pragma once
#include <restinio/null_mutex.hpp>
#include <restinio/impl/include_fmtlib.hpp>
#include <string>
#include <iostream>
#include <chrono>
#include <mutex>
namespace restinio
{
//
// ostream_logger_t
//
template < typename Lock >
class ostream_logger_t
{
public:
ostream_logger_t( const ostream_logger_t & ) = delete;
ostream_logger_t & operator = ( const ostream_logger_t & ) = delete;
ostream_logger_t() noexcept
: m_out{ &std::cout }
{}
ostream_logger_t( std::ostream & out ) noexcept
: m_out{ &out }
{}
template< typename Message_Builder >
void
trace( Message_Builder && msg_builder )
{
log_message( "TRACE", msg_builder() );
}
template< typename Message_Builder >
void
info( Message_Builder && msg_builder )
{
log_message( " INFO", msg_builder() );
}
template< typename Message_Builder >
void
warn( Message_Builder && msg_builder )
{
log_message( " WARN", msg_builder() );
}
template< typename Message_Builder >
void
error( Message_Builder && msg_builder )
{
log_message( "ERROR", msg_builder() );
}
private:
void
log_message( const char * tag, const std::string & msg )
{
std::lock_guard< Lock > lock{ m_lock };
namespace stdchrono = std::chrono;
auto now = stdchrono::system_clock::now();
auto ms = stdchrono::duration_cast<
stdchrono::milliseconds >( now.time_since_epoch() );
std::time_t unix_time = stdchrono::duration_cast<
stdchrono::seconds >( ms ).count();
( *m_out )
<< fmt::format(
"[{:%Y-%m-%d %H:%M:%S}.{:03d}] {}: {}",
make_localtime( unix_time ),
static_cast< int >( ms.count() % 1000u ),
tag,
msg )
<< std::endl;
}
Lock m_lock;
std::ostream * m_out;
};
using single_threaded_ostream_logger_t = ostream_logger_t< null_mutex_t >;
using shared_ostream_logger_t = ostream_logger_t< std::mutex >;
} /* namespace restinio */
restinio::ostream_logger_t
Logger for std::ostream.
Definition: ostream_logger.hpp:33
restinio::ostream_logger_t::m_out
std::ostream * m_out
Definition: ostream_logger.hpp:99
restinio::ostream_logger_t::ostream_logger_t
ostream_logger_t(std::ostream &out) noexcept
Definition: ostream_logger.hpp:42
restinio::ostream_logger_t::m_lock
Lock m_lock
Definition: ostream_logger.hpp:98
restinio::ostream_logger_t::trace
void trace(Message_Builder &&msg_builder)
Definition: ostream_logger.hpp:48
restinio::ostream_logger_t::ostream_logger_t
ostream_logger_t() noexcept
Definition: ostream_logger.hpp:38
restinio::ostream_logger_t::info
void info(Message_Builder &&msg_builder)
Definition: ostream_logger.hpp:55
restinio::ostream_logger_t::log_message
void log_message(const char *tag, const std::string &msg)
Definition: ostream_logger.hpp:76
restinio::ostream_logger_t::error
void error(Message_Builder &&msg_builder)
Definition: ostream_logger.hpp:69
restinio::ostream_logger_t::warn
void warn(Message_Builder &&msg_builder)
Definition: ostream_logger.hpp:62
restinio::ostream_logger_t::operator=
ostream_logger_t & operator=(const ostream_logger_t &)=delete
restinio::ostream_logger_t::ostream_logger_t
ostream_logger_t(const ostream_logger_t &)=delete
include_fmtlib.hpp
A special wrapper around fmtlib include files.
restinio
Definition: asio_include.hpp:21
null_mutex.hpp
Definition of null_mutex.
make_localtime
std::tm make_localtime(std::time_t t)
Definition: os_posix.ipp:12