From 4cf9eced4d40806fa04ea26a73b54d61f9672dbf Mon Sep 17 00:00:00 2001 From: Vladimir Krivosheev Date: Wed, 7 Oct 2015 14:05:27 +0200 Subject: [PATCH] =?UTF-8?q?cleanup,=20simplify=20=E2=80=94=20introduce=20L?= =?UTF-8?q?ogger.debug(lazyMessage:=20()=20->=20String)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/FileBasedStorage.kt | 10 +++----- .../src/StoreAwareProjectManager.kt | 5 ++-- .../src/XmlElementStorage.kt | 10 +++----- .../impl/stores/StateStorageBase.kt | 25 ++++++------------- .../com/intellij/openapi/diagnostic/util.kt | 22 ++++++++++++++++ .../src/BaseRepositoryManager.kt | 19 +++++--------- plugins/settings-repository/src/git/commit.kt | 5 ++-- plugins/settings-repository/src/git/pull.kt | 5 ++-- plugins/settings-repository/src/git/reset.kt | 5 ++-- 9 files changed, 50 insertions(+), 56 deletions(-) create mode 100644 platform/platform-impl/src/com/intellij/openapi/diagnostic/util.kt diff --git a/platform/configuration-store-impl/src/FileBasedStorage.kt b/platform/configuration-store-impl/src/FileBasedStorage.kt index 3d799134b6bc..27f1e106e14f 100644 --- a/platform/configuration-store-impl/src/FileBasedStorage.kt +++ b/platform/configuration-store-impl/src/FileBasedStorage.kt @@ -27,6 +27,7 @@ import com.intellij.openapi.components.StoragePathMacros import com.intellij.openapi.components.TrackingPathMacroSubstitutor import com.intellij.openapi.components.impl.stores.StorageUtil import com.intellij.openapi.components.store.ReadOnlyModificationException +import com.intellij.openapi.diagnostic.debug import com.intellij.openapi.fileEditor.impl.LoadTextUtil import com.intellij.openapi.util.JDOMUtil import com.intellij.openapi.util.io.BufferExposingByteArrayOutputStream @@ -113,9 +114,7 @@ open class FileBasedStorage(file: File, try { val file = getVirtualFile() if (file == null || file.isDirectory || !file.isValid) { - if (LOG.isDebugEnabled) { - LOG.debug("Document was not loaded for $fileSpec file is ${if (file == null) "null" else "directory"}") - } + LOG.debug { "Document was not loaded for $fileSpec file is ${if (file == null) "null" else "directory"}" } } else if (file.length == 0L) { processReadException(null) @@ -194,10 +193,7 @@ private fun isEqualContent(result: VirtualFile, lineSeparator: LineSeparator, co } private fun doWrite(requestor: Any, file: VirtualFile, content: Any, lineSeparator: LineSeparator, prependXmlProlog: Boolean) { - if (LOG.isDebugEnabled) { - LOG.debug("Save ${file.presentableUrl}") - } - + LOG.debug { "Save ${file.presentableUrl}" } val token = WriteAction.start() try { val out = file.getOutputStream(requestor) diff --git a/platform/configuration-store-impl/src/StoreAwareProjectManager.kt b/platform/configuration-store-impl/src/StoreAwareProjectManager.kt index b180c871612b..2f1ad5d405ae 100644 --- a/platform/configuration-store-impl/src/StoreAwareProjectManager.kt +++ b/platform/configuration-store-impl/src/StoreAwareProjectManager.kt @@ -24,6 +24,7 @@ import com.intellij.openapi.components.ComponentManager import com.intellij.openapi.components.StateStorage import com.intellij.openapi.components.impl.stores.* import com.intellij.openapi.components.stateStore +import com.intellij.openapi.diagnostic.debug import com.intellij.openapi.module.Module import com.intellij.openapi.progress.ProgressManager import com.intellij.openapi.project.Project @@ -117,9 +118,7 @@ class StoreAwareProjectManager(virtualFileManager: VirtualFileManager, progressM private fun isReloadUnblocked(): Boolean { val count = reloadBlockCount.get() - if (LOG.isDebugEnabled) { - LOG.debug("[RELOAD] myReloadBlockCount = $count") - } + LOG.debug { "[RELOAD] myReloadBlockCount = $count" } return count == 0 } diff --git a/platform/configuration-store-impl/src/XmlElementStorage.kt b/platform/configuration-store-impl/src/XmlElementStorage.kt index e50f262475ac..d0125c08bc4a 100644 --- a/platform/configuration-store-impl/src/XmlElementStorage.kt +++ b/platform/configuration-store-impl/src/XmlElementStorage.kt @@ -19,13 +19,13 @@ import com.intellij.openapi.components.RoamingType import com.intellij.openapi.components.StateStorage import com.intellij.openapi.components.TrackingPathMacroSubstitutor import com.intellij.openapi.components.impl.stores.FileStorageCoreUtil +import com.intellij.openapi.diagnostic.debug import com.intellij.openapi.util.JDOMUtil import com.intellij.util.containers.ContainerUtil import com.intellij.util.containers.SmartHashSet import gnu.trove.THashMap import org.jdom.Attribute import org.jdom.Element -import java.io.IOException abstract class XmlElementStorage protected constructor(protected val fileSpec: String, protected val rootElementName: String, @@ -87,16 +87,12 @@ abstract class XmlElementStorage protected constructor(protected val fileSpec: S val oldData = storageDataRef.get() val newData = getStorageData(true) if (oldData == null) { - if (LOG.isDebugEnabled) { - LOG.debug("analyzeExternalChangesAndUpdateIfNeed: old data null, load new for ${toString()}") - } + LOG.debug { "analyzeExternalChangesAndUpdateIfNeed: old data null, load new for ${toString()}" } componentNames.addAll(newData.keys()) } else { val changedComponentNames = oldData.getChangedComponentNames(newData) - if (LOG.isDebugEnabled) { - LOG.debug("analyzeExternalChangesAndUpdateIfNeed: changedComponentNames $changedComponentNames for ${toString()}") - } + LOG debug { "analyzeExternalChangesAndUpdateIfNeed: changedComponentNames $changedComponentNames for ${toString()}" } if (!ContainerUtil.isEmpty(changedComponentNames)) { componentNames.addAll(changedComponentNames) } diff --git a/platform/platform-impl/src/com/intellij/openapi/components/impl/stores/StateStorageBase.kt b/platform/platform-impl/src/com/intellij/openapi/components/impl/stores/StateStorageBase.kt index 03f9840274fb..6e2168a4554a 100644 --- a/platform/platform-impl/src/com/intellij/openapi/components/impl/stores/StateStorageBase.kt +++ b/platform/platform-impl/src/com/intellij/openapi/components/impl/stores/StateStorageBase.kt @@ -17,14 +17,13 @@ package com.intellij.openapi.components.impl.stores import com.intellij.openapi.components.StateStorage import com.intellij.openapi.diagnostic.Logger +import com.intellij.openapi.diagnostic.debug import org.jdom.Element import java.util.concurrent.atomic.AtomicReference -abstract class StateStorageBase : StateStorage { - companion object { - private val LOG: Logger = Logger.getInstance(StateStorageBase::class.java) - } +private val LOG: Logger = Logger.getInstance(StateStorageBase::class.java) +abstract class StateStorageBase : StateStorage { private var mySavingDisabled = false protected val storageDataRef: AtomicReference = AtomicReference() @@ -45,13 +44,11 @@ abstract class StateStorageBase : StateStorage { protected abstract fun hasState(storageData: T, componentName: String): Boolean - override fun hasState(componentName: String, reloadData: Boolean): Boolean { + override final fun hasState(componentName: String, reloadData: Boolean): Boolean { return hasState(getStorageData(reloadData), componentName) } - public fun getStorageData(): T = getStorageData(false) - - protected fun getStorageData(reload: Boolean): T { + protected fun getStorageData(reload: Boolean = false): T { val storageData = storageDataRef.get() if (storageData != null && !reload) { return storageData @@ -69,23 +66,17 @@ abstract class StateStorageBase : StateStorage { protected abstract fun loadData(): T public fun disableSaving() { - if (LOG.isDebugEnabled()) { - LOG.debug("Disabled saving for " + toString()) - } + LOG.debug { "Disabled saving for ${toString()}" } mySavingDisabled = true } public fun enableSaving() { - if (LOG.isDebugEnabled()) { - LOG.debug("Enabled saving " + toString()) - } + LOG.debug { "Enabled saving ${toString()}" } mySavingDisabled = false } protected fun checkIsSavingDisabled(): Boolean { - if (mySavingDisabled && LOG.isDebugEnabled()) { - LOG.debug("Saving disabled for " + toString()) - } + LOG.debug { "Saving disabled for ${toString()}" } return mySavingDisabled } } \ No newline at end of file diff --git a/platform/platform-impl/src/com/intellij/openapi/diagnostic/util.kt b/platform/platform-impl/src/com/intellij/openapi/diagnostic/util.kt new file mode 100644 index 000000000000..71ffc357970f --- /dev/null +++ b/platform/platform-impl/src/com/intellij/openapi/diagnostic/util.kt @@ -0,0 +1,22 @@ +/* + * Copyright 2000-2015 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.openapi.diagnostic + +inline fun Logger.debug(lazyMessage: () -> String) { + if (isDebugEnabled) { + debug(lazyMessage()) + } +} \ No newline at end of file diff --git a/plugins/settings-repository/src/BaseRepositoryManager.kt b/plugins/settings-repository/src/BaseRepositoryManager.kt index b0b9fdb9fe3f..34d9dba052b1 100644 --- a/plugins/settings-repository/src/BaseRepositoryManager.kt +++ b/plugins/settings-repository/src/BaseRepositoryManager.kt @@ -17,6 +17,7 @@ package org.jetbrains.settingsRepository import com.intellij.openapi.application.ApplicationManager import com.intellij.openapi.application.invokeAndWaitIfNeed +import com.intellij.openapi.diagnostic.debug import com.intellij.openapi.fileTypes.StdFileTypes import com.intellij.openapi.util.io.FileUtil import com.intellij.openapi.vcs.merge.MergeDialogCustomizer @@ -80,9 +81,7 @@ public abstract class BaseRepositoryManager(protected val dir: File) : Repositor override fun read(path: String): InputStream? { if (isPathIgnored(path)) { - if (LOG.isDebugEnabled) { - LOG.debug("$path is ignored") - } + LOG.debug { "$path is ignored" } return null } @@ -114,15 +113,11 @@ public abstract class BaseRepositoryManager(protected val dir: File) : Repositor override fun write(path: String, content: ByteArray, size: Int): Boolean { if (isPathIgnored(path)) { - if (LOG.isDebugEnabled) { - LOG.debug("$path is ignored") - } + LOG.debug { "$path is ignored" } return false } - if (LOG.isDebugEnabled) { - LOG.debug("Write $path") - } + LOG.debug { "Write $path" } try { lock.write { @@ -145,9 +140,7 @@ public abstract class BaseRepositoryManager(protected val dir: File) : Repositor protected abstract fun addToIndex(file: File, path: String, content: ByteArray, size: Int) override fun delete(path: String) { - if (LOG.isDebugEnabled) { - LOG.debug("Remove $path") - } + LOG.debug { "Remove $path"} lock.write { val file = File(dir, path) @@ -212,7 +205,7 @@ class RepositoryVirtualFile(private val path: String) : LightVirtualFile(PathUti override fun getPath() = path override fun setBinaryContent(content: ByteArray, newModificationStamp: Long, newTimeStamp: Long, requestor: Any?) { - $content = content + this.content = content } override fun getOutputStream(requestor: Any?, newModificationStamp: Long, newTimeStamp: Long): OutputStream { diff --git a/plugins/settings-repository/src/git/commit.kt b/plugins/settings-repository/src/git/commit.kt index 997e9324fd24..379db2d50c76 100644 --- a/plugins/settings-repository/src/git/commit.kt +++ b/plugins/settings-repository/src/git/commit.kt @@ -15,6 +15,7 @@ */ package org.jetbrains.settingsRepository.git +import com.intellij.openapi.diagnostic.debug import com.intellij.openapi.progress.ProgressIndicator import com.intellij.util.PathUtilRt import com.intellij.util.SmartList @@ -54,9 +55,7 @@ fun commit(repository: Repository, indicator: ProgressIndicator?, commitMessageF } } - if (LOG.isDebugEnabled) { - LOG.debug(indexDiffToString(diff)) - } + LOG.debug { indexDiffToString(diff) } indicator?.checkCanceled() diff --git a/plugins/settings-repository/src/git/pull.kt b/plugins/settings-repository/src/git/pull.kt index 7a25678a7f02..e90cc5ac2aac 100644 --- a/plugins/settings-repository/src/git/pull.kt +++ b/plugins/settings-repository/src/git/pull.kt @@ -15,6 +15,7 @@ */ package org.jetbrains.settingsRepository.git +import com.intellij.openapi.diagnostic.debug import com.intellij.openapi.progress.ProgressIndicator import com.intellij.openapi.vfs.VirtualFile import com.intellij.util.SmartList @@ -64,9 +65,7 @@ open internal class Pull(val manager: GitRepositoryManager, val indicator: Progr var refToMerge = prefetchedRefToMerge ?: fetch() ?: return null val mergeResult = merge(refToMerge, mergeStrategy, commitMessage = commitMessage) val mergeStatus = mergeResult.status - if (LOG.isDebugEnabled) { - LOG.debug(mergeStatus.toString()) - } + LOG.debug { mergeStatus.toString() } if (mergeStatus == MergeStatus.CONFLICTING) { return resolveConflicts(mergeResult, repository) diff --git a/plugins/settings-repository/src/git/reset.kt b/plugins/settings-repository/src/git/reset.kt index 294bf47e29f4..94d95a9c4566 100644 --- a/plugins/settings-repository/src/git/reset.kt +++ b/plugins/settings-repository/src/git/reset.kt @@ -15,6 +15,7 @@ */ package org.jetbrains.settingsRepository.git +import com.intellij.openapi.diagnostic.debug import com.intellij.openapi.progress.ProgressIndicator import org.eclipse.jgit.api.MergeResult import org.eclipse.jgit.merge.MergeStrategy @@ -26,9 +27,7 @@ import org.jetbrains.settingsRepository.UpdateResult internal class Reset(manager: GitRepositoryManager, indicator: ProgressIndicator) : Pull(manager, indicator) { fun reset(toTheirs: Boolean, localRepositoryInitializer: (() -> Unit)? = null): UpdateResult { val message = if (toTheirs) "Overwrite local to ${manager.getUpstream()}" else "Overwrite remote ${manager.getUpstream()} to local" - if (LOG.isDebugEnabled) { - LOG.debug(message) - } + LOG.debug { message } val resetResult = repository.resetHard() val result = MutableUpdateResult(resetResult.updated.keySet(), resetResult.removed)