Skip to content

Commit

Permalink
Keep the filter-chain consistent when using the LoggingTester
Browse files Browse the repository at this point in the history
Manipulating the next-pointer of the linked list is error-prone
and corrupted the filter chain. It's safer to re-create the filter chain
from scratch by calling addFilter again.

Signed-off-by: Sebastian Zarnekow <[email protected]>
  • Loading branch information
szarnekow committed Jan 8, 2024
1 parent d364c05 commit d0682d7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (c) 2015, 2022 itemis AG (http://www.itemis.eu) and others.
* Copyright (c) 2015, 2024 itemis AG (http://www.itemis.eu) and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
Expand Down Expand Up @@ -333,16 +333,18 @@ private static List<Appender> appenderHierarchy(Logger logger) {
}

private static void removeFilter(Appender appender, Filter filter) {
if (Objects.equal(appender.getFilter(), filter)) {
appender.clearFilters();
appender.addFilter(filter.getNext());
} else {
for (Filter current = appender.getFilter(); (current != null); current = current.getNext()) {
if (Objects.equal(current.getNext(), filter)) {
current.setNext(filter.getNext());
return;
}
List<Filter> filtersToKeep = new ArrayList<>();
Filter present = appender.getFilter();
while (present != null) {
if (!Objects.equal(present, filter)) {
filtersToKeep.add(present);
}
present = present.getNext();
}
appender.clearFilters();
for(Filter addMe: filtersToKeep) {
addMe.setNext(null);
appender.addFilter(addMe);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (c) 2015, 2020 itemis AG (http://www.itemis.eu) and others.
* Copyright (c) 2015, 2024 itemis AG (http://www.itemis.eu) and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
Expand Down Expand Up @@ -69,17 +69,19 @@ private static List<Appender> appenderHierarchy(Logger logger) {
}

private static void removeFilter(Appender appender, Filter filter) {
if (Objects.equals(appender.getFilter(), filter)) {
appender.clearFilters();
appender.addFilter(filter.getNext());
} else {
for (Filter current = appender.getFilter(); current != null; current = current.getNext()) {
if (Objects.equals(current.getNext(), filter)) {
current.setNext(filter.getNext());
return;
}
List<Filter> filtersToKeep = new ArrayList<>();
Filter present = appender.getFilter();
while (present != null) {
if (!Objects.equals(present, filter)) {
filtersToKeep.add(present);
}
present = present.getNext();
}
appender.clearFilters();
filtersToKeep.forEach(addMe -> {
addMe.setNext(null);
appender.addFilter(addMe);
});
}

public static class LogCapture {
Expand Down

0 comments on commit d0682d7

Please sign in to comment.