001: /*
002: * <copyright>
003: *
004: * Copyright 2004 InfoEther
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026:
027: package org.cougaar.util.log.log4j;
028:
029: import java.io.OutputStream;
030: import java.io.PrintStream;
031:
032: import org.apache.log4j.BasicConfigurator;
033: import org.apache.log4j.Category;
034: import org.apache.log4j.Level;
035:
036: import org.cougaar.bootstrap.SystemProperties;
037:
038: /**
039: * This class is used by the {@link SocketAppender} to redirect
040: * stdout/stdout the logger stream.
041: *
042: * @property org.cougaar.util.log.log4j.stdoutLogLevel
043: * Log level for stdout redirected to the log by the
044: * StreamCapture class (by the log4j SocketAppender for ACME).
045: * Defaults to WARN.
046: *
047: * @property org.cougaar.util.log.log4j.stderrLogLevel
048: * Log level for stderr redirected to the log by the
049: * StreamCapture class (by the log4j SocketAppender for ACME).
050: * Defaults to ERROR.
051: */
052: public final class StreamCapture extends PrintStream {
053:
054: private final boolean isStdOut;
055: private PrintStream origStream;
056: private final Category cat;
057: private final Level level;
058:
059: /**
060: * Private constructor to enforce the use of the factory methods
061: */
062: private StreamCapture(boolean isStdOut) throws SecurityException {
063: super (isStdOut ? System.out : System.err);
064: this .isStdOut = isStdOut;
065: String name;
066: if (isStdOut) {
067: origStream = System.out;
068: name = "STDOUT";
069: } else {
070: origStream = System.err;
071: name = "STDERR";
072: }
073: this .cat = Category.getInstance(name);
074: this .level = getLevel(isStdOut);
075: if (isStdOut) {
076: System.setOut(this );
077: } else {
078: System.setErr(this );
079: }
080: }
081:
082: private void log(String s) {
083: cat.log(level, s);
084: }
085:
086: /**
087: * Factory method for capturing the Stdout stream and forwarding to Log4J
088: */
089: public static StreamCapture captureStdOut()
090: throws SecurityException {
091: return new StreamCapture(true);
092: }
093:
094: /**
095: * Factory method for capturing the StdErr stream and forwarding to Log4J
096: */
097: public static StreamCapture captureStdErr()
098: throws SecurityException {
099: return new StreamCapture(false);
100: }
101:
102: public void closeStream() {
103: if (origStream != null) {
104: try {
105: if (isStdOut) {
106: System.setOut(origStream);
107: } else {
108: System.setErr(origStream);
109: }
110: } finally {
111: origStream = null;
112: }
113: }
114: }
115:
116: protected void finalize() throws java.lang.Throwable {
117: closeStream();
118: super .finalize();
119: }
120:
121: public void println(char x) {
122: log(String.valueOf(x));
123: }
124:
125: public void println(long x) {
126: log(String.valueOf(x));
127: }
128:
129: public void write(int b) {
130: log(String.valueOf(b));
131: }
132:
133: public void print(char[] parm1) {
134: log(new String(parm1));
135: }
136:
137: public void println(float x) {
138: log(String.valueOf(x));
139: }
140:
141: public void println(double x) {
142: log(String.valueOf(x));
143: }
144:
145: public void println(Object x) {
146: log(x.toString());
147: }
148:
149: public void println(boolean x) {
150: log(String.valueOf(x));
151: }
152:
153: public void println(char[] parm1) {
154: log(new String(parm1));
155: }
156:
157: public void print(char c) {
158: log(String.valueOf(c));
159: }
160:
161: public void print(long l) {
162: log(String.valueOf(l));
163: }
164:
165: public void println(String x) {
166: log(String.valueOf(x));
167: }
168:
169: public void print(Object obj) {
170: log(obj.toString());
171: }
172:
173: public void print(double d) {
174: log(String.valueOf(d));
175: }
176:
177: public void print(boolean b) {
178: log(String.valueOf(b));
179: }
180:
181: public void println(int x) {
182: log(String.valueOf(x));
183: }
184:
185: public void print(float f) {
186: log(String.valueOf(f));
187: }
188:
189: public void println() {
190: log("");
191: }
192:
193: public void print(String s) {
194: log(s);
195: }
196:
197: public void write(byte[] chars, int offset, int len) {
198: log(new String(chars, offset, len));
199: }
200:
201: public void print(int i) {
202: log(String.valueOf(i));
203: }
204:
205: private static final Level getLevel(boolean isStdOut) {
206: String prop = "org.cougaar.util.log.log4j.std"
207: + (isStdOut ? "out" : "err") + "LogLevel";
208: String def = (isStdOut ? "WARN" : "ERROR");
209: String s = SystemProperties.getProperty(prop, def);
210: int i = Util.convertStringToInt(s);
211: if (i < 0) {
212: System.err.println("Unknown logging level: -D" + prop + "="
213: + s + ", using " + def + " instead");
214: i = Util.convertStringToInt(def);
215: }
216: return Util.convertIntToLevel(i);
217: }
218:
219: public static void main(String[] args) {
220: /**
221: Test
222: */
223: BasicConfigurator.configure();
224: StreamCapture outStr = StreamCapture.captureStdOut();
225: StreamCapture.captureStdErr();
226: System.out.println("This is a test");
227: System.err.println("This is a test");
228: outStr.closeStream();
229: System.out.println("This is a test");
230: }
231: }
|