Skip to content

Commit

Permalink
Merge pull request #379 from dmlloyd/ndc
Browse files Browse the repository at this point in the history
Add NDC Provider
  • Loading branch information
jamezp committed May 5, 2023
2 parents 0267b2a + 6251d62 commit 4ec187e
Show file tree
Hide file tree
Showing 3 changed files with 211 additions and 88 deletions.
122 changes: 34 additions & 88 deletions src/main/java/org/jboss/logmanager/NDC.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,42 @@

package org.jboss.logmanager;

import java.util.Arrays;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Iterator;
import java.util.ServiceConfigurationError;
import java.util.ServiceLoader;

/**
* Nested diagnostic context. This is basically a thread-local stack that holds a string which can be included
* in a log message.
*/
public final class NDC {
private static final NDCProvider ndcProvider = getDefaultNDCProvider();

private NDC() {}

private static final Holder ndc = new Holder();
static NDCProvider getNDCProvider() {
return ndcProvider;
}

static NDCProvider getDefaultNDCProvider() {
return System.getSecurityManager() == null ? doGetDefaultNDCProvider() : AccessController.doPrivileged((PrivilegedAction<NDCProvider>) NDC::doGetDefaultNDCProvider);
}

static NDCProvider doGetDefaultNDCProvider() {
final ServiceLoader<NDCProvider> configLoader = ServiceLoader.load(NDCProvider.class, NDC.class.getClassLoader());
final Iterator<NDCProvider> iterator = configLoader.iterator();
for (;;) try {
if (! iterator.hasNext()) {
return new ThreadLocalNDC();
}
return iterator.next();
} catch (ServiceConfigurationError | RuntimeException e) {
System.err.print("Warning: failed to load NDC Provider: ");
e.printStackTrace(System.err);
}
}

/**
* Push a value on to the NDC stack, returning the new stack depth which should later be used to restore the stack.
Expand All @@ -38,12 +63,7 @@ private NDC() {}
* @return the new stack depth
*/
public static int push(String context) {
final Stack<String> stack = ndc.get();
try {
return stack.depth();
} finally {
stack.push(context);
}
return ndcProvider.push(context);
}

/**
Expand All @@ -52,19 +72,14 @@ public static int push(String context) {
* @return the old topmost value
*/
public static String pop() {
final Stack<String> stack = ndc.get();
if (stack.isEmpty()) {
return "";
} else {
return stack.pop();
}
return ndcProvider.pop();
}

/**
* Clear the thread's NDC stack.
*/
public static void clear() {
ndc.get().trimTo(0);
ndcProvider.clear();
}

/**
Expand All @@ -74,7 +89,7 @@ public static void clear() {
* @param size the new size
*/
public static void trimTo(int size) {
ndc.get().trimTo(size);
ndcProvider.trimTo(size);
}

/**
Expand All @@ -83,7 +98,7 @@ public static void trimTo(int size) {
* @return the stack depth
*/
public static int getDepth() {
return ndc.get().depth();
return ndcProvider.getDepth();
}

/**
Expand All @@ -92,12 +107,7 @@ public static int getDepth() {
* @return the current NDC value, or {@code ""} if there is none
*/
public static String get() {
final Stack<String> stack = ndc.get();
if (stack.isEmpty()) {
return "";
} else {
return stack.toString();
}
return ndcProvider.get();
}

/**
Expand All @@ -107,70 +117,6 @@ public static String get() {
* @return the value or {@code null} if there is none
*/
public static String get(int n) {
return ndc.get().get(n);
}

private static final class Holder extends ThreadLocal<Stack<String>> {
protected Stack<String> initialValue() {
return new Stack<String>();
}
}

private static final class Stack<T> {
@SuppressWarnings("unchecked")
private T[] data = (T[]) new Object[32];
private int sp;

public void push(T value) {
final int oldlen = data.length;
if (sp == oldlen) {
data = Arrays.copyOf(data, (oldlen << 1) + oldlen >>> 1);
}
data[sp++] = value;
}

public T pop() {
try {
return data[--sp];
} finally {
data[sp] = null;
}
}

public T top() {
return data[sp - 1];
}

public boolean isEmpty() {
return sp == 0;
}

public int depth() {
return sp;
}

public void trimTo(int max) {
final int sp = this.sp;
if (sp > max) {
Arrays.fill(data, max, sp - 1, null);
this.sp = max;
}
}

public T get(int n) {
return n < sp ? data[n] : null;
}

public String toString() {
final StringBuilder b = new StringBuilder();
final int sp = this.sp;
for (int i = 0; i < sp; i++) {
b.append(data[i]);
if ((i + 1) < sp) {
b.append('.');
}
}
return b.toString();
}
return ndcProvider.get(n);
}
}
55 changes: 55 additions & 0 deletions src/main/java/org/jboss/logmanager/NDCProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package org.jboss.logmanager;

public interface NDCProvider {

/**
* Push a value on to the NDC stack, returning the new stack depth which should later be used to restore the stack.
*
* @param context the new value
* @return the new stack depth
*/
int push(String context);

/**
* Pop the topmost value from the NDC stack and return it.
*
* @return the old topmost value
*/
String pop();

/**
* Clear the thread's NDC stack.
*/
void clear();

/**
* Trim the thread NDC stack down to no larger than the given size. Used to restore the stack to the depth returned
* by a {@code push()}.
*
* @param size the new size
*/
void trimTo(int size);

/**
* Get the current NDC stack depth.
*
* @return the stack depth
*/
int getDepth();

/**
* Get the current NDC value.
*
* @return the current NDC value, or {@code ""} if there is none
*/
String get();

/**
* Provided for compatibility with log4j. Get the NDC value that is {@code n} entries from the bottom.
*
* @param n the index
* @return the value or {@code null} if there is none
*/
String get(int n);

}
122 changes: 122 additions & 0 deletions src/main/java/org/jboss/logmanager/ThreadLocalNDC.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package org.jboss.logmanager;

import java.util.Arrays;

final class ThreadLocalNDC implements NDCProvider {
private static final Holder ndc = new Holder();

@Override
public int push(String context) {
final Stack<String> stack = ndc.get();
try {
return stack.depth();
} finally {
stack.push(context);
}
}

@Override
public String pop() {
final Stack<String> stack = ndc.get();
if (stack.isEmpty()) {
return "";
} else {
return stack.pop();
}
}

@Override
public void clear() {
ndc.get().trimTo(0);
}

@Override
public void trimTo(int size) {
ndc.get().trimTo(size);
}

@Override
public int getDepth() {
return ndc.get().depth();
}

@Override
public String get() {
final Stack<String> stack = ndc.get();
if (stack.isEmpty()) {
return "";
} else {
return stack.toString();
}
}

@Override
public String get(int n) {
return ndc.get().get(n);
}

private static final class Holder extends ThreadLocal<Stack<String>> {
protected Stack<String> initialValue() {
return new Stack<>();
}
}

private static final class Stack<T> {
private Object[] data = new Object[32];
private int sp;

public void push(T value) {
if (sp == data.length) {
data = Arrays.copyOf(data, (data.length << 1) + data.length >>> 1);
}
data[sp++] = value;
}

@SuppressWarnings("unchecked")
public T pop() {
try {
return (T) data[--sp];
} finally {
data[sp] = null;
}
}

@SuppressWarnings("unchecked")
public T top() {
return (T) data[sp - 1];
}

public boolean isEmpty() {
return sp == 0;
}

public int depth() {
return sp;
}

public void trimTo(int max) {
final int sp = this.sp;
if (sp > max) {
Arrays.fill(data, max, sp - 1, null);
this.sp = max;
}
}

@SuppressWarnings("unchecked")
public T get(int n) {
return n < sp ? (T) data[n] : null;
}

public String toString() {
final StringBuilder b = new StringBuilder();
final int sp = this.sp;
for (int i = 0; i < sp; i++) {
b.append(data[i]);
if ((i + 1) < sp) {
b.append('.');
}
}
return b.toString();
}
}
}

0 comments on commit 4ec187e

Please sign in to comment.