001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.harmony.jndi.tests.javax.naming.util;
018:
019: import java.io.ByteArrayOutputStream;
020: import java.io.PrintStream;
021:
022: public class Log {
023:
024: private static PrintStream _out = null;
025:
026: private static String latestClzname = null;
027:
028: private static String latestMethod = null;
029:
030: public synchronized static PrintStream getPrintStream() {
031: if (null != _out) {
032: return _out;
033: }
034:
035: try {
036: // SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
037: // _out =
038: // new PrintStream(
039: // new FileOutputStream(
040: // "/bluetrace_" + sdf.format(new Date()) + ".log",
041: // true));
042: _out = new PrintStream(new ByteArrayOutputStream());
043: } catch (Exception e) {
044: e.printStackTrace();
045: _out = System.err;
046: }
047: return _out;
048: }
049:
050: public synchronized static void close() {
051: if (null != _out && System.err != _out) {
052: _out.println("Close log.");
053: _out.close();
054: }
055: _out = null;
056: }
057:
058: private String clzname;
059:
060: private String method;
061:
062: /**
063: * Constructor.
064: *
065: * @param clz
066: */
067: public Log(Class<?> clz) {
068: if (null != clz) {
069: clzname = clz.getName();
070: int dot = clzname.lastIndexOf('.');
071: clzname = clzname.substring(dot + 1);
072: }
073:
074: // getPrintStream().println();
075: // getPrintStream().println(clzname + " inited.");
076: // getPrintStream().println();
077: }
078:
079: /**
080: * Set method.
081: *
082: * @param method
083: */
084: public void setMethod(String method) {
085: this .method = method;
086:
087: latestClzname = clzname;
088: latestMethod = method;
089: }
090:
091: public void log(boolean b) {
092: log("" + b);
093: }
094:
095: public void log(int i) {
096: log("" + i);
097: }
098:
099: public void log(String msg) {
100: _log(clzname, method, msg);
101: }
102:
103: public void log(Throwable t) {
104: if (t == null) {
105: log("Throwable instance is null.");
106: } else {
107: log(t.getMessage());
108: log("Throwable occured, type is " + t.getClass().getName());
109: }
110: //
111: // try {
112: // StringWriter strbuf = new StringWriter();
113: // PrintWriter tempout = new PrintWriter(strbuf);
114: // tempout.println(t.getMessage());
115: // t.printStackTrace(tempout);
116: // tempout.close();
117: // BufferedReader tempin =
118: // new BufferedReader(
119: // new StringReader(strbuf.getBuffer().toString()));
120: // String line;
121: // while ((line = tempin.readLine()) != null) {
122: // line = line.trim();
123: // if (line.startsWith("at javax.naming.Test")) {
124: // String method =
125: // line.substring(
126: // "at javax.naming.".length(),
127: // line.indexOf("("));
128: // String lineNumber =
129: // line.substring(
130: // line.indexOf(":") + 1,
131: // line.indexOf(")"));
132: // try {
133: // Integer.parseInt(lineNumber);
134: // } catch (NumberFormatException e1) {
135: // lineNumber = "";
136: // }
137: // log(method + "(" + lineNumber + ")");
138: // }
139: // }
140: // } catch (IOException e) {
141: // }
142: }
143:
144: /**
145: * Log a message and an exception.
146: *
147: * @param method
148: * @param msg
149: * @param t
150: */
151: public void log(String msg, Throwable t) {
152: log(msg);
153: log(t);
154: }
155:
156: private static void _log(String clzname, String method, String msg) {
157: if (clzname != null && method != null) {
158: latestClzname = clzname;
159: latestMethod = method;
160: } else {
161: clzname = latestClzname;
162: method = latestMethod;
163: }
164:
165: PrintStream out = getPrintStream();
166:
167: out.print(clzname);
168: out.print("\t");
169: out.print(method);
170: out.print("\t");
171: out.print(msg);
172: out.println();
173: }
174:
175: public static void main(String args[]) throws Exception {
176:
177: // NamingException ex = new NamingException("test purpose");
178: // ex.setRemainingName(new CompositeName("RemainingName"));
179: // ex.setResolvedName(new CompositeName("RemainingName"));
180: // ex.setResolvedObj(new Integer(1));
181: // ex.setRootCause(new Exception("root exception"));
182: // ObjectOutputStream out =
183: // (ObjectOutputStream) new ObjectOutputStream(
184: // new FileOutputStream("test/data/NamingException.ser"));
185: // out.writeObject(ex);
186: // out.close();
187: }
188:
189: }
|