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.html;
015
016import ch.qos.logback.classic.spi.ILoggingEvent;
017import ch.qos.logback.classic.spi.IThrowableProxy;
018import ch.qos.logback.classic.spi.StackTraceElementProxy;
019import ch.qos.logback.core.CoreConstants;
020import ch.qos.logback.core.helpers.Transform;
021import ch.qos.logback.core.html.IThrowableRenderer;
022
023public class DefaultThrowableRenderer implements IThrowableRenderer<ILoggingEvent> {
024
025    static final String TRACE_PREFIX = "<br />&nbsp;&nbsp;&nbsp;&nbsp;";
026
027    public void render(StringBuilder sbuf, ILoggingEvent event) {
028        IThrowableProxy tp = event.getThrowableProxy();
029        sbuf.append("<tr><td class=\"Exception\" colspan=\"6\">");
030        while (tp != null) {
031            render(sbuf, tp);
032            tp = tp.getCause();
033        }
034        sbuf.append("</td></tr>");
035    }
036
037    void render(StringBuilder sbuf, IThrowableProxy tp) {
038        printFirstLine(sbuf, tp);
039
040        int commonFrames = tp.getCommonFrames();
041        StackTraceElementProxy[] stepArray = tp.getStackTraceElementProxyArray();
042
043        for (int i = 0; i < stepArray.length - commonFrames; i++) {
044            StackTraceElementProxy step = stepArray[i];
045            sbuf.append(TRACE_PREFIX);
046            sbuf.append(Transform.escapeTags(step.toString()));
047            sbuf.append(CoreConstants.LINE_SEPARATOR);
048        }
049
050        if (commonFrames > 0) {
051            sbuf.append(TRACE_PREFIX);
052            sbuf.append("\t... ").append(commonFrames).append(" common frames omitted")
053                    .append(CoreConstants.LINE_SEPARATOR);
054        }
055    }
056
057    public void printFirstLine(StringBuilder sb, IThrowableProxy tp) {
058        int commonFrames = tp.getCommonFrames();
059        if (commonFrames > 0) {
060            sb.append("<br />").append(CoreConstants.CAUSED_BY);
061        }
062        sb.append(tp.getClassName()).append(": ").append(Transform.escapeTags(tp.getMessage()));
063        sb.append(CoreConstants.LINE_SEPARATOR);
064    }
065
066}