RESTinio

ostream_logger.hpp
Go to the documentation of this file.
1/*
2 restinio
3*/
4
9#pragma once
10
11#include <restinio/null_mutex.hpp>
12
13#include <restinio/impl/include_fmtlib.hpp>
14
15#include <string>
16#include <iostream>
17#include <chrono>
18#include <mutex>
19
20 namespace restinio
21{
22
23//
24// ostream_logger_t
25//
26
28
31 template < typename Lock >
32 class ostream_logger_t
33{
34 public:
35 ostream_logger_t( const ostream_logger_t & ) = delete;
36 ostream_logger_t & operator = ( const ostream_logger_t & ) = delete;
37
38 ostream_logger_t() noexcept
39 : m_out{ &std::cout }
40 {}
41
42 ostream_logger_t( std::ostream & out ) noexcept
43 : m_out{ &out }
44 {}
45
46 template< typename Message_Builder >
47 void
48 trace( Message_Builder && msg_builder )
49 {
50 log_message( "TRACE", msg_builder() );
51 }
52
53 template< typename Message_Builder >
54 void
55 info( Message_Builder && msg_builder )
56 {
57 log_message( " INFO", msg_builder() );
58 }
59
60 template< typename Message_Builder >
61 void
62 warn( Message_Builder && msg_builder )
63 {
64 log_message( " WARN", msg_builder() );
65 }
66
67 template< typename Message_Builder >
68 void
69 error( Message_Builder && msg_builder )
70 {
71 log_message( "ERROR", msg_builder() );
72 }
73
74 private:
75 void
76 log_message( const char * tag, const std::string & msg )
77 {
78 std::lock_guard< Lock > lock{ m_lock };
79
80 namespace stdchrono = std::chrono;
81
82 auto now = stdchrono::system_clock::now();
83 auto ms = stdchrono::duration_cast<
84 stdchrono::milliseconds >( now.time_since_epoch() );
85 std::time_t unix_time = stdchrono::duration_cast<
86 stdchrono::seconds >( ms ).count();
87
88 ( *m_out )
89 << fmt::format(
90 "[{:%Y-%m-%d %H:%M:%S}.{:03d}] {}: {}",
91 make_localtime( unix_time ),
92 static_cast< int >( ms.count() % 1000u ),
93 tag,
94 msg )
95 << std::endl;
96 }
97
98 Lock m_lock;
99 std::ostream * m_out;
100};
101
102 using single_threaded_ostream_logger_t = ostream_logger_t< null_mutex_t >;
103 using shared_ostream_logger_t = ostream_logger_t< std::mutex >;
104
105} /* 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