Convert FakeLoggingDelegate to Kotlin (#37731)

Summary:
Converts FakeLoggingDelegate from Java to Kotlin, as per issue https://github.com/facebook/react-native/issues/37708

## Changelog:

[Internal] [Changed] - Convert FakeLoggingDelegate to Kotlin

Pull Request resolved: https://github.com/facebook/react-native/pull/37731

Test Plan:
Tests pass: `./gradlew :packages:react-native:ReactAndroid:test`
Formatted with [KtFmt](https://facebook.github.io/ktfmt/)

Reviewed By: rshest

Differential Revision: D46515330

Pulled By: cortinico

fbshipit-source-id: 29fb66386aca194fdaba1217ef9fa0a38c5c2fc4
This commit is contained in:
M0Coding
2023-06-08 04:54:11 -07:00
committed by Facebook GitHub Bot
parent 79ae710cc5
commit 2ac607803c
2 changed files with 94 additions and 138 deletions
@@ -1,138 +0,0 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
package com.facebook.common.logging;
import androidx.annotation.Nullable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
public final class FakeLoggingDelegate implements LoggingDelegate {
public static final class LogLine {
public final int priority;
public final String tag;
public final String msg;
public final @Nullable Throwable tr;
private LogLine(int priority, String tag, String msg, @Nullable Throwable tr) {
this.priority = priority;
this.tag = tag;
this.msg = msg;
this.tr = tr;
}
}
public static final int ASSERT = FLog.ASSERT;
public static final int DEBUG = FLog.DEBUG;
public static final int ERROR = FLog.ERROR;
public static final int INFO = FLog.INFO;
public static final int VERBOSE = FLog.VERBOSE;
public static final int WARN = FLog.WARN;
/**
* There is no log level for Terrible Failures (we emit them at the Error Log-level), but to test
* that WTF errors are being logged, we are making up a new log level here, guaranteed to be
* larger than any of the other log levels.
*/
public static final int WTF =
1 + Collections.max(Arrays.asList(ASSERT, DEBUG, ERROR, INFO, VERBOSE, WARN));
private int mMinLogLevel = FLog.VERBOSE;
private final ArrayList<LogLine> mLogs = new ArrayList<>();
/** Test Harness */
private static boolean matchLogQuery(
int priority, String tag, @Nullable String throwMsg, LogLine line) {
return priority == line.priority
&& tag.equals(line.tag)
&& (throwMsg == null || throwMsg.equals(line.tr.getMessage()));
}
public boolean logContains(int priority, String tag, String throwMsg) {
for (FakeLoggingDelegate.LogLine line : mLogs) {
if (matchLogQuery(priority, tag, throwMsg, line)) {
return true;
}
}
return false;
}
/** LoggingDelegate API */
public int getMinimumLoggingLevel() {
return mMinLogLevel;
}
public void setMinimumLoggingLevel(int level) {
mMinLogLevel = level;
}
public boolean isLoggable(int level) {
return level >= mMinLogLevel;
}
private void logImpl(int priority, String tag, String msg, Throwable tr) {
if (isLoggable(priority)) {
mLogs.add(new LogLine(priority, tag, msg, tr));
}
}
public void log(int priority, String tag, String msg) {
logImpl(priority, tag, msg, null);
}
public void d(String tag, String msg, Throwable tr) {
logImpl(DEBUG, tag, msg, tr);
}
public void d(String tag, String msg) {
logImpl(DEBUG, tag, msg, null);
}
public void e(String tag, String msg, Throwable tr) {
logImpl(ERROR, tag, msg, tr);
}
public void e(String tag, String msg) {
logImpl(ERROR, tag, msg, null);
}
public void i(String tag, String msg, Throwable tr) {
logImpl(INFO, tag, msg, tr);
}
public void i(String tag, String msg) {
logImpl(INFO, tag, msg, null);
}
public void v(String tag, String msg, Throwable tr) {
logImpl(VERBOSE, tag, msg, tr);
}
public void v(String tag, String msg) {
logImpl(VERBOSE, tag, msg, null);
}
public void w(String tag, String msg, Throwable tr) {
logImpl(WARN, tag, msg, tr);
}
public void w(String tag, String msg) {
logImpl(WARN, tag, msg, null);
}
public void wtf(String tag, String msg, Throwable tr) {
logImpl(WTF, tag, msg, tr);
}
public void wtf(String tag, String msg) {
logImpl(WTF, tag, msg, null);
}
}
@@ -0,0 +1,94 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
package com.facebook.common.logging
import java.util.*
class FakeLoggingDelegate : LoggingDelegate {
class LogLine(val priority: Int, val tag: String, val msg: String, val tr: Throwable? = null)
private var minLogLevel = FLog.VERBOSE
private val logs = ArrayList<LogLine>()
fun logContains(priority: Int, tag: String, throwMsg: String?): Boolean {
for (line in logs) {
if (matchLogQuery(priority, tag, throwMsg, line)) {
return true
}
}
return false
}
/** LoggingDelegate API */
override fun getMinimumLoggingLevel(): Int = minLogLevel
override fun setMinimumLoggingLevel(level: Int) {
minLogLevel = level
}
override fun isLoggable(level: Int): Boolean = level >= minLogLevel
private fun logImpl(priority: Int, tag: String, msg: String, tr: Throwable? = null) {
if (isLoggable(priority)) {
logs.add(LogLine(priority, tag, msg, tr))
}
}
override fun log(priority: Int, tag: String, msg: String) = logImpl(priority, tag, msg, null)
override fun d(tag: String, msg: String, tr: Throwable) = logImpl(DEBUG, tag, msg, tr)
override fun d(tag: String, msg: String) = logImpl(DEBUG, tag, msg, null)
override fun e(tag: String, msg: String, tr: Throwable) = logImpl(ERROR, tag, msg, tr)
override fun e(tag: String, msg: String) = logImpl(ERROR, tag, msg, null)
override fun i(tag: String, msg: String, tr: Throwable) = logImpl(INFO, tag, msg, tr)
override fun i(tag: String, msg: String) = logImpl(INFO, tag, msg, null)
override fun v(tag: String, msg: String, tr: Throwable) = logImpl(VERBOSE, tag, msg, tr)
override fun v(tag: String, msg: String) = logImpl(VERBOSE, tag, msg, null)
override fun w(tag: String, msg: String, tr: Throwable) = logImpl(WARN, tag, msg, tr)
override fun w(tag: String, msg: String) = logImpl(WARN, tag, msg, null)
override fun wtf(tag: String, msg: String, tr: Throwable) = logImpl(WTF, tag, msg, tr)
override fun wtf(tag: String, msg: String) = logImpl(WTF, tag, msg, null)
companion object {
const val ASSERT = FLog.ASSERT
const val DEBUG = FLog.DEBUG
const val ERROR = FLog.ERROR
const val INFO = FLog.INFO
const val VERBOSE = FLog.VERBOSE
const val WARN = FLog.WARN
/**
* There is no log level for Terrible Failures (we emit them at the Error Log-level), but to
* test that WTF errors are being logged, we are making up a new log level here, guaranteed to
* be larger than any of the other log levels.
*/
@JvmField val WTF = 1 + (listOf(ASSERT, DEBUG, ERROR, INFO, VERBOSE, WARN).maxOrNull() ?: 0)
/** Test Harness */
private fun matchLogQuery(
priority: Int,
tag: String,
throwMsg: String?,
line: LogLine
): Boolean =
priority == line.priority &&
tag == line.tag &&
(throwMsg == null || throwMsg == line.tr?.message)
}
}