Skip to content

Commit 46d564c

Browse files
committed
Merge pull request OpenHFT#9 from lburgazzoli/HFT-HCOLL-47
HCOLL-47 Change all logging statements to use the slf4j-api
2 parents 7784856 + 78e78fb commit 46d564c

File tree

9 files changed

+78
-65
lines changed

9 files changed

+78
-65
lines changed

affinity/pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@
5656
<dependencies>
5757
<!-- on linux install with sudo apt-get install libjna-java -->
5858

59+
<dependency>
60+
<groupId>org.slf4j</groupId>
61+
<artifactId>slf4j-api</artifactId>
62+
</dependency>
63+
5964
<dependency>
6065
<groupId>net.java.dev.jna</groupId>
6166
<artifactId>jna</artifactId>
@@ -82,6 +87,13 @@
8287
<artifactId>junit</artifactId>
8388
<scope>test</scope>
8489
</dependency>
90+
91+
<dependency>
92+
<groupId>org.slf4j</groupId>
93+
<artifactId>slf4j-simple</artifactId>
94+
<scope>test</scope>
95+
</dependency>
96+
8597
</dependencies>
8698

8799
<build>

affinity/src/main/java/net/openhft/affinity/AffinityLock.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,19 @@
2020
import net.openhft.affinity.impl.VanillaCpuLayout;
2121
import org.jetbrains.annotations.NotNull;
2222
import org.jetbrains.annotations.Nullable;
23+
import org.slf4j.Logger;
24+
import org.slf4j.LoggerFactory;
2325

2426
import java.io.File;
2527
import java.io.IOException;
26-
import java.util.logging.Level;
27-
import java.util.logging.Logger;
2828

2929
/**
3030
* This utility class support locking a thread to a single core, or reserving a whole core for a thread.
3131
*
3232
* @author peter.lawrey
3333
*/
3434
public class AffinityLock {
35-
private static final Logger LOGGER = Logger.getLogger(AffinityLock.class.getName());
35+
private static final Logger LOGGER = LoggerFactory.getLogger(AffinityLock.class);
3636

3737
// TODO It seems like on virtualized platforms .availableProcessors() value can change at
3838
// TODO runtime. We should think about how to adopt to such change
@@ -52,7 +52,7 @@ public class AffinityLock {
5252
cpuLayout(VanillaCpuLayout.fromCpuInfo());
5353
}
5454
} catch (IOException e) {
55-
LOGGER.log(Level.WARNING, "Unable to load /proc/cpuinfo", e);
55+
LOGGER.warn("Unable to load /proc/cpuinfo", e);
5656
}
5757
}
5858

@@ -81,7 +81,7 @@ private static long getReservedAffinity0() {
8181
if (reservedAffinity == null || reservedAffinity.trim().isEmpty()) {
8282
long reserverable = ((1 << PROCESSORS) - 1) ^ BASE_AFFINITY;
8383
if (reserverable == 0 && PROCESSORS > 1) {
84-
LOGGER.log(Level.INFO, "No isolated CPUs found, so assuming CPUs 1 to " + (PROCESSORS - 1) + " available.");
84+
LOGGER.info("No isolated CPUs found, so assuming CPUs 1 to {} available.",(PROCESSORS - 1));
8585
return ((1 << PROCESSORS) - 2);
8686
}
8787
return reserverable;
@@ -216,8 +216,7 @@ public void bind(boolean wholeCore) {
216216
} else if (cpuId >= 0) {
217217
bound = true;
218218
assignedThread = Thread.currentThread();
219-
if (LOGGER.isLoggable(Level.INFO))
220-
LOGGER.info("Assigning cpu " + cpuId + " to " + assignedThread);
219+
LOGGER.info("Assigning cpu {} to {}", cpuId, assignedThread);
221220
}
222221
if (cpuId >= 0)
223222
AffinitySupport.setAffinity(1L << cpuId);
@@ -226,8 +225,11 @@ public void bind(boolean wholeCore) {
226225
final boolean canReserve() {
227226
if (!reservable) return false;
228227
if (assignedThread != null) {
229-
if (assignedThread.isAlive()) return false;
230-
LOGGER.severe("Lock assigned to " + assignedThread + " but this thread is dead.");
228+
if (assignedThread.isAlive()) {
229+
return false;
230+
}
231+
232+
LOGGER.warn("Lock assigned to {} but this thread is dead.", assignedThread);
231233
}
232234
return true;
233235
}
@@ -255,7 +257,7 @@ public void release() {
255257
@Override
256258
protected void finalize() throws Throwable {
257259
if (reservable) {
258-
LOGGER.warning("Affinity lock for " + assignedThread + " was discarded rather than release()d in a controlled manner.");
260+
LOGGER.warn("Affinity lock for {} was discarded rather than release()d in a controlled manner.", assignedThread);
259261
release();
260262
}
261263
super.finalize();

affinity/src/main/java/net/openhft/affinity/AffinitySupport.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@
2121
import net.openhft.affinity.impl.PosixJNAAffinity;
2222
import net.openhft.affinity.impl.WindowsJNAAffinity;
2323
import org.jetbrains.annotations.NotNull;
24+
import org.slf4j.Logger;
25+
import org.slf4j.LoggerFactory;
2426

2527
import java.io.PrintWriter;
2628
import java.io.StringWriter;
2729
import java.lang.reflect.Field;
28-
import java.util.logging.Logger;
2930

3031
/**
3132
* Library to wrap low level JNI or JNA calls. Can be called without needing to know the actual implementation used.
@@ -36,19 +37,19 @@ public enum AffinitySupport {
3637
;
3738
@NotNull
3839
private static final IAffinity AFFINITY_IMPL;
39-
private static final Logger LOGGER = Logger.getLogger(AffinitySupport.class.getName());
40+
private static final Logger LOGGER = LoggerFactory.getLogger(AffinitySupport.class);
4041
private static Boolean JNAAvailable;
4142

4243
static {
4344
String osName = System.getProperty("os.name");
4445
if (osName.contains("Win") && isWindowsJNAAffinityUsable()) {
45-
LOGGER.fine("Using Windows JNA-based affinity control implementation");
46+
LOGGER.trace("Using Windows JNA-based affinity control implementation");
4647
AFFINITY_IMPL = WindowsJNAAffinity.INSTANCE;
4748
} else if (osName.contains("x") && isPosixJNAAffinityUsable()) {
48-
LOGGER.fine("Using Posix JNA-based affinity control implementation");
49+
LOGGER.trace("Using Posix JNA-based affinity control implementation");
4950
AFFINITY_IMPL = PosixJNAAffinity.INSTANCE;
5051
} else if (osName.contains("Mac") && isMacJNAAffinityUsable()) {
51-
LOGGER.fine("Using MAC OSX JNA-based thread id implementation");
52+
LOGGER.trace("Using MAC OSX JNA-based thread id implementation");
5253
AFFINITY_IMPL = OSXJNAAffinity.INSTANCE;
5354
} else {
5455
LOGGER.info("Using dummy affinity control implementation");
@@ -65,7 +66,7 @@ private static boolean isWindowsJNAAffinityUsable() {
6566
return false;
6667
}
6768
} else {
68-
LOGGER.warning("Windows JNA-based affinity not usable due to JNA not being available!");
69+
LOGGER.warn("Windows JNA-based affinity not usable due to JNA not being available!");
6970
return false;
7071
}
7172
}
@@ -79,7 +80,7 @@ private static boolean isPosixJNAAffinityUsable() {
7980
return false;
8081
}
8182
} else {
82-
LOGGER.warning("Posix JNA-based affinity not usable due to JNA not being available!");
83+
LOGGER.warn("Posix JNA-based affinity not usable due to JNA not being available!");
8384
return false;
8485
}
8586
}
@@ -88,7 +89,7 @@ private static boolean isMacJNAAffinityUsable() {
8889
if (isJNAAvailable()) {
8990
return true;
9091
} else {
91-
LOGGER.warning("MAX OSX JNA-based affinity not usable due to JNA not being available!");
92+
LOGGER.warn("MAX OSX JNA-based affinity not usable due to JNA not being available!");
9293
return false;
9394
}
9495
}
@@ -98,7 +99,7 @@ private static void logThrowable(Throwable t, String description) {
9899
sw.append(description);
99100
sw.append(" Reason: ");
100101
t.printStackTrace(new PrintWriter(sw));
101-
LOGGER.warning(sw.toString());
102+
LOGGER.warn(sw.toString());
102103
}
103104

104105
public static long getAffinity() {
@@ -135,7 +136,7 @@ public static void setThreadId() {
135136
tid.setAccessible(true);
136137
final Thread thread = Thread.currentThread();
137138
tid.setLong(thread, threadId);
138-
Logger.getAnonymousLogger().info("Set " + thread.getName() + " to thread id " + threadId);
139+
LOGGER.info("Set {} to thread id {}", thread.getName(), threadId);
139140
} catch (Exception e) {
140141
throw new IllegalStateException(e);
141142
}

affinity/src/main/java/net/openhft/affinity/BootClassPath.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
package net.openhft.affinity;
22

3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
35
import sun.misc.URLClassPath;
46

57
import java.io.File;
68
import java.net.MalformedURLException;
79
import java.net.URL;
8-
import java.util.logging.Logger;
910

1011
enum BootClassPath {
1112
INSTANCE;
1213

13-
private final Logger logger = Logger.getLogger(BootClassPath.class.getName());
14+
private final static Logger LOGGER = LoggerFactory.getLogger(BootClassPath.class);
1415

1516
private final URLClassPath bootClassPath = new URLClassPath(getBootClassPathURLs());
1617

@@ -22,10 +23,10 @@ public final boolean has(String binaryClassName) {
2223
private URL[] getBootClassPathURLs() {
2324
try {
2425
String bootClassPath = System.getProperty("sun.boot.class.path");
25-
logger.fine("Boot class-path is: " + bootClassPath);
26+
LOGGER.trace("Boot class-path is: {}",bootClassPath);
2627

2728
String pathSeparator = System.getProperty("path.separator");
28-
logger.fine("Path separator is: '" + pathSeparator + "'");
29+
LOGGER.trace("Path separator is: '{}'", pathSeparator);
2930

3031
String[] pathElements = bootClassPath.split(pathSeparator);
3132
URL[] pathURLs = new URL[pathElements.length];
@@ -35,7 +36,7 @@ private URL[] getBootClassPathURLs() {
3536

3637
return pathURLs;
3738
} catch (MalformedURLException e) {
38-
logger.warning("Parsing the boot class-path failed! Reason: " + e.getMessage());
39+
LOGGER.warn("Parsing the boot class-path failed! Reason: {}", e.getMessage());
3940
return new URL[0];
4041
}
4142
}

affinity/src/main/java/net/openhft/affinity/LockInventory.java

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
package net.openhft.affinity;
22

33
import org.jetbrains.annotations.NotNull;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
46

57
import java.util.NavigableMap;
68
import java.util.TreeMap;
7-
import java.util.logging.Level;
8-
import java.util.logging.Logger;
99

1010
class LockInventory {
1111

12-
private static final Logger LOGGER = Logger.getLogger(LockInventory.class.getName());
12+
private static final Logger LOGGER = LoggerFactory.getLogger(LockInventory.class);
1313

1414
private CpuLayout cpuLayout;
1515

@@ -40,9 +40,8 @@ public final synchronized void set(CpuLayout cpuLayout) {
4040
for (int i = 0; i < cpuLayout.cpus(); i++) {
4141
boolean base = ((AffinityLock.BASE_AFFINITY >> i) & 1) != 0;
4242
boolean reservable = ((AffinityLock.RESERVED_AFFINITY >> i) & 1) != 0;
43-
if (LOGGER.isLoggable(Level.FINE)) {
44-
LOGGER.fine("cpu " + i + " base= " + base + " reservable= " + reservable);
45-
}
43+
44+
LOGGER.trace("cpu " + i + " base={} reservable= {}", i, base, reservable);
4645
AffinityLock lock = logicalCoreLocks[i] = newLock(i, base, reservable);
4746

4847
int layoutId = lock.cpuId();
@@ -67,9 +66,9 @@ public final synchronized AffinityLock acquireLock(boolean bind, int cpuId, Affi
6766
}
6867
}
6968
}
70-
if (LOGGER.isLoggable(Level.WARNING)) {
71-
LOGGER.warning("No reservable CPU for " + Thread.currentThread());
72-
}
69+
70+
LOGGER.warn("No reservable CPU for {}", Thread.currentThread());
71+
7372
return newLock(-1, false, false);
7473
}
7574

@@ -86,29 +85,29 @@ public final synchronized AffinityLock acquireCore(boolean bind, int cpuId, Affi
8685
return al;
8786
}
8887
}
89-
if (LOGGER.isLoggable(Level.WARNING)) {
90-
LOGGER.warning("No reservable Core for " + Thread.currentThread());
91-
}
88+
89+
LOGGER.warn("No reservable Core for {}", Thread.currentThread());
90+
9291
return acquireLock(bind, cpuId, strategies);
9392
}
9493

9594
public final synchronized void bindWholeCore(int logicalCoreID) {
9695
if (logicalCoreID < 0) {
97-
LOGGER.warning("Can't bind core since it was not possible to reserve it!");
96+
LOGGER.warn("Can't bind core since it was not possible to reserve it!");
9897
return;
9998
}
10099

101100
int core = toPhysicalCore(logicalCoreID);
102101
for (AffinityLock al : physicalCoreLocks.get(core)) {
103102
if (al.isBound() && al.assignedThread != null && al.assignedThread.isAlive()) {
104-
LOGGER.severe("cpu " + al.cpuId() + " already bound to " + al.assignedThread);
103+
LOGGER.warn("cpu {} already bound to {}", al.cpuId(), al.assignedThread);
105104
} else {
106105
al.bound = true;
107106
al.assignedThread = Thread.currentThread();
108107
}
109108
}
110109

111-
if (LOGGER.isLoggable(Level.INFO)) {
110+
if (LOGGER.isInfoEnabled()) {
112111
StringBuilder sb = new StringBuilder().append("Assigning core ").append(core);
113112
String sep = ": cpus ";
114113
for (AffinityLock al : physicalCoreLocks.get(core)) {
@@ -125,12 +124,11 @@ public final synchronized void release() {
125124
for (AffinityLock al : logicalCoreLocks) {
126125
Thread at = al.assignedThread;
127126
if (at == t) {
128-
if (LOGGER.isLoggable(Level.INFO))
129-
LOGGER.info("Releasing cpu " + al.cpuId() + " from " + t);
127+
LOGGER.info("Releasing cpu {} from {}", al.cpuId(), t);
130128
al.assignedThread = null;
131129
al.bound = false;
132130
} else if (at != null && !at.isAlive()) {
133-
LOGGER.warning("Releasing cpu " + al.cpuId() + " from " + t + " as it is not alive.");
131+
LOGGER.warn("Releasing cpu {} from {} as it is not alive.", al.cpuId(), t);
134132
al.assignedThread = null;
135133
al.bound = false;
136134
}

affinity/src/main/java/net/openhft/affinity/impl/NullAffinity.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,17 @@
1818

1919

2020
import net.openhft.affinity.IAffinity;
21+
import org.slf4j.Logger;
22+
import org.slf4j.LoggerFactory;
2123

2224
import java.lang.management.ManagementFactory;
23-
import java.util.logging.Level;
24-
import java.util.logging.Logger;
2525

2626
/**
2727
* @author peter.lawrey
2828
*/
2929
public enum NullAffinity implements IAffinity {
3030
INSTANCE;
31-
private static final Logger LOGGER = Logger.getLogger(NullAffinity.class.getName());
31+
private static final Logger LOGGER = LoggerFactory.getLogger(NullAffinity.class);
3232

3333
@Override
3434
public long getAffinity() {
@@ -37,8 +37,7 @@ public long getAffinity() {
3737

3838
@Override
3939
public void setAffinity(final long affinity) {
40-
if (LOGGER.isLoggable(Level.FINE))
41-
LOGGER.fine("unable to set mask to " + Long.toHexString(affinity) + " as the JNIa nd JNA libraries and not loaded");
40+
LOGGER.trace("unable to set mask to {} as the JNIa nd JNA libraries and not loaded", Long.toHexString(affinity));
4241
}
4342

4443
@Override

affinity/src/main/java/net/openhft/affinity/impl/OSXJNAAffinity.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@
2121
import com.sun.jna.Library;
2222
import com.sun.jna.Native;
2323
import net.openhft.affinity.IAffinity;
24+
import org.slf4j.Logger;
25+
import org.slf4j.LoggerFactory;
2426

2527
import java.lang.management.ManagementFactory;
26-
import java.util.logging.Level;
27-
import java.util.logging.Logger;
2828

2929
/**
3030
* This is essentially the same as the NullAffinity implementation but with concrete
@@ -33,7 +33,7 @@
3333
*/
3434
public enum OSXJNAAffinity implements IAffinity {
3535
INSTANCE;
36-
private static final Logger LOGGER = Logger.getLogger(OSXJNAAffinity.class.getName());
36+
private static final Logger LOGGER = LoggerFactory.getLogger(OSXJNAAffinity.class);
3737

3838
@Override
3939
public long getAffinity() {
@@ -42,8 +42,7 @@ public long getAffinity() {
4242

4343
@Override
4444
public void setAffinity(final long affinity) {
45-
if (LOGGER.isLoggable(Level.FINE))
46-
LOGGER.fine("unable to set mask to " + Long.toHexString(affinity) + " as the JNIa nd JNA libraries and not loaded");
45+
LOGGER.trace("unable to set mask to {} as the JNIa nd JNA libraries and not loaded", Long.toHexString(affinity));
4746
}
4847

4948
@Override

0 commit comments

Comments
 (0)