HytaleFileHandler class

Пакет: com.hypixel.hytale.logger.backend

Файл: com/hypixel/hytale/logger/backend/HytaleFileHandler.java

extends: Thread

Поля (6)

МодификаторыТипИмя
final DateTimeFormatter LOG_FILE_DATE_FORMAT
Path var1
List var1
String var2
String var3
var3

Методы (11)

МодификаторыВозвратСигнатура
abstract throw new IllegalStateExceptionthrow new IllegalStateException("Thread should not be started when no file handler exists!")
abstract throw new IllegalStateExceptionthrow new IllegalStateException("Already enabled!")
abstract throw new RuntimeExceptionthrow new RuntimeException("Failed to create file handler!", var4)
public void enablevoid enable()
if if(this.fileHandler == null)
if if(this.fileHandler != null)
if if(this.fileHandler != null)
if if(this.fileHandler != null)
public void logvoid log(@Nonnull LogRecord var1)
public void shutdownvoid shutdown()
abstract super super("HytaleLogger")

Исходный код

Показать/скрыть
class="kw">package com.hypixel.hytale.logger.backend;

class="kw">import it.unimi.dsi.fastutil.objects.ObjectArrayList;
class="kw">import java.io.IOException;
class="kw">import java.nio.file.Files;
class="kw">import java.nio.file.Path;
class="kw">import java.nio.file.Paths;
class="kw">import java.time.LocalDateTime;
class="kw">import java.time.ZoneId;
class="kw">import java.time.format.DateTimeFormatter;
class="kw">import java.util.List;
class="kw">import java.util.concurrent.BlockingQueue;
class="kw">import java.util.concurrent.LinkedBlockingQueue;
class="kw">import java.util.logging.FileHandler;
class="kw">import java.util.logging.Level;
class="kw">import java.util.logging.LogRecord;
class="kw">import javax.annotation.Nonnull;
class="kw">import javax.annotation.Nullable;

class="kw">public class HytaleFileHandler class="kw">extends Thread {
   class="kw">public class="kw">static class="kw">final DateTimeFormatter LOG_FILE_DATE_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd_HH-mm-ss");
   class="kw">public class="kw">static class="kw">final HytaleFileHandler INSTANCE = new HytaleFileHandler();
   class="kw">private class="kw">final BlockingQueue<LogRecord> logRecords = new LinkedBlockingQueue<>();
   @Nullable
   class="kw">private FileHandler fileHandler;

   class="kw">public HytaleFileHandler() {
      super("HytaleLogger");
      this.setDaemon(true);
   }

   @Override
   class="kw">public void run() {
      if (this.fileHandler == null) {
         throw new IllegalStateException("Thread should not be started when no file handler exists!");
      }

      try {
         while (!this.isInterrupted()) {
            this.fileHandler.publish(this.logRecords.take());
         }
      } catch (InterruptedException var2) {
         Thread.currentThread().interrupt();
      }
   }

   @Nullable
   class="kw">public FileHandler getFileHandler() {
      class="kw">return this.fileHandler;
   }

   class="kw">public void enable() {
      if (this.fileHandler != null) {
         throw new IllegalStateException("Already enabled!");
      }

      try {
         Path var1 = Paths.get("logs/");
         if (!Files.isDirectory(var1)) {
            Files.createDirectory(var1);
         }

         String var2 = "logs/" + LOG_FILE_DATE_FORMAT.format(LocalDateTime.now(ZoneId.systemDefault()));
         String var3 = var2 + "_server.log";
         if (Files.exists(Paths.get(var3))) {
            var3 = var2 + "%u_server.log";
         }

         this.fileHandler = new FileHandler(var3);
         this.fileHandler.setEncoding("UTF-8");
         this.fileHandler.setLevel(Level.ALL);
         this.fileHandler.setFormatter(new HytaleLogFormatter(() -> false));
      } catch (IOException var4) {
         throw new RuntimeException("Failed to create file handler!", var4);
      }

      this.start();
   }

   class="kw">public void log(@Nonnull LogRecord var1) {
      if (!this.isAlive()) {
         if (this.fileHandler != null) {
            this.fileHandler.publish(var1);
         }
      } else {
         this.logRecords.add(var1);
      }
   }

   class="kw">public void shutdown() {
      if (this.fileHandler != null) {
         this.interrupt();

         try {
            this.join();
         } catch (InterruptedException var2) {
            Thread.currentThread().interrupt();
         }

         List var1 = new ObjectArrayList();
         this.logRecords.drainTo(var1);
         var1.forEach(this.fileHandler::publish);
         this.fileHandler.flush();
         this.fileHandler.close();
         this.fileHandler = null;
      }
   }
}