Line data Source code
1 : //
2 : // Copyright (c) 2025 Vinnie Falco (vinnie dot falco at gmail dot com)
3 : //
4 : // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 : // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 : //
7 : // Official repository: https://github.com/cppalliance/beast2
8 : //
9 :
10 : #ifndef BOOST_BEAST2_LOGGER_HPP
11 : #define BOOST_BEAST2_LOGGER_HPP
12 :
13 : #include <boost/beast2/detail/config.hpp>
14 : #include <boost/beast2/format.hpp>
15 : #include <memory>
16 : #include <sstream>
17 : #include <string>
18 : #include <vector>
19 :
20 : namespace boost {
21 : namespace beast2 {
22 :
23 : struct section
24 : {
25 : BOOST_BEAST2_DECL
26 : section() noexcept;
27 :
28 : /** Return the name of this section
29 : */
30 : auto
31 : name() const noexcept ->
32 : core::string_view
33 : {
34 : return impl_->name;
35 : }
36 :
37 : /** Return the level below which logging is squelched
38 : */
39 0 : int threshold() const noexcept
40 : {
41 0 : return impl_->level;
42 : }
43 :
44 : template<class... Args>
45 0 : void operator()(
46 : core::string_view const& fs,
47 : Args const&... args)
48 : {
49 0 : int level = 0;
50 0 : std::string s;
51 0 : format_to(s, fs, args...);
52 0 : write(level, std::move(s));
53 0 : }
54 :
55 : private:
56 : BOOST_BEAST2_DECL
57 : void write(int, std::string);
58 :
59 : section(core::string_view);
60 :
61 : friend class log_sections;
62 :
63 : struct impl
64 : {
65 : std::string name;
66 : int level = 0;
67 : };
68 :
69 : std::shared_ptr<impl> impl_;
70 : };
71 :
72 : //------------------------------------------------
73 :
74 : #if 0
75 : class log_stream
76 : {
77 : public:
78 : log_stream() = default;
79 :
80 : log_stream(section& sect, int level)
81 : : sect_(§)
82 : , level_(level)
83 : {
84 : }
85 :
86 : template<class... Args>
87 : void operator()(Args const&... args)
88 : {
89 : if( sect_)
90 : sect_->operator()(args...);
91 : }
92 :
93 : private:
94 : section* sect_ = nullptr;
95 : int level_ = 0;
96 : };
97 : #endif
98 :
99 : //------------------------------------------------
100 :
101 : class log_sections
102 : {
103 : public:
104 : /** Destructor
105 : */
106 : BOOST_BEAST2_DECL
107 : ~log_sections();
108 :
109 : /** Constructor
110 : */
111 : BOOST_BEAST2_DECL
112 : log_sections();
113 :
114 : /** Return a log section by name.
115 :
116 : If the section does not already exist, it is created.
117 : The name is case sensitive.
118 : */
119 : BOOST_BEAST2_DECL
120 : section
121 : get(core::string_view name);
122 :
123 : BOOST_BEAST2_DECL
124 : auto
125 : get_sections() const noexcept ->
126 : std::vector<section>;
127 :
128 : private:
129 : struct impl;
130 : impl* impl_;
131 : };
132 :
133 : //------------------------------------------------
134 :
135 : #ifndef LOG_AT_LEVEL
136 : #define LOG_AT_LEVEL(sect, level) \
137 : if((level) < (sect).threshold()) {} else sect
138 : #endif
139 :
140 : /// Log at trace level
141 : #ifndef LOG_TRC
142 : #define LOG_TRC(sect) LOG_AT_LEVEL(sect, 0)
143 : #endif
144 :
145 : /// Log at debug level
146 : #ifndef LOG_DBG
147 : #define LOG_DBG(sect) LOG_AT_LEVEL(sect, 1)
148 : #endif
149 :
150 : /// Log at info level (normal)
151 : #ifndef LOG_INF
152 : #define LOG_INF(sect) LOG_AT_LEVEL(sect, 2)
153 : #endif
154 :
155 : /// Log at warning level
156 : #ifndef LOG_WRN
157 : #define LOG_WRN(sect) LOG_AT_LEVEL(sect, 3)
158 : #endif
159 :
160 : /// Log at error level
161 : #ifndef LOG_ERR
162 : #define LOG_ERR(sect) LOG_AT_LEVEL(sect, 4)
163 : #endif
164 :
165 : /// Log at fatal level
166 : #ifndef LOG_FTL
167 : #define LOG_FTL(sect, ...) LOG_AT_LEVEL(sect, 5, __VA_ARGS__)
168 : #endif
169 :
170 : } // beast2
171 : } // boost
172 :
173 : #endif
|