Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions java/fory-core/src/main/java/org/apache/fory/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ public class Config implements Serializable {
private final boolean deserializeNonexistentEnumValueAsNull;
private final boolean serializeEnumByName;
private final int bufferSizeLimitBytes;
private final ExceptionLogMode exceptionLogMode;
private final int logSampleStep;

public Config(ForyBuilder builder) {
name = builder.name;
Expand Down Expand Up @@ -99,6 +101,16 @@ public Config(ForyBuilder builder) {
deserializeNonexistentEnumValueAsNull = builder.deserializeNonexistentEnumValueAsNull;
serializeEnumByName = builder.serializeEnumByName;
bufferSizeLimitBytes = builder.bufferSizeLimitBytes;
exceptionLogMode = builder.exceptionLogMode;
logSampleStep = builder.logSampleStep;
}

public ExceptionLogMode getExceptionLogMode() {
return exceptionLogMode;
}

public int getLogSampleStep() {
return logSampleStep;
}

/** Returns the name for Fory serialization. */
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.apache.fory.config;

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

public enum ExceptionLogMode {
ALL_PRINT,
SAMPLE_PRINT,
NONE_PRINT
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ public final class ForyBuilder {
boolean serializeEnumByName = false;
int bufferSizeLimitBytes = 128 * 1024;
MetaCompressor metaCompressor = new DeflaterMetaCompressor();
ExceptionLogMode exceptionLogMode = ExceptionLogMode.ALL_PRINT;
int logSampleStep;

public ForyBuilder() {}

Expand Down Expand Up @@ -145,6 +147,18 @@ public ForyBuilder serializeEnumByName(boolean serializeEnumByName) {
return this;
}

/** exception log level choose the log level, print the error message. */
public ForyBuilder withExceptionLogMode(ExceptionLogMode exceptionLogMode) {
this.exceptionLogMode = exceptionLogMode;
return this;
}

public ForyBuilder withExceptionLogMode(ExceptionLogMode exceptionLogMode, int logSampleStep) {
this.exceptionLogMode = exceptionLogMode;
this.logSampleStep = logSampleStep;
return this;
}

/**
* Whether ignore reference tracking of all time types registered in {@link TimeSerializers} when
* ref tracking is enabled.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.apache.fory.util;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.fory.Fory;
Expand Down Expand Up @@ -57,15 +58,35 @@ public static StackOverflowError trySetStackOverflowErrorMessage(

public static RuntimeException handleReadFailed(Fory fory, Throwable t) {
if (fory.getRefResolver() instanceof MapRefResolver) {
ObjectArray readObjects = ((MapRefResolver) fory.getRefResolver()).getReadObjects();
// carry with read objects for better trouble shooting.
List<Object> objects = Arrays.asList(readObjects.objects).subList(0, readObjects.size);
throw new DeserializationException(objects, t);
List<Object> exceptionObjects = getExceptionObjects(fory);
throw new DeserializationException(exceptionObjects, t);
} else {
Platform.throwException(t);
throw new IllegalStateException("unreachable");
}
}

public static List<Object> getExceptionObjects(Fory fory) {
ObjectArray readObjects = ((MapRefResolver) fory.getRefResolver()).getReadObjects();
// carry with read objects for better trouble shooting.
List<Object> objects = Arrays.asList(readObjects.objects).subList(0, readObjects.size);
switch (fory.getConfig().getExceptionLogMode()) {
case NONE_PRINT:
return new ArrayList<>();
case SAMPLE_PRINT:
return systematicSample(objects, Math.max(1, fory.getConfig().getLogSampleStep()));
default:
return objects;
}
}

public static <T> List<T> systematicSample(List<T> list, int step) {
List<T> result = new ArrayList<>();
for (int i = 0; i < list.size(); i += step) {
result.add(list.get(i));
}
return result;
}

public static void ignore(Object... args) {}
}
Loading