|
| 1 | +package io.sentry.util; |
| 2 | + |
| 3 | +import io.sentry.IScopes; |
| 4 | +import io.sentry.Scopes; |
| 5 | +import io.sentry.ScopesAdapter; |
| 6 | +import io.sentry.Sentry; |
| 7 | +import org.jetbrains.annotations.Nullable; |
| 8 | + |
| 9 | +/** |
| 10 | + * A util for printing the scopes chain from passed in scopes all the way up to its initial |
| 11 | + * ancestor. |
| 12 | + * |
| 13 | + * <p>It walks up the parents of each scopes instance until it reaches a parent of null. The scopes |
| 14 | + * without a parent are the ones created in Sentry.init. |
| 15 | + */ |
| 16 | +public final class ScopesUtil { |
| 17 | + |
| 18 | + public static void printScopesChain(final @Nullable IScopes scopes) { |
| 19 | + System.out.println("=========================================="); |
| 20 | + System.out.println("=============== v Scopes v ==============="); |
| 21 | + System.out.println("=========================================="); |
| 22 | + |
| 23 | + printScopesChainInternal(scopes); |
| 24 | + |
| 25 | + System.out.println("=========================================="); |
| 26 | + System.out.println("=============== ^ Scopes ^ ==============="); |
| 27 | + System.out.println("=========================================="); |
| 28 | + } |
| 29 | + |
| 30 | + @SuppressWarnings({"ObjectToString", "deprecation"}) |
| 31 | + private static void printScopesChainInternal(final @Nullable IScopes someScopes) { |
| 32 | + if (someScopes != null) { |
| 33 | + if (someScopes instanceof Scopes) { |
| 34 | + Scopes scopes = (Scopes) someScopes; |
| 35 | + String info = |
| 36 | + String.format( |
| 37 | + "%-25s {g=%-25s, i=%-25s, c=%-25s} [%s]", |
| 38 | + scopes, |
| 39 | + scopes.getGlobalScope(), |
| 40 | + scopes.getIsolationScope(), |
| 41 | + scopes.getScope(), |
| 42 | + scopes.getCreator()); |
| 43 | + System.out.println(info); |
| 44 | + printScopesChainInternal(someScopes.getParentScopes()); |
| 45 | + } else if (someScopes instanceof ScopesAdapter |
| 46 | + || someScopes instanceof io.sentry.HubAdapter) { |
| 47 | + printScopesChainInternal(Sentry.getCurrentScopes()); |
| 48 | + } else if (someScopes instanceof io.sentry.HubScopesWrapper) { |
| 49 | + io.sentry.HubScopesWrapper wrapper = (io.sentry.HubScopesWrapper) someScopes; |
| 50 | + printScopesChainInternal(wrapper.getScopes()); |
| 51 | + } else { |
| 52 | + System.out.println("Hit unhandled Scopes class" + someScopes.getClass()); |
| 53 | + } |
| 54 | + } else { |
| 55 | + System.out.println("-"); |
| 56 | + } |
| 57 | + } |
| 58 | +} |
0 commit comments