View Javadoc

1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2011, 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.classic.pattern;
15  
16  import ch.qos.logback.classic.spi.IThrowableProxy;
17  import ch.qos.logback.classic.spi.StackTraceElementProxy;
18  import ch.qos.logback.classic.spi.ThrowableProxyUtil;
19  import ch.qos.logback.core.CoreConstants;
20  
21  /**
22   * @author Tomasz Nurkiewicz
23   * @since 0.9.30
24   */
25  public class RootCauseFirstThrowableProxyConverter extends ExtendedThrowableProxyConverter {
26  
27    @Override
28    protected String throwableProxyToString(IThrowableProxy tp) {
29      StringBuilder buf = new StringBuilder(2048);
30      subjoinRootCauseFirst(tp, buf);
31      return buf.toString();
32    }
33  
34    private void subjoinRootCauseFirst(IThrowableProxy tp, StringBuilder buf) {
35      if (tp.getCause() != null)
36        subjoinRootCauseFirst(tp.getCause(), buf);
37      subjoinRootCause(tp, buf);
38    }
39  
40    private void subjoinRootCause(IThrowableProxy tp, StringBuilder buf) {
41      ThrowableProxyUtil.subjoinFirstLineRootCauseFirst(buf, tp);
42      buf.append(CoreConstants.LINE_SEPARATOR);
43      StackTraceElementProxy[] stepArray = tp.getStackTraceElementProxyArray();
44      int commonFrames = tp.getCommonFrames();
45  
46      boolean unrestrictedPrinting = lengthOption > stepArray.length;
47  
48  
49      int maxIndex = (unrestrictedPrinting) ? stepArray.length : lengthOption;
50      if (commonFrames > 0 && unrestrictedPrinting) {
51        maxIndex -= commonFrames;
52      }
53  
54      for (int i = 0; i < maxIndex; i++) {
55        String string = stepArray[i].toString();
56        buf.append(CoreConstants.TAB);
57        buf.append(string);
58        extraData(buf, stepArray[i]); // allow other data to be added
59        buf.append(CoreConstants.LINE_SEPARATOR);
60      }
61  
62    }
63  
64  
65  }