Skip to content

Commit 1050987

Browse files
yunhanw-googlepull[bot]
authored andcommitted
use java util logging for example matter java test app (#23943)
1 parent ef774e8 commit 1050987

File tree

3 files changed

+78
-49
lines changed

3 files changed

+78
-49
lines changed

examples/java-matter-controller/java/src/com/matter/controller/Main.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,12 @@
2424
import com.matter.controller.commands.discover.*;
2525
import com.matter.controller.commands.pairing.*;
2626
import java.util.ArrayList;
27+
import java.util.logging.Level;
28+
import java.util.logging.Logger;
2729

2830
public class Main {
31+
private static Logger logger = Logger.getLogger(Main.class.getName());
32+
2933
private static void registerCommandsDiscover(
3034
ChipDeviceController controller,
3135
CommandManager commandManager,
@@ -109,9 +113,9 @@ public static void main(String[] args) {
109113
try {
110114
commandManager.run(args);
111115
} catch (IllegalArgumentException e) {
112-
System.out.println("Arguments init failed with exception: " + e.getMessage());
116+
logger.log(Level.INFO, "Arguments init failed with exception: " + e.getMessage());
113117
} catch (Exception e) {
114-
System.out.println("Run command failed with exception: " + e.getMessage());
118+
logger.log(Level.INFO, "Run command failed with exception: " + e.getMessage());
115119
}
116120
controller.shutdownCommissioning();
117121
}

examples/java-matter-controller/java/src/com/matter/controller/commands/common/CommandManager.java

+57-36
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,14 @@
2323
import java.util.HashMap;
2424
import java.util.Map;
2525
import java.util.Optional;
26+
import java.util.logging.Level;
27+
import java.util.logging.Logger;
2628

2729
public final class CommandManager {
2830
private final ArrayList<Command> mCommandMgr = new ArrayList<Command>();
2931
private final Map<String, ArrayList<Command>> mClusters =
3032
new HashMap<String, ArrayList<Command>>();
33+
private static Logger logger = Logger.getLogger(CommandManager.class.getName());
3134

3235
public final void register(String clusterName, ArrayList<Command> commandsList) {
3336
mClusters.put(clusterName, commandsList);
@@ -37,20 +40,20 @@ public final void run(String[] args) {
3740
Command command;
3841

3942
if (args.length < 1) {
40-
System.out.println("Missing cluster name");
43+
logger.log(Level.INFO, "Missing cluster name");
4144
showClusters();
4245
return;
4346
}
4447

4548
ArrayList<Command> commands = mClusters.get(args[0]);
4649
if (commands == null) {
47-
System.out.println("Unknown cluster: " + args[0]);
50+
logger.log(Level.INFO, "Unknown cluster: " + args[0]);
4851
showClusters();
4952
return;
5053
}
5154

5255
if (args.length < 2) {
53-
System.out.println("Missing command name");
56+
logger.log(Level.INFO, "Missing command name");
5457
showCluster(args[0], commands);
5558
return;
5659
}
@@ -64,27 +67,27 @@ public final void run(String[] args) {
6467
}
6568
} else if (isEventCommand(args[1])) {
6669
if (args.length < 3) {
67-
System.out.println("Missing event name");
70+
logger.log(Level.INFO, "Missing event name");
6871
showClusterEvents(args[0], args[1], commands);
6972
throw new IllegalArgumentException();
7073
}
7174

7275
command = getGlobalCommand(commands, args[1], args[2]);
7376
if (command == null) {
74-
System.out.println("Unknown event: " + args[2]);
77+
logger.log(Level.INFO, "Unknown event: " + args[2]);
7578
showClusterEvents(args[0], args[1], commands);
7679
throw new IllegalArgumentException();
7780
}
7881
} else {
7982
if (args.length < 3) {
80-
System.out.println("Missing attribute name");
83+
logger.log(Level.INFO, "Missing attribute name");
8184
showClusterAttributes(args[0], args[1], commands);
8285
throw new IllegalArgumentException();
8386
}
8487

8588
command = getGlobalCommand(commands, args[1], args[2]);
8689
if (command == null) {
87-
System.out.println("Unknown attribute: " + args[2]);
90+
logger.log(Level.INFO, "Unknown attribute: " + args[2]);
8891
showClusterAttributes(args[0], args[1], commands);
8992
throw new IllegalArgumentException();
9093
}
@@ -100,7 +103,7 @@ public final void run(String[] args) {
100103
System.out.println("Run command failed with exception: " + e.getMessage());
101104
showCommand(args[0], command);
102105
} catch (Exception e) {
103-
System.out.println("Run command failed with exception: " + e.getMessage());
106+
logger.log(Level.INFO, "Run command failed with exception: " + e.getMessage());
104107
}
105108
}
106109

@@ -140,34 +143,43 @@ private Command getGlobalCommand(
140143
}
141144

142145
private void showClusters() {
143-
System.out.println("Usage:");
144-
System.out.println(" java-matter-controller cluster_name command_name [param1 param2 ...]");
145-
System.out.println("\n");
146-
System.out.println(
146+
logger.log(Level.INFO, "Usage:");
147+
logger.log(
148+
Level.INFO, " java-matter-controller cluster_name command_name [param1 param2 ...]");
149+
logger.log(Level.INFO, "\n");
150+
logger.log(
151+
Level.INFO,
147152
" +-------------------------------------------------------------------------------------+");
148-
System.out.println(
153+
logger.log(
154+
Level.INFO,
149155
" | Clusters: |");
150-
System.out.println(
156+
logger.log(
157+
Level.INFO,
151158
" +-------------------------------------------------------------------------------------+");
152159

153160
for (String key : mClusters.keySet()) {
154161
System.out.printf(" | * %-82s|\n", key.toLowerCase());
155162
}
156163

157-
System.out.println(
164+
logger.log(
165+
Level.INFO,
158166
" +-------------------------------------------------------------------------------------+");
159167
}
160168

161169
private void showCluster(String clusterName, ArrayList<Command> commands) {
162-
System.out.println("Usage:");
163-
System.out.println(
170+
logger.log(Level.INFO, "Usage:");
171+
logger.log(
172+
Level.INFO,
164173
" java-matter-controller " + clusterName + " command_name [param1 param2 ...]");
165-
System.out.println("\n");
166-
System.out.println(
174+
logger.log(Level.INFO, "\n");
175+
logger.log(
176+
Level.INFO,
167177
" +-------------------------------------------------------------------------------------+");
168-
System.out.println(
178+
logger.log(
179+
Level.INFO,
169180
" | Commands: |");
170-
System.out.println(
181+
logger.log(
182+
Level.INFO,
171183
" +-------------------------------------------------------------------------------------+");
172184
boolean readCommand = false;
173185
boolean writeCommand = false;
@@ -198,57 +210,66 @@ private void showCluster(String clusterName, ArrayList<Command> commands) {
198210
System.out.printf(" | * %-82s|\n", cmdName);
199211
}
200212
}
201-
System.out.println(
213+
logger.log(
214+
Level.INFO,
202215
" +-------------------------------------------------------------------------------------+\n");
203216
}
204217

205218
private void showClusterAttributes(
206219
String clusterName, String commandName, ArrayList<Command> commands) {
207-
System.out.println("Usage:");
220+
logger.log(Level.INFO, "Usage:");
208221
System.out.printf(
209222
" java-matter-controller %s %s attribute-name [param1 param2 ...]\n",
210223
clusterName, commandName);
211-
System.out.println("\n");
212-
System.out.println(
224+
logger.log(Level.INFO, "\n");
225+
logger.log(
226+
Level.INFO,
213227
" +-------------------------------------------------------------------------------------+");
214-
System.out.println(
228+
logger.log(
229+
Level.INFO,
215230
" | Attributes: |");
216-
System.out.println(
231+
logger.log(
232+
Level.INFO,
217233
" +-------------------------------------------------------------------------------------+");
218234
for (Command command : commands) {
219235
if (commandName.equals(command.getName())) {
220236
System.out.printf(" | * %-82s|\n", command.getAttribute().get());
221237
}
222238
}
223-
System.out.println(
239+
logger.log(
240+
Level.INFO,
224241
" +-------------------------------------------------------------------------------------+");
225242
}
226243

227244
private void showClusterEvents(
228245
String clusterName, String commandName, ArrayList<Command> commands) {
229-
System.out.println("Usage:");
246+
logger.log(Level.INFO, "Usage:");
230247
System.out.printf(
231248
" java-matter-controller %s %s event-name [param1 param2 ...]\n",
232249
clusterName, commandName);
233-
System.out.println("\n");
234-
System.out.println(
250+
logger.log(Level.INFO, "\n");
251+
logger.log(
252+
Level.INFO,
235253
" +-------------------------------------------------------------------------------------+");
236-
System.out.println(
254+
logger.log(
255+
Level.INFO,
237256
" | Events: |");
238-
System.out.println(
257+
logger.log(
258+
Level.INFO,
239259
" +-------------------------------------------------------------------------------------+");
240260

241261
for (Command command : commands) {
242262
if (commandName.equals(command.getName())) {
243263
System.out.printf(" | * %-82s|\n", command.getAttribute().get());
244264
}
245265
}
246-
System.out.println(
266+
logger.log(
267+
Level.INFO,
247268
" +-------------------------------------------------------------------------------------+");
248269
}
249270

250271
private void showCommand(String clusterName, Command command) {
251-
System.out.println("Usage:");
272+
logger.log(Level.INFO, "Usage:");
252273

253274
String arguments = command.getName();
254275
String description = "";
@@ -284,7 +305,7 @@ private void showCommand(String clusterName, Command command) {
284305
}
285306

286307
if (!description.isEmpty()) {
287-
System.out.println(description);
308+
logger.log(Level.INFO, description);
288309
}
289310
}
290311
}

examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairingCommand.java

+15-11
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import java.util.concurrent.atomic.AtomicBoolean;
2828
import java.util.concurrent.atomic.AtomicInteger;
2929
import java.util.concurrent.atomic.AtomicLong;
30+
import java.util.logging.Level;
31+
import java.util.logging.Logger;
3032

3133
public abstract class PairingCommand extends MatterCommand
3234
implements ChipDeviceController.CompletionListener {
@@ -48,6 +50,8 @@ public abstract class PairingCommand extends MatterCommand
4850
private final StringBuffer mOnboardingPayload = new StringBuffer();
4951
private final StringBuffer mDiscoveryFilterInstanceName = new StringBuffer();
5052

53+
private static Logger logger = Logger.getLogger(PairingCommand.class.getName());
54+
5155
public long getNodeId() {
5256
return mNodeId.get();
5357
}
@@ -66,30 +70,30 @@ public long getTimeoutMillis() {
6670

6771
@Override
6872
public void onConnectDeviceComplete() {
69-
System.out.println("onConnectDeviceComplete");
73+
logger.log(Level.INFO, "onConnectDeviceComplete");
7074
}
7175

7276
@Override
7377
public void onStatusUpdate(int status) {
74-
System.out.println("onStatusUpdate with status: " + status);
78+
logger.log(Level.INFO, "onStatusUpdate with status: " + status);
7579
}
7680

7781
@Override
7882
public void onPairingComplete(int errorCode) {
79-
System.out.println("onPairingComplete with error code: " + errorCode);
83+
logger.log(Level.INFO, "onPairingComplete with error code: " + errorCode);
8084
if (errorCode != 0) {
8185
setTestResult("Failure");
8286
}
8387
}
8488

8589
@Override
8690
public void onPairingDeleted(int errorCode) {
87-
System.out.println("onPairingDeleted with error code: " + errorCode);
91+
logger.log(Level.INFO, "onPairingDeleted with error code: " + errorCode);
8892
}
8993

9094
@Override
9195
public void onCommissioningComplete(long nodeId, int errorCode) {
92-
System.out.println("onCommissioningComplete with error code: " + errorCode);
96+
logger.log(Level.INFO, "onCommissioningComplete with error code: " + errorCode);
9397
if (errorCode == 0) {
9498
setTestResult("Success");
9599
} else {
@@ -100,33 +104,33 @@ public void onCommissioningComplete(long nodeId, int errorCode) {
100104
@Override
101105
public void onReadCommissioningInfo(
102106
int vendorId, int productId, int wifiEndpointId, int threadEndpointId) {
103-
System.out.println("onReadCommissioningInfo");
107+
logger.log(Level.INFO, "onReadCommissioningInfo");
104108
}
105109

106110
@Override
107111
public void onCommissioningStatusUpdate(long nodeId, String stage, int errorCode) {
108-
System.out.println("onCommissioningStatusUpdate");
112+
logger.log(Level.INFO, "onCommissioningStatusUpdate");
109113
}
110114

111115
@Override
112116
public void onNotifyChipConnectionClosed() {
113-
System.out.println("onNotifyChipConnectionClosed");
117+
logger.log(Level.INFO, "onNotifyChipConnectionClosed");
114118
}
115119

116120
@Override
117121
public void onCloseBleComplete() {
118-
System.out.println("onCloseBleComplete");
122+
logger.log(Level.INFO, "onCloseBleComplete");
119123
}
120124

121125
@Override
122126
public void onError(Throwable error) {
123127
setTestResult(error.toString());
124-
System.out.println("onError with error: " + error.toString());
128+
logger.log(Level.INFO, "onError with error: " + error.toString());
125129
}
126130

127131
@Override
128132
public void onOpCSRGenerationComplete(byte[] csr) {
129-
System.out.println("onOpCSRGenerationComplete");
133+
logger.log(Level.INFO, "onOpCSRGenerationComplete");
130134
for (int i = 0; i < csr.length; i++) {
131135
System.out.print(csr[i] + " ");
132136
}

0 commit comments

Comments
 (0)