From 327701722d18891196ba70c3ee81ba865be1a719 Mon Sep 17 00:00:00 2001 From: yunhanw-google Date: Wed, 7 Dec 2022 06:44:20 -0800 Subject: [PATCH] use java util logging for example matter java test app (#23943) --- .../java/src/com/matter/controller/Main.java | 8 +- .../commands/common/CommandManager.java | 93 ++++++++++++------- .../commands/pairing/PairingCommand.java | 26 +++--- 3 files changed, 78 insertions(+), 49 deletions(-) diff --git a/examples/java-matter-controller/java/src/com/matter/controller/Main.java b/examples/java-matter-controller/java/src/com/matter/controller/Main.java index 5478add7cfea56..8a22143b17a767 100644 --- a/examples/java-matter-controller/java/src/com/matter/controller/Main.java +++ b/examples/java-matter-controller/java/src/com/matter/controller/Main.java @@ -24,8 +24,12 @@ import com.matter.controller.commands.discover.*; import com.matter.controller.commands.pairing.*; import java.util.ArrayList; +import java.util.logging.Level; +import java.util.logging.Logger; public class Main { + private static Logger logger = Logger.getLogger(Main.class.getName()); + private static void registerCommandsDiscover( ChipDeviceController controller, CommandManager commandManager, @@ -109,9 +113,9 @@ public static void main(String[] args) { try { commandManager.run(args); } catch (IllegalArgumentException e) { - System.out.println("Arguments init failed with exception: " + e.getMessage()); + logger.log(Level.INFO, "Arguments init failed with exception: " + e.getMessage()); } catch (Exception e) { - System.out.println("Run command failed with exception: " + e.getMessage()); + logger.log(Level.INFO, "Run command failed with exception: " + e.getMessage()); } controller.shutdownCommissioning(); } diff --git a/examples/java-matter-controller/java/src/com/matter/controller/commands/common/CommandManager.java b/examples/java-matter-controller/java/src/com/matter/controller/commands/common/CommandManager.java index fb6d9ccf3bf52f..933a1d5b9c1796 100644 --- a/examples/java-matter-controller/java/src/com/matter/controller/commands/common/CommandManager.java +++ b/examples/java-matter-controller/java/src/com/matter/controller/commands/common/CommandManager.java @@ -23,11 +23,14 @@ import java.util.HashMap; import java.util.Map; import java.util.Optional; +import java.util.logging.Level; +import java.util.logging.Logger; public final class CommandManager { private final ArrayList mCommandMgr = new ArrayList(); private final Map> mClusters = new HashMap>(); + private static Logger logger = Logger.getLogger(CommandManager.class.getName()); public final void register(String clusterName, ArrayList commandsList) { mClusters.put(clusterName, commandsList); @@ -37,20 +40,20 @@ public final void run(String[] args) { Command command; if (args.length < 1) { - System.out.println("Missing cluster name"); + logger.log(Level.INFO, "Missing cluster name"); showClusters(); return; } ArrayList commands = mClusters.get(args[0]); if (commands == null) { - System.out.println("Unknown cluster: " + args[0]); + logger.log(Level.INFO, "Unknown cluster: " + args[0]); showClusters(); return; } if (args.length < 2) { - System.out.println("Missing command name"); + logger.log(Level.INFO, "Missing command name"); showCluster(args[0], commands); return; } @@ -64,27 +67,27 @@ public final void run(String[] args) { } } else if (isEventCommand(args[1])) { if (args.length < 3) { - System.out.println("Missing event name"); + logger.log(Level.INFO, "Missing event name"); showClusterEvents(args[0], args[1], commands); throw new IllegalArgumentException(); } command = getGlobalCommand(commands, args[1], args[2]); if (command == null) { - System.out.println("Unknown event: " + args[2]); + logger.log(Level.INFO, "Unknown event: " + args[2]); showClusterEvents(args[0], args[1], commands); throw new IllegalArgumentException(); } } else { if (args.length < 3) { - System.out.println("Missing attribute name"); + logger.log(Level.INFO, "Missing attribute name"); showClusterAttributes(args[0], args[1], commands); throw new IllegalArgumentException(); } command = getGlobalCommand(commands, args[1], args[2]); if (command == null) { - System.out.println("Unknown attribute: " + args[2]); + logger.log(Level.INFO, "Unknown attribute: " + args[2]); showClusterAttributes(args[0], args[1], commands); throw new IllegalArgumentException(); } @@ -100,7 +103,7 @@ public final void run(String[] args) { System.out.println("Run command failed with exception: " + e.getMessage()); showCommand(args[0], command); } catch (Exception e) { - System.out.println("Run command failed with exception: " + e.getMessage()); + logger.log(Level.INFO, "Run command failed with exception: " + e.getMessage()); } } @@ -140,34 +143,43 @@ private Command getGlobalCommand( } private void showClusters() { - System.out.println("Usage:"); - System.out.println(" java-matter-controller cluster_name command_name [param1 param2 ...]"); - System.out.println("\n"); - System.out.println( + logger.log(Level.INFO, "Usage:"); + logger.log( + Level.INFO, " java-matter-controller cluster_name command_name [param1 param2 ...]"); + logger.log(Level.INFO, "\n"); + logger.log( + Level.INFO, " +-------------------------------------------------------------------------------------+"); - System.out.println( + logger.log( + Level.INFO, " | Clusters: |"); - System.out.println( + logger.log( + Level.INFO, " +-------------------------------------------------------------------------------------+"); for (String key : mClusters.keySet()) { System.out.printf(" | * %-82s|\n", key.toLowerCase()); } - System.out.println( + logger.log( + Level.INFO, " +-------------------------------------------------------------------------------------+"); } private void showCluster(String clusterName, ArrayList commands) { - System.out.println("Usage:"); - System.out.println( + logger.log(Level.INFO, "Usage:"); + logger.log( + Level.INFO, " java-matter-controller " + clusterName + " command_name [param1 param2 ...]"); - System.out.println("\n"); - System.out.println( + logger.log(Level.INFO, "\n"); + logger.log( + Level.INFO, " +-------------------------------------------------------------------------------------+"); - System.out.println( + logger.log( + Level.INFO, " | Commands: |"); - System.out.println( + logger.log( + Level.INFO, " +-------------------------------------------------------------------------------------+"); boolean readCommand = false; boolean writeCommand = false; @@ -198,44 +210,52 @@ private void showCluster(String clusterName, ArrayList commands) { System.out.printf(" | * %-82s|\n", cmdName); } } - System.out.println( + logger.log( + Level.INFO, " +-------------------------------------------------------------------------------------+\n"); } private void showClusterAttributes( String clusterName, String commandName, ArrayList commands) { - System.out.println("Usage:"); + logger.log(Level.INFO, "Usage:"); System.out.printf( " java-matter-controller %s %s attribute-name [param1 param2 ...]\n", clusterName, commandName); - System.out.println("\n"); - System.out.println( + logger.log(Level.INFO, "\n"); + logger.log( + Level.INFO, " +-------------------------------------------------------------------------------------+"); - System.out.println( + logger.log( + Level.INFO, " | Attributes: |"); - System.out.println( + logger.log( + Level.INFO, " +-------------------------------------------------------------------------------------+"); for (Command command : commands) { if (commandName.equals(command.getName())) { System.out.printf(" | * %-82s|\n", command.getAttribute().get()); } } - System.out.println( + logger.log( + Level.INFO, " +-------------------------------------------------------------------------------------+"); } private void showClusterEvents( String clusterName, String commandName, ArrayList commands) { - System.out.println("Usage:"); + logger.log(Level.INFO, "Usage:"); System.out.printf( " java-matter-controller %s %s event-name [param1 param2 ...]\n", clusterName, commandName); - System.out.println("\n"); - System.out.println( + logger.log(Level.INFO, "\n"); + logger.log( + Level.INFO, " +-------------------------------------------------------------------------------------+"); - System.out.println( + logger.log( + Level.INFO, " | Events: |"); - System.out.println( + logger.log( + Level.INFO, " +-------------------------------------------------------------------------------------+"); for (Command command : commands) { @@ -243,12 +263,13 @@ private void showClusterEvents( System.out.printf(" | * %-82s|\n", command.getAttribute().get()); } } - System.out.println( + logger.log( + Level.INFO, " +-------------------------------------------------------------------------------------+"); } private void showCommand(String clusterName, Command command) { - System.out.println("Usage:"); + logger.log(Level.INFO, "Usage:"); String arguments = command.getName(); String description = ""; @@ -284,7 +305,7 @@ private void showCommand(String clusterName, Command command) { } if (!description.isEmpty()) { - System.out.println(description); + logger.log(Level.INFO, description); } } } diff --git a/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairingCommand.java b/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairingCommand.java index 2d4a6bbfae234d..c6e209095bfca8 100644 --- a/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairingCommand.java +++ b/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairingCommand.java @@ -27,6 +27,8 @@ import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicLong; +import java.util.logging.Level; +import java.util.logging.Logger; public abstract class PairingCommand extends MatterCommand implements ChipDeviceController.CompletionListener { @@ -48,6 +50,8 @@ public abstract class PairingCommand extends MatterCommand private final StringBuffer mOnboardingPayload = new StringBuffer(); private final StringBuffer mDiscoveryFilterInstanceName = new StringBuffer(); + private static Logger logger = Logger.getLogger(PairingCommand.class.getName()); + public long getNodeId() { return mNodeId.get(); } @@ -66,17 +70,17 @@ public long getTimeoutMillis() { @Override public void onConnectDeviceComplete() { - System.out.println("onConnectDeviceComplete"); + logger.log(Level.INFO, "onConnectDeviceComplete"); } @Override public void onStatusUpdate(int status) { - System.out.println("onStatusUpdate with status: " + status); + logger.log(Level.INFO, "onStatusUpdate with status: " + status); } @Override public void onPairingComplete(int errorCode) { - System.out.println("onPairingComplete with error code: " + errorCode); + logger.log(Level.INFO, "onPairingComplete with error code: " + errorCode); if (errorCode != 0) { setTestResult("Failure"); } @@ -84,12 +88,12 @@ public void onPairingComplete(int errorCode) { @Override public void onPairingDeleted(int errorCode) { - System.out.println("onPairingDeleted with error code: " + errorCode); + logger.log(Level.INFO, "onPairingDeleted with error code: " + errorCode); } @Override public void onCommissioningComplete(long nodeId, int errorCode) { - System.out.println("onCommissioningComplete with error code: " + errorCode); + logger.log(Level.INFO, "onCommissioningComplete with error code: " + errorCode); if (errorCode == 0) { setTestResult("Success"); } else { @@ -100,33 +104,33 @@ public void onCommissioningComplete(long nodeId, int errorCode) { @Override public void onReadCommissioningInfo( int vendorId, int productId, int wifiEndpointId, int threadEndpointId) { - System.out.println("onReadCommissioningInfo"); + logger.log(Level.INFO, "onReadCommissioningInfo"); } @Override public void onCommissioningStatusUpdate(long nodeId, String stage, int errorCode) { - System.out.println("onCommissioningStatusUpdate"); + logger.log(Level.INFO, "onCommissioningStatusUpdate"); } @Override public void onNotifyChipConnectionClosed() { - System.out.println("onNotifyChipConnectionClosed"); + logger.log(Level.INFO, "onNotifyChipConnectionClosed"); } @Override public void onCloseBleComplete() { - System.out.println("onCloseBleComplete"); + logger.log(Level.INFO, "onCloseBleComplete"); } @Override public void onError(Throwable error) { setTestResult(error.toString()); - System.out.println("onError with error: " + error.toString()); + logger.log(Level.INFO, "onError with error: " + error.toString()); } @Override public void onOpCSRGenerationComplete(byte[] csr) { - System.out.println("onOpCSRGenerationComplete"); + logger.log(Level.INFO, "onOpCSRGenerationComplete"); for (int i = 0; i < csr.length; i++) { System.out.print(csr[i] + " "); }