private class TerminalOutputStream extends ByteArrayOutputStream {
    private final String separator = System.getProperty("line.separator");

    @Override
    public synchronized void flush() throws IOException {
      super.flush();
      String record = this.toString();
      super.reset();

      if (record.length() > 0 && !record.equals(separator)) {
        jTerminal.print(record);
        jFrame.repaint();
      }
    }
  }
  private class LoggerOutputStream extends ByteArrayOutputStream {
    private final String separator = System.getProperty("line.separator");
    private final Level level;

    public LoggerOutputStream(Level level) {
      super();
      this.level = level;
    }

    @Override
    public synchronized void flush() throws IOException {
      super.flush();
      String record = this.toString();
      super.reset();

      if (record.length() > 0 && !record.equals(separator)) {
        server.getLogger().logp(level, "LoggerOutputStream", "log" + level, record);
      }
    }
  }
  public ConsoleManager(GlowServer server, String mode) {
    this.server = server;

    if (mode.equalsIgnoreCase("gui")) {
      JTerminalListener listener = new JTerminalListener();

      jFrame = new JFrame("Glowstone");
      jTerminal = new JTerminal();
      jInput =
          new JTextField(80) {
            @Override
            public void setBorder(Border border) {}
          };
      jInput.paint(null);
      jInput.setFont(new Font("Monospaced", Font.PLAIN, 12));
      jInput.setBackground(Color.BLACK);
      jInput.setForeground(Color.WHITE);
      jInput.setMargin(new Insets(0, 0, 0, 0));
      jInput.addKeyListener(listener);

      JLabel caret = new JLabel("> ");
      caret.setFont(new Font("Monospaced", Font.PLAIN, 12));
      caret.setForeground(Color.WHITE);

      JPanel ipanel = new JPanel();
      ipanel.add(caret, BorderLayout.WEST);
      ipanel.add(jInput, BorderLayout.EAST);
      ipanel.setBorder(BorderFactory.createEmptyBorder());
      ipanel.setBackground(Color.BLACK);
      ipanel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
      ipanel.setSize(jTerminal.getWidth(), ipanel.getHeight());

      jFrame.getContentPane().add(jTerminal, BorderLayout.NORTH);
      jFrame.getContentPane().add(ipanel, BorderLayout.SOUTH);
      jFrame.addWindowListener(listener);
      jFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
      jFrame.setLocationRelativeTo(null);
      jFrame.pack();
      jFrame.setVisible(true);
    } else if (mode.equalsIgnoreCase("jline")) {
      jLine = true;
    }

    consoleHandler = new FancyConsoleHandler();

    String logFile = server.getLogFile();
    if (new File(logFile).getParentFile() != null) {
      new File(logFile).getParentFile().mkdirs();
    }
    fileHandler = new RotatingFileHandler(logFile);

    consoleHandler.setFormatter(new DateOutputFormatter(new SimpleDateFormat("HH:mm:ss")));
    fileHandler.setFormatter(new DateOutputFormatter(new SimpleDateFormat("yyyy/MM/dd HH:mm:ss")));

    Logger logger = Logger.getLogger("");
    for (Handler h : logger.getHandlers()) {
      logger.removeHandler(h);
    }
    logger.addHandler(consoleHandler);
    logger.addHandler(fileHandler);

    try {
      reader = new ConsoleReader();
    } catch (IOException ex) {
      server
          .getLogger()
          .log(Level.SEVERE, "Exception inintializing console reader: {0}", ex.getMessage());
      ex.printStackTrace();
    }

    Runtime.getRuntime().addShutdownHook(new ServerShutdownThread());

    System.setOut(new PrintStream(new LoggerOutputStream(Level.INFO), true));
    System.setErr(new PrintStream(new LoggerOutputStream(Level.SEVERE), true));
  }