001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.classic.corpus;
015
016import java.io.FileWriter;
017import java.io.IOException;
018import java.net.URL;
019import java.util.List;
020
021import ch.qos.logback.classic.ClassicConstants;
022import ch.qos.logback.classic.spi.ILoggingEvent;
023import ch.qos.logback.classic.spi.IThrowableProxy;
024import ch.qos.logback.classic.spi.LoggerContextVO;
025import ch.qos.logback.classic.spi.PubLoggingEventVO;
026import ch.qos.logback.classic.spi.ThrowableProxyUtil;
027import ch.qos.logback.core.CoreConstants;
028
029/**
030 * 
031 * <p>Usage:
032 * 
033 * <p><code>ILoggingEvent[] eventArray = Corpus.makeStandardCorpus();</code>
034 * 
035 * <p>if you wish to dump the events into a file, say "/corpus.log" :
036 * 
037 * <p> <code>Corpus.dump(eventArray, "/corpus.log");
038 * 
039 * <p>For the model behind the corpus, refer to {@link CorpusModel}.
040 * 
041 * @author Ceki G&uuml;lc&uuml;
042 *
043 */
044public class Corpus {
045
046    static public final int STANDARD_CORPUS_SIZE = 50 * 1000;
047    private static final int STANDARD_SEED = 34780;
048
049    static public List<String> getStandatdCorpusWordList() throws IOException {
050        ClassLoader classLoader = Corpus.class.getClassLoader();
051        URL originOfSpeciesURL = classLoader.getResource("corpus/origin_of_species.txt");
052        return TextFileUtil.toWords(originOfSpeciesURL);
053    }
054
055    /**
056     * Make a standard corpus. The standard corpus has
057     * {@link #STANDARD_CORPUS_SIZE} elements.
058     * 
059     * @return event array representing the standard corpus
060     * @throws IOException
061     */
062    static public ILoggingEvent[] makeStandardCorpus() throws IOException {
063        List<String> worldList = getStandatdCorpusWordList();
064        CorpusModel corpusMaker = new CorpusModel(STANDARD_SEED, worldList);
065        return make(corpusMaker, STANDARD_CORPUS_SIZE, true);
066    }
067
068    static public ILoggingEvent[] make(CorpusModel corpusModel, int n, boolean withCallerData) {
069        LoggerContextVO lcVO = corpusModel.getRandomlyNamedLoggerContextVO();
070        PubLoggingEventVO[] plevoArray = new PubLoggingEventVO[n];
071        for (int i = 0; i < n; i++) {
072            PubLoggingEventVO e = new PubLoggingEventVO();
073            plevoArray[i] = e;
074            e.loggerContextVO = lcVO;
075            e.timeStamp = corpusModel.getRandomTimeStamp();
076
077            LogStatement logStatement = corpusModel.getRandomLogStatementFromPool();
078            e.loggerName = logStatement.loggerName;
079            e.level = logStatement.level;
080            e.message = logStatement.mat.message;
081            e.argumentArray = corpusModel.getRandomArgumentArray(logStatement.mat.numberOfArguments);
082
083            if (withCallerData) {
084                e.callerDataArray = corpusModel.getRandomCallerData(ClassicConstants.DEFAULT_MAX_CALLEDER_DATA_DEPTH, e.loggerName);
085            }
086            e.throwableProxy = logStatement.throwableProxy;
087            e.threadName = corpusModel.getRandomThreadNameFromPool();
088        }
089        return plevoArray;
090    }
091
092    /**
093     * Dump the events passed as argument into the file named targetFile.
094     * 
095     * @param eventArray
096     * @param targetFile
097     * @throws IOException
098     */
099    public static void dump(ILoggingEvent[] eventArray, String targetFile) throws IOException {
100        FileWriter fw = new FileWriter(targetFile);
101        for (ILoggingEvent e : eventArray) {
102            fw.write(e.toString());
103            fw.append(CoreConstants.LINE_SEPARATOR);
104            if (e.getThrowableProxy() != null) {
105                IThrowableProxy tp = e.getThrowableProxy();
106                fw.write(ThrowableProxyUtil.asString(tp));
107            }
108        }
109        fw.flush();
110        fw.close();
111    }
112
113}