StdDebugFile.java revision 9c613e28bb91f464d2e937b21b8174b23499038a
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/**
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * The contents of this file are subject to the terms of the Common Development and
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Distribution License (the License). You may not use this file except in compliance with the
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * License.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * specific language governing permission and limitations under the License.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * When distributing Covered Software, include this CDDL Header Notice in each file and include
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Header, with the fields enclosed by brackets [] replaced by your own identifying
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * information: "Portions copyright [year] [name of copyright owner]".
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Copyright 2014-2015 ForgeRock AS.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortepackage com.sun.identity.shared.debug.file.impl;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteimport com.sun.identity.shared.debug.DebugConstants;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteimport com.sun.identity.shared.debug.IDebug;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteimport com.sun.identity.shared.debug.file.DebugFile;
570de38f63910201fdd77246630b7aa8f9dc5661Surya Prakkiimport com.sun.identity.shared.debug.impl.DebugImpl;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
570de38f63910201fdd77246630b7aa8f9dc5661Surya Prakkiimport java.io.IOException;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteimport java.io.PrintWriter;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteimport java.io.StringWriter;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteimport java.text.SimpleDateFormat;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteimport java.util.Date;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/**
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Debug file dedicated to std out
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortepublic class StdDebugFile implements DebugFile {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte private static final StdDebugFile INSTANCE = new StdDebugFile();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte private PrintWriter stdoutWriter = new PrintWriter(System.out, true);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte private StdDebugFile() {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /**
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Get std out debug file
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @return std debug file
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte public static StdDebugFile getInstance() {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return INSTANCE;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte @Override
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte public void writeIt(String prefix, String msg, Throwable th) throws IOException {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte StringBuilder buf = new StringBuilder(prefix);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte buf.append('\n');
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte buf.append(msg);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (th != null) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte buf.append('\n');
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte StringWriter stBuf = new StringWriter(DebugConstants.MAX_BUFFER_SIZE_EXCEPTION);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte PrintWriter stackStream = new PrintWriter(stBuf);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte th.printStackTrace(stackStream);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte stackStream.flush();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte buf.append(stBuf.toString());
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte stdoutWriter.println(buf.toString());
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /**
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Printing error directly into the stdout. A log header will be generated
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @param debugName debug name
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @param message the error message
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @param ex the exception (can be null)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte public static void printError(String debugName, String message, Throwable ex) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss:SSS a zzz");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte String prefix = debugName + ":" + dateFormat.format(new Date()) + ": " + Thread.currentThread().toString() +
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte "\n";
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte System.err.println(prefix + message);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (ex != null) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ex.printStackTrace(System.err);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte