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

📄 hlog.java~

📁 多线程的日志纪录插件程序
💻 JAVA~
字号:
package HUtil.HLog;

import java.io.*;
import java.util.*;
import HUtil.HCollection.*;

public class HLog extends java.lang.Thread
{
    final String MSTR_LOGFILENAME = "HLog";	//log file's name
    final String MSTR_LOGFILETYPE = ".dat";		//log file's type 
    private String mstrFileName;
    private boolean runnable;
    private HCollection mcolLogMsg;		//log message
    private int MINT_LOGFILEMAX = 5;			//log file's max number
    private int MINT_LOGMAXSIZE = 51200;  //500KB	//log file's size(byte)
    private int MINT_LOGBUFFER = 5;       		//log message's buffer(number of the colmessage's item)
    private long SLEEPTIME = 500;				//thread sleep time
    
    //==========================================================================
      /**
        * Constractor
        *
        *@param     arg_strFileName     Log file's name
        *
       **/
    //==========================================================================
    public HLog(String arg_strFileName){
        this();
        if (arg_strFileName.length()>0){
            mstrFileName = arg_strFileName;
        }
    }
	
	//==========================================================================
      /**
        * Constractor
        *
       **/
    //==========================================================================
    public HLog(){
        runnable = true;
        mcolLogMsg = new HCollection();
        mstrFileName = MSTR_LOGFILENAME;
    }
    
    //==========================================================================
      /**
        * set runnable
        *
        *@param     b     				runnable
        *
        *@return    
        *           
       **/
    //==========================================================================
    public void setRunnable(boolean b) throws Exception{
        try{
            if (!b){
                synchronized(this){
                    if (mcolLogMsg.getCount()>0){
                        writeLog(mcolLogMsg);
                        runnable = b;
                    }
                }
            }else{
                runnable = b;
            }
        }catch(Exception e){
            throw new Exception("HLog.setRunnable(boolean) : " + e.toString() + "\n" );
        }
    }
    
    //==========================================================================
      /**
        * set log buffer size
        *
        *@param     i     				log buffer size
        *
        *@return    
        *           
       **/
    //==========================================================================
    public void setLogBufferSize(int i){
        MINT_LOGBUFFER = i;
    }
    
    //==========================================================================
      /**
        * set log file size
        *
        *@param     i     				log file max size (byte)
        *
        *@return    
        *           
       **/
    //==========================================================================
    public void setLogFileSize(int i){
        MINT_LOGMAXSIZE = i;
    }
    
    //==========================================================================
      /**
        * set log file's number
        *
        *@param     i     				log file max nunmber
        *
        *@return    
        *           
       **/
    //==========================================================================
    public void setLogFileNumber(int i){
        MINT_LOGFILEMAX = i;
    }
    
    //==========================================================================
      /**
        *	set log message to buffer
        *
        *@param     s     				log message
        *
        *@return    
        *           
       **/
    //==========================================================================
    public synchronized void setLogMsg(String s){
        try{
            mcolLogMsg.add(getNowTime() + " \" " + s + " \"");
        }catch(Exception e){
            System.out.println("HLog.setLogMsg(String) : " + e.toString());
        }
    }
    
    //==========================================================================
      /**
        *	set thread sleep time
        *
        *@param     l     				thread sleep time (ms)
        *
        *@return    
        *           
       **/
    //==========================================================================
    public void setSleepTimeg(long l){
        SLEEPTIME = l;
    }
    
    //==========================================================================
      /**
        * run
        *
        *@param
        *
        *@return    
        *           
       **/
    //==========================================================================
    public void run(){
        try{
            while(runnable){
                if (mcolLogMsg.getCount()>=MINT_LOGBUFFER){
                    synchronized(this){
                        writeLog(mcolLogMsg);
                    }
                }    
                this.sleep(SLEEPTIME);
            }
        }catch(Exception e){
            System.out.println("HLog.run():" + e.toString());
        }   
    }
    
    private synchronized void writeLog(HCollection arg_colLogMsg){
        String logfile;
        RandomAccessFile ramfil;
        int cnt;
        try{
            cnt = arg_colLogMsg.getCount();
            if (cnt > 0){
                logfile = mgetLogFileName();
                File file = new File(logfile);
                ramfil = new RandomAccessFile(file, "rw");
                ramfil.seek(file.length());
                for (int i=0;i<cnt;i++){
                    ramfil.write((arg_colLogMsg.getItem(i).toString()+"\n").getBytes());
                }
                //System.out.println(mstrLogFileName+" : writed!!");
                ramfil.close();
                arg_colLogMsg.clear();
            }             
        }catch(Exception e){
            System.out.println("HLog.writeLog(HCollection):" + e.toString());
        }
    }
    
    private String mgetLogFileName(){
        String tempfile;
        String result;
        result = this.MSTR_LOGFILENAME + MSTR_LOGFILETYPE;
        
        try{
            File file;    
            for(int i=0;i<=this.MINT_LOGFILEMAX;i++){
                tempfile = mstrFileName + i + MSTR_LOGFILETYPE;
                file = new File(tempfile);
                if (file.exists()){
                    if (!(file.length()>MINT_LOGMAXSIZE)){
                        result = tempfile;
                        break;
                    }
                }else{
                    result = tempfile;
                    break;
                }
            }
            if (result.equals(mstrFileName + MSTR_LOGFILETYPE)){
                file = new File(result);
                if (file.length()>this.MINT_LOGMAXSIZE){
                    file.delete();
                }
            }   
        }catch(Exception e){
            System.out.println("HLog.wrimgetLogFileNameteLog():" + e.toString());
        }
        return result;
    }
    
    //==========================================================================
      /**
        * write log to file
        *
        *@param     arg_strFilename     log file name
        *@param     arg_bytMsg			log message
        *
        *@return    
        *           
       **/
    //==========================================================================
    public static void writeLog(String arg_strFilename, byte[] arg_bytMsg){
        RandomAccessFile ramfil;
        int cnt;
        try{
            File file = new File(arg_strFilename);
            ramfil = new RandomAccessFile(file, "rw");
            ramfil.seek(file.length());
            ramfil.write(arg_bytMsg);
            //System.out.println(mstrLogFileName+" : writed!!");
            ramfil.close();            
        }catch(Exception e){
            System.out.println("HLog.writeLog(byte[]):" + e.toString());
        }
    }
    
    public static String getNowTime(){
        Calendar calender;
        String now_time;
        int minute;
        int hour;
        calender = Calendar.getInstance();
        minute = calender.get(Calendar.MINUTE);
        hour = calender.get(Calendar.HOUR_OF_DAY);
        if (minute<=9){
                now_time = (hour + ":0" + minute);
        }else{
                now_time = (hour + ":" + minute);
        }
        return now_time;
    }
    
    public static String getDateTime(){
        Calendar calender;
        String now_time;
        int minute;
        int hour;
        calender = Calendar.getInstance();
        minute = calender.get(Calendar.MINUTE);
        hour = calender.get(Calendar.HOUR_OF_DAY);
        if (minute<=9){
                now_time = (hour + ":0" + minute);
        }else{
                now_time = (hour + ":" + minute);
        }
        now_time = calender.get(Calendar.YEAR) + "/" + (calender.get(Calendar.MONTH) + 1) + "/" +
            calender.get(Calendar.DATE) + " " + now_time;
        return now_time;
    }
}

⌨️ 快捷键说明

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