LogEntryImpl.java 4.08 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  ch.qos.logback.classic.Level
 *  ch.qos.logback.classic.spi.ILoggingEvent
 *  ch.qos.logback.classic.spi.IThrowableProxy
 *  ch.qos.logback.classic.spi.StackTraceElementProxy
 *  ch.qos.logback.classic.spi.ThrowableProxyUtil
 */
package com.adobe.granite.logging.impl;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.classic.spi.IThrowableProxy;
import ch.qos.logback.classic.spi.StackTraceElementProxy;
import ch.qos.logback.classic.spi.ThrowableProxyUtil;
import com.adobe.granite.logging.LogEntry;
import com.adobe.granite.logging.LogLevel;

public class LogEntryImpl
implements LogEntry {
    private final LogLevel level;
    private final String exceptionOutput;
    private final String message;
    private final String name;
    private final String threadName;
    private final long timestamp;
    private static final char TAB = '\t';
    private static final String LINE_SEPARATOR = System.getProperty("line.separator");

    public LogEntryImpl(ILoggingEvent event, int maxSize) {
        LogLevel localLevel;
        String exception;
        String l = event.getLevel().toString();
        try {
            localLevel = LogLevel.valueOf(l);
        }
        catch (IllegalArgumentException iae) {
            localLevel = LogLevel.DEBUG;
        }
        this.level = localLevel;
        String msg = event.getFormattedMessage();
        IThrowableProxy tp = event.getThrowableProxy();
        if (tp == null) {
            exception = "";
        } else {
            StringBuilder buf = new StringBuilder(32);
            for (IThrowableProxy currentThrowable = tp; currentThrowable != null; currentThrowable = currentThrowable.getCause()) {
                LogEntryImpl.subjoinThrowableProxy(buf, currentThrowable);
            }
            exception = buf.toString();
        }
        if (maxSize > 0 && msg.length() + exception.length() > maxSize) {
            if (msg.length() > maxSize / 2) {
                msg = msg.substring(0, maxSize / 2).concat("...");
            }
            if (exception.length() > maxSize / 2) {
                exception = exception.substring(0, maxSize / 2).concat("...");
            }
        }
        this.message = msg;
        this.exceptionOutput = exception;
        this.name = event.getLoggerName();
        this.timestamp = event.getTimeStamp();
        this.threadName = event.getThreadName();
    }

    @Override
    public LogLevel getLogLevel() {
        return this.level;
    }

    @Override
    public String getMessage() {
        return this.message;
    }

    @Override
    public String getLoggerName() {
        return this.name;
    }

    @Override
    public long getTimeStamp() {
        return this.timestamp;
    }

    @Override
    public String getThreadName() {
        return this.threadName;
    }

    @Override
    public String getExceptionOutput() {
        return this.exceptionOutput;
    }

    private static void subjoinThrowableProxy(StringBuilder buf, IThrowableProxy tp) {
        ThrowableProxyUtil.subjoinFirstLine((StringBuilder)buf, (IThrowableProxy)tp);
        buf.append(LINE_SEPARATOR);
        StackTraceElementProxy[] stepArray = tp.getStackTraceElementProxyArray();
        int commonFrames = tp.getCommonFrames();
        int maxIndex = stepArray.length;
        if (commonFrames > 0) {
            maxIndex -= commonFrames;
        }
        for (int i = 0; i < maxIndex; ++i) {
            String string = stepArray[i].toString();
            buf.append('\t');
            buf.append(string);
            LogEntryImpl.extraData(buf, stepArray[i]);
            buf.append(LINE_SEPARATOR);
        }
        if (commonFrames > 0) {
            buf.append("\t... ").append(tp.getCommonFrames()).append(" common frames omitted").append(LINE_SEPARATOR);
        }
    }

    private static void extraData(StringBuilder builder, StackTraceElementProxy step) {
        if (step != null) {
            ThrowableProxyUtil.subjoinPackagingData((StringBuilder)builder, (StackTraceElementProxy)step);
        }
    }
}