Skip to content

Commit cbe8875

Browse files
author
Peter Thorson
committed
Documentation and code style for syslog logger references XRPLF#386
1 parent 8a9b0be commit cbe8875

File tree

4 files changed

+145
-30
lines changed

4 files changed

+145
-30
lines changed

changelog.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ HEAD
55
- Security: Disabled SSLv3 in example servers.
66
- Feature: Adds basic support for accessing HTTP request bodies in the http
77
handler. #181
8-
- Feature: Adds the ability to register a shutdown handler when using the
9-
iostream transport. This provides a clean interface for triggering the shut
8+
- Feature: Adds the ability to register a shutdown handler when using the
9+
iostream transport. This provides a clean interface for triggering the shut
1010
down of external sockets and other cleanup without hooking in to higher level
1111
WebSocket handlers.
1212
- Feature: Adds the ability to register a write handler when using the iostream
1313
transport. This handler can be used to handle transport output in place of
1414
registering an ostream to write to.
15+
- Feature: Adds a new logging policy that outputs to syslog. #386 Thank you Tom
16+
Hughes for submitting the initial version of this policy.
1517
- Improvement: Message payload logging now prints text for text messages rather
1618
than binary.
1719
- Documentation: Add Sending & Receiving Messages step to chapter one of the

examples/debug_server/debug_server.cpp

+46-2
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,55 @@
3131

3232
#include <websocketpp/config/debug_asio_no_tls.hpp>
3333

34+
// Custom logger
35+
#include <websocketpp/logger/syslog.hpp>
36+
3437
#include <websocketpp/server.hpp>
3538

3639
#include <iostream>
3740

38-
typedef websocketpp::server<websocketpp::config::debug_asio> server;
41+
////////////////////////////////////////////////////////////////////////////////
42+
///////////////// Custom Config for debugging custom policies //////////////////
43+
////////////////////////////////////////////////////////////////////////////////
44+
45+
struct debug_custom : public websocketpp::config::debug_asio {
46+
typedef debug_custom type;
47+
typedef debug_asio base;
48+
49+
typedef base::concurrency_type concurrency_type;
50+
51+
typedef base::request_type request_type;
52+
typedef base::response_type response_type;
53+
54+
typedef base::message_type message_type;
55+
typedef base::con_msg_manager_type con_msg_manager_type;
56+
typedef base::endpoint_msg_manager_type endpoint_msg_manager_type;
57+
58+
/// Custom Logging policies
59+
typedef websocketpp::log::syslog<concurrency_type,
60+
websocketpp::log::elevel> elog_type;
61+
typedef websocketpp::log::syslog<concurrency_type,
62+
websocketpp::log::alevel> alog_type;
63+
64+
typedef base::rng_type rng_type;
65+
66+
struct transport_config : public base::transport_config {
67+
typedef type::concurrency_type concurrency_type;
68+
typedef type::alog_type alog_type;
69+
typedef type::elog_type elog_type;
70+
typedef type::request_type request_type;
71+
typedef type::response_type response_type;
72+
typedef websocketpp::transport::asio::basic_socket::endpoint
73+
socket_type;
74+
};
75+
76+
typedef websocketpp::transport::asio::endpoint<transport_config>
77+
transport_type;
78+
};
79+
80+
////////////////////////////////////////////////////////////////////////////////
81+
82+
typedef websocketpp::server<debug_custom> server;
3983

4084
using websocketpp::lib::placeholders::_1;
4185
using websocketpp::lib::placeholders::_2;
@@ -93,7 +137,7 @@ int main() {
93137

94138
// Register our message handler
95139
echo_server.set_message_handler(bind(&on_message,&echo_server,::_1,::_2));
96-
140+
97141
echo_server.set_http_handler(bind(&on_http,&echo_server,::_1));
98142
echo_server.set_fail_handler(&on_fail);
99143
echo_server.set_close_handler(&on_close);

websocketpp/logger/stub.hpp

+10-10
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class stub {
4545
* @param hint A channel type specific hint for how to construct the logger
4646
*/
4747
explicit stub(channel_type_hint::value) {}
48-
48+
4949
/// Construct the logger
5050
/**
5151
* @param default_channels A set of channels to statically enable
@@ -56,16 +56,16 @@ class stub {
5656

5757
/// Dynamically enable the given list of channels
5858
/**
59-
* All operations on the stub logger are no-ops and all arguments are
59+
* All operations on the stub logger are no-ops and all arguments are
6060
* ignored
6161
*
6262
* @param channels The package of channels to enable
6363
*/
6464
void set_channels(level) {}
65-
65+
6666
/// Dynamically disable the given list of channels
6767
/**
68-
* All operations on the stub logger are no-ops and all arguments are
68+
* All operations on the stub logger are no-ops and all arguments are
6969
* ignored
7070
*
7171
* @param channels The package of channels to disable
@@ -76,34 +76,34 @@ class stub {
7676
/**
7777
* Writing on the stub logger is a no-op and all arguments are ignored
7878
*
79-
* @param channel The package of channels to write to
79+
* @param channel The channel to write to
8080
* @param msg The message to write
8181
*/
8282
void write(level, std::string const &) {}
83-
83+
8484
/// Write a cstring message to the given channel
8585
/**
8686
* Writing on the stub logger is a no-op and all arguments are ignored
8787
*
88-
* @param channel The package of channels to write to
88+
* @param channel The channel to write to
8989
* @param msg The message to write
9090
*/
9191
void write(level, char const *) {}
9292

9393
/// Test whether a channel is statically enabled
9494
/**
95-
* The stub logger has no channels so all arguments are ignored and
95+
* The stub logger has no channels so all arguments are ignored and
9696
* `static_test` always returns false.
9797
*
9898
* @param channel The package of channels to test
9999
*/
100100
_WEBSOCKETPP_CONSTEXPR_TOKEN_ bool static_test(level) const {
101101
return false;
102102
}
103-
103+
104104
/// Test whether a channel is dynamically enabled
105105
/**
106-
* The stub logger has no channels so all arguments are ignored and
106+
* The stub logger has no channels so all arguments are ignored and
107107
* `dynamic_test` always returns false.
108108
*
109109
* @param channel The package of channels to test

websocketpp/logger/syslog.hpp

+85-16
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,33 @@
1+
/*
2+
* Copyright (c) 2014, Peter Thorson. All rights reserved.
3+
*
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions are met:
6+
* * Redistributions of source code must retain the above copyright
7+
* notice, this list of conditions and the following disclaimer.
8+
* * Redistributions in binary form must reproduce the above copyright
9+
* notice, this list of conditions and the following disclaimer in the
10+
* documentation and/or other materials provided with the distribution.
11+
* * Neither the name of the WebSocket++ Project nor the
12+
* names of its contributors may be used to endorse or promote products
13+
* derived from this software without specific prior written permission.
14+
*
15+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18+
* ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
19+
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21+
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22+
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25+
*
26+
*
27+
* The initial version of this logging policy was contributed to the WebSocket++
28+
* project by Tom Hughes.
29+
*/
30+
131
#ifndef WEBSOCKETPP_LOGGER_SYSLOG_HPP
232
#define WEBSOCKETPP_LOGGER_SYSLOG_HPP
333

@@ -15,37 +45,71 @@ namespace log {
1545
template <typename concurrency, typename names>
1646
class syslog : public basic<concurrency, names> {
1747
public:
18-
syslog<concurrency,names>(channel_type_hint::value h =
48+
typedef basic<concurrency, names> base;
49+
50+
/// Construct the logger
51+
/**
52+
* @param hint A channel type specific hint for how to construct the logger
53+
*/
54+
syslog<concurrency,names>(channel_type_hint::value hint =
1955
channel_type_hint::access)
20-
: basic<concurrency,names>(h), channel_type_hint_(h) {}
56+
: basic<concurrency,names>(hint), m_channel_type_hint(hint) {}
2157

22-
syslog<concurrency,names>(level c, channel_type_hint::value h =
58+
/// Construct the logger
59+
/**
60+
* @param channels A set of channels to statically enable
61+
* @param hint A channel type specific hint for how to construct the logger
62+
*/
63+
syslog<concurrency,names>(level channels, channel_type_hint::value hint =
2364
channel_type_hint::access)
24-
: basic<concurrency,names>(c, h), channel_type_hint_(h) {}
65+
: basic<concurrency,names>(channels, hint), m_channel_type_hint(hint) {}
2566

67+
/// Write a string message to the given channel
68+
/**
69+
* @param channel The channel to write to
70+
* @param msg The message to write
71+
*/
2672
void write(level channel, std::string const & msg) {
2773
write(channel, msg.c_str());
2874
}
2975

76+
/// Write a cstring message to the given channel
77+
/**
78+
* @param channel The channel to write to
79+
* @param msg The message to write
80+
*/
3081
void write(level channel, char const * msg) {
31-
scoped_lock_type lock(basic<concurrency,names>::m_lock);
82+
scoped_lock_type lock(base::m_lock);
3283
if (!this->dynamic_test(channel)) { return; }
33-
::syslog(syslog_priority(channel), "[%s] %s", names::channel_name(channel), msg);
84+
::syslog(syslog_priority(channel), "[%s] %s",
85+
names::channel_name(channel), msg);
3486
}
35-
3687
private:
37-
typedef typename basic<concurrency,names>::scoped_lock_type scoped_lock_type;
38-
const int kDefaultSyslogLevel = LOG_INFO;
88+
typedef typename base::scoped_lock_type scoped_lock_type;
89+
90+
/// The default level is used for all access logs and any error logs that
91+
/// don't trivially map to one of the standard syslog levels.
92+
static int const default_level = LOG_INFO;
3993

40-
const int syslog_priority(level channel) const {
41-
if (channel_type_hint_ == channel_type_hint::access) {
94+
/// retrieve the syslog priority code given a WebSocket++ channel
95+
/**
96+
* @param channel The level to look up
97+
* @return The syslog level associated with `channel`
98+
*/
99+
int syslog_priority(level channel) const {
100+
if (m_channel_type_hint == channel_type_hint::access) {
42101
return syslog_priority_access(channel);
43102
} else {
44103
return syslog_priority_error(channel);
45104
}
46105
}
47106

48-
const int syslog_priority_error(level channel) const {
107+
/// retrieve the syslog priority code given a WebSocket++ error channel
108+
/**
109+
* @param channel The level to look up
110+
* @return The syslog level associated with `channel`
111+
*/
112+
int syslog_priority_error(level channel) const {
49113
switch (channel) {
50114
case elevel::devel:
51115
return LOG_DEBUG;
@@ -60,15 +124,20 @@ class syslog : public basic<concurrency, names> {
60124
case elevel::fatal:
61125
return LOG_CRIT;
62126
default:
63-
return kDefaultSyslogLevel;
127+
return default_level;
64128
}
65129
}
66130

67-
_WEBSOCKETPP_CONSTEXPR_TOKEN_ int syslog_priority_access(level channel) const {
68-
return kDefaultSyslogLevel;
131+
/// retrieve the syslog priority code given a WebSocket++ access channel
132+
/**
133+
* @param channel The level to look up
134+
* @return The syslog level associated with `channel`
135+
*/
136+
_WEBSOCKETPP_CONSTEXPR_TOKEN_ int syslog_priority_access(level) const {
137+
return default_level;
69138
}
70139

71-
channel_type_hint::value channel_type_hint_;
140+
channel_type_hint::value m_channel_type_hint;
72141
};
73142

74143
} // log

0 commit comments

Comments
 (0)