Skip to content

Commit d6c255a

Browse files
committed
Added a Boost.Log example cpp-netlib#14
1 parent 5b0dc2a commit d6c255a

File tree

1 file changed

+117
-0
lines changed

1 file changed

+117
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
// Copyright 2009 (c) Tarro, Inc.
2+
// Copyright 2009 (c) Dean Michael Berris <[email protected]>
3+
// Copyright 2011 (c) Matt Leisinger <[email protected]>
4+
// Distributed under the Boost Software License, Version 1.0.
5+
// (See accompanying file LICENSE_1_0.txt or copy at
6+
// http://www.boost.org/LICENSE_1_0.txt)
7+
//
8+
9+
//[ hello_world_server_main
10+
/*`
11+
This is a part of the 'Hello World' example. It's used to
12+
demonstrate how easy it is to set up an HTTP server. All we do in
13+
this example is create a request handler and run the server. This
14+
version of the server simply adds logging
15+
*/
16+
#include <iostream>
17+
18+
#include <boost/network/protocol/http/server.hpp>
19+
20+
#include <boost/log/common.hpp>
21+
#include <boost/log/formatters.hpp>
22+
#include <boost/log/filters.hpp>
23+
24+
#include <boost/log/utility/init/to_file.hpp>
25+
#include <boost/log/utility/init/to_console.hpp>
26+
#include <boost/log/utility/init/common_attributes.hpp>
27+
28+
#include <boost/log/attributes.hpp>
29+
#include <boost/log/attributes/timer.hpp>
30+
31+
namespace http = boost::network::http;
32+
namespace logging = boost::log;
33+
namespace fmt = boost::log::formatters;
34+
namespace flt = boost::log::filters;
35+
namespace sinks = boost::log::sinks;
36+
namespace attrs = boost::log::attributes;
37+
namespace src = boost::log::sources;
38+
namespace keywords = boost::log::keywords;
39+
40+
/*<< Defines the server. >>*/
41+
struct hello_world;
42+
typedef http::server<hello_world> server;
43+
44+
/*<< Defines the request handler. It's a class that defines two
45+
functions, `operator()` and `log()` >>*/
46+
struct hello_world {
47+
/*<< This is the function that handles the incoming request. >>*/
48+
void operator() (server::request const &request,
49+
server::response &response) {
50+
server::string_type ip = source(request);
51+
std::ostringstream data;
52+
data << "Hello, " << ip << "!";
53+
response = server::response::stock_reply(
54+
server::response::ok, data.str());
55+
}
56+
/*<< Using Boost.Log we'll simply log the number of errors >>*/
57+
void log(const char *error) {
58+
BOOST_LOG(lg) << "Error occured (" << error << "). Total number of errors reported is " << ++numErrors;
59+
}
60+
61+
/*<< Catch all for any other errors >>*/
62+
void log(...) {
63+
BOOST_LOG(lg) << "Unknown error occured. Total number of error reported is " << ++numErrors;
64+
}
65+
private:
66+
src::logger_mt lg;
67+
uint32_t numErrors;
68+
69+
};
70+
71+
/*<< Sets up two logger sinks, one to the console and the other to a file >>*/
72+
void init_logging()
73+
{
74+
/*<< Setup the console logger >>*/
75+
logging::init_log_to_console(std::clog, keywords::format = "%TimeStamp%: %_%");
76+
77+
/*<< Setup the file logger >>*/
78+
logging::init_log_to_file
79+
(
80+
"hello_world_server.log",
81+
keywords::format = fmt::format("[%1%] %2%")
82+
% fmt::date_time("TimeStamp", std::nothrow)
83+
% fmt::message(),
84+
keywords::auto_flush = true
85+
);
86+
87+
logging::add_common_attributes();
88+
}
89+
90+
int main(int argc, char * argv[]) {
91+
92+
if (argc != 3) {
93+
std::cerr << "Usage: " << argv[0] << " address port" << std::endl;
94+
return 1;
95+
}
96+
97+
init_logging();
98+
src::logger_mt lg;
99+
100+
try {
101+
/*<< Creates the request handler. >>*/
102+
hello_world handler;
103+
/*<< Creates the server. >>*/
104+
server server_(argv[1], argv[2], handler);
105+
/*<< Write to the log source >>*/
106+
BOOST_LOG(lg) << "Starting HTTP Hello World Server...";
107+
/*<< Runs the server. >>*/
108+
server_.run();
109+
}
110+
catch (std::exception &e) {
111+
std::cerr << e.what() << std::endl;
112+
return 1;
113+
}
114+
115+
return 0;
116+
}
117+
//]

0 commit comments

Comments
 (0)