001/*
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2026, 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 v2.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.kryo;
015
016import ch.qos.logback.classic.spi.IThrowableProxy;
017import ch.qos.logback.classic.spi.StackTraceElementProxy;
018import ch.qos.logback.classic.spi.ThrowableProxyVO;
019import ch.qos.logback.core.kryo.SerializerBase;
020import com.esotericsoftware.kryo.kryo5.Kryo;
021import com.esotericsoftware.kryo.kryo5.Serializer;
022import com.esotericsoftware.kryo.kryo5.io.Input;
023import com.esotericsoftware.kryo.kryo5.io.Output;
024import com.esotericsoftware.kryo.kryo5.minlog.Log;
025
026public class ThrowableProxyVOSerializerV5 extends Serializer<ThrowableProxyVO> {
027    private final BasicStringSerializer classNameSerializer;
028    private final BasicStringSerializer messageSerializer;
029    private final Serializer<StackTraceElementProxy> stackTraceElementProxySerializerV5;
030
031    public ThrowableProxyVOSerializerV5(int capacity) {
032        this(capacity, new StackTraceElementProxySerializerV5(capacity));
033    }
034
035    /**
036     * @param stackTraceElementProxySerializer frame codec; V5 passes one whose class names
037     *                                         use {@link BasicStringSerializer}
038     */
039    public ThrowableProxyVOSerializerV5(int capacity, Serializer<StackTraceElementProxy> stackTraceElementProxySerializer) {
040        setAcceptsNull(false);
041        this.classNameSerializer = new BasicStringSerializer(SerializerBase.BiMapType.DIRECT_MAP, capacity);
042        // acceptsNull stays false: Kryo writeObjectOrNull/readObjectOrNull emit NULL/NOT_NULL
043        // flags. StringSerializer must not receive null (it rejects them).
044        this.messageSerializer = new BasicStringSerializer(SerializerBase.BiMapType.DIRECT_MAP, capacity);
045        // Stack frames dominate size and repeat heavily across exceptions
046        this.stackTraceElementProxySerializerV5 = stackTraceElementProxySerializer;
047
048    }
049
050    @Override
051    public void write(Kryo kryo, Output output, ThrowableProxyVO throwableProxyVO) {
052        kryo.writeObject(output, throwableProxyVO.getClassName(), classNameSerializer);
053        kryo.writeObjectOrNull(output, throwableProxyVO.getOverridingMessage(), messageSerializer);
054        kryo.writeObjectOrNull(output, throwableProxyVO.getMessage(), messageSerializer);
055        output.writeVarInt(throwableProxyVO.getCommonFrames(), true);
056
057        writeStackTrace(kryo, output, throwableProxyVO.getStackTraceElementProxyArray());
058
059        IThrowableProxy cause = throwableProxyVO.getCause();
060        if (cause == null) {
061            output.writeBoolean(false);
062        } else {
063            output.writeBoolean(true);
064            kryo.writeObject(output, asThrowableProxyVO(cause), this);
065        }
066
067        IThrowableProxy[] suppressed = throwableProxyVO.getSuppressed();
068        if (suppressed == null) {
069            output.writeVarInt(-1, true);
070        } else {
071            output.writeVarInt(suppressed.length, true);
072            for (IThrowableProxy s : suppressed) {
073                kryo.writeObject(output, asThrowableProxyVO(s), this);
074            }
075        }
076
077        output.writeBoolean(throwableProxyVO.isCyclic());
078
079    }
080
081    @Override
082    public ThrowableProxyVO read(Kryo kryo, Input input, Class<? extends ThrowableProxyVO> type) {
083        Log.trace("readNewEntry for ThrowableProxyVO");
084
085        String className = kryo.readObject(input, String.class, classNameSerializer);
086        String overridingMessage = kryo.readObjectOrNull(input, String.class, messageSerializer);
087        String message = kryo.readObjectOrNull(input, String.class, messageSerializer);
088        int commonFrames = input.readVarInt(true);
089
090        StackTraceElementProxy[] stackTraceElementProxyArray = readStackTrace(kryo, input);
091
092        ThrowableProxyVO cause = null;
093        if (input.readBoolean()) {
094            cause = kryo.readObject(input, ThrowableProxyVO.class, this);
095        }
096
097        int suppressedLength = input.readVarInt(true);
098        IThrowableProxy[] suppressed = null;
099        if (suppressedLength >= 0) {
100            suppressed = new IThrowableProxy[suppressedLength];
101            for (int i = 0; i < suppressedLength; i++) {
102                suppressed[i] = kryo.readObject(input, ThrowableProxyVO.class, this);
103            }
104        }
105
106        boolean cyclic = input.readBoolean();
107
108
109        return ThrowableProxyVO.builder()
110                .setClassName(className)
111                .setOverridingMessage(overridingMessage)
112                .setMessage(message)
113                .setCommonFramesCount(commonFrames)
114                .setStackTraceElementProxyArray(stackTraceElementProxyArray)
115                .setCause(cause)
116                .setSuppressed(suppressed)
117                .setCyclic(cyclic)
118                .build();
119
120    }
121
122    private void writeStackTrace(Kryo kryo, Output output, StackTraceElementProxy[] steps) {
123        if (steps == null) {
124            output.writeVarInt(-1, true);
125            return;
126        }
127        output.writeVarInt(steps.length, true);
128        for (StackTraceElementProxy step : steps) {
129            kryo.writeObject(output, step, stackTraceElementProxySerializerV5);
130        }
131    }
132
133    private StackTraceElementProxy[] readStackTrace(Kryo kryo, Input input) {
134        int length = input.readVarInt(true);
135        if (length < 0) {
136            return null;
137        }
138        StackTraceElementProxy[] steps = new StackTraceElementProxy[length];
139        for (int i = 0; i < length; i++) {
140            steps[i] = kryo.readObject(input, StackTraceElementProxy.class, stackTraceElementProxySerializerV5);
141        }
142        return steps;
143    }
144
145    private static ThrowableProxyVO asThrowableProxyVO(IThrowableProxy proxy) {
146        if (proxy instanceof ThrowableProxyVO) {
147            return (ThrowableProxyVO) proxy;
148        }
149        return ThrowableProxyVO.build(proxy);
150    }
151}