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 com.esotericsoftware.kryo.kryo5.Kryo; 020import com.esotericsoftware.kryo.kryo5.Serializer; 021import com.esotericsoftware.kryo.kryo5.io.Input; 022import com.esotericsoftware.kryo.kryo5.io.Output; 023 024/** 025 * Serializer for {@link ThrowableProxyVO} used by the vanilla stream format. 026 * 027 * <p>Relies only on the public API of {@link ThrowableProxyVO}, i.e. its getters and 028 * {@link ThrowableProxyVO#builder()}. Kryo's reflection based FieldSerializer would require 029 * module ch.qos.logback.classic to open package ch.qos.logback.classic.spi. 030 */ 031public class VanillaThrowableProxyVOSerializer extends Serializer<ThrowableProxyVO> { 032 033 @Override 034 public void write(Kryo kryo, Output output, ThrowableProxyVO throwableProxyVO) { 035 output.writeString(throwableProxyVO.getClassName()); 036 output.writeString(throwableProxyVO.getOverridingMessage()); 037 output.writeString(throwableProxyVO.getMessage()); 038 output.writeVarInt(throwableProxyVO.getCommonFrames(), true); 039 kryo.writeObjectOrNull(output, throwableProxyVO.getStackTraceElementProxyArray(), StackTraceElementProxy[].class); 040 kryo.writeObjectOrNull(output, asThrowableProxyVO(throwableProxyVO.getCause()), ThrowableProxyVO.class); 041 042 IThrowableProxy[] suppressed = throwableProxyVO.getSuppressed(); 043 if (suppressed == null) { 044 output.writeVarInt(-1, true); 045 } else { 046 output.writeVarInt(suppressed.length, true); 047 for (IThrowableProxy s : suppressed) { 048 kryo.writeObjectOrNull(output, asThrowableProxyVO(s), ThrowableProxyVO.class); 049 } 050 } 051 output.writeBoolean(throwableProxyVO.isCyclic()); 052 } 053 054 @Override 055 public ThrowableProxyVO read(Kryo kryo, Input input, Class<? extends ThrowableProxyVO> type) { 056 String className = input.readString(); 057 String overridingMessage = input.readString(); 058 String message = input.readString(); 059 int commonFrames = input.readVarInt(true); 060 StackTraceElementProxy[] steps = kryo.readObjectOrNull(input, StackTraceElementProxy[].class); 061 ThrowableProxyVO cause = kryo.readObjectOrNull(input, ThrowableProxyVO.class); 062 063 IThrowableProxy[] suppressed = null; 064 int suppressedLength = input.readVarInt(true); 065 if (suppressedLength >= 0) { 066 suppressed = new IThrowableProxy[suppressedLength]; 067 for (int i = 0; i < suppressedLength; i++) { 068 suppressed[i] = kryo.readObjectOrNull(input, ThrowableProxyVO.class); 069 } 070 } 071 boolean cyclic = input.readBoolean(); 072 073 return ThrowableProxyVO.builder() 074 .setClassName(className) 075 .setOverridingMessage(overridingMessage) 076 .setMessage(message) 077 .setCommonFramesCount(commonFrames) 078 .setStackTraceElementProxyArray(steps) 079 .setCause(cause) 080 .setSuppressed(suppressed) 081 .setCyclic(cyclic) 082 .build(); 083 } 084 085 private static ThrowableProxyVO asThrowableProxyVO(IThrowableProxy proxy) { 086 if (proxy == null || proxy instanceof ThrowableProxyVO) { 087 return (ThrowableProxyVO) proxy; 088 } 089 return ThrowableProxyVO.build(proxy); 090 } 091}