001/* Copyright (C) 2013 TU Dortmund
002 * This file is part of LearnLib, http://www.learnlib.de/.
003 * 
004 * LearnLib is free software; you can redistribute it and/or
005 * modify it under the terms of the GNU Lesser General Public
006 * License version 3.0 as published by the Free Software Foundation.
007 * 
008 * LearnLib is distributed in the hope that it will be useful,
009 * but WITHOUT ANY WARRANTY; without even the implied warranty of
010 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
011 * Lesser General Public License for more details.
012 * 
013 * You should have received a copy of the GNU Lesser General Public
014 * License along with LearnLib; if not, see
015 * <http://www.gnu.de/documents/lgpl.en.html>.
016 */
017
018package de.learnlib.logging;
019
020import de.learnlib.statistics.StatisticData;
021import java.util.logging.ConsoleHandler;
022import java.util.logging.Filter;
023import java.util.logging.Handler;
024import java.util.logging.Level;
025import java.util.logging.LogManager;
026import java.util.logging.Logger;
027
028/**
029 * LearnLib specific logger. Adds some methods to Logger 
030 * for logging artifacts specific to learning. 
031 * 
032 * @author falkhowar
033 */
034public class LearnLogger extends Logger {
035 
036    private LearnLogger(String name) {
037        super(name,null);
038    }
039    
040    
041                    
042    /**
043     * get an instance of a logger for name. assumes that there is 
044     * no ordinary logger of the same name. 
045     * 
046     * @param name
047     * @return 
048     */
049    public static LearnLogger getLogger(String name) {
050        LogManager m = LogManager.getLogManager();
051        Logger log = m.getLogger(name);
052        if (log == null) {
053            log = new LearnLogger(name);            
054            m.addLogger(log);
055        }
056        return (LearnLogger)log;
057    }
058    
059    /**
060     * Convenience method for easing the common practice of using a class name as
061     * the name for the logger. Calling this method is equivalent to
062     * <pre>
063     * LearnLogger.getLogger(clazz.getName())
064     * </pre>
065     * @param clazz the class from which to retrieve the name
066     * @return the logger for the given class name
067     */
068    public static LearnLogger getLogger(Class<?> clazz) {
069        return getLogger(clazz.getName());
070    }
071    
072    /**
073     * remove all handlers of root logger and add a console hander with
074     * LLConsoleFormatter instead.
075     * 
076     * @deprecated The use of this method is discouraged as it interferes with 
077     * (proper) file-based or class-based configuration of logging. 
078     * 
079     */
080    @Deprecated
081    public static void defaultSetup() {
082        ConsoleHandler handler = new ConsoleHandler();
083        handler.setFormatter(new LLConsoleFormatter());
084        Logger logger = Logger.getLogger("");
085        for (Handler h : logger.getHandlers()) {
086            logger.removeHandler(h);
087        }
088        logger.addHandler(handler);
089    }
090    
091    /**
092     * apply a filter to all handlers of the root logger.
093     * 
094     * 
095     */
096    public static void setGlobalFilter(Filter f) {
097        Logger logger = Logger.getLogger("");
098        for (Handler h : logger.getHandlers()) {
099            h.setFilter(f);
100        }
101    }
102    
103    /**
104     * logs a learning phase at level INFO.
105     * 
106     * @param phase 
107     */
108    public void logPhase(String phase) {
109        LearnLogRecord rec = new LearnLogRecord(Level.INFO, phase, Category.PHASE);
110        this.log(rec);
111    }
112    
113    /**
114     * logs a learning query at level INFO.
115     * 
116     * @param phase 
117     */
118    public void logQuery(String phase) {
119        LearnLogRecord rec = new LearnLogRecord(Level.INFO, phase, Category.QUERY);
120        this.log(rec);
121    }
122    
123    /**
124     * logs setup details
125     * 
126     * @param config 
127     */
128    public void logConfig(String config) {
129        LearnLogRecord rec = new LearnLogRecord(Level.INFO, config, Category.CONFIG);
130        this.log(rec);        
131    }
132
133    /**
134     * log counterexample 
135     * 
136     * @param ce 
137     */
138    public void logCounterexample(String ce) {    
139        LearnLogRecord rec = new LearnLogRecord(Level.INFO, ce, Category.COUNTEREXAMPLE);
140        this.log(rec);          
141    }
142        
143    /**
144     * logs an event. E.g., creation of new table row 
145     * 
146     * @param desc 
147     */
148    public void logEvent(String desc) {
149        LearnLogRecord rec = new LearnLogRecord(Level.INFO, desc, Category.EVENT);
150        this.log(rec);
151    }
152    
153    /**
154     * log a piece of profiling info
155     * 
156     * @param profiling 
157     */
158    public void logProfilingInfo(StatisticData profiling) {
159        LearnLogRecord rec = new StatisticLogRecord(Level.INFO, profiling, Category.PROFILING);
160        this.log(rec);
161    }
162    
163    /**
164     * log statistic info
165     * 
166     * @param statistics 
167     */
168    public void logStatistic(StatisticData statistics) {
169        LearnLogRecord rec = new StatisticLogRecord(Level.INFO, statistics, Category.STATISTIC);
170        this.log(rec);        
171    }
172
173    /**
174     * log a model
175     * 
176     * @param o 
177     */
178    public void logModel(Object o) {
179        LearnLogRecord rec = new PlottableLogRecord(Level.INFO, o, Category.MODEL);
180        this.log(rec);        
181    }
182    
183    /**
184     * log a data structure
185     * 
186     * @param o 
187     */
188    public void logDataStructure(Object o) {
189        LearnLogRecord rec = new PlottableLogRecord(Level.INFO, o, Category.DATASTRUCTURE);
190        this.log(rec);        
191    }
192    
193}