[jboss-svn-commits] JBoss Common SVN: r3017 - jboss-logmanager/trunk/src/main/java/org/jboss/logmanager.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Thu Mar 5 13:13:24 EST 2009
Author: david.lloyd at jboss.com
Date: 2009-03-05 13:13:24 -0500 (Thu, 05 Mar 2009)
New Revision: 3017
Added:
jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/ExtLogRecord.java
jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/MDC.java
jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/NDC.java
Log:
Re-merge (original commit on 2009-02-19 23:34:07 -0600 (Thu, 19 Feb 2009)): True NDC/MDC support (including log records)
Added: jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/ExtLogRecord.java
===================================================================
--- jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/ExtLogRecord.java (rev 0)
+++ jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/ExtLogRecord.java 2009-03-05 18:13:24 UTC (rev 3017)
@@ -0,0 +1,85 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2008, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.logmanager;
+
+import java.util.Map;
+import java.io.ObjectOutputStream;
+import java.io.IOException;
+
+import java.util.logging.LogRecord;
+
+/**
+ *
+ */
+public class ExtLogRecord extends LogRecord {
+
+ private static final long serialVersionUID = -9174374711278052369L;
+
+ /**
+ * Construct a new instance. Grabs the current NDC immediately. MDC is deferred.
+ *
+ * @param level a logging level value
+ * @param msg the raw non-localized logging message (may be null)
+ */
+ public ExtLogRecord(java.util.logging.Level level, String msg) {
+ super(level, msg);
+ ndc = NDC.get();
+ }
+
+ private Map<String, String> mdcCopy;
+ private String ndc;
+
+ private void writeObject(ObjectOutputStream oos) throws IOException {
+ copyMdc();
+ oos.defaultWriteObject();
+ }
+
+ /**
+ * Copy the MDC. Call this method before passing this log record to another thread. Calling this method
+ * more than once has no additional effect and will not incur extra copies.
+ */
+ public void copyMdc() {
+ if (mdcCopy == null) {
+ mdcCopy = MDC.copy();
+ }
+ }
+
+ /**
+ * Get the value of an MDC property.
+ *
+ * @param key the property key
+ * @return the property value
+ */
+ public String getMdc(String key) {
+ final Map<String, String> mdcCopy = this.mdcCopy;
+ if (mdcCopy == null) {
+ return MDC.get(key);
+ } else {
+ return mdcCopy.get(key);
+ }
+ }
+
+ public String getNdc() {
+ return ndc;
+ }
+}
Added: jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/MDC.java
===================================================================
--- jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/MDC.java (rev 0)
+++ jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/MDC.java 2009-03-05 18:13:24 UTC (rev 3017)
@@ -0,0 +1,63 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2008, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.logmanager;
+
+import java.util.Map;
+import java.util.HashMap;
+
+/**
+ *
+ */
+public final class MDC {
+
+ private MDC() {}
+
+ private static final Holder mdc = new Holder();
+
+ public static String get(String key) {
+ return mdc.get().get(key);
+ }
+
+ public static String put(String key, String value) {
+ return mdc.get().put(key, value);
+ }
+
+ public static String remove(String key) {
+ return mdc.get().remove(key);
+ }
+
+ public static Map<String, String> copy() {
+ return new HashMap<String, String>(mdc.get());
+ }
+
+ private static final class Holder extends InheritableThreadLocal<Map<String, String>> {
+
+ protected Map<String, String> childValue(final Map<String, String> parentValue) {
+ return new HashMap<String, String>(parentValue);
+ }
+
+ protected Map<String, String> initialValue() {
+ return new HashMap<String, String>();
+ }
+ }
+}
Added: jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/NDC.java
===================================================================
--- jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/NDC.java (rev 0)
+++ jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/NDC.java 2009-03-05 18:13:24 UTC (rev 3017)
@@ -0,0 +1,121 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2008, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.logmanager;
+
+import java.util.Arrays;
+
+/**
+ *
+ */
+public final class NDC {
+
+ private NDC() {}
+
+ private static final Holder ndc = new Holder();
+
+ public static int push(String context) {
+ final Stack<String> stack = ndc.get();
+ try {
+ return stack.depth();
+ } finally {
+ stack.push(context);
+ }
+ }
+
+ public static String pop() {
+ final Stack<String> stack = ndc.get();
+ if (stack.isEmpty()) {
+ return "";
+ } else {
+ return stack.pop();
+ }
+ }
+
+ public static void clear() {
+ ndc.get().trimTo(0);
+ }
+
+ public static void trimTo(int size) {
+ ndc.get().trimTo(size);
+ }
+
+ public static String get() {
+ final Stack<String> stack = ndc.get();
+ if (stack.isEmpty()) {
+ return "";
+ } else {
+ return stack.top();
+ }
+ }
+
+ private static final class Holder extends ThreadLocal<Stack<String>> {
+ protected Stack<String> initialValue() {
+ return new Stack<String>();
+ }
+ }
+
+ private static final class Stack<T> {
+ private Object[] data = new Object[32];
+ private int sp;
+
+ public void push(T value) {
+ final int oldlen = data.length;
+ if (sp == oldlen) {
+ Object[] newdata = new Object[oldlen * 3 / 2];
+ System.arraycopy(data, 0, newdata, 0, oldlen);
+ data = newdata;
+ }
+ 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;
+ }
+ }
+ }
+}
More information about the jboss-svn-commits
mailing list