Click here to Skip to main content
15,892,927 members
Articles / Programming Languages / Java / Java SE

Free "tail -f" Tool for all Platforms: Tail for Windows, Tail for Linux and Tail for Mac

Rate me:
Please Sign up or sign in to vote.
2.08/5 (8 votes)
19 Jul 2006CPOL3 min read 56K   1.1K   24  
MakeLogic Tail is a freeware similar to " tail -f " of Linux. It is Tail for Windows, Tail for Linux and Tail for Mac
/******************************************************************************
 
Copyright (C) 2005  MakeLogic

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA


Date		: 14th December 2005
Authors		: MakeLogic

Design		: Ujjwal
Developer	: Dharma

********************************************************************************/


package com.makeLogic;

import java.io.LineNumberReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintStream;

import javax.swing.*;
import java.net.InetAddress;

import javax.swing.JFrame;

//import com.makeLogic.messaging.Hub;


public class Tail implements Runnable{


	//...Fields
	String fileName;
	LineNumberReader in;
	File file;
	TailInternalFrame frame;
	long lastModifiedLength;
	long currentModifiedLength;
	long refreshRate;
	long bytesToSkip;
	boolean initialLineNoObtained;

	private ClassLoader cl;

	//..Static TailMDI object
	public static TailMDI tailMDI = null;

	//...Constructors
	public Tail(String fileName){
		this(fileName,500);
	}

	public Tail(String fileName,long refreshRate){

		this.refreshRate = refreshRate;
		this.fileName = fileName;

		initialLineNoObtained = false;

		file = new File(fileName);
		cl = this.getClass().getClassLoader();
		
		//..If the file is very long, skip the initial lines
		long fileLength = file.length();
		//..Assume to read the last 10 lines
		//..Each line may have about 80 chars => 800 chars
		//..Say each char takes 2 bytes ie 1600 bytes 
		//..As a better assumtion - read the last 3 kb if the file is bigger than 3 kb

		long minSize = 3*1024;

		if(fileLength > minSize)
		{
			bytesToSkip = fileLength - minSize;
			//System.outprintln("ML... Bytes Skipped : "+bytesToSkip);
		}
		else
		{
			bytesToSkip = 0;
		}

		try
		{
			in = new LineNumberReader(new FileReader(fileName));
			in.skip(bytesToSkip);
		}
		catch(FileNotFoundException fnfe)
		{
			//fnfe.printStackTrace();
			//System.exit(1);
		}
		catch(Exception excp)
		{
			//excp.printStackTrace();
		}

		frame=new TailInternalFrame("Tail - "+fileName,fileName);
		frame.setFrameIcon(new ImageIcon(cl.getResource("green-ball-static.gif")));

	}

	//...methods
	//...show
	public void show(){
		new Thread(this).start();
		
		//..Show the InternalFrame
		frame.show();
		frame.pack();
		frame.setSize(700,300);
	}

	//...getFrame
	public TailInternalFrame getInternalFrame(){
	    return frame;
	}


	//...run from Runnable interface
	public void run()
	{

		String temp=null;

		while(true)
		{
			frame.setFrameIcon(new ImageIcon(cl.getResource("green-ball-static.gif")));

			try
			{
				Thread.sleep(refreshRate);
			}
			catch(InterruptedException ie)
			{
				//System.outprintln("Refresh cycle skipped" );
			}

			currentModifiedLength=file.length();

			//..Debug
			////System.outprintln("File sizes "+ lastModifiedLength + ":"+currentModifiedLength);

			if(lastModifiedLength!=currentModifiedLength)
			//if(true)
			{
				frame.setFrameIcon(new ImageIcon(cl.getResource("green-ball.gif")));

				lastModifiedLength = currentModifiedLength;
				
				//..Show the contents in the frame
				while(true)
				{
					try
					{
						temp=in.readLine();
						////System.outprintln("ML... LineNo. : "+ in.getLineNumber()+" >>>> Temp : "+temp);
						if(!initialLineNoObtained)
						{
							frame.setInitialLineNo(in.getLineNumber());
							initialLineNoObtained = true;
						}
						//temp = temp.trim();
					}
					catch(IOException ie)
					{
						//ie.printStackTrace();
						//System.exit(1);
					}

					if(temp==null)
					{
						//frame.setEOFReached();
						break;
					}

					frame.append(temp+"\r\n");
				}
			}
		}
	}

public static void main(String args[]){

    try{
		MakeLogicTailSplashScreen splashScreen = new MakeLogicTailSplashScreen();

		//..Put the Look and feel if OS is windows

		String osName = System.getProperty("os.name");

		if(osName.startsWith("Windows"))
		{

			try 
			{
				//..Set the windowsClassName
				String windowsClassName = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";

				//..Set the look and feel to windows
				UIManager.setLookAndFeel(windowsClassName);
			} 
			catch (UnsupportedLookAndFeelException exc) 
			{
				//System.outprintln("Error loading Look & Feel");
			}
		}

    	tailMDI = new TailMDI();
	    tailMDI.show();

		new CheckForNewUpdates(tailMDI);

		splashScreen.setVisible(false);

	    //Hub hub = new Hub(InetAddress.getByName("localhost"),80);

	}
	catch(Exception e){
	    //System.outprintln("Exception : " + e);
		//e.printStackTrace();
	}

}

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
India India
Founder of MakeLogic - A Mobile Solutions and XML Web Services Company. B-Tech engineer from India's reputed IITs.

Designed and Authored products like MicroGraphs, MakeLogic VersionManager and MakeLogic Device Broker. Pls see http://www.makelogic.com

MakeLogic is a strategic business partner of Microsoft, Sun, IBM, HP and Citrix.

Contact : madanuuk@makelogic.com OR +91 - 40 - 55322433

Comments and Discussions