Skip to content

Commit

Permalink
refactor(logger): deprecate iSuccess/iFail in favor of s/f with a sev…
Browse files Browse the repository at this point in the history
…erity argument

fix(logger): Update emote prefixes for success and failure to be rendered as plaintext in terminals
  • Loading branch information
0ffz committed Mar 18, 2024
1 parent 2c2117b commit fff0578
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ abstract class FeatureManager<T : FeatureDSL>(
val featuresWithMetDeps = context.features.filter { feature -> feature.dependsOn.all { Plugins.isEnabled(it) } }
(context.features - featuresWithMetDeps.toSet()).forEach { feature ->
val featureName = feature::class.simpleName
logger.iFail("Could not enable $featureName, missing dependencies: ${feature.dependsOn.filterNot(Plugins::isEnabled)}")
logger.f("Could not enable $featureName, missing dependencies: ${feature.dependsOn.filterNot(Plugins::isEnabled)}")
}
"Registering feature contexts" {
featuresWithMetDeps
.filterIsInstance<FeatureWithContext<*>>()
.forEach {
runCatching {
it.createAndInjectContext()
}.onFailure { error -> logger.iFail("Failed to create context for ${it::class.simpleName}: $error") }
}.onFailure { error -> logger.f("Failed to create context for ${it::class.simpleName}: $error") }
}
}

Expand Down Expand Up @@ -114,8 +114,8 @@ abstract class FeatureManager<T : FeatureDSL>(
"Disabling features" {
context.features.forEach { feature ->
runCatching { feature.disable(context) }
.onSuccess { logger.iSuccess("Disabled ${feature::class.simpleName}") }
.onFailure { e -> logger.iFail("Failed to disable ${feature::class.simpleName}: $e") }
.onSuccess { logger.s("Disabled ${feature::class.simpleName}") }
.onFailure { e -> logger.f("Failed to disable ${feature::class.simpleName}: $e") }
}
}
removeContext()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,51 @@ open class ComponentLogger(
i(message.miniMsg())
}

fun iSuccess(message: String) {
iSuccess(message.miniMsg())
@Deprecated("Replaced with s", ReplaceWith("s(message)"))
fun iSuccess(message: String) = s(message)

@Deprecated("Replaced with s", ReplaceWith("s(message)"))
fun iSuccess(message: ComponentLike) = s(message)

@Deprecated("Replaced with f", ReplaceWith("f(message)"))
fun iFail(message: String) = f(message)

@Deprecated("Replaced with f", ReplaceWith("f(message)"))
fun iFail(message: ComponentLike) = f(message)

/**
* Sends a green success message with an emote prefixed.
* Uses default console colors when [severity] is above [Severity.Warn]
*/
fun s(message: String, severity: Severity = Severity.Info) {
s(message.miniMsg(), severity)
}

fun iSuccess(message: ComponentLike) {
if (config.minSeverity <= Severity.Info)
/**
* Sends a green success message with an emote prefixed.
* Uses default console colors when [severity] is above [Severity.Warn]
*/
fun s(message: ComponentLike, severity: Severity = Severity.Info) {
if (config.minSeverity <= severity)
logComponent(Severity.Info, successComp.append(message), TextColor.color(0x008000))
}

fun iFail(message: String) {
iFail(message.miniMsg())
/**
* Sends a red failure message with an emote prefixed.
* Should only be used to notify users of a handled error, ex. config failing to load and thus being skipped.
* Uses default console colors when [severity] is above [Severity.Warn]
*/
fun f(message: String, severity: Severity = Severity.Info) {
f(message.miniMsg(), severity)
}

fun iFail(message: ComponentLike) {
if (config.minSeverity <= Severity.Info)
/**
* Sends a red failure message with an emote prefixed.
* Should only be used to notify users of a handled error, ex. config failing to load and thus being skipped.
* Uses default console colors when [severity] is above [Severity.Warn]
*/
fun f(message: ComponentLike, severity: Severity = Severity.Info) {
if (config.minSeverity <= severity)
logComponent(Severity.Info, errorComp.append(message), TextColor.color(0xFF0000))
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.mineinabyss.idofront.messaging

import com.mineinabyss.idofront.messaging.IdoLogging.ERROR_PREFIX
import com.mineinabyss.idofront.messaging.IdoLogging.MINECRAFT_ERROR_PREFIX
import com.mineinabyss.idofront.messaging.IdoLogging.MINECRAFT_SUCCESS_PREFIX
import com.mineinabyss.idofront.messaging.IdoLogging.MINECRAFT_WARN_PREFIX
import com.mineinabyss.idofront.messaging.IdoLogging.SUCCESS_PREFIX
import com.mineinabyss.idofront.messaging.IdoLogging.WARN_PREFIX
import com.mineinabyss.idofront.messaging.IdoLogging.logWithFallback
Expand All @@ -12,10 +15,16 @@ import org.bukkit.command.CommandSender
import org.bukkit.command.ConsoleCommandSender

object IdoLogging {
const val ERROR_PREFIX = "<dark_red><b>\u274C</b><red> "
const val SUCCESS_PREFIX = "<green><b>\u2714</b> "
// Prefixes optimized for console
const val ERROR_PREFIX = "<dark_red><b>✗</b><red> "
const val SUCCESS_PREFIX = "<green><b>✓</b> "
const val WARN_PREFIX = "<yellow>\u26A0<gray> "

// Prefixes that are sent to minecraft clients (these are rendered better on the client, but poorly in console)
const val MINECRAFT_ERROR_PREFIX = "<dark_red><b>\u274C</b><red> "
const val MINECRAFT_SUCCESS_PREFIX = "<green><b>\u2714</b> "
const val MINECRAFT_WARN_PREFIX = "<yellow>\u26A0<gray> "

val successComp = SUCCESS_PREFIX.miniMsg()
val errorComp = ERROR_PREFIX.miniMsg()
val warnComp = WARN_PREFIX.miniMsg()
Expand Down Expand Up @@ -65,19 +74,19 @@ fun CommandSender.info(message: Any?) = logWithFallback(message, printBukkit = :
fun CommandSender.error(message: Any?) {
if (this is ConsoleCommandSender)
logWithFallback(message) { Bukkit.getLogger().severe(it.toPlainText()) }
else info("$ERROR_PREFIX$message")
else info("$MINECRAFT_ERROR_PREFIX$message")
}

fun CommandSender.success(message: Any?) {
if (this is ConsoleCommandSender)
logWithFallback("<green>$message") { Bukkit.getConsoleSender().sendMessage(it) }
else info("$SUCCESS_PREFIX$message")
else info("$MINECRAFT_SUCCESS_PREFIX$message")
}

fun CommandSender.warn(message: Any?) {
if (this is ConsoleCommandSender)
logWithFallback(message) { Bukkit.getLogger().warning(it.toPlainText()) }
else info("$WARN_PREFIX$message")
else info("$MINECRAFT_WARN_PREFIX$message")
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class ActionScope(val logger: ComponentLogger) {
var printed = false
inline operator fun <T> String.invoke(block: AttemptBlock.() -> T): Result<T> {
if (!printed) {
scope.logger.iSuccess(msg)
scope.logger.s(msg)
printed = true
}
return scope.attempt(this, this, level + 1, block)
Expand All @@ -39,11 +39,11 @@ class ActionScope(val logger: ComponentLogger) {
return runCatching { attempt.block() }
.onSuccess {
if (attempt.printed) return@onSuccess
logger.iSuccess(success.addIndent(level))
logger.s(success.addIndent(level))
}
.onFailure {
if (attempt.printed) return@onFailure
logger.iFail(fail.addIndent(level))
logger.f(fail.addIndent(level))
if (level == 0)
it.printStackTrace()
}
Expand Down

0 comments on commit fff0578

Please sign in to comment.