Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement IdeaConsoleLogger for enhanced logging in IntelliJ plugin #211

Merged
merged 1 commit into from
Dec 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package com.itangcent.intellij.context
import com.itangcent.intellij.extend.guice.KotlinModule
import com.itangcent.intellij.extend.guice.singleton
import com.itangcent.intellij.extend.guice.with
import com.itangcent.intellij.logger.ConsoleRunnerLogger
import com.itangcent.intellij.logger.IdeaConsoleLogger
import com.itangcent.intellij.logger.Logger

open class BasicPluginModule : KotlinModule() {
override fun configure() {
super.configure()
bind(Logger::class).with(ConsoleRunnerLogger::class).singleton()
bind(Logger::class).with(IdeaConsoleLogger::class).singleton()
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package com.itangcent.intellij.logger

import com.google.inject.Inject
import com.google.inject.Singleton
import com.intellij.execution.filters.TextConsoleBuilderFactory
import com.intellij.execution.ui.ConsoleView
import com.intellij.execution.ui.ConsoleViewContentType
import com.intellij.openapi.project.Project
import com.intellij.openapi.wm.RegisterToolWindowTask
import com.intellij.openapi.wm.ToolWindowAnchor
import com.intellij.openapi.wm.ToolWindowManager
import com.itangcent.common.spi.SpiUtils
import com.itangcent.intellij.CustomInfo
import com.itangcent.intellij.context.ActionContext

/**
* A logger implementation that outputs messages to the IntelliJ IDEA console window.
* This class creates and manages a tool window in the IDE's bottom panel for displaying log messages.
*
* @author tangcent
*/
@Singleton
class IdeaConsoleLogger : AbstractLogger() {

@Inject
private lateinit var project: Project

@Inject
private lateinit var actionContext: ActionContext

/**
* The ID for the tool window.
*/
private val toolWindowId: String by lazy {
SpiUtils.loadService(CustomInfo::class)?.pluginName() ?: "Plugin Log"
}

/**
* Lazily initialized console view that handles the actual display of log messages.
* Creates a tool window in the IDE's bottom panel if it doesn't exist,
* or updates the existing one if it does.
*/
private val consoleView: ConsoleView by lazy {
// Create a new console builder for the current project
val console = TextConsoleBuilderFactory.getInstance()
.createBuilder(project)
.console

// Run UI operations in the Swing EDT thread
actionContext.runInSwingUI {
val toolWindowManager = ToolWindowManager.getInstance(project)
val toolWindow = toolWindowManager.getToolWindow(toolWindowId)
if (toolWindow == null) {
// Register a new tool window if it doesn't exist
toolWindowManager.registerToolWindow(
RegisterToolWindowTask(
id = toolWindowId,
anchor = ToolWindowAnchor.BOTTOM,
canCloseContent = false,
component = console.component
)
)
} else {
// Update existing tool window with the new console component
toolWindow.contentManager.getContent(0)?.component = console.component
}
}

console
}

/**
* Processes and displays a log message in the console.
*
* @param logData The message to be logged. If null, the method returns without action.
*/
override fun processLog(logData: String?) {
if (logData == null) return

actionContext.runInSwingUI {
consoleView.print("$logData\n", ConsoleViewContentType.NORMAL_OUTPUT)
}
}
}

/**
* @deprecated Use IdeaConsoleLogger instead
*/
@Deprecated("Use IdeaConsoleLogger instead", ReplaceWith("IdeaConsoleLogger"))
typealias ConsoleRunnerLogger = IdeaConsoleLogger

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.itangcent.intellij.logger
import com.google.inject.ImplementedBy


@ImplementedBy(ConsoleRunnerLogger::class)
@ImplementedBy(IdeaConsoleLogger::class)
interface Logger : com.itangcent.common.logger.ILogger {
override fun log(msg: String) {
log(BasicLevel.ALL, msg)
Expand Down
Loading