-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(android): add prop to control debug log level (#3277)
* feat: add prop to allow controlling of debug log level * fix: move props parsing to safeGetters --------- Co-authored-by: olivier <[email protected]>
- Loading branch information
Showing
7 changed files
with
324 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
android/src/main/java/com/brentvatne/common/toolbox/DebugLog.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package com.brentvatne.common.toolbox | ||
|
||
import android.os.Build | ||
import android.util.Log | ||
import java.lang.Exception | ||
|
||
/* log utils | ||
* This class allow defining a log level for the package | ||
* This is useful for debugging real time issue or tricky use cases | ||
*/ | ||
|
||
object DebugLog { | ||
// log level to display | ||
private var level = Log.WARN | ||
// enable thread display in logs | ||
private var displayThread = true | ||
// add a common prefix for easy filtering | ||
private const val TAG_PREFIX = "RNV" | ||
|
||
@JvmStatic | ||
fun setConfig(_level: Int, _displayThread: Boolean) { | ||
level = _level | ||
displayThread = _displayThread | ||
} | ||
|
||
@JvmStatic | ||
private fun getTag(tag: String): String { | ||
return TAG_PREFIX + tag | ||
} | ||
|
||
@JvmStatic | ||
private fun getMsg(msg: String): String { | ||
return if (displayThread) { | ||
"[" + Thread.currentThread().name + "] " + msg | ||
} else msg | ||
} | ||
|
||
@JvmStatic | ||
fun v(tag: String, msg: String) { | ||
if (level <= Log.VERBOSE) Log.v(getTag(tag), getMsg(msg)) | ||
} | ||
|
||
@JvmStatic | ||
fun d(tag: String, msg: String) { | ||
if (level <= Log.DEBUG) Log.d(getTag(tag), getMsg(msg)) | ||
} | ||
|
||
@JvmStatic | ||
fun i(tag: String, msg: String) { | ||
if (level <= Log.INFO) Log.i(getTag(tag), getMsg(msg)) | ||
} | ||
|
||
@JvmStatic | ||
fun w(tag: String, msg: String) { | ||
if (level <= Log.WARN) Log.w(getTag(tag), getMsg(msg)) | ||
} | ||
|
||
@JvmStatic | ||
fun e(tag: String, msg: String) { | ||
if (level <= Log.ERROR) Log.e(getTag(tag), getMsg(msg)) | ||
} | ||
|
||
@JvmStatic | ||
fun wtf(tag: String, msg: String) { | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) { | ||
Log.wtf(getTag(tag), "--------------->" + getMsg(msg)) | ||
} else { | ||
Log.e(getTag(tag), "--------------->" + getMsg(msg)) | ||
} | ||
printCallStack() | ||
} | ||
|
||
@JvmStatic | ||
fun printCallStack() { | ||
if (level <= Log.VERBOSE) { | ||
val e = Exception() | ||
e.printStackTrace() | ||
} | ||
} | ||
|
||
// Additionnal thread safety checkers | ||
@JvmStatic | ||
fun checkUIThread(tag: String, msg: String) { | ||
if (Thread.currentThread().name != "main") { | ||
wtf(tag, "------------------------>" + getMsg(msg)) | ||
} | ||
} | ||
|
||
@JvmStatic | ||
fun checkNotUIThread(tag: String, msg: String) { | ||
if (Thread.currentThread().name == "main") { | ||
wtf(tag, "------------------------>" + getMsg(msg)) | ||
} | ||
} | ||
} |
142 changes: 142 additions & 0 deletions
142
android/src/main/java/com/brentvatne/common/toolbox/ReactBridgeUtils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
package com.brentvatne.common.toolbox | ||
|
||
import com.facebook.react.bridge.Dynamic | ||
import com.facebook.react.bridge.ReadableMap | ||
import com.facebook.react.bridge.ReadableArray | ||
import java.util.HashMap | ||
|
||
/* | ||
* Toolbox to safe parsing of <Video props | ||
* These are just safe accessors to ReadableMap | ||
*/ | ||
|
||
object ReactBridgeUtils { | ||
@JvmStatic | ||
fun safeGetString(map: ReadableMap?, key: String?, fallback: String?): String? { | ||
return if (map != null && map.hasKey(key!!) && !map.isNull(key)) map.getString(key) else fallback | ||
} | ||
|
||
@JvmStatic | ||
fun safeGetString(map: ReadableMap?, key: String?): String? { | ||
return safeGetString(map, key, null) | ||
} | ||
|
||
@JvmStatic | ||
fun safeGetDynamic(map: ReadableMap?, key: String?, fallback: Dynamic?): Dynamic? { | ||
return if (map != null && map.hasKey(key!!) && !map.isNull(key)) map.getDynamic(key) else fallback | ||
} | ||
|
||
@JvmStatic | ||
fun safeGetDynamic(map: ReadableMap?, key: String?): Dynamic? { | ||
return safeGetDynamic(map, key, null) | ||
} | ||
|
||
@JvmStatic | ||
fun safeGetBool(map: ReadableMap?, key: String?, fallback: Boolean): Boolean { | ||
return if (map != null && map.hasKey(key!!) && !map.isNull(key)) map.getBoolean(key) else fallback | ||
} | ||
|
||
@JvmStatic | ||
fun safeGetMap(map: ReadableMap?, key: String?): ReadableMap? { | ||
return if (map != null && map.hasKey(key!!) && !map.isNull(key)) map.getMap(key) else null | ||
} | ||
|
||
@JvmStatic | ||
fun safeGetArray(map: ReadableMap?, key: String?): ReadableArray? { | ||
return if (map != null && map.hasKey(key!!) && !map.isNull(key)) map.getArray(key) else null | ||
} | ||
|
||
@JvmStatic | ||
fun safeGetInt(map: ReadableMap?, key: String?, fallback: Int): Int { | ||
return if (map != null && map.hasKey(key!!) && !map.isNull(key)) map.getInt(key) else fallback | ||
} | ||
|
||
@JvmStatic | ||
fun safeGetInt(map: ReadableMap?, key: String?): Int { | ||
return safeGetInt(map, key, 0); | ||
} | ||
|
||
@JvmStatic | ||
fun safeGetDouble(map: ReadableMap?, key: String?, fallback: Double): Double { | ||
return if (map != null && map.hasKey(key!!) && !map.isNull(key)) map.getDouble(key) else fallback | ||
} | ||
@JvmStatic | ||
fun safeGetDouble(map: ReadableMap?, key: String?): Double { | ||
return safeGetDouble(map, key, 0.0); | ||
} | ||
/** | ||
* toStringMap converts a [ReadableMap] into a HashMap. | ||
* | ||
* @param readableMap The ReadableMap to be conveted. | ||
* @return A HashMap containing the data that was in the ReadableMap. | ||
* @see 'Adapted from https://github.com/artemyarulin/react-native-eval/blob/master/android/src/main/java/com/evaluator/react/ConversionUtil.java' | ||
*/ | ||
@JvmStatic | ||
fun toStringMap(readableMap: ReadableMap?): Map<String, String?>? { | ||
if (readableMap == null) return null | ||
val iterator = readableMap.keySetIterator() | ||
if (!iterator.hasNextKey()) return null | ||
val result: MutableMap<String, String?> = HashMap() | ||
while (iterator.hasNextKey()) { | ||
val key = iterator.nextKey() | ||
result[key] = readableMap.getString(key) | ||
} | ||
return result | ||
} | ||
|
||
/** | ||
* toIntMap converts a [ReadableMap] into a HashMap. | ||
* | ||
* @param readableMap The ReadableMap to be conveted. | ||
* @return A HashMap containing the data that was in the ReadableMap. | ||
* @see 'Adapted from https://github.com/artemyarulin/react-native-eval/blob/master/android/src/main/java/com/evaluator/react/ConversionUtil.java' | ||
*/ | ||
@JvmStatic | ||
fun toIntMap(readableMap: ReadableMap?): Map<String, Int>? { | ||
if (readableMap == null) return null | ||
val iterator = readableMap.keySetIterator() | ||
if (!iterator.hasNextKey()) return null | ||
val result: MutableMap<String, Int> = HashMap() | ||
while (iterator.hasNextKey()) { | ||
val key = iterator.nextKey() | ||
result[key] = readableMap.getInt(key) | ||
} | ||
return result | ||
} | ||
|
||
@JvmStatic | ||
fun safeStringEquals(str1: String?, str2: String?): Boolean { | ||
if (str1 == null && str2 == null) return true // both are null | ||
return if (str1 == null || str2 == null) false else str1 == str2 // only 1 is null | ||
} | ||
|
||
@JvmStatic | ||
fun safeStringArrayEquals(str1: Array<String>?, str2: Array<String>?): Boolean { | ||
if (str1 == null && str2 == null) return true // both are null | ||
if (str1 == null || str2 == null) return false // only 1 is null | ||
if (str1.size != str2.size) return false // only 1 is null | ||
for (i in str1.indices) { | ||
if (str1[i] == str2[i]) // standard check | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
@JvmStatic | ||
fun safeStringMapEquals( | ||
first: Map<String?, String?>?, | ||
second: Map<String?, String?>? | ||
): Boolean { | ||
if (first == null && second == null) return true // both are null | ||
if (first == null || second == null) return false // only 1 is null | ||
if (first.size != second.size) { | ||
return false | ||
} | ||
for (key in first.keys) { | ||
if (!safeStringEquals(first[key], second[key])) { | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.