1 4 package net.sourceforge.tracelog.listeners; 5 6 import java.io.BufferedReader ; 7 import java.io.DataInputStream ; 8 import java.io.FileInputStream ; 9 import java.io.FileNotFoundException ; 10 import java.io.IOException ; 11 import java.io.InputStreamReader ; 12 13 public class FileListener implements Runnable { 14 private String logPath; 15 private LogViewerHandler logRepository; 16 17 public FileListener(LogViewerHandler logRepository, String logPath) { 18 this.logPath = logPath; 19 this.logRepository = logRepository; 20 } 21 22 28 public void run() { 29 BufferedReader br = null; 30 String line = null; 31 32 try { 33 br = new BufferedReader (new InputStreamReader (new DataInputStream (new FileInputStream (logPath)))); 34 35 while ((line = br.readLine()) != null) { 37 } 38 39 logRepository.write("Listening on " + logPath + "..."); 41 42 while (!Thread.interrupted()) { 43 try { 44 line = br.readLine(); 45 } 46 catch (IOException e) { 51 line = null; 52 } 53 54 if (line == null) { 55 Thread.sleep(1000); 56 } 57 else { 58 logRepository.write(line); 59 } 60 } 61 } 62 catch (InterruptedException ignored) { 63 logRepository.write("Stopping log... done."); 64 } 65 catch (FileNotFoundException e) { 66 logRepository.write("ERROR: Log file not found: " + logPath); 67 logRepository.write("ERROR: Please make sure this log path is correct and you have enough privilege to access this log path."); 68 } 69 catch (Exception e) { 70 e.printStackTrace(); 71 } 72 finally { 73 try { 74 br.close(); 75 } 76 catch (Exception ignored) { 77 } 78 } 79 } 80 } 81 | Popular Tags |