Skip to content

Commit

Permalink
Do not use deprecated SmallRye Common OS Process
Browse files Browse the repository at this point in the history
See smallrye/smallrye-common#299. Prevents caching of process ID across native image (where the PID may change).
  • Loading branch information
dmlloyd committed Apr 3, 2024
1 parent cebf45c commit b66d0f0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 16 deletions.
5 changes: 4 additions & 1 deletion src/main/java/org/jboss/logmanager/ExtLogRecord.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@

package org.jboss.logmanager;

import static java.security.AccessController.doPrivileged;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.reflect.UndeclaredThrowableException;
import java.security.PrivilegedAction;
import java.text.MessageFormat;
import java.util.Map;
import java.util.MissingResourceException;
Expand Down Expand Up @@ -108,7 +111,7 @@ public ExtLogRecord(final java.util.logging.Level level, final String msg, final
longThreadID = Thread.currentThread().getId(); // todo: threadId() on 19+
hostName = HostName.getQualifiedHostName();
processName = io.smallrye.common.os.Process.getProcessName();
processId = io.smallrye.common.os.Process.getProcessId();
processId = doPrivileged((PrivilegedAction<ProcessHandle>) ProcessHandle::current).pid();
}

/**
Expand Down
20 changes: 5 additions & 15 deletions src/main/java/org/jboss/logmanager/handlers/SyslogHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package org.jboss.logmanager.handlers;

import static java.security.AccessController.doPrivileged;
import static java.time.temporal.ChronoField.DAY_OF_MONTH;
import static java.time.temporal.ChronoField.HOUR_OF_DAY;
import static java.time.temporal.ChronoField.MILLI_OF_SECOND;
Expand All @@ -34,6 +35,7 @@
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
import java.security.PrivilegedAction;
import java.text.Normalizer;
import java.text.Normalizer.Form;
import java.time.ZoneId;
Expand Down Expand Up @@ -346,7 +348,6 @@ public static enum SyslogType {
private String hostname;
private Facility facility;
private SyslogType syslogType;
private final String pid;
private OutputStream out;
private Protocol protocol;
private boolean useCountingFraming;
Expand Down Expand Up @@ -503,8 +504,6 @@ public SyslogHandler(final InetAddress serverAddress, final int port, final Faci
this.serverAddress = serverAddress;
this.port = port;
this.facility = facility;
final long pid = io.smallrye.common.os.Process.getProcessId();
this.pid = (pid != -1 ? Long.toString(pid) : null);
this.appName = "java";
this.hostname = checkPrintableAscii("host name", hostname);
this.syslogType = (syslogType == null ? SyslogType.RFC5424 : syslogType);
Expand Down Expand Up @@ -780,14 +779,11 @@ public void setEscapeEnabled(final boolean escapeEnabled) {
* Returns the pid being used as the PROCID for RFC5424 messages.
*
* @return the pid or {@code null} if the pid could not be determined
* @deprecated Always returns the current process ID, as a string.
*/
@Deprecated
public String getPid() {
lock.lock();
try {
return pid;
} finally {
lock.unlock();
}
return String.valueOf(doPrivileged((PrivilegedAction<ProcessHandle>) ProcessHandle::current).pid());
}

/**
Expand Down Expand Up @@ -1308,9 +1304,6 @@ protected byte[] createRFC5424Header(final ExtLogRecord record) throws IOExcepti
if (recordProcId != -1) {
buffer.append(recordProcId);
buffer.append(' ');
} else if (pid != null) {
buffer.appendPrintUSASCII(pid, 128);
buffer.append(' ');
} else {
buffer.appendUSASCII(NILVALUE_SP);
}
Expand Down Expand Up @@ -1381,9 +1374,6 @@ protected byte[] createRFC3164Header(final ExtLogRecord record) throws IOExcepti
if (recordProcId != -1) {
buffer.append('[').append(recordProcId).append(']');
colon = true;
} else if (pid != null) {
buffer.append('[').appendUSASCII(pid).append(']');
colon = true;
}
if (colon) {
buffer.append(':').append(' ');
Expand Down

0 comments on commit b66d0f0

Please sign in to comment.