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

📄 log.java

📁 利用SyncML开发客户端程序的中间件
💻 JAVA
字号:
/*
 * Copyright (C) 2006-2007 Funambol
 *
 * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package com.funambol.util;

import com.funambol.storage.DataAccessException;
import java.util.Vector;

/**
 * Generic Log class
 */
public class Log {
    
    //---------------------------------------------------------------- Constants
    
    /**
     * Log level DISABLED: used to speed up applications using logging features
     */
    public static final int DISABLED = -1;
    
    /**
     * Log level ERROR: used to log error messages.
     */
    public static final int ERROR = 0;
    
    /**
     * Log level INFO: used to log information messages.
     */
    public static final int INFO = 1;
    
    /**
     * Log level DEBUG: used to log debug messages.
     */
    public static final int DEBUG = 2;
    
    /**
     * Log level TRACE: used to trace the program execution.
     */
    public static final int TRACE = 3;
    
    //---------------------------------------------------------------- Variables
    /**
     * The default appender is the console
     */
    private static Appender out = new ConsoleAppender();
    
    /**
     * The default log level is INFO
     */
    private static int level = INFO;

    /**
     * Probes for performances tracking
     */
    public static Vector probes;
    
    
    //------------------------------------------------------------- Constructors
    /**
     * This class is static and cannot be intantiated
     */
    private Log(){
    }
    
    //----------------------------------------------------------- Public methods
    /**
     * Initialize log file with a specific log level.
     * With this implementation of initLog the initialization is skipped.
     * Only a delete of log is performed.
     *
     * @param object the appender object that write log file
     * @param level the log level
     */
    public static void initLog(Appender object, int level){
        setLogLevel(level);
        out = object;
        deleteLog();
    }
    
    /**
     * Ititialize log file
     * @param object the appender object that write log file
     */
    public static void initLog(Appender object){
        out = object;
        out.initLogFile();
    }
    
    /**
     * Delete log file
     *
     */
    public static void deleteLog() {
        out.deleteLogFile();
    }
    
    /**
     * Accessor method to define log level:
     * @param newlevel log level to be set
     */
    public static void setLogLevel(int newlevel) {
        level = newlevel;
        if ((level!=DISABLED) && (out instanceof RMSAppender)) {
            if (newlevel==DEBUG) {
                RMSAppender.setDefaultLogFileSize(RMSAppender.EXTENDED_SIZE);
            } else {
                RMSAppender.setDefaultLogFileSize(RMSAppender.DEFAULTLOGFILESIZE);
            }
        }
    }
    
    /**
     * Accessor method to retrieve log level:
     * @return actual log level
     */
    public static int getLogLevel() {
        return level;
    }
    
    public static void addProbe(String msg) {
        if (probes==null) {
            probes = new Vector();
        }
        probes.addElement(msg);
    }
    
    /**
     * ERROR: Error message
     * @param msg the message to be logged
     */
    public static void error(String msg) {
        if (level!=DISABLED) {
            try {
                out.writeLogMessage("ERROR", msg);
            } catch (DataAccessException ex) {
                ex.printStackTrace();
            }
        }
    }
    
    /**
     * ERROR: Error message
     * @param msg the message to be logged
     * @param obj the object that send error message
     */
    public static void error(Object obj, String msg) {
        if (level!=DISABLED) {
            try {
                out.writeLogMessage("ERROR", "["+ obj.getClass().getName() + "] " +msg);
            } catch (DataAccessException ex) {
                ex.printStackTrace();
            }
        }
    }
    
    /**
     * INFO: Information message
     * @param msg the message to be logged
     */
    public static void info(String msg) {
        if (level!=DISABLED) {
            if (level >= INFO) {
                try {
                    out.writeLogMessage("INFO", msg);
                } catch (DataAccessException ex) {
                    ex.printStackTrace();
                }
            }
        }
    }
    
    /**
     * INFO: Information message
     * @param msg the message to be logged
     * @param obj the object that send log message
     */
    public static void info(Object obj, String msg) {
        if (level!=DISABLED) {
            if (level >= INFO) {
            }
            try {
                out.writeLogMessage
                        ("INFO", "["+ obj.getClass().getName() + "] " +msg);
            } catch (DataAccessException ex) {
                ex.printStackTrace();
            }
        }
    }
    
    
    
    /**
     * DEBUG: Debug message
     * @param msg the message to be logged
     */
    public static void debug(String msg) {
        if (level!=DISABLED) {
            if (level >= DEBUG) {
                try {
                    out.writeLogMessage("DEBUG", msg);
                } catch (DataAccessException ex) {
                    ex.printStackTrace();
                }
            }
        }
    }
    
    /**
     * DEBUG: Information message
     * @param msg the message to be logged
     * @param obj the object that send log message
     */
    public static void debug(Object obj, String msg) {
        if (level!=DISABLED) {
            if (level >= DEBUG) {
                try {
                    out.writeLogMessage
                            ("DEBUG", "["+ obj.getClass().getName() + "] " +msg);
                } catch (DataAccessException ex) {
                    ex.printStackTrace();
                }
            }
        }
    }
    
    /**
     * TRACE: Debugger mode
     */
    public static void trace(String msg) {
        if (level!=DISABLED) {
            if (level >= TRACE) {
                try {
                    out.writeLogMessage("TRACE", msg);
                } catch (DataAccessException ex) {
                    ex.printStackTrace();
                }
            }
        }
    }
    
    /**
     * TRACE: Information message
     * @param msg the message to be logged
     * @param obj the object that send log message
     */
    public static void trace(Object obj, String msg) {
        if (level!=DISABLED) {
            if (level >= TRACE) {
                try {
                    out.writeLogMessage
                            ("TRACE", "["+ obj.getClass().getName() + "] " +msg);
                } catch (DataAccessException ex) {
                    ex.printStackTrace();
                }
            }
        }
    }
}

⌨️ 快捷键说明

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