View Javadoc
1   /*
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2026, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v2.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.classic.kryo;
15  
16  import ch.qos.logback.core.kryo.SerializerBase;
17  import com.esotericsoftware.kryo.kryo5.Kryo;
18  import com.esotericsoftware.kryo.kryo5.io.Input;
19  import com.esotericsoftware.kryo.kryo5.io.Output;
20  
21  /**
22   * Caller-data frames. The same {@link StackTraceElement} recurs across events,
23   * so the whole frame is cached by equality. A repeat is a 2-byte id. The first
24   * time a frame is seen, class, method, and file names go through their own
25   * caches, and the line number is a signed varint.
26   *
27   * <p>This replaced the raw six-string layout. Streams written by that layout
28   * no longer decode.
29   */
30  public class StackTraceElementSerializer extends SerializerBase<StackTraceElement> {
31  
32      private static final int DEFAULT_CAPACITY = 1024;
33  
34      private final BasicStringSerializer moduleNameSerializer = new BasicStringSerializer(DEFAULT_CAPACITY);
35      private final BasicStringSerializer moduleVersionSerializer = new BasicStringSerializer(DEFAULT_CAPACITY);
36      private final SerializerBase<String> classNameSerializer;
37      private final BasicStringSerializer methodNameSerializer;
38      private final BasicStringSerializer fileNameSerializer;
39  
40      public StackTraceElementSerializer() {
41          this(DEFAULT_CAPACITY);
42      }
43  
44      /**
45       * Class names use {@link ClassNameSerializer} (Vanilla streams).
46       */
47      public StackTraceElementSerializer(int capacity) {
48          this(capacity, new ClassNameSerializer(capacity));
49      }
50  
51      /**
52       * @param classNameSerializer class-name codec; V5 passes a {@link BasicStringSerializer}
53       */
54      public StackTraceElementSerializer(int capacity, SerializerBase<String> classNameSerializer) {
55          super(BiMapType.DIRECT_MAP, capacity);
56          setAcceptsNull(false);
57          this.classNameSerializer = classNameSerializer;
58          this.methodNameSerializer = new BasicStringSerializer(capacity);
59          // acceptsNull stays false: Kryo writeObjectOrNull handles a null file name.
60          this.fileNameSerializer = new BasicStringSerializer(capacity);
61      }
62  
63      @Override
64      protected void writeNewEntry(Kryo kryo, Output output, StackTraceElement ste) {
65          // Rarely set. writeString accepts null. Module name and version are
66          // usually null outside the module path; writeObject would throw.
67          output.writeString(ste.getClassLoaderName());
68          kryo.writeObjectOrNull(output, ste.getModuleName(), moduleNameSerializer);
69          kryo.writeObjectOrNull(output, ste.getModuleVersion(), moduleVersionSerializer);
70  
71          kryo.writeObject(output, ste.getClassName(), classNameSerializer);
72          kryo.writeObject(output, ste.getMethodName(), methodNameSerializer);
73          kryo.writeObjectOrNull(output, ste.getFileName(), fileNameSerializer);
74  
75          // Line numbers can be negative (-1 unknown, -2 native).
76          output.writeVarInt(ste.getLineNumber(), false);
77      }
78  
79      @Override
80      protected StackTraceElement readNewEntry(Kryo kryo, Input input) {
81          String classLoaderName = input.readString();
82          String moduleName = kryo.readObjectOrNull(input, String.class, moduleNameSerializer);
83          String moduleVersion = kryo.readObjectOrNull(input, String.class, moduleVersionSerializer);
84          String className = kryo.readObject(input, String.class, classNameSerializer);
85          String methodName = kryo.readObject(input, String.class, methodNameSerializer);
86          String fileName = kryo.readObjectOrNull(input, String.class, fileNameSerializer);
87          int lineNumber = input.readVarInt(false);
88          return new StackTraceElement(classLoaderName, moduleName, moduleVersion,
89                  className, methodName, fileName, lineNumber);
90      }
91  }