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

📄 rotateloglistener.java

📁 pos机交易实现源代码 含金融卡交易8583协议实现 开发环境:linux 典型应用:嵌入系统开发 仅供参考
💻 JAVA
字号:
/* * Copyright (c) 2000 jPOS.org.  All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright *    notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright *    notice, this list of conditions and the following disclaimer in *    the documentation and/or other materials provided with the *    distribution. * * 3. The end-user documentation included with the redistribution, *    if any, must include the following acknowledgment: *    "This product includes software developed by the jPOS project  *    (http://www.jpos.org/)". Alternately, this acknowledgment may  *    appear in the software itself, if and wherever such third-party  *    acknowledgments normally appear. * * 4. The names "jPOS" and "jPOS.org" must not be used to endorse  *    or promote products derived from this software without prior  *    written permission. For written permission, please contact  *    license@jpos.org. * * 5. Products derived from this software may not be called "jPOS", *    nor may "jPOS" appear in their name, without prior written *    permission of the jPOS project. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.   * IN NO EVENT SHALL THE JPOS PROJECT OR ITS CONTRIBUTORS BE LIABLE FOR  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE  * POSSIBILITY OF SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the jPOS Project.  For more * information please see <http://www.jpos.org/>. */package org.jpos.util;import java.io.*;import java.util.*;import org.jpos.core.Configurable;import org.jpos.core.Configuration;import org.jpos.core.ConfigurationException;/** * Rotates logs * @author <a href="mailto:apr@cs.com.uy">Alejandro P. Revilla</a> * @version $Revision: 1.8 $ $Date: 2001/04/30 13:28:13 $ * @see org.jpos.core.Configurable * @since jPOS 1.2 */public class RotateLogListener extends SimpleLogListener     implements Configurable{    FileOutputStream f;    String logName;    int maxCopies;    long sleepTime;    long maxSize;    int  msgCount;    public static final int CHECK_INTERVAL = 100;    public static final long DEFAULT_MAXSIZE = 10000000;    /**     * @param name base log filename     * @param sleepTime switch logs every t seconds     * @param maxCopies number of old logs     * @param maxSize in bytes      */    public RotateLogListener         (String logName, int sleepTime, int maxCopies, long maxSize) 	throws IOException    {	super();	this.logName   = logName;	this.maxCopies = maxCopies;	this.sleepTime = sleepTime * 1000;        this.maxSize   = maxSize;	f = null;	openLogFile ();        Timer timer = DefaultTimer.getTimer();	if (sleepTime != 0)             timer.schedule (new Rotate(), sleepTime, sleepTime);    }    public RotateLogListener         (String logName, int sleepTime, int maxCopies) 	throws IOException    {        this (logName, sleepTime, maxCopies, DEFAULT_MAXSIZE);     }    public RotateLogListener () {	super();    }   /**    * Configure this RotateLogListener<br>    * Properties:<br>    * <ul>    *  <li>file      base log filename    *  <li>[window]  in seconds (default 0 - never rotate)    *  <li>[count]   number of copies (default 0 == single copy)    *  <li>[maxsize] max log size in bytes (aprox)    * </ul>    * @param cfg Configuration     * @throws ConfigurationException    */    public void setConfiguration (Configuration cfg)	throws ConfigurationException    {	maxCopies = cfg.getInt  ("copies");	sleepTime = cfg.getInt  ("window") * 1000;	logName   = cfg.get     ("file");        maxSize   = cfg.getLong ("maxsize");        maxSize   = maxSize <= 0 ? DEFAULT_MAXSIZE : maxSize;	try {            openLogFile();	} catch (IOException e) {	    throw new ConfigurationException (e);	}        Timer timer = DefaultTimer.getTimer();	if (sleepTime != 0)             timer.schedule (new Rotate(), sleepTime, sleepTime);    }    public synchronized void log (LogEvent ev) {        if (msgCount++ > CHECK_INTERVAL) {            checkSize();            msgCount = 0;        }                super.log (ev);    }    private synchronized void openLogFile() throws IOException {        if (f != null)            f.close();	f = new FileOutputStream (logName, true);	setPrintStream (new PrintStream(f));    }    public synchronized void logRotate ()	throws IOException    {	setPrintStream (null);	super.close();	f.close();	for (int i=maxCopies; i>0; ) {	    File dest   = new File (logName + "." + i);	    File source = new File (logName + ((--i > 0) ? ("." + i) : ""));	    dest.delete();	    source.renameTo(dest);	}	openLogFile();    }    protected void logDebug (String msg) {        if (p != null) {            p.println ("<log realm=\"rotate-log-listener\" at=\""+new Date().toString() +"\">");            p.println ("   "+msg);            p.println ("</log-debug>");        }    }    private void checkSize() {        File logFile = new File (logName);        if (logFile.length() > maxSize) {            try {                logDebug ("maxSize ("+maxSize+") threshold reached");                logRotate();            } catch (IOException e) {                e.printStackTrace (System.err);            }        }    }    public class Rotate extends TimerTask {        public void run() {            try {                logDebug ("time exceeded - log rotated");                logRotate();            } catch (IOException e) {                e.printStackTrace (System.err);            }        }    }}

⌨️ 快捷键说明

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