Add log_facility for non-Windows OSs.

This commit is contained in:
Roger A. Light 2014-11-02 21:49:33 +00:00
parent 77ffd9f602
commit fc1e514ad4
8 changed files with 78 additions and 18 deletions

View File

@ -46,6 +46,8 @@ Broker:
- When "require_certificate" was false, the broker was incorrectly asking for
a certificate (but not checking it). This caused problems with some clients
and has been fixed so the broker no longer asks.
- When using syslog logging on non-Windows OSs, it is now possible to specify
the logging facility to one of local0-7 instead of the default "daemon".
Clients:
- Both clients can now load default configuration options from a file.

View File

@ -300,6 +300,17 @@
<para>Reloaded on reload signal.</para>
</listitem>
</varlistentry>
<varlistentry>
<term><option>log_facility</option> <replaceable>local facility</replaceable></term>
<listitem>
<para>If using syslog logging (not on Windows), messages
will be logged to the "daemon" facility by default. Use
the <option>log_facility</option> option to choose
which of local0 to local7 to log to instead. The option
value should be an integer value, e.g. "log_facility 5"
to use local5.</para>
</listitem>
</varlistentry>
<varlistentry>
<term><option>log_timestamp</option> [ true | false ]</term>
<listitem>

View File

@ -461,6 +461,12 @@
# Use "log_dest none" if you wish to disable logging.
#log_dest stderr
# If using syslog logging (not on Windows), messages will be logged to the
# "daemon" facility by default. Use the log_facility option to choose which of
# local0 to local7 to log to instead. The option value should be an integer
# value, e.g. "log_facility 5" to use local5.
#log_facility
# Types of messages to log. Use multiple log_type lines for logging
# multiple types of messages.
# Possible types are: debug, error, warning, notice, information,

View File

@ -35,6 +35,10 @@ Contributors:
# include <ws2tcpip.h>
#endif
#if !defined(WIN32) && !defined(__CYGWIN__)
# include <sys/syslog.h>
#endif
#include <mosquitto_broker.h>
#include <memory_mosq.h>
#include "tls_mosq.h"
@ -130,6 +134,7 @@ static void _config_init_reload(struct mqtt3_config *config)
config->log_dest = MQTT3_LOG_STDERR;
}
#else
config->log_facility = LOG_DAEMON;
config->log_dest = MQTT3_LOG_STDERR;
if(config->verbose){
config->log_type = INT_MAX;
@ -534,7 +539,7 @@ int _config_read_file_core(struct mqtt3_config *config, bool reload, const char
int rc;
char buf[1024];
char *token;
int port_tmp;
int tmp_int;
char *saveptr = NULL;
#ifdef WITH_BRIDGE
struct _mqtt3_bridge *cur_bridge = NULL;
@ -598,12 +603,12 @@ int _config_read_file_core(struct mqtt3_config *config, bool reload, const char
if(address){
token = strtok_r(NULL, ":", &saveptr);
if(token){
port_tmp = atoi(token);
if(port_tmp < 1 || port_tmp > 65535){
_mosquitto_log_printf(NULL, MOSQ_LOG_ERR, "Error: Invalid port value (%d).", port_tmp);
tmp_int = atoi(token);
if(tmp_int < 1 || tmp_int > 65535){
_mosquitto_log_printf(NULL, MOSQ_LOG_ERR, "Error: Invalid port value (%d).", tmp_int);
return MOSQ_ERR_INVAL;
}
cur_bridge->addresses[i].port = port_tmp;
cur_bridge->addresses[i].port = tmp_int;
}else{
cur_bridge->addresses[i].port = 1883;
}
@ -1139,15 +1144,15 @@ int _config_read_file_core(struct mqtt3_config *config, bool reload, const char
_mosquitto_log_printf(NULL, MOSQ_LOG_ERR, "Error: Out of memory.");
return MOSQ_ERR_NOMEM;
}
port_tmp = atoi(token);
if(port_tmp < 1 || port_tmp > 65535){
_mosquitto_log_printf(NULL, MOSQ_LOG_ERR, "Error: Invalid port value (%d).", port_tmp);
tmp_int = atoi(token);
if(tmp_int < 1 || tmp_int > 65535){
_mosquitto_log_printf(NULL, MOSQ_LOG_ERR, "Error: Invalid port value (%d).", tmp_int);
return MOSQ_ERR_INVAL;
}
cur_listener = &config->listeners[config->listener_count-1];
memset(cur_listener, 0, sizeof(struct _mqtt3_listener));
cur_listener->protocol = mp_mqtt;
cur_listener->port = port_tmp;
cur_listener->port = tmp_int;
token = strtok_r(NULL, " ", &saveptr);
if(token){
cur_listener->host = _mosquitto_strdup(token);
@ -1286,6 +1291,41 @@ int _config_read_file_core(struct mqtt3_config *config, bool reload, const char
_mosquitto_log_printf(NULL, MOSQ_LOG_ERR, "Error: Empty log_dest value in configuration.");
return MOSQ_ERR_INVAL;
}
}else if(!strcmp(token, "log_facility")){
#if defined(WIN32) || defined(__CYGWIN__)
_mosquitto_log_printf(NULL, MOSQ_LOG_WARNING, "Warning: log_facility not supported on Windows.");
#else
if(_conf_parse_int(&token, "log_facility", &tmp_int, saveptr)) return MOSQ_ERR_INVAL;
switch(tmp_int){
case 0:
config->log_facility = LOG_LOCAL0;
break;
case 1:
config->log_facility = LOG_LOCAL1;
break;
case 2:
config->log_facility = LOG_LOCAL2;
break;
case 3:
config->log_facility = LOG_LOCAL3;
break;
case 4:
config->log_facility = LOG_LOCAL4;
break;
case 5:
config->log_facility = LOG_LOCAL5;
break;
case 6:
config->log_facility = LOG_LOCAL6;
break;
case 7:
config->log_facility = LOG_LOCAL7;
break;
default:
_mosquitto_log_printf(NULL, MOSQ_LOG_ERR, "Error: Invalid log_facility value (%d).", tmp_int);
return MOSQ_ERR_INVAL;
}
#endif
}else if(!strcmp(token, "log_timestamp")){
if(_conf_parse_bool(&token, token, &config->log_timestamp, saveptr)) return MOSQ_ERR_INVAL;
}else if(!strcmp(token, "log_type")){
@ -1459,12 +1499,12 @@ int _config_read_file_core(struct mqtt3_config *config, bool reload, const char
if(config->default_listener.port){
_mosquitto_log_printf(NULL, MOSQ_LOG_WARNING, "Warning: Default listener port specified multiple times. Only the latest will be used.");
}
if(_conf_parse_int(&token, "port", &port_tmp, saveptr)) return MOSQ_ERR_INVAL;
if(port_tmp < 1 || port_tmp > 65535){
_mosquitto_log_printf(NULL, MOSQ_LOG_ERR, "Error: Invalid port value (%d).", port_tmp);
if(_conf_parse_int(&token, "port", &tmp_int, saveptr)) return MOSQ_ERR_INVAL;
if(tmp_int < 1 || tmp_int > 65535){
_mosquitto_log_printf(NULL, MOSQ_LOG_ERR, "Error: Invalid port value (%d).", tmp_int);
return MOSQ_ERR_INVAL;
}
config->default_listener.port = port_tmp;
config->default_listener.port = tmp_int;
}else if(!strcmp(token, "protocol")){
token = strtok_r(NULL, " ", &saveptr);
if(token){

View File

@ -49,7 +49,7 @@ HANDLE syslog_h;
static int log_destinations = MQTT3_LOG_STDERR;
static int log_priorities = MOSQ_LOG_ERR | MOSQ_LOG_WARNING | MOSQ_LOG_NOTICE | MOSQ_LOG_INFO;
int mqtt3_log_init(int priorities, int destinations)
int mqtt3_log_init(int priorities, int destinations, int facility)
{
int rc = 0;
@ -58,7 +58,7 @@ int mqtt3_log_init(int priorities, int destinations)
if(log_destinations & MQTT3_LOG_SYSLOG){
#ifndef WIN32
openlog("mosquitto", LOG_PID, LOG_DAEMON);
openlog("mosquitto", LOG_PID|LOG_CONS, facility);
#else
syslog_h = OpenEventLog(NULL, "mosquitto");
#endif

View File

@ -314,7 +314,7 @@ int mosquitto_main_loop(struct mosquitto_db *db, int *listensock, int listensock
mosquitto_security_cleanup(db, true);
mosquitto_security_init(db, true);
mosquitto_security_apply(db);
mqtt3_log_init(db->config->log_type, db->config->log_dest);
mqtt3_log_init(db->config->log_type, db->config->log_dest, db->config->log_facility);
flag_reload = false;
}
if(flag_tree_print){

View File

@ -239,7 +239,7 @@ int main(int argc, char *argv[])
/* Initialise logging only after initialising the database in case we're
* logging to topics */
mqtt3_log_init(config.log_type, config.log_dest);
mqtt3_log_init(config.log_type, config.log_dest, config.log_facility);
_mosquitto_log_printf(NULL, MOSQ_LOG_INFO, "mosquitto version %s (build date %s) starting", VERSION, TIMESTAMP);
if(config.config_file){
_mosquitto_log_printf(NULL, MOSQ_LOG_INFO, "Config loaded from %s.", config.config_file);

View File

@ -97,6 +97,7 @@ struct mqtt3_config {
struct _mqtt3_listener *listeners;
int listener_count;
int log_dest;
int log_facility;
int log_type;
bool log_timestamp;
char *log_file;
@ -404,7 +405,7 @@ void mosquitto__free_disused_contexts(struct mosquitto_db *db);
/* ============================================================
* Logging functions
* ============================================================ */
int mqtt3_log_init(int level, int destinations);
int mqtt3_log_init(int level, int destinations, int facility);
int mqtt3_log_close(void);
int _mosquitto_log_printf(struct mosquitto *mosq, int level, const char *fmt, ...) __attribute__((format(printf, 3, 4)));