RESTinio

suppress_exceptions.hpp
Go to the documentation of this file.
1/*
2 * RESTinio
3 */
4
12#pragma once
13
14#include <restinio/impl/include_fmtlib.hpp>
15
16#include <restinio/null_logger.hpp>
17
18#include <exception>
19
20 namespace restinio
21{
22
23 namespace utils
24{
25
26//
27// Wrappers for logging with suppressing of exceptions.
28//
29
30 template< typename Logger, typename Message_Builder >
31 void
32 log_trace_noexcept( Logger && logger, Message_Builder && builder ) noexcept
33{
34 try { logger.trace( std::forward<Message_Builder>(builder) ); }
35 catch( ... ) {}
36}
37
38 template< typename Message_Builder >
39 void
40 log_trace_noexcept( null_logger_t &, Message_Builder && ) noexcept
41{}
42
43 template< typename Logger, typename Message_Builder >
44 void
45 log_info_noexcept( Logger && logger, Message_Builder && builder ) noexcept
46{
47 try { logger.info( std::forward<Message_Builder>(builder) ); }
48 catch( ... ) {}
49}
50
51 template< typename Message_Builder >
52 void
53 log_info_noexcept( null_logger_t &, Message_Builder && ) noexcept
54{}
55
56 template< typename Logger, typename Message_Builder >
57 void
58 log_warn_noexcept( Logger && logger, Message_Builder && builder ) noexcept
59{
60 try { logger.warn( std::forward<Message_Builder>(builder) ); }
61 catch( ... ) {}
62}
63
64 template< typename Message_Builder >
65 void
66 log_warn_noexcept( null_logger_t &, Message_Builder && ) noexcept
67{}
68
69 template< typename Logger, typename Message_Builder >
70 void
71 log_error_noexcept( Logger && logger, Message_Builder && builder ) noexcept
72{
73 try { logger.error( std::forward<Message_Builder>(builder) ); }
74 catch( ... ) {}
75}
76
77 template< typename Message_Builder >
78 void
79 log_error_noexcept( null_logger_t &, Message_Builder && ) noexcept
80{}
81
91 template<
92 typename Logger,
93 typename Lambda >
94 void suppress_exceptions(
96 Logger && logger,
99 const char * block_description,
101 Lambda && lambda ) noexcept
102{
103 try
104 {
105 lambda();
106 }
107 catch( const std::exception & x )
108 {
109 log_error_noexcept( logger, [&] {
110 return fmt::format( "an exception in '{}': {}",
111 block_description, x.what() );
112 } );
113 }
114 catch( ... )
115 {
116 log_error_noexcept( logger, [&] {
117 return fmt::format( "an unknown exception in '{}'",
118 block_description );
119 } );
120 }
121}
122
132 template< typename Lambda >
133 void suppress_exceptions_quietly( Lambda && lambda ) noexcept
134{
135 try
136 {
137 lambda();
138 }
139 catch( ... ) {}
140}
141
142
143} /* namespace utils */
144
145} /* namespace restinio */
146
restinio::null_logger_t
No operation logger.
Definition: null_logger.hpp:23
include_fmtlib.hpp
A special wrapper around fmtlib include files.
restinio::utils::suppress_exceptions
void suppress_exceptions(Logger &&logger, const char *block_description, Lambda &&lambda) noexcept
Helper function for execution a block of code with suppression of any exceptions raised inside that b...
Definition: suppress_exceptions.hpp:94
restinio::utils::log_info_noexcept
void log_info_noexcept(Logger &&logger, Message_Builder &&builder) noexcept
Definition: suppress_exceptions.hpp:45
restinio::utils::log_error_noexcept
void log_error_noexcept(Logger &&logger, Message_Builder &&builder) noexcept
Definition: suppress_exceptions.hpp:71
restinio::utils::log_warn_noexcept
void log_warn_noexcept(Logger &&logger, Message_Builder &&builder) noexcept
Definition: suppress_exceptions.hpp:58
restinio::utils::suppress_exceptions_quietly
void suppress_exceptions_quietly(Lambda &&lambda) noexcept
Helper function for execution a block of code with suppression of any exceptions raised inside that b...
Definition: suppress_exceptions.hpp:133
restinio::utils::log_trace_noexcept
void log_trace_noexcept(Logger &&logger, Message_Builder &&builder) noexcept
Definition: suppress_exceptions.hpp:32
restinio
Definition: asio_include.hpp:21
null_logger.hpp