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

📄 logwriter.java

📁 用Java写的面相对象的数据库管理系统
💻 JAVA
字号:
// You can redistribute this software and/or modify it under the terms of// the Ozone Library License version 1 published by ozone-db.org.//// The original code and portions created by SMB are// Copyright (C) 1997-2000 by SMB GmbH. All rights reserved.//// $Id: LogWriter.java,v 1.3 2000/10/28 16:55:21 daniela Exp $package org.ozoneDB.util;import java.io.*;import java.util.*;import org.ozoneDB.DxLib.*;/** * @author <a href="http://www.softwarebuero.de/">SMB</a> * @version $Revision: 1.3 $Date: 2000/10/28 16:55:21 $ */public final class LogWriter {        public final static int INFO = 1;    public final static int WARN = 2;    public final static int ERROR = 4;    public final static int DEBUG = 8;    public final static int DEBUG2 = 16;    public final static int DEBUG3 = 32;        protected final static String INFO_PREFIX = "[info] ";    protected final static String WARN_PREFIX = "[warn] ";    protected final static String ERROR_PREFIX = "[error]";    protected final static String DEBUG_PREFIX = "[debug]";        public final static int LINE_WIDTH = 70;        protected DxBag logTargets;        protected int allLevels;            public LogWriter() {        logTargets = new DxArrayBag();    }            /**     * This method allows to quickly find out if there is any log target that     * would receive entries of the specified level.     */    public boolean hasTarget( int level ) {        return (allLevels & level) > 0;    }             public void addLogTarget( OutputStream _out, int _levels ) {        // set also lower debug levels when higher levels are requested        _levels = (_levels & DEBUG3) > 0 ? _levels | DEBUG2 : _levels;        _levels = (_levels & DEBUG2) > 0 ? _levels | DEBUG : _levels;                addLogTarget( new PrintWriter( _out ), _levels );    }             public void addLogTarget( PrintWriter _writer, int _levels ) {        // set also lower debug levels when higher levels are requested        _levels = (_levels & DEBUG3) > 0 ? _levels | DEBUG2 : _levels;        _levels = (_levels & DEBUG2) > 0 ? _levels | DEBUG : _levels;                logTargets.add( new LogTarget( _writer, _levels ) );        allLevels = allLevels | _levels;    }             public void newEntry( Object sender, String msg, int levels ) {        newEntry( sender, msg, null, levels );    }             public void newEntry( Object sender, String msg, Throwable e, int levels ) {        //check if at least one of the specified levels is supported by        //any of the LogTargets        if ((allLevels & levels) > 0) {            for (DxIterator it = logTargets.iterator(); it.next() != null;) {                LogTarget logTarget = (LogTarget)it.object();                if ((logTarget.levels & levels & INFO) > 0) {                    printToWriter( logTarget.writer, sender, msg, INFO_PREFIX, e );                }                 if ((logTarget.levels & levels & WARN) > 0) {                    printToWriter( logTarget.writer, sender, msg, WARN_PREFIX, e );                }                 if ((logTarget.levels & levels & ERROR) > 0) {                    printToWriter( logTarget.writer, sender, msg, ERROR_PREFIX, e );                }                 if ((logTarget.levels & levels & (DEBUG | DEBUG2 | DEBUG3)) > 0) {                    printToWriter( logTarget.writer, sender, msg, DEBUG_PREFIX, e );                }             }         }     }             protected void printToWriter( PrintWriter writer, Object sender, String msg, String status, Throwable e ) {        StringBuffer sb = new StringBuffer( 120 );        sb.append( status );        sb.append( '(' );        String threadNum = String.valueOf( Thread.currentThread().hashCode() );        sb.append( threadNum.substring( threadNum.length() - 3 ) );        sb.append( ") " );        sb.append( rawClassName( sender ) );        sb.append( ": " );        sb.append( msg );        writer.println( sb );        writer.flush();        if (e != null) {            // writer.print ("    exception: " + e.getMessage());            printFormatedException( writer, e, LINE_WIDTH, "    " );        }     }             public static String rawClassName( Object obj ) {        if (obj != null) {            String name = obj.getClass().getName();            int index = name.lastIndexOf( '.' );            return name.substring( index + 1 );        } else {            return "(null)";        }     }             protected void printFormatedException( OutputStream out, Throwable e, int lineWidth, String pre ) {        printFormatedException( new PrintWriter( out ), e, lineWidth, pre );    }             protected void printFormatedException( PrintWriter out, Throwable e, int lineWidth, String pre ) {        StringWriter buff = new StringWriter();        e.printStackTrace( new PrintWriter( buff ) );        StringTokenizer st = new StringTokenizer( buff.toString(), "\n" );        while (st.hasMoreTokens()) {            String line = st.nextToken();            boolean firstSubLine = true;            // while (line.length() > lineWidth) {            do {                int subLineLength = Math.min( lineWidth, line.length() );                String subLine = line.substring( 0, subLineLength );                line = line.substring( subLineLength );                if (!firstSubLine) {                    out.print( "        " );                }                 out.print( pre );                out.println( subLine );                firstSubLine = false;            } while (line.length() > 0);        }         out.flush();    }     }

⌨️ 快捷键说明

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