From ea9be2b5e6ff3cbdefce713ddcf5fb726fdfc015 Mon Sep 17 00:00:00 2001 From: "Artem.Bukhonov" Date: Mon, 13 May 2024 17:45:26 +0200 Subject: [PATCH] [IJPL] Add logger tracing helpers (cherry picked from commit 1bf41fd81af1a57b4375014c9b5017895f723dc0) IJ-MR-134201 GitOrigin-RevId: 26cf466cfa7550d5d1f2a2f803af94cb420d35fe --- platform/util/api-dump-unreviewed.txt | 2 ++ .../src/com/intellij/openapi/diagnostic/logger.kt | 14 ++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/platform/util/api-dump-unreviewed.txt b/platform/util/api-dump-unreviewed.txt index ed0dcca48eea..40b0fc731e07 100644 --- a/platform/util/api-dump-unreviewed.txt +++ b/platform/util/api-dump-unreviewed.txt @@ -543,8 +543,10 @@ f:com.intellij.openapi.diagnostic.LoggerKt - bs:debug$default(com.intellij.openapi.diagnostic.Logger,java.lang.Exception,kotlin.jvm.functions.Function0,I,java.lang.Object):V - sf:fileLogger():com.intellij.openapi.diagnostic.Logger - sf:getOrLogException(java.lang.Object,com.intellij.openapi.diagnostic.Logger):java.lang.Object +- sf:ifTraceEnabled(com.intellij.openapi.diagnostic.Logger,kotlin.jvm.functions.Function0):java.lang.Object - sf:runAndLogException(com.intellij.openapi.diagnostic.Logger,kotlin.jvm.functions.Function0):java.lang.Object - sf:trace(com.intellij.openapi.diagnostic.Logger,kotlin.jvm.functions.Function0):V +- sf:traceThrowable(com.intellij.openapi.diagnostic.Logger,kotlin.jvm.functions.Function0):V *:com.intellij.openapi.diagnostic.ReportingClassSubstitutor - s:getClassToReport(java.lang.Object):java.lang.Class - a:getSubstitutedClass():java.lang.Class diff --git a/platform/util/src/com/intellij/openapi/diagnostic/logger.kt b/platform/util/src/com/intellij/openapi/diagnostic/logger.kt index cc14afb0f820..083a667d5700 100644 --- a/platform/util/src/com/intellij/openapi/diagnostic/logger.kt +++ b/platform/util/src/com/intellij/openapi/diagnostic/logger.kt @@ -67,6 +67,20 @@ inline fun Logger.trace(@NonNls lazyMessage: () -> String) { } } +/* Cannot name it `trace` due to the clash with the function above */ +inline fun Logger.traceThrowable(lazyThrowable: () -> Throwable) { + if (isTraceEnabled) { + return trace(lazyThrowable()) + } +} + +inline fun Logger.ifTraceEnabled(lazyValue: () -> T): T? { + if (isTraceEnabled) { + return lazyValue() + } + return null +} + /** Consider using [Result.getOrLogException] for more straight-forward API instead. */ inline fun Logger.runAndLogException(runnable: () -> T): T? { return runCatching {