View Javadoc

1   package cz.cuni.amis.pogamut.sposh.engine;
2   
3   import cz.cuni.amis.pogamut.sposh.elements.LapPath;
4   import cz.cuni.amis.pogamut.sposh.elements.PoshElement;
5   import java.util.Collections;
6   import java.util.LinkedList;
7   import java.util.List;
8   import java.util.logging.Level;
9   import java.util.logging.Logger;
10  
11  /**
12   * The logger of engine activity. It logs which paths were visited, this class
13   * is used by Dash to determine which paths are about to be executed.
14   *
15   * Dash puts Java breakpoint at entry of {@link #pathReachedExit() } and each
16   * time it is reached, it retrieves the reached path and displayes it in the
17   * scene.
18   *
19   * @author Honza
20   */
21  public class EngineLog {
22  
23      private final List<LapPath> paths = new LinkedList<LapPath>();
24      private final List<LapPath> pathsUm = Collections.unmodifiableList(paths);
25      private final Logger log;
26  
27      EngineLog(Logger log) {
28          this.log = log;
29      }
30  
31      /**
32       * Every time some executor of {@link PoshElement} is about to execute it,
33       * it calls this method.
34       *
35       * This method will write to SPOSH {@link Logger} with level {@link Level#FINEST}
36       * that path has been reached.
37       *
38       * @param path Path of element that is about to be executed.
39       */
40      public void pathReached(LapPath path) {
41          finest("Reached path: " + path.toString());
42          paths.add(path);
43          // DO NOT REMOVE, USE BY DASH. For details see javadoc of the method.
44          pathReachedExit();
45      }
46  
47      /**
48       * <em>DO NOT REMOVE!!!</em> This method is here as a workaround of slow
49       * Netbeans breakpoint API. The API is quite fast when adding a breakpoint
50       * of {@link MethodBreakpoint#TYPE_METHOD_ENTRY} type, but <em>very
51       * slow</em> (about 1.2 seconds penalty) when using the
52       * {@link MethodBreakpoint#TYPE_METHOD_EXIT}. As a workaround, I am adding
53       * an {@link MethodBreakpoint#TYPE_METHOD_ENTRY entry type} breakpoint to
54       * this method that is used directly before returning from
55       * {@link #pathReached(cz.cuni.amis.pogamut.sposh.elements.LapPath)
56       * }.
57       *
58       * Dash is adding an entry breakpoint at this method and calls {@link #getLastReachedPath()
59       * } in order to get the firing path.
60       */
61      public void pathReachedExit() {
62          // intentionally left empty
63      }
64  
65      /**
66       * This method is used by the Dash to retrieve the last firing path. Dash
67       * has a breakpoint at entry of method {@link #pathReachedExit() }.
68       *
69       * @return String serialization ({@link LapPath}) of last firing path.
70       */
71      public String getLastReachedPath() {
72          return paths.get(paths.size() - 1).toString();
73      }
74  
75      List<LapPath> getPaths() {
76          return pathsUm;
77      }
78  
79      void clear() {
80          paths.clear();
81      }
82  
83      void finest(String msg) {
84          if (log != null) {
85              log.finest(msg);
86          }
87      }
88  
89      void fine(String msg) {
90          if (log != null) {
91              log.fine(msg);
92          }
93      }
94  
95      void info(String msg) {
96          if (log != null) {
97              log.info(msg);
98          }
99      }
100 
101     void warning(String msg) {
102         if (log != null) {
103             log.warning(msg);
104         }
105     }
106 
107     Logger getLogger() {
108         return this.log;
109     }
110 }