1   /**
2    * Logback: the generic, reliable, fast and flexible logging framework.
3    * 
4    * Copyright (C) 2000-2008, QOS.ch
5    * 
6    * This library is free software, you can redistribute it and/or modify it under
7    * the terms of the GNU Lesser General Public License as published by the Free
8    * Software Foundation.
9    */
10  package ch.qos.logback.classic.control;
11  
12  import java.util.LinkedList;
13  
14  import ch.qos.logback.classic.ClassicGlobal;
15  import ch.qos.logback.classic.Level;
16  
17  public class ScenarioMaker {
18  
19    private final static int AVERAGE_LOGGER_DEPTH = 4;
20    private final static int LOGGER_DEPT_DEV = 2;
21    // the frequency of a set levelInt event for every create logger event
22    private final static int CREATE_LOGGER_TO_SET_LEVEL_FREQUENCY = 5;
23    private final static int SECOND_SET_LEVEL_FREQUENCY = 3;
24  
25    private static long count = 0;
26  
27    /**
28     * Makes a scenario with len logger creations. Logger names are generated
29     * independently such that the overwhelming majority of logger names will be
30     * unrelated to each other. Each logger creation may be followed with a
31     * randomly generated set levelInt action on that logger.
32     * 
33     * @param len
34     * @return
35     */
36    static public Scenario makeTypeAScenario(int len) {
37      Scenario scenario = new Scenario();
38      ;
39      for (int i = 0; i < len; i++) {
40        String loggerName = ScenarioRandomUtil.randomLoggerName(
41            AVERAGE_LOGGER_DEPTH, LOGGER_DEPT_DEV);
42        scenario.add(new CreateLogger(loggerName));
43      }
44      return scenario;
45    }
46  
47    static public Scenario makeRealisticCreationScenario(int len) {
48      Scenario scenario = new Scenario();
49      LinkedList<String> queue = new LinkedList<String>();
50      int loggerCreationCount = 0;
51  
52      // add an empty string to get going
53      queue.add("");
54  
55      while (loggerCreationCount < len) {
56        if ((count % 100) == 0) {
57          System.out.println("count=" + count);
58        }
59  
60        String loggerName = (String) queue.removeFirst();
61        int randomChildrenCount = ScenarioRandomUtil
62            .randomChildrenCount(loggerName);
63  
64        if (randomChildrenCount == 0) {
65          scenario.add(new CreateLogger(loggerName));
66          addSetLevelSubScenario(scenario, loggerName);
67          loggerCreationCount++;
68        } else {
69          for (int i = 0; i < randomChildrenCount; i++) {
70            String childName;
71            if (loggerName.equals("")) {
72              childName = ScenarioRandomUtil.randomId();
73              count += childName.length();
74            } else {
75              childName = loggerName + ClassicGlobal.LOGGER_SEPARATOR
76                  + ScenarioRandomUtil.randomId();
77              count += childName.length();
78            }
79            queue.add(childName);
80            addSetLevelSubScenario(scenario, loggerName);
81            loggerCreationCount++;
82          }
83        }
84      }
85      return scenario;
86    }
87  
88    static void addSetLevelSubScenario(Scenario scenario, String loggerName) {
89      if (ScenarioRandomUtil.oneInFreq(CREATE_LOGGER_TO_SET_LEVEL_FREQUENCY)) {
90        Level l = ScenarioRandomUtil.randomLevel();
91        scenario.add(new SetLevel(l, loggerName));
92        if (ScenarioRandomUtil.oneInFreq(SECOND_SET_LEVEL_FREQUENCY)) {
93          l = ScenarioRandomUtil.randomLevel();
94          scenario.add(new SetLevel(l, loggerName));
95        }
96      }
97    }
98  
99  }