RESTinio
Loading...
Searching...
No Matches
ostream_logger.hpp
Go to the documentation of this file.
1/*
2 restinio
3*/
4
9#pragma once
10
12
14
15#include <string>
16#include <iostream>
17#include <chrono>
18#include <mutex>
19
20namespace restinio
21{
22
23//
24// ostream_logger_t
25//
26
28
31template < typename Lock >
33{
34 public:
37
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
49 {
50 log_message( "TRACE", msg_builder() );
51 }
52
53 template< typename Message_Builder >
54 void
56 {
57 log_message( " INFO", msg_builder() );
58 }
59
60 template< typename Message_Builder >
61 void
63 {
64 log_message( " WARN", msg_builder() );
65 }
66
67 template< typename Message_Builder >
68 void
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(
91 "[{:%Y-%m-%d %H:%M:%S}.{:03d}] {}: {}" ),
93 static_cast< int >( ms.count() % 1000u ),
94 tag,
95 msg )
96 << std::endl;
97 }
98
100 std::ostream * m_out;
101};
102
105
106} /* namespace restinio */
Logger for std::ostream.
ostream_logger_t(std::ostream &out) noexcept
void trace(Message_Builder &&msg_builder)
void info(Message_Builder &&msg_builder)
void log_message(const char *tag, const std::string &msg)
void error(Message_Builder &&msg_builder)
void warn(Message_Builder &&msg_builder)
ostream_logger_t & operator=(const ostream_logger_t &)=delete
ostream_logger_t(const ostream_logger_t &)=delete
A special wrapper around fmtlib include files.
#define RESTINIO_FMT_FORMAT_STRING(s)
run_on_this_thread_settings_t< Traits > on_this_thread()
A special marker for the case when http_server must be run on the context of the current thread.
Definition of null_mutex.
std::tm make_localtime(std::time_t t)
Definition os_posix.ipp:12