|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +package com.facebook.react.bridge |
| 9 | + |
| 10 | +import com.facebook.common.logging.FLog |
| 11 | +import com.facebook.proguard.annotations.DoNotStrip |
| 12 | +import java.util.concurrent.CopyOnWriteArrayList |
| 13 | +import kotlin.jvm.JvmStatic |
| 14 | + |
| 15 | +@DoNotStrip |
| 16 | +public object ReactSoftExceptionLogger { |
| 17 | + |
| 18 | + // Use a list instead of a set here because we expect the number of listeners |
| 19 | + // to be very small, and we want listeners to be called in a deterministic |
| 20 | + // order. |
| 21 | + private val listeners: MutableList<ReactSoftExceptionListener> = CopyOnWriteArrayList() |
| 22 | + |
| 23 | + @JvmStatic |
| 24 | + public fun addListener(listener: ReactSoftExceptionListener): Unit { |
| 25 | + if (!listeners.contains(listener)) { |
| 26 | + listeners.add(listener) |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + @JvmStatic |
| 31 | + public fun removeListener(listener: ReactSoftExceptionListener): Unit { |
| 32 | + listeners.remove(listener) |
| 33 | + } |
| 34 | + |
| 35 | + @JvmStatic |
| 36 | + public fun clearListeners(): Unit { |
| 37 | + listeners.clear() |
| 38 | + } |
| 39 | + |
| 40 | + @JvmStatic |
| 41 | + public fun logSoftExceptionVerbose(category: String, cause: Throwable): Unit { |
| 42 | + logSoftException("${category}|${cause.javaClass.simpleName}:${cause.message}", cause) |
| 43 | + } |
| 44 | + |
| 45 | + @JvmStatic |
| 46 | + public fun logSoftException(category: String, cause: Throwable): Unit { |
| 47 | + if (!listeners.isEmpty()) { |
| 48 | + for (listener in listeners) { |
| 49 | + listener.logSoftException(category, cause) |
| 50 | + } |
| 51 | + } else { |
| 52 | + FLog.e(category, "Unhandled SoftException", cause) |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + @JvmStatic |
| 57 | + @DoNotStrip |
| 58 | + private fun logNoThrowSoftExceptionWithMessage(category: String, message: String) { |
| 59 | + logSoftException(category, ReactNoCrashSoftException(message)) |
| 60 | + } |
| 61 | + |
| 62 | + public fun interface ReactSoftExceptionListener { |
| 63 | + public fun logSoftException(category: String, cause: Throwable) |
| 64 | + } |
| 65 | +} |
0 commit comments