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.core.kryo.SerializerBase; 017import com.esotericsoftware.kryo.kryo5.Kryo; 018import com.esotericsoftware.kryo.kryo5.io.Input; 019import com.esotericsoftware.kryo.kryo5.io.Output; 020 021/** 022 * Caller-data frames. The same {@link StackTraceElement} recurs across events, 023 * so the whole frame is cached by equality. A repeat is a 2-byte id. The first 024 * time a frame is seen, class, method, and file names go through their own 025 * caches, and the line number is a signed varint. 026 * 027 * <p>This replaced the raw six-string layout. Streams written by that layout 028 * no longer decode. 029 */ 030public class StackTraceElementSerializer extends SerializerBase<StackTraceElement> { 031 032 private static final int DEFAULT_CAPACITY = 1024; 033 034 private final BasicStringSerializer moduleNameSerializer = new BasicStringSerializer(DEFAULT_CAPACITY); 035 private final BasicStringSerializer moduleVersionSerializer = new BasicStringSerializer(DEFAULT_CAPACITY); 036 private final SerializerBase<String> classNameSerializer; 037 private final BasicStringSerializer methodNameSerializer; 038 private final BasicStringSerializer fileNameSerializer; 039 040 public StackTraceElementSerializer() { 041 this(DEFAULT_CAPACITY); 042 } 043 044 /** 045 * Class names use {@link ClassNameSerializer} (Vanilla streams). 046 */ 047 public StackTraceElementSerializer(int capacity) { 048 this(capacity, new ClassNameSerializer(capacity)); 049 } 050 051 /** 052 * @param classNameSerializer class-name codec; V5 passes a {@link BasicStringSerializer} 053 */ 054 public StackTraceElementSerializer(int capacity, SerializerBase<String> classNameSerializer) { 055 super(BiMapType.DIRECT_MAP, capacity); 056 setAcceptsNull(false); 057 this.classNameSerializer = classNameSerializer; 058 this.methodNameSerializer = new BasicStringSerializer(capacity); 059 // acceptsNull stays false: Kryo writeObjectOrNull handles a null file name. 060 this.fileNameSerializer = new BasicStringSerializer(capacity); 061 } 062 063 @Override 064 protected void writeNewEntry(Kryo kryo, Output output, StackTraceElement ste) { 065 // Rarely set. writeString accepts null. Module name and version are 066 // usually null outside the module path; writeObject would throw. 067 output.writeString(ste.getClassLoaderName()); 068 kryo.writeObjectOrNull(output, ste.getModuleName(), moduleNameSerializer); 069 kryo.writeObjectOrNull(output, ste.getModuleVersion(), moduleVersionSerializer); 070 071 kryo.writeObject(output, ste.getClassName(), classNameSerializer); 072 kryo.writeObject(output, ste.getMethodName(), methodNameSerializer); 073 kryo.writeObjectOrNull(output, ste.getFileName(), fileNameSerializer); 074 075 // Line numbers can be negative (-1 unknown, -2 native). 076 output.writeVarInt(ste.getLineNumber(), false); 077 } 078 079 @Override 080 protected StackTraceElement readNewEntry(Kryo kryo, Input input) { 081 String classLoaderName = input.readString(); 082 String moduleName = kryo.readObjectOrNull(input, String.class, moduleNameSerializer); 083 String moduleVersion = kryo.readObjectOrNull(input, String.class, moduleVersionSerializer); 084 String className = kryo.readObject(input, String.class, classNameSerializer); 085 String methodName = kryo.readObject(input, String.class, methodNameSerializer); 086 String fileName = kryo.readObjectOrNull(input, String.class, fileNameSerializer); 087 int lineNumber = input.readVarInt(false); 088 return new StackTraceElement(classLoaderName, moduleName, moduleVersion, 089 className, methodName, fileName, lineNumber); 090 } 091}