|
| 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.util |
| 9 | + |
| 10 | +import android.util.Log |
| 11 | +import com.facebook.common.logging.FLog |
| 12 | +import com.facebook.react.bridge.ReactContext |
| 13 | +import com.facebook.react.common.ReactConstants |
| 14 | + |
| 15 | +/** Logging wrapper for FLog with LogBox support. */ |
| 16 | +object RNLog { |
| 17 | + |
| 18 | + const val MINIMUM_LEVEL_FOR_UI = Log.WARN |
| 19 | + const val LOG = Log.VERBOSE |
| 20 | + const val TRACE = Log.DEBUG |
| 21 | + const val ADVICE = Log.INFO |
| 22 | + const val WARN = Log.WARN |
| 23 | + const val ERROR = Log.ERROR |
| 24 | + |
| 25 | + /** |
| 26 | + * Log a log level message tagged as React Native to the console. |
| 27 | + * |
| 28 | + * @param message The message to log. |
| 29 | + */ |
| 30 | + @JvmStatic |
| 31 | + fun l(message: String) { |
| 32 | + FLog.i(ReactConstants.TAG, message) |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Log a trace level message tagged as React Native to the console. |
| 37 | + * |
| 38 | + * @param message The message to log. |
| 39 | + */ |
| 40 | + @JvmStatic |
| 41 | + fun t(message: String) { |
| 42 | + FLog.i(ReactConstants.TAG, message) |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Log a warning level message tagged as React Native to the console. This warning will not be |
| 47 | + * shown in LogBox. |
| 48 | + * |
| 49 | + * @param message The message to log. |
| 50 | + */ |
| 51 | + @JvmStatic |
| 52 | + fun a(message: String) { |
| 53 | + FLog.w(ReactConstants.TAG, "(ADVICE)$message") |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Log a warning level message tagged as React Native to the console and display in the app. |
| 58 | + * |
| 59 | + * @param context The React context of the application use to display the warning. |
| 60 | + * @param message The message to log. |
| 61 | + */ |
| 62 | + @JvmStatic |
| 63 | + fun w(context: ReactContext?, message: String) { |
| 64 | + logInternal(context, message, WARN) |
| 65 | + FLog.w(ReactConstants.TAG, message) |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Log an error level message tagged as React Native to the console and display in the app. |
| 70 | + * |
| 71 | + * @param context The React context of the application use to display the error. |
| 72 | + * @param message The message to log. |
| 73 | + */ |
| 74 | + @JvmStatic |
| 75 | + fun e(context: ReactContext?, message: String) { |
| 76 | + logInternal(context, message, ERROR) |
| 77 | + FLog.e(ReactConstants.TAG, message) |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Log an error level message tagged as React Native to the console. This error will not be shown |
| 82 | + * in LogBox. |
| 83 | + * |
| 84 | + * @param message The message to log. |
| 85 | + */ |
| 86 | + @JvmStatic |
| 87 | + fun e(message: String) { |
| 88 | + FLog.e(ReactConstants.TAG, message) |
| 89 | + } |
| 90 | + |
| 91 | + private fun logInternal(context: ReactContext?, message: String?, level: Int) { |
| 92 | + if (level >= MINIMUM_LEVEL_FOR_UI) { |
| 93 | + if (context?.hasActiveReactInstance() == true && message != null) { |
| 94 | + context.getJSModule(RCTLog::class.java).logIfNoNativeHook(levelToString(level), message) |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + private fun levelToString(level: Int): String = |
| 100 | + when (level) { |
| 101 | + LOG, |
| 102 | + TRACE -> "log" |
| 103 | + ADVICE, |
| 104 | + WARN -> "warn" |
| 105 | + ERROR -> "error" |
| 106 | + else -> "none" |
| 107 | + } |
| 108 | +} |
0 commit comments