Example usage for java.util.logging ErrorManager FORMAT_FAILURE

List of usage examples for java.util.logging ErrorManager FORMAT_FAILURE

Introduction

In this page you can find the example usage for java.util.logging ErrorManager FORMAT_FAILURE.

Prototype

int FORMAT_FAILURE

To view the source code for java.util.logging ErrorManager FORMAT_FAILURE.

Click Source Link

Document

FORMAT_FAILURE is used when formatting fails for any reason.

Usage

From source file:ffx.ui.LogHandler.java

/**
 * {@inheritDoc}/*from   w w  w  .j av  a2s .co m*/
 *
 * Publish a LogRecord.
 *
 * @since 1.0.
 */
@Override
public synchronized void publish(LogRecord record) {
    if (record.getLevel() == Level.OFF) {
        if (record.getMessage().toLowerCase().contains("algorithm failure:")) {
            mainPanel.setExitType(MainPanel.ExitStatus.ALGORITHM_FAILURE);
        }
        return;
    }
    /**
     * Check if the record is loggable and that we have not already
     * encountered a fatal error.
     */
    if (!isLoggable(record) || fatal) {
        return;
    }
    String msg;
    try {
        msg = getFormatter().format(record);
    } catch (Exception e) {
        /**
         * We don't want to throw an exception here, but we report the
         * exception to any registered ErrorManager.
         */
        reportError(null, e, ErrorManager.FORMAT_FAILURE);
        return;
    }
    try {
        if (record.getLevel() == Level.SEVERE) {
            fatal = true;
            System.err.println(msg);

            Throwable throwable = record.getThrown();
            if (throwable != null) {
                System.err.println(String.format(" Exception %s logged.", throwable));
            }

            // If tryCatchSevere, and the throwable (if it exists) is not an Error, then...
            if (tryCatchSevere && (throwable == null || !(throwable instanceof Error))) {
                System.err.println(" Force Field X may not continue.");
                System.err.println(" Throwing new error...");
                fatal = false;
                if (throwable != null) {
                    throw new LoggerSevereError(throwable);
                } else {
                    throw new LoggerSevereError(" Unknown exception");
                }
            }

            System.err.println(" Force Field X will not continue.");
            System.err.println(" Shutting down...");
            flush();
            mainPanel.setExitType(MainPanel.ExitStatus.SEVERE);
            mainPanel.exit();
        }
        ModelingShell shell = null;
        if (mainPanel != null) {
            shell = mainPanel.getModelingShell();
        }

        if (!headless && shell != null) {
            shell.appendOutputNl(msg, shell.getResultStyle());
        } else {
            System.out.println(msg);
        }
    } catch (Exception e) {
        /**
         * We don't want to throw an exception here, but we report the
         * exception to any registered ErrorManager.
         */
        reportError(null, e, ErrorManager.WRITE_FAILURE);
    }
}

From source file:brut.apktool.Main.java

private static void setupLogging(Verbosity verbosity) {
    Logger logger = Logger.getLogger("");
    for (Handler handler : logger.getHandlers()) {
        logger.removeHandler(handler);//from   w ww  .  j  a  v  a2  s  .  co m
    }
    LogManager.getLogManager().reset();

    if (verbosity == Verbosity.QUIET) {
        return;
    }

    Handler handler = new Handler() {
        @Override
        public void publish(LogRecord record) {
            if (getFormatter() == null) {
                setFormatter(new SimpleFormatter());
            }

            try {
                String message = getFormatter().format(record);
                if (record.getLevel().intValue() >= Level.WARNING.intValue()) {
                    System.err.write(message.getBytes());
                } else {
                    System.out.write(message.getBytes());
                }
            } catch (Exception exception) {
                reportError(null, exception, ErrorManager.FORMAT_FAILURE);
            }
        }

        @Override
        public void close() throws SecurityException {
        }

        @Override
        public void flush() {
        }
    };

    logger.addHandler(handler);

    if (verbosity == Verbosity.VERBOSE) {
        handler.setLevel(Level.ALL);
        logger.setLevel(Level.ALL);
    } else {
        handler.setFormatter(new Formatter() {
            @Override
            public String format(LogRecord record) {
                return record.getLevel().toString().charAt(0) + ": " + record.getMessage()
                        + System.getProperty("line.separator");
            }
        });
    }
}