Skip to content

Commit

Permalink
Add websockets logging options.
Browse files Browse the repository at this point in the history
  • Loading branch information
ralight committed Feb 13, 2015
1 parent 6fd38b8 commit db86809
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 7 deletions.
1 change: 1 addition & 0 deletions lib/mosquitto.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ extern "C" {
#define MOSQ_LOG_DEBUG 0x10
#define MOSQ_LOG_SUBSCRIBE 0x20
#define MOSQ_LOG_UNSUBSCRIBE 0x40
#define MOSQ_LOG_WEBSOCKETS 0x80
#define MOSQ_LOG_ALL 0xFFFF

/* Error values */
Expand Down
21 changes: 19 additions & 2 deletions man/mosquitto.conf.5.xml
Original file line number Diff line number Diff line change
Expand Up @@ -332,9 +332,12 @@
<replaceable>warning</replaceable>,
<replaceable>notice</replaceable>,
<replaceable>information</replaceable>,
<replaceable>subscribe</replaceable>,
<replaceable>unsubscribe</replaceable>,
<replaceable>websockets</replaceable>,
<replaceable>none</replaceable>,
<replaceable>all</replaceable>. Defaults to
<replaceable>error</replaceable>,
<replaceable>all</replaceable>.</para>
<para>Defaults to <replaceable>error</replaceable>,
<replaceable>warning</replaceable>, <replaceable>notice
</replaceable>and
<replaceable>information</replaceable>. This option
Expand Down Expand Up @@ -711,6 +714,20 @@
<para>Not reloaded on reload signal.</para>
</listitem>
</varlistentry>
<varlistentry>
<term><option>websockets_log_level</option> <replaceable>level</replaceable></term>
<listitem>
<para>Change the websockets logging level. This is a
global option, it is not possible to set per
listener. This is an integer that is interpreted by
libwebsockets as a bit mask for its lws_log_levels
enum. See the libwebsockets documentation for more
details.</para>
<para>To use this option, <option>log_type
websockets</option> must also be enabled.
Defaults to 0.</para>
</listitem>
</varlistentry>
</variablelist>
</refsect2>
<refsect2>
Expand Down
9 changes: 8 additions & 1 deletion mosquitto.conf
Original file line number Diff line number Diff line change
Expand Up @@ -470,14 +470,21 @@
# Types of messages to log. Use multiple log_type lines for logging
# multiple types of messages.
# Possible types are: debug, error, warning, notice, information,
# none, subscribe, unsubscribe, all.
# none, subscribe, unsubscribe, websockets, all.
# Note that debug type messages are for decoding the incoming/outgoing
# network packets. They are not logged in "topics".
#log_type error
#log_type warning
#log_type notice
#log_type information

# Change the websockets logging level. This is a global option, it is not
# possible to set per listener. This is an integer that is interpreted by
# libwebsockets as a bit mask for its lws_log_levels enum. See the
# libwebsockets documentation for more details. "log_type websockets" must also
# be enabled.
#websockets_log_level 0

# If set to true, client connection and disconnection messages will be included
# in the log.
#connection_messages true
Expand Down
10 changes: 10 additions & 0 deletions src/conf.c
Original file line number Diff line number Diff line change
Expand Up @@ -1381,6 +1381,10 @@ int _config_read_file_core(struct mqtt3_config *config, bool reload, const char
cr->log_type |= MOSQ_LOG_SUBSCRIBE;
}else if(!strcmp(token, "unsubscribe")){
cr->log_type |= MOSQ_LOG_UNSUBSCRIBE;
#ifdef WITH_WEBSOCKETS
}else if(!strcmp(token, "websockets")){
cr->log_type |= MOSQ_LOG_WEBSOCKETS;
#endif
}else if(!strcmp(token, "all")){
cr->log_type = INT_MAX;
}else{
Expand Down Expand Up @@ -1884,6 +1888,12 @@ int _config_read_file_core(struct mqtt3_config *config, bool reload, const char
}
#else
_mosquitto_log_printf(NULL, MOSQ_LOG_WARNING, "Warning: Bridge support not available.");
#endif
}else if(!strcmp(token, "websockets_log_level")){
#ifdef WITH_WEBSOCKETS
if(_conf_parse_int(&token, "websockets_log_level", &config->websockets_log_level, saveptr)) return MOSQ_ERR_INVAL;
#else
_mosquitto_log_printf(NULL, MOSQ_LOG_WARNING, "Warning: Websockets support not available.");
#endif
}else if(!strcmp(token, "trace_level")
|| !strcmp(token, "ffdc_output")
Expand Down
10 changes: 10 additions & 0 deletions src/logging.c
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,16 @@ int _mosquitto_log_vprintf(struct mosquitto *mosq, int priority, const char *fmt
syslog_priority = EVENTLOG_INFORMATION_TYPE;
#endif
break;
#ifdef WITH_WEBSOCKETS
case MOSQ_LOG_WEBSOCKETS:
topic = "$SYS/broker/log/WS";
#ifndef WIN32
syslog_priority = LOG_DEBUG;
#else
syslog_priority = EVENTLOG_INFORMATION_TYPE;
#endif
break;
#endif
default:
topic = "$SYS/broker/log/E";
#ifndef WIN32
Expand Down
2 changes: 1 addition & 1 deletion src/mosquitto.c
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ int main(int argc, char *argv[])
}
}else if(config.listeners[i].protocol == mp_websockets){
#ifdef WITH_WEBSOCKETS
config.listeners[i].ws_context = mosq_websockets_init(&config.listeners[i]);
config.listeners[i].ws_context = mosq_websockets_init(&config.listeners[i], config.websockets_log_level);
if(!config.listeners[i].ws_context){
_mosquitto_log_printf(NULL, MOSQ_LOG_ERR, "Error: Unable to create websockets listener on port %d.", config.listeners[i].port);
return 1;
Expand Down
5 changes: 4 additions & 1 deletion src/mosquitto_broker.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ struct mqtt3_config {
bool upgrade_outgoing_qos;
char *user;
bool verbose;
#ifdef WITH_WEBSOCKETS
int websockets_log_level;
#endif
#ifdef WITH_BRIDGE
struct _mqtt3_bridge *bridges;
int bridge_count;
Expand Down Expand Up @@ -476,7 +479,7 @@ void service_run(void);
* Websockets related functions
* ============================================================ */
#ifdef WITH_WEBSOCKETS
struct libwebsocket_context *mosq_websockets_init(struct _mqtt3_listener *listener);
struct libwebsocket_context *mosq_websockets_init(struct _mqtt3_listener *listener, int log_level);
#endif
void do_disconnect(struct mosquitto_db *db, struct mosquitto *context);

Expand Down
12 changes: 10 additions & 2 deletions src/websockets.c
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,14 @@ static int callback_http(struct libwebsocket_context *context,
return 0;
}

struct libwebsocket_context *mosq_websockets_init(struct _mqtt3_listener *listener)
static void log_wrap(int level, const char *line)
{
char *l = (char *)line;
l[strlen(line)-1] = '\0'; // Remove \n
_mosquitto_log_printf(NULL, MOSQ_LOG_WEBSOCKETS, "%s", l);
}

struct libwebsocket_context *mosq_websockets_init(struct _mqtt3_listener *listener, int log_level)
{
struct lws_context_creation_info info;
struct libwebsocket_protocols *p;
Expand Down Expand Up @@ -538,7 +545,8 @@ struct libwebsocket_context *mosq_websockets_init(struct _mqtt3_listener *listen
info.user = user;
listener->ws_protocol = p;

lws_set_log_level(0, NULL);
printf("log level: %d\n", log_level);
lws_set_log_level(log_level, log_wrap);

_mosquitto_log_printf(NULL, MOSQ_LOG_INFO, "Opening websockets listen socket on port %d.", listener->port);
return libwebsocket_create_context(&info);
Expand Down

0 comments on commit db86809

Please sign in to comment.