Example usage for java.lang Throwable printStackTrace

List of usage examples for java.lang Throwable printStackTrace

Introduction

In this page you can find the example usage for java.lang Throwable printStackTrace.

Prototype

public void printStackTrace(PrintWriter s) 

Source Link

Document

Prints this throwable and its backtrace to the specified print writer.

Usage

From source file:com.twitter.distributedlog.basic.MultiReader.java

private static void readLoop(final DistributedLogManager dlm, final DLSN dlsn,
        final CountDownLatch keepAliveLatch) {
    System.out.println("Wait for records from " + dlm.getStreamName() + " starting from " + dlsn);
    dlm.openAsyncLogReader(dlsn).addEventListener(new FutureEventListener<AsyncLogReader>() {
        @Override/*from   ww w .  j ava  2 s .  c  o  m*/
        public void onFailure(Throwable cause) {
            System.err.println("Encountered error on reading records from stream " + dlm.getStreamName());
            cause.printStackTrace(System.err);
            keepAliveLatch.countDown();
        }

        @Override
        public void onSuccess(AsyncLogReader reader) {
            System.out.println("Open reader to read records from stream " + reader.getStreamName());
            readLoop(reader, keepAliveLatch);
        }
    });
}

From source file:de.kurashigegollub.dev.gcatest.Utils.java

public static String getStackTraceAsString(Throwable t) {
    StringWriter sw = new StringWriter();
    t.printStackTrace(new PrintWriter(sw));
    return sw.toString();
}

From source file:com.github.ibole.infrastructure.common.exception.MoreThrowables.java

/**
 * ErrorStackString.//www  .  jav a  2  s. co m
 */
public static String getStackTraceAsString(Throwable e) {
    if (e == null) {
        return "";
    }
    StringWriter stringWriter = new StringWriter();
    e.printStackTrace(new PrintWriter(stringWriter));
    return stringWriter.toString();
}

From source file:com.twitter.distributedlog.basic.MultiReader.java

private static void readLoop(final AsyncLogReader reader, final CountDownLatch keepAliveLatch) {
    final FutureEventListener<LogRecordWithDLSN> readListener = new FutureEventListener<LogRecordWithDLSN>() {
        @Override/*from   w  w  w . j a  v  a2  s .com*/
        public void onFailure(Throwable cause) {
            System.err.println("Encountered error on reading records from stream " + reader.getStreamName());
            cause.printStackTrace(System.err);
            keepAliveLatch.countDown();
        }

        @Override
        public void onSuccess(LogRecordWithDLSN record) {
            System.out
                    .println("Received record " + record.getDlsn() + " from stream " + reader.getStreamName());
            System.out.println("\"\"\"");
            System.out.println(new String(record.getPayload(), UTF_8));
            System.out.println("\"\"\"");
            reader.readNext().addEventListener(this);
        }
    };
    reader.readNext().addEventListener(readListener);
}

From source file:StringUtil.java

/**
 * Get the stack trace of the supplied exception.
 * //from w ww.  j  a  v a2 s .  c o  m
 * @param throwable the exception for which the stack trace is to be returned
 * @return the stack trace, or null if the supplied exception is null
 */
public static String getStackTrace(Throwable throwable) {
    if (throwable == null)
        return null;
    final ByteArrayOutputStream bas = new ByteArrayOutputStream();
    final PrintWriter pw = new PrintWriter(bas);
    throwable.printStackTrace(pw);
    pw.close();
    return bas.toString();
}

From source file:com.netflix.genie.agent.cli.UserConsole.java

/**
 * Load and print the Spring banner (if one is configured) to UserConsole.
 *
 * @param environment the Spring environment
 *///from  w ww  .j ava 2s . c o m
static void printBanner(final Environment environment) {
    try {
        final String bannerLocation = environment.getProperty(BANNER_LOCATION_SPRING_PROPERTY_KEY);
        if (StringUtils.isNotBlank(bannerLocation)) {
            final ResourceLoader resourceLoader = new DefaultResourceLoader();
            final Resource resource = resourceLoader.getResource(bannerLocation);
            if (resource.exists()) {
                final String banner = StreamUtils.copyToString(resource.getInputStream(),
                        environment.getProperty(BANNER_CHARSET_SPRING_PROPERTY_KEY, Charset.class,
                                StandardCharsets.UTF_8));
                UserConsole.getLogger().info(banner);
            }
        }
    } catch (final Throwable t) {
        System.err.println("Failed to print banner: " + t.getMessage());
        t.printStackTrace(System.err);
    }
}

From source file:com.stratuscom.harvester.Utils.java

public static String stackTrace(Throwable t) {
    StringWriter s = new StringWriter();
    PrintWriter pw = new PrintWriter(s);
    t.printStackTrace(pw);
    return s.toString();
}

From source file:StringUtils.java

/**
 * Convert an exception to a String with full stack trace
 * @param ex the exception/*from w  w w.j a v a2 s  .  c  o m*/
 * @return a String with the full stacktrace error text
 */
public static String getStringFromStackTrace(Throwable ex) {
    if (ex == null) {
        return "";
    }
    StringWriter str = new StringWriter();
    PrintWriter writer = new PrintWriter(str);
    try {
        ex.printStackTrace(writer);
        return str.getBuffer().toString();
    } finally {
        try {
            str.close();
            writer.close();
        } catch (IOException e) {
            //ignore
        }
    }
}

From source file:Main.java

/**
 * @param thread a thread//from  w  w  w .ja v  a 2s  .com
 * @return a human-readable representation of the thread's stack trace
 */
public static String formatStackTrace(Thread thread) {
    Throwable t = new Throwable(
            String.format("Stack trace for thread %s (State: %s):", thread.getName(), thread.getState()));
    t.setStackTrace(thread.getStackTrace());
    StringWriter sw = new StringWriter();
    t.printStackTrace(new PrintWriter(sw));
    return sw.toString();
}

From source file:Transform.java

/**
 * convert a Throwable into an array of Strings
 * @param throwable//  ww  w  .  j  a v a2 s.c om
 * @return string representation of the throwable
 */
public static String[] getThrowableStrRep(Throwable throwable) {
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    throwable.printStackTrace(pw);
    pw.flush();
    LineNumberReader reader = new LineNumberReader(new StringReader(sw.toString()));
    ArrayList<String> lines = new ArrayList<String>();
    try {
        String line = reader.readLine();
        while (line != null) {
            lines.add(line);
            line = reader.readLine();
        }
    } catch (IOException ex) {
        lines.add(ex.toString());
    }
    String[] rep = new String[lines.size()];
    lines.toArray(rep);
    return rep;
}