Advertisement
Superloup10

Console.java

Dec 14th, 2016
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.44 KB | None | 0 0
  1. package com.leviathanstudio.superloup10.wolfide.desktop.view;
  2.  
  3. import com.leviathanstudio.superloup10.wolfide.core.util.Util;
  4.  
  5. import java.io.File;
  6. import java.io.FileNotFoundException;
  7. import java.io.PrintStream;
  8. import java.time.LocalDate;
  9.  
  10. import javafx.scene.control.TextArea;
  11.  
  12. public class Console extends PrintStream
  13. {
  14.     private TextArea output;
  15.    
  16.     public Console(final TextArea out) throws FileNotFoundException
  17.     {
  18.         super(getLogName());
  19.         this.output = out;
  20.     }
  21.    
  22.     @Override
  23.     public void write(int b)
  24.     {
  25.         addToConsole(new String(new char[] {(char)b}));
  26.     }
  27.    
  28.     @Override
  29.     public void write(byte[] buf, int off, int len)
  30.     {
  31.         addToConsole(new String(buf, off, len));
  32.     }
  33.    
  34.     @Override
  35.     public void println(String x)
  36.     {
  37.         addToConsole(x + "\n");
  38.     }
  39.    
  40.     private void addToConsole(String text)
  41.     {
  42.         final String logContent = this.output.getText() + text;
  43.         this.output.setText(logContent);
  44.         writeLog(logContent);
  45.     }
  46.    
  47.     private static File getLogName()
  48.     {
  49.         LocalDate date = LocalDate.now();
  50.         String name = String.format("%1$4d-%2$02d-%3$02d", date.getYear(), date.getMonthValue(), date.getDayOfMonth());
  51.         return new File(Util.LOG_DIR, name + ".log");
  52.     }
  53.    
  54.     private void writeLog(String logContent)
  55.     {
  56.         Util.writeFile(getLogName(), logContent);
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement