Skip to content

Commit 3dd34d5

Browse files
committed
log work
1 parent cfd6c6f commit 3dd34d5

File tree

7 files changed

+97
-6
lines changed

7 files changed

+97
-6
lines changed

example/server/http_responder.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ class http_responder
3737
http_responder(
3838
server& srv,
3939
router_type& rr)
40-
: id_(
40+
: sect_(srv.sections().get("http_responder"))
41+
, id_(
4142
[]() noexcept
4243
{
4344
static std::size_t n = 0;

include/boost/http_io/server/impl/workers.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ workers(
6666
std::size_t num_workers,
6767
Args&&... args)
6868
: srv_(srv)
69+
, sect_(srv.sections().get("workers"))
6970
, ex_(ex)
7071
, vw_(num_workers)
7172
, concurrency_(concurrency)

include/boost/http_io/server/logger.hpp

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,24 @@
1212

1313
#include <boost/http_io/detail/config.hpp>
1414
#include <boost/core/detail/string_view.hpp>
15+
#include <memory>
1516
#include <sstream>
17+
#include <string>
1618

1719
namespace boost {
1820
namespace http_io {
1921

2022
struct section
2123
{
22-
int threshold() const noexcept { return 0; }
24+
BOOST_HTTP_IO_DECL
25+
section() noexcept;
26+
27+
/** Return the level below which logging is squelched
28+
*/
29+
int threshold() const noexcept
30+
{
31+
return impl_->level;
32+
}
2333

2434
template<class... Args>
2535
void operator()(
@@ -72,6 +82,18 @@ struct section
7282

7383
BOOST_HTTP_IO_DECL
7484
void write(int, boost::core::string_view);
85+
86+
section(core::string_view);
87+
88+
friend class log_sections;
89+
90+
struct impl
91+
{
92+
std::string name;
93+
int level = 0;
94+
};
95+
96+
std::shared_ptr<impl> impl_;
7597
};
7698

7799
//------------------------------------------------
@@ -103,7 +125,6 @@ class log_stream
103125

104126
//------------------------------------------------
105127

106-
#if 0
107128
class log_sections
108129
{
109130
public:
@@ -130,7 +151,6 @@ class log_sections
130151
struct impl;
131152
impl* impl_;
132153
};
133-
#endif
134154

135155
//------------------------------------------------
136156

include/boost/http_io/server/server.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
namespace boost {
1919
namespace http_io {
2020

21+
class log_sections;
22+
2123
/** A generic server interface
2224
2325
In this model a server contains zero or more parts, where each part
@@ -73,6 +75,9 @@ class BOOST_SYMBOL_VISIBLE
7375
BOOST_HTTP_IO_DECL
7476
rts::context& services() noexcept;
7577

78+
BOOST_HTTP_IO_DECL
79+
log_sections& sections() noexcept;
80+
7681
BOOST_HTTP_IO_DECL
7782
void install(std::unique_ptr<part> pp);
7883

include/boost/http_io/server/workers.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,12 @@ class workers
115115
void do_stop();
116116

117117
http_io::server& srv_;
118+
section sect_;
118119
Executor ex_;
119120
fixed_array<worker> vw_;
120121
std::vector<acceptor> va_;
121122
worker* idle_ = nullptr;
122123
std::size_t n_idle_ = 0;
123-
section sect_;
124124
unsigned concurrency_;
125125
};
126126

src/server/logger.cpp

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,14 @@
1010
#include <boost/http_io/server/logger.hpp>
1111
#include <iostream>
1212
#include <mutex>
13+
#include <unordered_map>
1314

1415
namespace boost {
1516
namespace http_io {
1617

18+
section::
19+
section() noexcept = default;
20+
1721
void
1822
section::
1923
write(
@@ -36,7 +40,8 @@ format_impl(
3640
std::size_t* plen,
3741
std::size_t n)
3842
{
39-
std::string s;
43+
std::string s = impl_->name;
44+
s.push_back(' ');
4045
char const* p = fs.data();
4146
char const* end = fs.data() + fs.size();
4247
auto p0 = p;
@@ -63,5 +68,55 @@ format_impl(
6368
write(level, s);
6469
}
6570

71+
section::
72+
section(core::string_view name)
73+
: impl_(std::make_shared<impl>())
74+
{
75+
impl_->name = name;
76+
}
77+
78+
//------------------------------------------------
79+
80+
struct log_sections::impl
81+
{
82+
struct hash
83+
{
84+
std::size_t
85+
operator()(core::string_view const& s) const noexcept
86+
{
87+
std::size_t hash = 1469598103934665603ull; // FNV offset basis
88+
for (unsigned char c : s)
89+
hash ^= c, hash *= 1099511628211ull; // FNV prime
90+
return hash;
91+
}
92+
};
93+
94+
std::unordered_map<core::string_view, section, hash> map;
95+
};
96+
97+
log_sections::
98+
~log_sections()
99+
{
100+
delete impl_;
101+
}
102+
103+
log_sections::
104+
log_sections()
105+
: impl_(new impl)
106+
{
107+
}
108+
109+
section
110+
log_sections::
111+
get(core::string_view name)
112+
{
113+
auto it = impl_->map.find(name);
114+
if(it != impl_->map.end())
115+
return it->second;
116+
auto v = section(name);
117+
impl_->map.emplace( core::string_view(v.impl_->name), v);
118+
return v;
119+
}
120+
66121
} // http_io
67122
} // boost

src/server/server.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
// Official repository: https://github.com/cppalliance/http_io
88
//
99

10+
#include <boost/http_io/server/logger.hpp>
1011
#include <boost/http_io/server/server.hpp>
1112
#include <boost/http_io/detail/except.hpp>
1213
#include <mutex>
@@ -28,6 +29,7 @@ struct server::impl
2829
{
2930
std::mutex m;
3031
rts::context services;
32+
log_sections sections;
3133
std::vector<std::unique_ptr<part>> v;
3234
state st = state::none;
3335
};
@@ -62,6 +64,13 @@ services() noexcept
6264
return impl_->services;
6365
}
6466

67+
log_sections&
68+
server::
69+
sections() noexcept
70+
{
71+
return impl_->sections;
72+
}
73+
6574
void
6675
server::
6776
install(std::unique_ptr<part> pp)

0 commit comments

Comments
 (0)