Beispiel #1
0
 public void reset() {
   Main main = new Main();
   try {
     main.init(oldStdin, stdinPipe, stdoutPipe);
     stop();
     try {
       main.start();
     } catch (XMPPException
         | NoSuchAlgorithmException
         | SmackException
         | KeyManagementException e) {
       e.printStackTrace();
       System.err.println("Failed to start up new main. Trying to revert previous one.");
       try {
         start();
       } catch (XMPPException
           | NoSuchAlgorithmException
           | SmackException
           | KeyManagementException e1) {
         e1.printStackTrace();
         System.err.println("Failed to revert. Shutting down.");
       }
     }
   } catch (IOException ignore) {
   }
 }
Beispiel #2
0
  public static void main(String[] args) {
    log = Logger.getLogger(Main.class.getName());
    try {
      // Setting up logging

      Logger packageLog = Logger.getLogger(ChatBot.class.getPackage().getName());
      File logFile = new File(LOGFILE + ".log");
      if (logFile.exists()) {
        Calendar cal = Calendar.getInstance();
        File backup =
            new File(
                String.format(
                    "%s_%d_%d_%d.log",
                    LOGFILE,
                    cal.get(Calendar.YEAR),
                    cal.get(Calendar.MONTH) + 1,
                    cal.get(Calendar.DAY_OF_MONTH)));
        if (backup.exists())
          try (BufferedReader reader = new BufferedReader(new FileReader(logFile));
              PrintWriter writer = new PrintWriter(new FileWriter(backup, true))) {
            char buff[] = new char[1024];
            int n;
            while ((n = reader.read(buff)) > 0) {
              writer.write(buff, 0, n);
            }
            log.info("Appened log to backup " + backup.getName());
          } catch (IOException e) {
            log.log(Level.SEVERE, "Couldn't append log to " + backup.getName(), e);
          }
        else {
          try {
            FileUtils.moveFile(logFile, backup);
            log.info("Moved log to backup " + backup.getName());
          } catch (IOException e) {
            log.log(Level.SEVERE, "Couldn't move log to " + backup.getName(), e);
          }
        }
      }

      //noinspection ResultOfMethodCallIgnored
      // logFile.delete();
      Handler handler = new FileHandler(LOGFILE + ".log");
      handler.setFormatter(new SimpleFormatter());
      packageLog.setLevel(Level.FINE);
      packageLog.addHandler(handler);

      // Starting up XMPPCraft
      Main main = new Main();
      PipeInputStream stdinPipe = new PipeInputStream(1048576);
      PipeOutputStream stdoutPipe = new PipeOutputStream();
      main.init(
          System.in, new PipeOutputStream(stdinPipe), new PipeInputStream(stdoutPipe, 1048576));
      System.setIn(stdinPipe);
      System.setOut(new PrintStream(new TeeOutputStream(System.out, stdoutPipe)));
      main.start();
      // Starting up Minecraft
      MinecraftServer.main(args);
      // DummyMinecraftServer.main(args);
    } catch (KeyManagementException
        | NoSuchAlgorithmException
        | SmackException
        | XMPPException
        | IOException e) {
      String filename =
          String.format(
              "crashreport_%s_%s.log",
              e.getClass().getSimpleName(), new SimpleDateFormat("MM_dd.HH_mm").format(new Date()));
      File f = new File(filename);
      try {
        if (f.createNewFile()) {
          PrintWriter out = new PrintWriter(f);
          out.println("Error on startup :");
          out.printf(
              "JVM: %s %s on %s\n",
              System.getProperty("java.vm.name"),
              System.getProperty("java.runtime.version"),
              System.getProperty("os.name"));
          e.printStackTrace(out);
          out.flush();
          out.close();
        }
      } catch (IOException ignore) {
      } // lol what can you do
      log.severe("Crash detected. Generating report.");
    }
  }