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.StackTraceElementProxy; 017import ch.qos.logback.core.kryo.SerializerBase; 018import com.esotericsoftware.kryo.kryo5.Kryo; 019import com.esotericsoftware.kryo.kryo5.io.Input; 020import com.esotericsoftware.kryo.kryo5.io.Output; 021import com.esotericsoftware.kryo.kryo5.minlog.Log; 022 023/** 024 * Caching serializer for {@link StackTraceElementProxy}. 025 * 026 * <p>Stack frames are the main source of redundancy in throwable payloads: the same frames 027 * reappear across exceptions and log events. This serializer: 028 * <ul> 029 * <li>Caches whole frames via {@link BiMapType#DIRECT_MAP} (frame equality is well-defined)</li> 030 * <li>Compresses class names with {@link ClassNameSerializer} by default (V5 passes a 031 * {@link BasicStringSerializer})</li> 032 * <li>Caches method and file names with {@link SingleItemCompressingStringSerializer}</li> 033 * </ul> 034 */ 035public class StackTraceElementProxySerializerV5 extends SerializerBase<StackTraceElementProxy> { 036 037 private static final int DEFAULT_CAPACITY = 4096; 038 039 private final SerializerBase<String> classNameSerializer; 040 private final SingleItemCompressingStringSerializer methodNameSerializer; 041 private final SingleItemCompressingStringSerializer fileNameSerializer; 042 043 public StackTraceElementProxySerializerV5() { 044 this(DEFAULT_CAPACITY); 045 } 046 047 /** 048 * Class names use {@link ClassNameSerializer}. 049 */ 050 public StackTraceElementProxySerializerV5(int capacity) { 051 this(capacity, new ClassNameSerializer(capacity)); 052 } 053 054 /** 055 * @param classNameSerializer class-name codec; V5 passes a {@link BasicStringSerializer} 056 */ 057 public StackTraceElementProxySerializerV5(int capacity, SerializerBase<String> classNameSerializer) { 058 super(BiMapType.DIRECT_MAP, capacity); 059 setAcceptsNull(false); 060 this.classNameSerializer = classNameSerializer; 061 this.methodNameSerializer = new SingleItemCompressingStringSerializer(capacity); 062 // acceptsNull stays false: Kryo writeObjectOrNull handles null file names via 063 // NULL/NOT_NULL flags. StringSerializer rejects null values. 064 this.fileNameSerializer = new SingleItemCompressingStringSerializer(capacity); 065 } 066 067 @Override 068 protected void writeNewEntry(Kryo kryo, Output output, StackTraceElementProxy step) { 069 StackTraceElement ste = step.getStackTraceElement(); 070 Log.trace("writeNewEntry for StackTraceElementProxy: " + ste); 071 072 // Rarely set; store raw (null-friendly) strings 073 output.writeString(ste.getClassLoaderName()); 074 output.writeString(ste.getModuleName()); 075 output.writeString(ste.getModuleVersion()); 076 077 // High-redundancy string fields 078 kryo.writeObject(output, ste.getClassName(), classNameSerializer); 079 kryo.writeObject(output, ste.getMethodName(), methodNameSerializer); 080 kryo.writeObjectOrNull(output, ste.getFileName(), fileNameSerializer); 081 082 // Line numbers can be negative (-1 unknown, -2 native) 083 output.writeVarInt(ste.getLineNumber(), false); 084 } 085 086 @Override 087 protected StackTraceElementProxy readNewEntry(Kryo kryo, Input input) { 088 String classLoaderName = input.readString(); 089 String moduleName = input.readString(); 090 String moduleVersion = input.readString(); 091 String className = kryo.readObject(input, String.class, classNameSerializer); 092 String methodName = kryo.readObject(input, String.class, methodNameSerializer); 093 String fileName = kryo.readObjectOrNull(input, String.class, fileNameSerializer); 094 int lineNumber = input.readVarInt(false); 095 096 StackTraceElement ste = new StackTraceElement(classLoaderName, moduleName, moduleVersion, 097 className, methodName, fileName, lineNumber); 098 Log.trace("readNewEntry for StackTraceElementProxy: " + ste); 099 return new StackTraceElementProxy(ste); 100 } 101}