Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Logging & Small fixes #119

Merged
merged 20 commits into from
Dec 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
[![Build Status](https://travis-ci.com/hivemq/mqtt-cli.svg?branch=develop)](https://travis-ci.com/hivemq/mqtt-cli)
[![picocli](https://img.shields.io/github/downloads/hivemq/mqtt-cli/total)](https://github.com/hivemq/mqtt-cli/releases)
[![picocli](https://img.shields.io/github/license/hivemq/mqtt-cli)](https://github.com/hivemq/mqtt-cli/blob/develop/LICENSE)
[![picocli](https://img.shields.io/badge/hivemq--mqtt--client-1.1.1-green)](https://github.com/hivemq/hivemq-mqtt-client)
[![picocli](https://img.shields.io/badge/hivemq--mqtt--client-1.1.3-green)](https://github.com/hivemq/hivemq-mqtt-client)
[![picocli](https://img.shields.io/badge/picocli-4.0.4-green.svg)](https://github.com/remkop/picocli)

MQTT 5.0 and 3.1.1 compatible and feature-rich MQTT Command Line Interface
Expand Down
11 changes: 6 additions & 5 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ ext {
jline3JansiVersion = '3.12.1'
daggerVersion = '2.21'
guavaVersion ='27.0.1-jre'
hivemqclientVersion = '1.1.1'
tinylogVersion = '1.3.6'
hivemqclientVersion = '1.1.3'
tinylogVersion = '2.0.1'
jcToolsVersion = '2.1.2'
jetbrainsAnnotationsVersion = '17.0.0'
bouncycastleVersion='1.62'
Expand All @@ -146,7 +146,8 @@ dependencies {
implementation group: 'info.picocli', name: 'picocli-codegen', version: picocliVersion

implementation group: 'com.google.guava', name: 'guava', version: guavaVersion
implementation group: 'org.tinylog', name: 'tinylog', version: tinylogVersion
implementation "org.tinylog:tinylog-api:$tinylogVersion"
implementation "org.tinylog:tinylog-impl:$tinylogVersion"
implementation group: 'org.jetbrains', name: 'annotations', version: jetbrainsAnnotationsVersion

implementation group: 'org.bouncycastle', name: 'bcprov-jdk15on', version: bouncycastleVersion
Expand Down Expand Up @@ -216,8 +217,8 @@ forbiddenApisMain {
}

forbiddenApis {
bundledSignatures = ['jdk-system-out']
ignoreFailures = true
bundledSignatures = ['jdk-deprecated', 'jdk-non-portable', 'jdk-reflection' ]
ignoreFailures = false
}

//disable
Expand Down
16 changes: 9 additions & 7 deletions src/main/java/com/hivemq/cli/DefaultCLIProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import com.hivemq.client.mqtt.MqttVersion;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.pmw.tinylog.Level;
import org.tinylog.Level;

import javax.inject.Singleton;
import java.io.*;
Expand All @@ -49,7 +49,7 @@ public class DefaultCLIProperties {
private static final String MQTT_VERSION = "mqtt.version";
private static final String HOST = "mqtt.host";
private static final String PORT = "mqtt.port";
private static final String DEBUG_LEVEL_SHELL = "debug.level.shell";
private static final String LOGFILE_DEBUG_LEVEL = "logfile.level";
private static final String CLIENT_ID_PREFIX = "client.id.prefix";
private static final String CLIENT_ID_LENGTH = "client.id.length";
private static final String SUBSCRIBE_OUTPUT_FILE = "client.subscribe.output";
Expand All @@ -67,7 +67,7 @@ public class DefaultCLIProperties {
put(MQTT_VERSION, "5");
put(HOST, "localhost");
put(PORT, "1883");
put(DEBUG_LEVEL_SHELL, "verbose");
put(LOGFILE_DEBUG_LEVEL, "debug");
put(CLIENT_ID_PREFIX, "mqtt");
put(CLIENT_ID_LENGTH, "8");
put(SUBSCRIBE_OUTPUT_FILE, null);
Expand Down Expand Up @@ -181,16 +181,18 @@ public int getPort() {
}

@NotNull
public Level getShellDebugLevel() {
final String shellDebugLevel = propertyToValue.get(DEBUG_LEVEL_SHELL);
switch (shellDebugLevel.toLowerCase()) {
public Level getLogfileDebugLevel() {
final String debugLevel = propertyToValue.get(LOGFILE_DEBUG_LEVEL);
switch (debugLevel.toLowerCase()) {
case "trace":
case "verbose":
return Level.TRACE;
case "debug": return Level.DEBUG;
case "info": return Level.INFO;
case "warn": return Level.WARN;
case "error": return Level.ERROR;
}
throw new IllegalArgumentException("'" + shellDebugLevel + "' is not a valid debug level");
throw new IllegalArgumentException("'" + debugLevel + "' is not a valid debug level");
}

@NotNull
Expand Down
12 changes: 1 addition & 11 deletions src/main/java/com/hivemq/cli/MqttCLIMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@
import com.hivemq.client.mqtt.MqttClient;
import com.hivemq.client.mqtt.mqtt3.Mqtt3Client;
import com.hivemq.client.mqtt.mqtt5.Mqtt5Client;
import org.pmw.tinylog.Configurator;
import org.pmw.tinylog.Level;
import org.pmw.tinylog.Logger;
import org.pmw.tinylog.writers.ConsoleWriter;
import picocli.CommandLine;

import java.security.Security;
Expand All @@ -42,20 +38,14 @@ public static void main(final String[] args) {

Security.setProperty("crypto.policy", "unlimited");

Configurator.defaultConfig()
.writer(new ConsoleWriter())
.formatPattern("{context:identifier}: {message}")
.level(Level.INFO)
.activate();

MQTTCLI = DaggerMqttCLI.create();
final CommandLine commandLine = MQTTCLI.cli();
final DefaultCLIProperties defaultCLIProperties = MQTTCLI.defaultCLIProperties();

try {
defaultCLIProperties.init();
} catch (Exception e) {
Logger.error(e.getMessage());
System.err.println(e.getMessage());
System.exit(-1);
}

Expand Down
5 changes: 0 additions & 5 deletions src/main/java/com/hivemq/cli/commands/AbstractCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@

package com.hivemq.cli.commands;

import com.hivemq.cli.commands.CliCommand;
import org.pmw.tinylog.Configurator;
import org.pmw.tinylog.Level;
import picocli.CommandLine;

@CommandLine.Command(sortOptions = false,
Expand All @@ -39,7 +36,6 @@ private void activateDebugMode(final boolean debug) {

if (debug && !verbose) {
this.debug = true;
Configurator.currentConfig().level(Level.DEBUG).activate();
}
}

Expand All @@ -49,7 +45,6 @@ private void activateVerboseMode(final boolean verbose) {
if (verbose) {
this.verbose = true;
debug = true;
Configurator.currentConfig().level(Level.TRACE).activate();
} else {
this.verbose = false;
}
Expand Down
64 changes: 13 additions & 51 deletions src/main/java/com/hivemq/cli/commands/AbstractCommonFlags.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package com.hivemq.cli.commands;

import com.google.common.base.Throwables;
import com.hivemq.cli.DefaultCLIProperties;
import com.hivemq.cli.MqttCLIMain;
import com.hivemq.cli.converters.*;
Expand All @@ -24,7 +25,7 @@
import com.hivemq.client.mqtt.MqttWebSocketConfig;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.pmw.tinylog.Logger;
import org.tinylog.Logger;
import picocli.CommandLine;

import javax.net.ssl.KeyManagerFactory;
Expand Down Expand Up @@ -111,23 +112,23 @@ public void setDefaultOptions() {
try {
password = defaultCLIProperties.getPassword();
} catch (Exception e) {
logPropertiesException(e, "Default password could not be loaded.");
Logger.error(e,"Default password could not be loaded ({})", Throwables.getRootCause(e).getMessage());
}
}

if (clientCertificate == null) {
try {
clientCertificate = defaultCLIProperties.getClientCertificate();
} catch (Exception e) {
logPropertiesException(e, "Default client certificate could not be loaded.");
Logger.error(e,"Default client certificate could not be loaded ({})", Throwables.getRootCause(e).getMessage());
}
}

if (clientPrivateKey == null) {
try {
clientPrivateKey = defaultCLIProperties.getClientPrivateKey();
} catch (Exception e) {
logPropertiesException(e, "Default client private key could not be loaded.");
Logger.error(e,"Default client private key could not be loaded ({})", Throwables.getRootCause(e).getMessage());
}
}

Expand All @@ -141,7 +142,7 @@ public void setDefaultOptions() {
certificates.add(defaultServerCertificate);
}
} catch (Exception e) {
logPropertiesException(e, "Default server certificate could not be loaded.");
Logger.error(e,"Default server certificate could not be loaded ({})", Throwables.getRootCause(e).getMessage());
}

}
Expand All @@ -154,34 +155,13 @@ public void setDefaultOptions() {
return doBuildSslConfig();
}
catch (Exception e) {
logPropertiesException(e);
Logger.error(e, Throwables.getRootCause(e).getMessage());
}
}

return null;
}

private void logPropertiesException(final @NotNull Exception e) {
if (isVerbose()) {
Logger.trace(e);
}
else if (isDebug()) {
Logger.debug(e.getMessage());
}
Logger.error(MqttUtils.getRootCause(e).getMessage());
}

private void logPropertiesException(final @NotNull Exception e, final @NotNull String message) {
if (isVerbose()) {
Logger.trace(e);
}
else if (isDebug()) {
Logger.debug(e.getMessage());
}
Logger.error(message + " ({})", MqttUtils.getRootCause(e).getMessage());
}


private boolean useBuiltSslConfig() {
return certificates != null ||
certificatesFromDir != null ||
Expand Down Expand Up @@ -285,20 +265,14 @@ public String toString() {

public String commonOptions() {
return super.toString() +
", user='" + user + '\'' +
", keepAlive=" + keepAlive +
", cleanStart=" + cleanStart +
(user != null ? (", user=" + user) : "") +
(keepAlive != null ? (", keepAlive=" + keepAlive) : "") +
(cleanStart != null ? (", cleanStart=" + cleanStart) : "") +
", useDefaultSsl=" + useSsl +
", sslConfig=" + (getSslConfig() != null) +
(getSslConfig() != null ? (", sslConfig=" + getSslConfig()) : "") +
", useWebSocket=" + useWebSocket +
", webSocketPath=" + webSocketPath +
", " + getWillOptions();
}

// GETTER AND SETTER

public void setUseSsl(final boolean useSsl) {
this.useSsl = useSsl;
(webSocketPath != null ? (", webSocketPath=" + webSocketPath) : "") +
getWillOptions();
}

@Nullable
Expand All @@ -315,28 +289,16 @@ public ByteBuffer getPassword() {
return password;
}

public void setPassword(final @Nullable ByteBuffer password) {
this.password = password;
}

@Nullable
public Integer getKeepAlive() {
return keepAlive;
}

public void setKeepAlive(final @Nullable Integer keepAlive) {
this.keepAlive = keepAlive;
}

@Nullable
public Boolean getCleanStart() {
return cleanStart;
}

public void setCleanStart(final @Nullable Boolean cleanStart) {
this.cleanStart = cleanStart;
}

@Nullable
public MqttWebSocketConfig getWebSocketConfig() {
if (useWebSocket) {
Expand Down
Loading