You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-cvs@jakarta.apache.org by ce...@apache.org on 2001/08/06 21:29:19 UTC
cvs commit: jakarta-log4j/src/java/org/apache/log4j/test MDCStress.java StressNDC.java
ceki 01/08/06 12:29:19
Modified: src/java/org/apache/log4j MDC.java
src/java/org/apache/log4j/helpers Makefile
PatternParser.java
src/java/org/apache/log4j/test MDCStress.java StressNDC.java
Log:
- PatternParser now takes 'X' as a conversion character.
- MDC.get does not automatically create a hashtable.
- Intermediary version of MDCStress
Revision Changes Path
1.2 +16 -27 jakarta-log4j/src/java/org/apache/log4j/MDC.java
Index: MDC.java
===================================================================
RCS file: /home/cvs/jakarta-log4j/src/java/org/apache/log4j/MDC.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- MDC.java 2001/08/06 16:38:14 1.1
+++ MDC.java 2001/08/06 19:29:19 1.2
@@ -3,53 +3,42 @@
package org.apache.log4j;
import java.util.Hashtable;
+import org.apache.log4j.helpers.ThreadLocalMap;
public class MDC {
- final static MappedContext context = new MappedContext();
+ final static ThreadLocalMap context = new ThreadLocalMap();
static final int HT_SIZE = 11;
static
public
void put(String key, Object o) {
- Hashtable ht = getMap();
+ Hashtable ht = (Hashtable) context.get();
+ if(ht == null) {
+ System.out.println("Creating new ht. [" + Thread.currentThread().getName()+
+ "]");
+ ht = new Hashtable(HT_SIZE);
+ context.set(ht);
+ }
ht.put(key, o);
}
static
public
Object get(String key) {
- Hashtable ht = getMap();
- return ht.get(key);
- }
-
- private
- static
- Hashtable getMap() {
Hashtable ht = (Hashtable) context.get();
- if(ht == null) {
- System.out.println("getMap creating new ht. [" + Thread.currentThread().getName()+
- "]");
- ht = new Hashtable(HT_SIZE);
- context.set(ht);
+ if(ht != null) {
+ return ht.get(key);
+ } else {
+ return null;
}
- return ht;
}
-}
-class MappedContext extends InheritableThreadLocal {
-
public
- Object childValue(Object parentValue) {
- Hashtable ht = (Hashtable) parentValue;
- System.out.println("childValue called. ["+Thread.currentThread().getName()+"]");
- return ht.clone();
+ static
+ Hashtable getContext() {
+ return (Hashtable) context.get();
}
- public
- void finalize() throws Throwable {
- System.out.println("finalize called. ["+Thread.currentThread().getName()+"]");
- super.finalize();
- }
}
1.9 +1 -1 jakarta-log4j/src/java/org/apache/log4j/helpers/Makefile
Index: Makefile
===================================================================
RCS file: /home/cvs/jakarta-log4j/src/java/org/apache/log4j/helpers/Makefile,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- Makefile 2001/06/26 19:40:12 1.8
+++ Makefile 2001/08/06 19:29:19 1.9
@@ -23,7 +23,7 @@
VersionHelper.java\
VersionHelper11.java\
VersionHelper20.java\
-
+ ThreadLocalMap.java\
SUBDIRS :=
1.12 +26 -0 jakarta-log4j/src/java/org/apache/log4j/helpers/PatternParser.java
Index: PatternParser.java
===================================================================
RCS file: /home/cvs/jakarta-log4j/src/java/org/apache/log4j/helpers/PatternParser.java,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -r1.11 -r1.12
--- PatternParser.java 2001/07/05 19:09:45 1.11
+++ PatternParser.java 2001/08/06 19:29:19 1.12
@@ -12,6 +12,7 @@
import org.apache.log4j.helpers.AbsoluteTimeDateFormat;
import org.apache.log4j.Layout;
import org.apache.log4j.NDC;
+import org.apache.log4j.MDC;
import org.apache.log4j.spi.LoggingEvent;
import org.apache.log4j.spi.LocationInfo;
import org.apache.log4j.or.ObjectRenderer;
@@ -345,6 +346,11 @@
//LogLog.debug("NDC converter.");
currentLiteral.setLength(0);
break;
+ case 'X':
+ String xOpt = extractOption();
+ pc = new MDCPatternConverter(formattingInfo, xOpt);
+ currentLiteral.setLength(0);
+ break;
default:
LogLog.error("Unexpected char [" +c+"] at position "+i
+" in conversion patterrn.");
@@ -439,6 +445,26 @@
return converted;
}
}
+
+ private static class MDCPatternConverter extends PatternConverter {
+ private String key;
+
+ MDCPatternConverter(FormattingInfo formattingInfo, String key) {
+ super(formattingInfo);
+ this.key = key;
+ }
+
+ public
+ String convert(LoggingEvent event) {
+ Object val = MDC.get(key);
+ if(val == null) {
+ return null;
+ } else {
+ return val.toString();
+ }
+ }
+ }
+
private class LocationPatternConverter extends PatternConverter {
int type;
1.2 +120 -27 jakarta-log4j/src/java/org/apache/log4j/test/MDCStress.java
Index: MDCStress.java
===================================================================
RCS file: /home/cvs/jakarta-log4j/src/java/org/apache/log4j/test/MDCStress.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- MDCStress.java 2001/08/06 16:45:09 1.1
+++ MDCStress.java 2001/08/06 19:29:19 1.2
@@ -3,49 +3,142 @@
import org.apache.log4j.*;
+
+import java.util.Random;
+
public class MDCStress extends Thread {
static Category root = Category.getRoot();
+ static Random random = new Random(17);
+
+ static final int BRANCHING_FACTOR = 2;
+ static final int LOOP_LENGTH = 12;
+
+ static int maxThreads;
+ static int threadCounter = 0;
+
+
public
static
void main(String args[]) {
- for(int i = 0; i < 2; i++) {
- MDC.put("x", new Integer(i));
- MDCStress ms = new MDCStress(true);
- ms.start();
- }
-
- try {Thread.currentThread().sleep(1000);}catch(Exception e){}
- System.out.println("==========");
- System.gc();
- System.gc();
- try {Thread.currentThread().sleep(1000);}catch(Exception e){}
- System.gc();
- System.out.println("==========");
- try {Thread.currentThread().sleep(1000);}catch(Exception e){}
- System.gc();
+
+ Layout layout = new PatternLayout("%r [%t] depth:%X{depth} - %m%n");
+ Appender appender = new ConsoleAppender(layout);
+ root.addAppender(appender);
+
+ if(args.length != 1) {
+ usage();
+ }
+
+ try {
+ maxThreads = Integer.parseInt(args[0]);
+ }
+ catch(java.lang.NumberFormatException e) {
+ System.err.println(e);
+ usage();
+ }
+
+ while(true) {
+ synchronized(MDCStress.class) {
+ // Adding 1 to ensure that at least 1 child is created.
+ createChildren(randomInt(BRANCHING_FACTOR) + 1, 0);
+
+ // wait until all threads are finished
+ try {
+ root.debug("About to wait for notification.");
+ MDCStress.class.wait();
+ root.debug( "Got a notification.");
+ }
+ catch(InterruptedException e) {
+ root.warn("Unpextected InterruptedException received.", e);
+ }
+ }
+ }
+
}
- boolean dosub;
- MDCStress(boolean dosub) {
- this.dosub = dosub;
+ static
+ void usage() {
+ System.err.println( "Usage: "+MDCStress.class + " maxThreads");
+ System.exit(1);
}
+
public
- void run() {
- System.out.println("x="+MDC.get("x")+ " y="+MDC.get("y"));
- if(dosub) {
- Object o = MDC.get("x");
- if(o instanceof Integer) {
- Integer io = (Integer) o;
- MDC.put("y", new Integer(io.intValue()*10));
- MDCStress ms = new MDCStress(false);
- ms.start();
+ static
+ void createChildren(int n, int depth) {
+ if (n <= 0)
+ return;
+
+ synchronized(MDCStress.class) {
+ n = maxThreadsConstained(n);
+ root.debug("Creating " + n+ " child MDCStress threads.");
+ for(int i = 0; i < n; i++) {
+ root.debug("New MDCStress, threadCounter = " + (++threadCounter));
+ new MDCStress(depth+1).start();
}
}
+ }
+
+ int depth;
+
+ MDCStress(int depth) {
+ this.depth = depth;
+ }
+
+ public
+ void run() {
+ MDC.put("depth", new Integer(depth));
+ System.out.println("depth="+MDC.get("depth"));
+
+ int loopLength = randomInt(LOOP_LENGTH);
+ root.debug("In run loop.debug( loopLength = "+loopLength);
+
+ int createIndex = loopLength/2;
+
+ for(int i = 0; i <= loopLength; i++) {
+ if(i==0) {
+ createChildren(randomInt(BRANCHING_FACTOR), depth+1);
+ }
+ }
+
+
+ synchronized(MDCStress.class) {
+ threadCounter--;
+ root.debug( "Exiting run loop. " + threadCounter);
+ if(threadCounter <= 0) {
+ root.debug( "Notifying [main] thread.");
+ MDCStress.class.notify(); // wake up the main thread
+ }
+ }
+
+ }
+
+
+ static
+ public
+ int maxThreadsConstained(int a) {
+ int maxAllowed = MDCStress.maxThreads - MDCStress.threadCounter;
+ return a <= maxAllowed ? a : maxAllowed;
+ }
+
+ /**
+ Return a random value in the range
+ */
+ public
+ static
+ int randomInt(int n) {
+ int r = random.nextInt() % n;
+ return r >= 0 ? r : -r;
+ }
+
+ public
+ static
+ String randomID() {
+ return Integer.toHexString(random.nextInt()& 0xFFFFFF);
}
}
1.3 +1 -1 jakarta-log4j/src/java/org/apache/log4j/test/StressNDC.java
Index: StressNDC.java
===================================================================
RCS file: /home/cvs/jakarta-log4j/src/java/org/apache/log4j/test/StressNDC.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- StressNDC.java 2000/12/14 21:08:14 1.2
+++ StressNDC.java 2001/08/06 19:29:19 1.3
@@ -174,6 +174,6 @@
public
static
String randomID() {
- return Integer.toHexString(random.nextInt()& 0xFFFFFF);
+ return Integer.toString(random.nextInt()& 0xFFFFFF);
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-cvs-unsubscribe@jakarta.apache.org
For additional commands, e-mail: log4j-cvs-help@jakarta.apache.org