Skip to content

Commit

Permalink
logging: use format specifiers instead of string concatenation
Browse files Browse the repository at this point in the history
  • Loading branch information
psiroky authored and gnodet committed Mar 12, 2023
1 parent b0b7115 commit 50653ae
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ private float getJavaVersion() {
sc.next();
sc.next();
String version = sc.next();
LOGGER.warn("JAVA VERSION: " + version);
LOGGER.warn("JAVA VERSION: {}", version);
int is = version.charAt(0) == '"' ? 1 : 0;
int ie = version.indexOf('.', version.indexOf('.', is));
return Float.parseFloat(version.substring(is, ie));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public void close() {
if (failure == null) {
failure = throwable;
} else if (!Thread.currentThread().isInterrupted()) {
LOGGER.error(String.format("Could not stop %s.", element), throwable);
LOGGER.error("Could not stop {}.", element, throwable);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,10 +293,9 @@ private void doUpdate(Runnable updater) {
} catch (IllegalStateException | ArrayIndexOutOfBoundsException | BufferUnderflowException e) {
String absPath = registryFile.toAbsolutePath().normalize().toString();
LOGGER.warn(
"Invalid daemon registry info, " + "trying to recover from this issue. "
+ "If you keep getting this warning, "
+ "try deleting the `registry.bin` file at ["
+ absPath + "]",
"Invalid daemon registry info, trying to recover from this issue. "
+ "If you keep getting this warning, try deleting the `registry.bin` file at [{}]",
absPath,
e);
this.reset();
return;
Expand Down Expand Up @@ -337,7 +336,7 @@ private static int getProcessId0() {
try {
return Integer.parseInt(pid);
} catch (NumberFormatException x) {
LOGGER.warn("Unable to determine PID from malformed /proc/self link `" + pid + "`");
LOGGER.warn("Unable to determine PID from malformed /proc/self link `{}`", pid);
}
}
}
Expand All @@ -352,8 +351,7 @@ private static int getProcessId0() {
return Integer.parseInt(pid);
} catch (NumberFormatException x) {
int rpid = new Random().nextInt(1 << 16);
LOGGER.warn(
"Unable to determine PID from malformed VM name `" + vmname + "`, picked a random number=" + rpid);
LOGGER.warn("Unable to determine PID from malformed VM name `{}`, picked a random number={}", vmname, rpid);
return rpid;
}
}
Expand Down
35 changes: 21 additions & 14 deletions common/src/main/java/org/mvndaemon/mvnd/common/OsUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,15 @@ public static long findProcessRssInKb(long pid) {
return Long.parseLong(output.get(0).trim());
} catch (NumberFormatException e) {
LOGGER.warn(
"Could not parse the output of " + Stream.of(cmd).collect(Collectors.joining(" "))
+ " as a long:\n"
+ output.stream().collect(Collectors.joining("\n")));
"Could not parse the output of {} as a long:\n{}",
Stream.of(cmd).collect(Collectors.joining(" ")),
output.stream().collect(Collectors.joining("\n")));
}
} else {
LOGGER.warn("Unexpected output of " + Stream.of(cmd).collect(Collectors.joining(" ")) + ":\n"
+ output.stream().collect(Collectors.joining("\n")));
LOGGER.warn(
"Unexpected output of {}:\n{}",
Stream.of(cmd).collect(Collectors.joining(" ")),
output.stream().collect(Collectors.joining("\n")));
}
return -1;
} else if (os == Os.WINDOWS) {
Expand All @@ -97,14 +99,16 @@ public static long findProcessRssInKb(long pid) {
try {
return Long.parseLong(nonEmptyLines.get(1).trim()) / KB;
} catch (NumberFormatException e) {
LOGGER.warn("Could not parse the second line of "
+ Stream.of(cmd).collect(Collectors.joining(" "))
+ " output as a long:\n"
+ nonEmptyLines.stream().collect(Collectors.joining("\n")));
LOGGER.warn(
"Could not parse the second line of {} output as a long:\n{}",
Stream.of(cmd).collect(Collectors.joining(" ")),
nonEmptyLines.stream().collect(Collectors.joining("\n")));
}
} else {
LOGGER.warn("Unexpected output of " + Stream.of(cmd).collect(Collectors.joining(" ")) + ":\n"
+ output.stream().collect(Collectors.joining("\n")));
LOGGER.warn(
"Unexpected output of {}:\n{}",
Stream.of(cmd).collect(Collectors.joining(" ")),
output.stream().collect(Collectors.joining("\n")));
}
return -1;
} else {
Expand Down Expand Up @@ -136,11 +140,14 @@ private static void exec(String[] cmd, final List<String> output) {
try (CommandProcess ps = new CommandProcess(builder.start(), output::add)) {
final int exitCode = ps.waitFor(1000);
if (exitCode != 0) {
LOGGER.warn(Stream.of(cmd).collect(Collectors.joining(" ")) + " exited with " + exitCode + ":\n"
+ output.stream().collect(Collectors.joining("\n")));
LOGGER.warn(
"{} exited with {}:\n{}",
Stream.of(cmd).collect(Collectors.joining(" ")),
exitCode,
output.stream().collect(Collectors.joining("\n")));
}
} catch (IOException e) {
LOGGER.warn("Could not execute " + Stream.of(cmd).collect(Collectors.joining(" ")));
LOGGER.warn("Could not execute {}", Stream.of(cmd).collect(Collectors.joining(" ")));
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
Expand Down
8 changes: 4 additions & 4 deletions daemon/src/main/java/org/mvndaemon/mvnd/daemon/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ boolean awaitStop() {
private void requestStop(String reason) {
DaemonState state = getState();
if (!(state == StopRequested || state == Stopped)) {
LOGGER.info("Daemon will be stopped at the end of the build " + reason);
LOGGER.info("Daemon will be stopped at the end of the build {}", reason);
stateLock.lock();
try {
if (state == Busy) {
Expand All @@ -405,7 +405,7 @@ private void requestStop(String reason) {
}

private void requestForcefulStop(String reason) {
LOGGER.info("Daemon is stopping immediately " + reason);
LOGGER.info("Daemon is stopping immediately {}", reason);
stopNow(reason);
}

Expand Down Expand Up @@ -533,7 +533,7 @@ private void handle(DaemonConnection connection, BuildRequest buildRequest) {
LOGGER.info("No more message to dispatch");
return;
}
LOGGER.info("Dispatch message: " + m);
LOGGER.info("Dispatch message: {}", m);
connection.dispatch(m);
}
} catch (Throwable t) {
Expand Down Expand Up @@ -637,7 +637,7 @@ public <T extends Message> T request(Message request, Class<T> responseType, Pre

private void updateState(DaemonState state) {
if (getState() != state) {
LOGGER.info("Updating state to: " + state);
LOGGER.info("Updating state to: {}", state);
stateLock.lock();
try {
registry.store(info = info.withState(state));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ private void doDisplay(String message) throws IOException {
Connection con = Objects.requireNonNull(Connection.getCurrent());
String projectId = ProjectBuildLogAppender.getProjectId();
Message.ProjectEvent msg = Message.display(projectId, message);
LOGGER.info("Sending display request: " + msg);
LOGGER.info("Sending display request: {}", msg);
con.dispatch(msg);
} catch (Exception e) {
throw new IOException("Unable to display message", e);
Expand All @@ -167,9 +167,9 @@ private String doPrompt(String message, boolean password) throws IOException {
String projectId = ProjectBuildLogAppender.getProjectId();
String uid = UUID.randomUUID().toString();
Message.Prompt msg = new Message.Prompt(projectId, uid, message, password);
LOGGER.info("Requesting prompt: " + msg);
LOGGER.info("Requesting prompt: {}", msg);
Message.PromptResponse res = con.request(msg, Message.PromptResponse.class, r -> uid.equals(r.getUid()));
LOGGER.info("Received response: " + res.getMessage());
LOGGER.info("Received response: {}", res.getMessage());
return res.getMessage();
} catch (Exception e) {
throw new IOException("Unable to prompt user", e);
Expand Down

0 comments on commit 50653ae

Please sign in to comment.