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.control;
015
016import java.util.LinkedList;
017
018import ch.qos.logback.classic.Level;
019import ch.qos.logback.core.CoreConstants;
020
021public class ScenarioMaker {
022
023    private final static int AVERAGE_LOGGER_DEPTH = 4;
024    private final static int LOGGER_DEPT_DEV = 2;
025    // the frequency of a set levelInt event for every create logger event
026    private final static int CREATE_LOGGER_TO_SET_LEVEL_FREQUENCY = 5;
027    private final static int SECOND_SET_LEVEL_FREQUENCY = 3;
028
029    private static long count = 0;
030
031    /**
032     * Makes a scenario with len logger creations. Logger names are generated
033     * independently such that the overwhelming majority of logger names will be
034     * unrelated to each other. Each logger creation may be followed with a
035     * randomly generated set levelInt action on that logger.
036     * 
037     * @param len
038     * @return
039     */
040    static public Scenario makeTypeAScenario(int len) {
041        Scenario scenario = new Scenario();
042        ;
043        for (int i = 0; i < len; i++) {
044            String loggerName = ScenarioRandomUtil.randomLoggerName(AVERAGE_LOGGER_DEPTH, LOGGER_DEPT_DEV);
045            scenario.add(new CreateLogger(loggerName));
046        }
047        return scenario;
048    }
049
050    static public Scenario makeRealisticCreationScenario(int len) {
051        Scenario scenario = new Scenario();
052        LinkedList<String> queue = new LinkedList<String>();
053        int loggerCreationCount = 0;
054
055        // add an empty string to get going
056        queue.add("");
057
058        while (loggerCreationCount < len) {
059            if ((count % 100) == 0) {
060                System.out.println("count=" + count);
061            }
062
063            String loggerName = (String) queue.removeFirst();
064            int randomChildrenCount = ScenarioRandomUtil.randomChildrenCount(loggerName);
065
066            if (randomChildrenCount == 0) {
067                scenario.add(new CreateLogger(loggerName));
068                addSetLevelSubScenario(scenario, loggerName);
069                loggerCreationCount++;
070            } else {
071                for (int i = 0; i < randomChildrenCount; i++) {
072                    String childName;
073                    if (loggerName.equals("")) {
074                        childName = ScenarioRandomUtil.randomId();
075                        count += childName.length();
076                    } else {
077                        childName = loggerName + CoreConstants.DOT + ScenarioRandomUtil.randomId();
078                        count += childName.length();
079                    }
080                    queue.add(childName);
081                    addSetLevelSubScenario(scenario, loggerName);
082                    loggerCreationCount++;
083                }
084            }
085        }
086        return scenario;
087    }
088
089    static void addSetLevelSubScenario(Scenario scenario, String loggerName) {
090        if (ScenarioRandomUtil.oneInFreq(CREATE_LOGGER_TO_SET_LEVEL_FREQUENCY)) {
091            Level l = ScenarioRandomUtil.randomLevel();
092            scenario.add(new SetLevel(l, loggerName));
093            if (ScenarioRandomUtil.oneInFreq(SECOND_SET_LEVEL_FREQUENCY)) {
094                l = ScenarioRandomUtil.randomLevel();
095                scenario.add(new SetLevel(l, loggerName));
096            }
097        }
098    }
099
100}