RESTinio
ostream_logger.hpp
Go to the documentation of this file.
1 /*
2  restinio
3 */
4 
5 /*!
6  Ready to use logger implementation for using with std::ostream.
7 */
8 
9 #pragma once
10 
11 #include <string>
12 #include <iostream>
13 #include <chrono>
14 #include <mutex>
15 
16 #include <fmt/format.h>
17 #include <fmt/ostream.h>
18 #include <fmt/time.h>
19 
20 namespace restinio
21 {
22 
23 //
24 // null_lock_t
25 //
26 
27 //! Fake lock.
29 {
30  constexpr void lock() const noexcept {}
31 
32  constexpr bool try_lock() const noexcept { return true; }
33 
34  constexpr void unlock() const noexcept {}
35 };
36 
37 //
38 // ostream_logger_t
39 //
40 
41 //! Logger for std::ostream.
42 /*!
43  \note It is not efficient.
44 */
45 template < typename Lock >
47 {
48  public:
49  ostream_logger_t( const ostream_logger_t & ) = delete;
50  ostream_logger_t & operator = ( const ostream_logger_t & ) = delete;
51 
52  ostream_logger_t() noexcept
53  : m_out{ &std::cout }
54  {}
55 
56  ostream_logger_t( std::ostream & out ) noexcept
57  : m_out{ &out }
58  {}
59 
60  template< typename Message_Builder >
61  void
62  trace( Message_Builder && msg_builder )
63  {
64  log_message( "TRACE", msg_builder() );
65  }
66 
67  template< typename Message_Builder >
68  void
69  info( Message_Builder && msg_builder )
70  {
71  log_message( " INFO", msg_builder() );
72  }
73 
74  template< typename Message_Builder >
75  void
76  warn( Message_Builder && msg_builder )
77  {
78  log_message( " WARN", msg_builder() );
79  }
80 
81  template< typename Message_Builder >
82  void
83  error( Message_Builder && msg_builder )
84  {
85  log_message( "ERROR", msg_builder() );
86  }
87 
88  private:
89  void
90  log_message( const char * tag, const std::string & msg )
91  {
93 
94  using namespace std;
95  using namespace chrono;
96 
97  auto now = system_clock::now();
100 
101  ( *m_out )
102  << fmt::format(
103  "[{:%Y-%m-%d %H:%M:%S}.{:03d}] {}: {}",
105  static_cast< int >( ms.count() % 1000u ),
106  tag,
107  msg )
108  << std::endl;
109  }
110 
111  Lock m_lock;
113 };
114 
115 using single_threaded_ostream_logger_t = ostream_logger_t< null_lock_t >;
117 
118 } /* namespace restinio */
Logger for std::ostream.
void trace(Message_Builder &&msg_builder)
ostream_logger_t(std::ostream &out) noexcept
void log_message(const char *tag, const std::string &msg)
constexpr void lock() const noexcept
constexpr void unlock() const noexcept
ostream_logger_t & operator=(const ostream_logger_t &)=delete
constexpr bool try_lock() const noexcept
ostream_logger_t(const ostream_logger_t &)=delete
void error(Message_Builder &&msg_builder)
void info(Message_Builder &&msg_builder)
void warn(Message_Builder &&msg_builder)
std::enable_if< std::is_same< Parameter_Container, query_string_params_t >::value||std::is_same< Parameter_Container, router::route_params_t >::value, optional_t< Value_Type > >::type opt_value(const Parameter_Container &params, string_view_t key)
Gets the value of a parameter specified by key wrapped in optional_t<Value_Type> if parameter exists ...
Definition: value_or.hpp:64