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

📄 logservice.java

📁 一个很好的LIBSVM的JAVA源码。对于要研究和改进SVM算法的学者。可以参考。来自数据挖掘工具YALE工具包。
💻 JAVA
字号:
/*
 *  YALE - Yet Another Learning Environment
 *  Copyright (C) 2001-2004
 *      Simon Fischer, Ralf Klinkenberg, Ingo Mierswa, 
 *          Katharina Morik, Oliver Ritthoff
 *      Artificial Intelligence Unit
 *      Computer Science Department
 *      University of Dortmund
 *      44221 Dortmund,  Germany
 *  email: yale-team@lists.sourceforge.net
 *  web:   http://yale.cs.uni-dortmund.de/
 *
 *  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 edu.udo.cs.yale.tools;

import edu.udo.cs.yale.Yale;
import edu.udo.cs.yale.gui.YaleGUI;
import edu.udo.cs.yale.Experiment;
import edu.udo.cs.yale.operator.Operator;
import java.io.*;
import java.util.*;

/** Utility class providing static methods for logging.<br>
 *  Parameters read from config file:
 *   <ul>
 *     <li>logfile (filename or "stdout" or "stderr")</li>
 *     <li>logverbosity (possible values are in {@link LogService#LOG_VERBOSITY_NAMES}</li>
 *   </ul> 
 *
 *  @author  Simon
 *  @version $Id: LogService.java,v 2.12 2004/08/27 11:57:45 ingomierswa Exp $
 */
public class LogService {

    /** The PrintStream to write the messages to. */
    private static FormattedPrintStream logOut;
    /** The minimal verbosity level. Message below this level are ignored. */
    private static int minVerbosityLevel;
    /** The last printed message. */
    private static String lastMessage;
    /** Counts how often a message was repeated. */
    private static int equalMessageCount;

    private static File logFile = null;

    private static final String[] VERBOSITYLEVEL = 
    {"",
     "",
     "",
     "",
     "",
     "$b[Warning]^b ",
     "$b[Exception]^b ",
     "$b[Error]^b ",
     "$b[Fatal]^b ",
     ""};

    // -------------------- Verbosity Level --------------------

    public static final int MINIMUM     = 0;
    public static final int TASK        = 1;
    public static final int INIT        = 2;
    public static final int OPERATOR    = 3;
    public static final int STATUS      = 4;
    public static final int WARNING     = 5;
    public static final int EXCEPTION   = 6;
    public static final int ERROR       = 7;
    public static final int FATAL       = 8;
    public static final int MAXIMUM     = 9;
    /** For switching off logging during testing. */
    public static final int OFF         = 10;

    public static final String LOG_VERBOSITY_NAMES[] = {"minimum", "task", "init", "operator", "status", "warning", "exception", "error", "fatal", "maximum" };

    // -------------------- Methoden zum Initialisieren / Beenden --------------------

    /** Initialises the LogService reading parameters form the ParameterService. */
    public static void init(Experiment experiment) {
	int verbosityLevel = experiment.getRootOperator().getParameterAsInt("logverbosity");
	String filename = experiment.getRootOperator().getParameterAsString("logfile");
    	boolean format = ParameterService.booleanValue(ParameterService.getProperty("yale.logfile.format"), false);

	if (filename == null) {
	    LogService.logMessage("Logfile not set, using stderr", LogService.WARNING);
	    logFile = null;
	    init(System.err, verbosityLevel, format, -1);
	} else if (filename.equals("stderr")) {
	    logFile = null;
	    init(System.err, verbosityLevel, format, -1);
	} else if (filename.equals("stdout")) {
	    logFile = null;
	    init(System.out, verbosityLevel, format, -1);
	} else {
	    OutputStream out = null;
	    logFile = experiment.resolveFileName(filename);
	    try {
		out = new FileOutputStream(logFile);
	    } catch (IOException e) {
		LogService.logMessage("Cannot create logfile '"+filename+"': "+e.getClass()+":"+e.getMessage(), LogService.MAXIMUM);
		LogService.logMessage("using stderr", LogService.MAXIMUM);
		out = System.err;		
	    }
	    init(out, verbosityLevel, format, -1);
	}
    }

    /** Initialises the LogService.
     *  @param out The stream to write the messages to.
     *  @param verbosityLevel Only messages with message.verbosityLevel >= verbosityLevel are logged
     *  @param format must be true if the output should be formatted by the FormattedPrintStream
     *  @param linewidth The linewidth. If -1 is used, no automatic linebreak is used. */
    public static void init(OutputStream out, int verbosityLevel, boolean format, int linewidth) {
	logOut = new FormattedPrintStream(out, format, 20, linewidth);
	LogService.minVerbosityLevel = verbosityLevel;
	logMessage("Started logging", INIT);
	lastMessage = ""; 
	equalMessageCount = 0;
    }

    public static void initGUI() {
	minVerbosityLevel = Yale.getExperiment().getRootOperator().getParameterAsInt("logverbosity");
	String logOut = Yale.getExperiment().getRootOperator().getParameterAsString("logfile");
	OutputStream out = null;
	if (logOut != null) {
	    try {
		logFile = Yale.getExperiment().resolveFileName(logOut);
		out = new StreamMultiplier(new java.io.OutputStream[] {
		    new FileOutputStream(logFile),
		    YaleGUI.getMainFrame().getMessageViewer().outputStream } );
	    } catch (java.io.IOException e) {
		throw new RuntimeException("Cannot create log file: "+e);
	    }
	} else {
	    out = YaleGUI.getMainFrame().getMessageViewer().outputStream;
	}
	setOutputStream(out);
	logMessage("Log verbosity is: "+LOG_VERBOSITY_NAMES[minVerbosityLevel], LogService.MAXIMUM);
    }

    public static void setOutputStream(OutputStream out) {
	logOut = new FormattedPrintStream(out, false, 0, -1);
    }

    /** Closes the stream. */
    public static void close() {
	logMessage("Ended logging", INIT);
	logOut.close();
    }

    public static void setVerbosityLevel(int level) {
	LogService.minVerbosityLevel = level;
    }

    // ----------------------------------------
    
    private static Operator getCurrentOperator() {
	Experiment exp = Yale.getExperiment();
	if (exp != null) return exp.getRootOperator();
	else return null;
    }
    // -------------------- Methoden zum Protokollieren --------------------

    /** Writes the message to the output stream if the verbosity level is high enough. */
    public static void logMessage(String message, int verbosityLevel) {
	if (message == null) return;
	if (verbosityLevel < minVerbosityLevel) return;
	if (message.equals(lastMessage)) {
	    equalMessageCount++;
	    return;
	}
	if (equalMessageCount > 0) {
	    logOut.println("Last message repeated " + equalMessageCount + " times.");
	    equalMessageCount = 0;
	}
	lastMessage = message;
	logOut.println(getTime() + " " + VERBOSITYLEVEL[verbosityLevel] + message);
    }

    /** Writes the message to the output stream if the verbosity level is high enough and appends the
     *  experiment tree with operator op marked. */
    public static void logMessageWithTree(String message, int verbosityLevel, Operator op) {
	if (verbosityLevel < minVerbosityLevel) return;
	logOut.setIndented(true);
	logOut.println(Yale.getExperiment().getRootOperator().createMarkedExperimentTree(2, "here ==>", op));
	logOut.setIndented(false);
	logMessage(message, verbosityLevel);
    }


    /** Writes the message and the stack trace of the exception. */
    public static void logException(String message, Throwable exception) {
	if (EXCEPTION < minVerbosityLevel) return;
	Operator op = getCurrentOperator();
	if (op != null) {
	    logMessage(Tools.classNameWOPackage(exception.getClass()) + " occured in "+
		       Tools.ordinalNumber(op.getApplyCount())+" application of "+op,
		       EXCEPTION);
	} else {
	    logMessage(Tools.classNameWOPackage(exception.getClass()) + " occured.", EXCEPTION);
	}
	logMessage(exception.getMessage(), EXCEPTION);
//  	if ((Yale.getExperiment().getRootOperator() != null) && (op != null))
//  	    logOut.println(Yale.getExperiment().getRootOperator().createMarkedExperimentTree(2, "here ==>", op));
	//logOut.println(getTime() + " " + VERBOSITYLEVEL[EXCEPTION] + message);
	if (message != null) logMessage(message, EXCEPTION);
	//logOut.setIndented(true);
	exception.printStackTrace(logOut);
	//logOut.setIndented(false);
    }


    // -------------------- private Hilfsmethoden --------------------

    /** Returns the current system time nicely formatted. */
    private static String getTime() {
	return java.text.DateFormat.getDateTimeInstance().format(new Date()) + ":";
    }

    /** Adds a leading null if necessary. */
    static String zweistellig(int i) {
	return (i < 10 ? "0" : "") + i;
    } 

    static {
	minVerbosityLevel = MINIMUM;
	logOut = new FormattedPrintStream(System.err);
    }

    /** Terminates the program. */
    private static void fatal() {
	LogService.logMessage("Experiment not successful", LogService.MAXIMUM);
	System.err.println("Experiment not successful");
	if (Yale.getExperiment() != null) {
	    Yale.getExperiment().fatal();
	}
	else System.exit(2);
    }

    public static File getLogFile() {
	return logFile;
    }
}

⌨️ 快捷键说明

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