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:Main.java

public static String getStackTrace(@Nullable Throwable e) {
    if (e == null) {
        return "";
    }//from   ww w  .ja  v  a2 s  . c om
    StringWriter sw = null;
    PrintWriter pw = null;
    try {
        sw = new StringWriter();
        pw = new PrintWriter(sw);
        e.printStackTrace(pw);
        pw.flush();
        sw.flush();
    } finally {
        if (sw != null) {
            try {
                sw.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
        if (pw != null) {
            pw.close();
        }
    }
    return sw.toString();
}

From source file:org.commonjava.cartographer.rest.util.ResponseUtils.java

public static CharSequence formatEntity(final String id, final Throwable error, final String message) {
    final StringWriter sw = new StringWriter();
    sw.append("Id: ").append(id);
    if (message != null) {
        sw.append("\nMessage: ").append(message);
    }//from  ww w .  ja  va  2 s  . co m

    sw.append(error.getMessage());

    final Throwable cause = error.getCause();
    if (cause != null) {
        sw.append("\nError:\n\n");
        cause.printStackTrace(new PrintWriter(sw));
    }

    sw.write('\n');

    return sw.toString();
}

From source file:cc.sion.core.utils.Exceptions.java

/**
 * ErrorStackString.//from ww w .j av  a  2 s.  c o m
 */
public static String getStackTraceAsString(Throwable ex) {
    StringWriter stringWriter = new StringWriter();
    ex.printStackTrace(new PrintWriter(stringWriter));
    return stringWriter.toString();
}

From source file:mobisocial.metrics.MusubiExceptionHandler.java

static JSONObject jsonForException(Context context, Throwable ex, boolean caught) {
    JSONObject json = new JSONObject();
    try {//from   w w w . j  av a  2  s  .c o  m
        Writer traceWriter = new StringWriter();
        PrintWriter printer = new PrintWriter(traceWriter);
        ex.printStackTrace(printer);
        json.put("type", "exception");
        json.put("caught", caught);
        json.put("app", context.getPackageName());
        json.put("message", ex.getMessage());
        json.put("trace", traceWriter.toString());
        json.put("timestamp", Long.toString(new Date().getTime()));

        boolean devmode = MusubiBaseActivity.isDeveloperModeEnabled(context);
        json.put("musubi_devmode", Boolean.toString(devmode));
        if (devmode) {
            IdentitiesManager im = new IdentitiesManager(App.getDatabaseSource(context));
            MIdentity id = im.getMyDefaultIdentity();
            String user = "Unknown";
            if (id != null) {
                user = UiUtil.safeNameForIdentity(id);
            }
            json.put("musubi_devmode_user_id", user);
            user = App.getMusubi(context).userForLocalDevice(null).getName();
            json.put("musubi_devmode_user_name", user);
        }
        try {
            PackageInfo info = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
            json.put("musubi_version_name", info.versionName);
            json.put("musubi_version_code", Integer.toString(info.versionCode));
        } catch (NameNotFoundException e) {
            // shouldn't happen, but not fatal.
        }
        json.put("android_api", Integer.toString(Build.VERSION.SDK_INT));
        json.put("android_release", Build.VERSION.RELEASE);
        json.put("android_model", Build.MODEL);
        json.put("android_make", Build.MANUFACTURER);
    } catch (JSONException e) {
    }
    return json;
}

From source file:de.alpharogroup.exception.ExceptionExtensions.java

/**
 * Gets the stacktrace as string./*from  w  w  w.  j  ava2  s.c o m*/
 *
 * @param throwable
 *            the throwable
 * @return the stacktrace as string.
 */
public static String getStackTrace(final Throwable throwable) {
    if (null == throwable) {
        return null;
    }
    StringWriter sw = null;
    PrintWriter pw = null;
    String stacktrace = null;
    try {
        sw = new StringWriter();
        pw = new PrintWriter(sw);
        throwable.printStackTrace(pw);
        stacktrace = sw.toString();
    } finally {
        StreamExtensions.closeWriter(sw);
        StreamExtensions.closeWriter(pw);
    }
    return stacktrace;
}

From source file:com.moss.appsnap.keeper.HandyTool.java

private static void printStackTrace(Throwable t, StringBuilder text) {
    try {//from   w w  w.ja va  2 s.  c  om
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        PrintStream s = new PrintStream(out);
        t.printStackTrace(s);
        s.flush();
        out.close();
        text.append(new String(out.toByteArray()));
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.facebook.infrastructure.utils.FBUtilities.java

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

From source file:com.indicator_engine.dao.GLAEventDaoImpl.java

/**
 * Adds a new GLA Entity Object to the Database.
 * @param glaEntity New GLA Entity Object to be saved.
 * @return ID of the Newly Created GLA Entity object in DB.
 **///from  w  w  w. ja  v a2  s.c  o m
public static String getStackTrace(final Throwable throwable) {
    final StringWriter sw = new StringWriter();
    final PrintWriter pw = new PrintWriter(sw, true);
    throwable.printStackTrace(pw);
    return sw.getBuffer().toString();
}

From source file:io.opentracing.contrib.elasticsearch.common.SpanDecorator.java

private static Map<String, Object> errorLogs(Throwable throwable) {
    Map<String, Object> errorLogs = new HashMap<>(4);
    errorLogs.put("event", Tags.ERROR.getKey());
    errorLogs.put("error.kind", throwable.getClass().getName());
    errorLogs.put("error.object", throwable);

    errorLogs.put("message", throwable.getMessage());

    StringWriter sw = new StringWriter();
    throwable.printStackTrace(new PrintWriter(sw));
    errorLogs.put("stack", sw.toString());

    return errorLogs;
}

From source file:ch.cyberduck.cli.Terminal.java

protected static void open(final String[] args, final TerminalPreferences defaults) {
    // Register preferences
    PreferencesFactory.set(defaults);
    final Options options = TerminalOptionsBuilder.options();
    final Console console = new Console();
    try {//from ww  w  . j  a v  a  2  s .com
        final CommandLineParser parser = new PosixParser();
        final CommandLine input = parser.parse(options, args);
        final Terminal terminal = new Terminal(defaults, options, input);
        switch (terminal.execute()) {
        case success:
            console.printf("%s%n", StringUtils.EMPTY);
            System.exit(0);
        case failure:
            console.printf("%s%n", StringUtils.EMPTY);
            System.exit(1);
        }
    } catch (ParseException e) {
        console.printf("%s%n", e.getMessage());
        console.printf("Try '%s' for more options.%n", "duck --help");
        System.exit(1);
    } catch (FactoryException e) {
        console.printf("%s%n", e.getMessage());
        System.exit(1);
    } catch (Throwable error) {
        error.printStackTrace(System.err);
        System.exit(1);
    }
}