Skip to content

Commit

Permalink
Log View hierarchy if removeViewAt crashes
Browse files Browse the repository at this point in the history
Summary:
If removeViewAt crashes, log the children of the parent view, and all of the parent's ancestors.

Changelog: [Internal]

Reviewed By: shergin

Differential Revision: D24019515

fbshipit-source-id: c5b1ca0948ebc47f2648e161770affa8542ca5dd
  • Loading branch information
JoshuaGross authored and facebook-github-bot committed Oct 1, 2020
1 parent 553fb8b commit 8b7fd37
Showing 1 changed file with 24 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,34 @@ public MountingManager(@NonNull ViewManagerRegistry viewManagerRegistry) {
mViewManagerRegistry = viewManagerRegistry;
}

private static void logViewHierarchy(ViewGroup parent) {
private static void logViewHierarchy(ViewGroup parent, boolean recurse) {
int parentTag = parent.getId();
FLog.e(TAG, " <ViewGroup tag=" + parentTag + ">");
FLog.e(TAG, " <ViewGroup tag=" + parentTag + " class=" + parent.getClass().toString() + ">");
for (int i = 0; i < parent.getChildCount(); i++) {
FLog.e(
TAG,
" <View idx="
+ i
+ " tag="
+ parent.getChildAt(i).getId()
+ " toString="
+ parent.getChildAt(i).toString()
+ " class="
+ parent.getChildAt(i).getClass().toString()
+ ">");
}
FLog.e(TAG, " </ViewGroup tag=" + parentTag + ">");

if (recurse) {
FLog.e(TAG, "Displaying Ancestors:");
ViewParent ancestor = parent.getParent();
while (ancestor != null) {
ViewGroup ancestorViewGroup = (ancestor instanceof ViewGroup ? (ViewGroup) ancestor : null);
int ancestorId = ancestorViewGroup == null ? View.NO_ID : ancestorViewGroup.getId();
FLog.e(
TAG,
"<ViewParent tag=" + ancestorId + " class=" + ancestor.getClass().toString() + ">");
ancestor = ancestor.getParent();
}
}
}

/**
Expand Down Expand Up @@ -218,7 +231,7 @@ public void addViewAt(final int parentTag, final int tag, final int index) {
// Display children before inserting
if (SHOW_CHANGED_VIEW_HIERARCHIES) {
FLog.e(TAG, "addViewAt: [" + tag + "] -> [" + parentTag + "] idx: " + index + " BEFORE");
logViewHierarchy(parentView);
logViewHierarchy(parentView, false);
}

try {
Expand All @@ -243,7 +256,7 @@ public void addViewAt(final int parentTag, final int tag, final int index) {
public void run() {
FLog.e(
TAG, "addViewAt: [" + tag + "] -> [" + parentTag + "] idx: " + index + " AFTER");
logViewHierarchy(parentView);
logViewHierarchy(parentView, false);
}
});
}
Expand Down Expand Up @@ -359,7 +372,7 @@ public void removeViewAt(final int tag, final int parentTag, final int index) {
if (SHOW_CHANGED_VIEW_HIERARCHIES) {
// Display children before deleting any
FLog.e(TAG, "removeViewAt: [" + tag + "] -> [" + parentTag + "] idx: " + index + " BEFORE");
logViewHierarchy(parentView);
logViewHierarchy(parentView, false);
}

ViewGroupManager<ViewGroup> viewGroupManager = getViewGroupManager(viewState);
Expand Down Expand Up @@ -398,6 +411,7 @@ public void removeViewAt(final int tag, final int parentTag, final int index) {
return;
}

logViewHierarchy(parentView, true);
throw new IllegalStateException(
"Tried to remove view ["
+ tag
Expand Down Expand Up @@ -431,6 +445,8 @@ public void removeViewAt(final int tag, final int parentTag, final int index) {
// enough that we shouldn't try to change this invariant, without a lot of thought.
int childCount = viewGroupManager.getChildCount(parentView);

logViewHierarchy(parentView, true);

throw new IllegalStateException(
"Cannot remove child at index "
+ index
Expand All @@ -451,7 +467,7 @@ public void run() {
FLog.e(
TAG,
"removeViewAt: [" + tag + "] -> [" + parentTag + "] idx: " + index + " AFTER");
logViewHierarchy(parentView);
logViewHierarchy(parentView, false);
}
});
}
Expand Down

0 comments on commit 8b7fd37

Please sign in to comment.