⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 log.java

📁 分布式计算平台P2HP-1的源代码;P2HP-1是基于P2P的高性能计算平台
💻 JAVA
字号:
/*
 * Created on 2005-9-19
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package cn.edu.hust.cgcl.biogrid.comm;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author Administrator
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class Log {

	private File mLogFile 		= null;
	private long mlSize 		= 0;
	private long mlMaxSize      = -1;
	private Writer mOfs        	= null;
	private boolean mbAutoFlush	= false;
	private SimpleDateFormat mDateFmt;
	
	protected final static String LINE_SEP = System.getProperty("line.separator","\n");
	
	public Log(File logFile) {
		if (logFile == null) {
            throw new NullPointerException("Log file is null.");
        }

        mDateFmt = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
        mLogFile = logFile;
        open();
	}
	
	public Log(String logFileName){
		File lf=new File(logFileName);
		try{
		if(!lf.exists()) lf.createNewFile();
		}catch(Exception e)
		{
			e.printStackTrace();
		}
		mDateFmt = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
	    mLogFile = lf;
	    open();
	}
	
	/**
     * Open log file.
     */
    private synchronized void open() {
        try {
            if (mLogFile != null) {
                mlSize = mLogFile.length();
                RandomAccessFile raf = new RandomAccessFile(mLogFile, "rw");
                raf.seek(raf.length());
                FileWriter fw = new FileWriter(raf.getFD()); 
                mOfs = new BufferedWriter(fw);
                //mOfs = fw;
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            mOfs = null;
        }
    }
    
    /**
     * Get the current file size.
     */
    public long getCurrentSize() {
        return mlSize;
    }
    /**
     * Print log data.
     *
     * @param str - log message
     */
    public synchronized void write(String str) {

        // write the string
//            String logStr = "[" + mDateFmt.format(new Date()) + " (" + mstLogHeader[level] +")] " + str + LINE_SEP;
//            long strLen = logStr.length();
            
            if (mOfs != null) {

                /*// save file if needed
                if ( (mlMaxSize > 0) && (mlSize > mlMaxSize) ) {
                    //save();
                }
                */
                // now write it
                try {
                    mOfs.write(str+LINE_SEP);
                    mbAutoFlush = true;
                    if (mbAutoFlush) {
                        mOfs.flush();
                    }
                    mlSize += str.length();
                } catch (Exception ex) {
                    mOfs = null;
                    ex.printStackTrace();
                }
            } 
            else {
                System.out.print(str);
            }
    }
    
    public synchronized void dispose() {
    	if(mOfs != null) {
    		try {
				mOfs.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
    	}
    }
    
    /**
	 * log the time.
	 */
	public void logTime(){ 
		String line=mDateFmt.format(new Date());
		write(line);
	}		
	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -