Created
April 19, 2016 21:53
-
-
Save lolson/5bffb17b16bac0c9315814762f3a6ec8 to your computer and use it in GitHub Desktop.
A java implementation of tail
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// See JLogTailer | |
public void tailLog(File file) { | |
boolean running = true; | |
int updateInterval = 1000; | |
long filePointer = file.length(); | |
try { | |
while (running) { | |
Thread.sleep(updateInterval); | |
long length = file.length(); | |
if (length < filePointer) { | |
print("Log file was reset. Restarting logging from start of file."); | |
filePointer = length; | |
} else if (length > filePointer) { | |
RandomAccessFile localRandomAccessFile = new RandomAccessFile(file, "r"); | |
localRandomAccessFile.seek(filePointer); | |
String str = null; | |
while ((str = localRandomAccessFile.readLine()) != null) { | |
// TODO add log cache logic | |
print(str); | |
} | |
filePointer = localRandomAccessFile.getFilePointer(); | |
localRandomAccessFile.close(); | |
} | |
} | |
} catch (Exception ex) { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment