View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.core.status;
15  
16  import ch.qos.logback.core.spi.ContextAwareBase;
17  import ch.qos.logback.core.spi.LifeCycle;
18  import ch.qos.logback.core.util.StatusPrinter;
19  
20  import java.io.PrintStream;
21  import java.util.List;
22  
23  /**
24   *  Print all new incoming status messages on the on the designated PrintStream.
25   * @author Ceki Gülcü
26   */
27  abstract public class OnPrintStreamStatusListenerBase extends ContextAwareBase implements StatusListener, LifeCycle {
28  
29      boolean isStarted = false;
30  
31      static final long DEFAULT_RETROSPECTIVE = 300;
32      long retrospectiveThresold = DEFAULT_RETROSPECTIVE;
33      
34      /**
35       * The prefix to place before each status message
36       * @since 1.1.10
37       */
38      String prefix;
39      
40      /**
41       * The PrintStream used by derived classes
42       * @return
43       */
44      abstract protected PrintStream getPrintStream();
45  
46      private void print(Status status) {
47          StringBuilder sb = new StringBuilder();
48          if(prefix != null)
49              sb.append(prefix);
50          
51          StatusPrinter.buildStr(sb, "", status);
52          getPrintStream().print(sb);
53      }
54  
55      public void addStatusEvent(Status status) {
56          if (!isStarted)
57              return;
58          print(status);
59      }
60  
61      /**
62       * Print status messages retrospectively
63       */
64      private void retrospectivePrint() {
65          if (context == null)
66              return;
67          long now = System.currentTimeMillis();
68          StatusManager sm = context.getStatusManager();
69          List<Status> statusList = sm.getCopyOfStatusList();
70          for (Status status : statusList) {
71              long timestampOfStatusMesage = status.getDate();
72              if (isElapsedTimeLongerThanThreshold(now, timestampOfStatusMesage)) {
73                  print(status);
74              }
75          }
76      }
77  
78      private boolean isElapsedTimeLongerThanThreshold(long now, long timestamp) {
79          long elapsedTime = now - timestamp;
80          return elapsedTime < retrospectiveThresold;
81      }
82  
83      /**
84       * Invoking the start method can cause the instance to print status messages created less than 
85       * value of retrospectiveThresold. 
86       */
87      public void start() {
88          isStarted = true;
89          if (retrospectiveThresold > 0) {
90              retrospectivePrint();
91          }
92      }
93  
94      public String getPrefix() {
95          return prefix;
96      }
97  
98      public void setPrefix(String prefix) {
99          this.prefix = prefix;
100     }
101     
102     public void setRetrospective(long retrospective) {
103         this.retrospectiveThresold = retrospective;
104     }
105 
106     public long getRetrospective() {
107         return retrospectiveThresold;
108     }
109 
110     public void stop() {
111         isStarted = false;
112     }
113 
114     public boolean isStarted() {
115         return isStarted;
116     }
117 
118 }