-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
plugin: Support LogOutputChannel #12429
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
packages/plugin-ext/src/plugin/output-channel/log-output-channel.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
// ***************************************************************************** | ||
// Copyright (C) 2023 STMicroelectronics and others. | ||
// | ||
// This program and the accompanying materials are made available under the | ||
// terms of the Eclipse Public License v. 2.0 which is available at | ||
// http://www.eclipse.org/legal/epl-2.0. | ||
// | ||
// This Source Code may also be made available under the following Secondary | ||
// Licenses when the conditions for such availability set forth in the Eclipse | ||
// Public License v. 2.0 are satisfied: GNU General Public License, version 2 | ||
// with the GNU Classpath Exception which is available at | ||
// https://www.gnu.org/software/classpath/license.html. | ||
// | ||
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 | ||
// ***************************************************************************** | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
|
||
import { Emitter } from '@theia/core/shared/vscode-languageserver-protocol'; | ||
import * as theia from '@theia/plugin'; | ||
|
||
import { OutputChannelRegistryMain, PluginInfo } from '../../common/plugin-api-rpc'; | ||
import { OutputChannelImpl } from './output-channel-item'; | ||
import { LogLevel } from '../types-impl'; | ||
import { isArray, isObject } from '@theia/core'; | ||
|
||
export class LogOutputChannelImpl extends OutputChannelImpl implements theia.LogOutputChannel { | ||
|
||
readonly onDidChangeLogLevelEmitter: Emitter<theia.LogLevel> = new Emitter<theia.LogLevel>(); | ||
readonly onDidChangeLogLevel: theia.Event<theia.LogLevel> = this.onDidChangeLogLevelEmitter.event; | ||
public logLevel: theia.LogLevel; | ||
|
||
constructor(override readonly name: string, protected override readonly proxy: OutputChannelRegistryMain, protected override readonly pluginInfo: PluginInfo) { | ||
super(name, proxy, pluginInfo); | ||
this.setLogLevel(LogLevel.Info); | ||
} | ||
|
||
setLogLevel(level: theia.LogLevel): void { | ||
if (this.logLevel !== level) { | ||
this.logLevel = level; | ||
this.onDidChangeLogLevelEmitter.fire(this.logLevel); | ||
} | ||
} | ||
|
||
getLogLevel(): theia.LogLevel { | ||
return this.logLevel; | ||
} | ||
|
||
override append(value: string): void { | ||
super.validate(); | ||
this.info(value); | ||
tsmaeder marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
override appendLine(value: string): void { | ||
super.validate(); | ||
this.append(value + '\n'); | ||
} | ||
|
||
override dispose(): void { | ||
super.dispose(); | ||
this.onDidChangeLogLevelEmitter.dispose(); | ||
} | ||
|
||
protected log(level: theia.LogLevel, message: string): void { | ||
super.validate(); | ||
if (this.checkLogLevel(level)) { | ||
const now = new Date(); | ||
const eol = message.endsWith('\n') ? '' : '\n'; | ||
const logMessage = `${now.toISOString()} [${LogLevel[level]}] ${message}${eol}`; | ||
this.proxy.$append(this.name, logMessage, this.pluginInfo); | ||
} | ||
} | ||
|
||
private checkLogLevel(level: theia.LogLevel): boolean { | ||
return this.logLevel <= level; | ||
} | ||
|
||
trace(message: string, ...args: any[]): void { | ||
this.log(LogLevel.Trace, this.format(message, args)); | ||
} | ||
|
||
debug(message: string, ...args: any[]): void { | ||
this.log(LogLevel.Debug, this.format(message, args)); | ||
} | ||
|
||
info(message: string, ...args: any[]): void { | ||
this.log(LogLevel.Info, this.format(message, args)); | ||
} | ||
|
||
warn(message: string, ...args: any[]): void { | ||
this.log(LogLevel.Warning, this.format(message, args)); | ||
} | ||
|
||
error(errorMsg: string | Error, ...args: any[]): void { | ||
if (errorMsg instanceof Error) { | ||
this.log(LogLevel.Error, this.format(errorMsg.stack || errorMsg.message, args)); | ||
} else { | ||
this.log(LogLevel.Error, this.format(errorMsg, args)); | ||
} | ||
} | ||
|
||
private format(message: string, args: any[]): string { | ||
if (args.length > 0) { | ||
return `${message} ${args.map((arg: any) => isObject(arg) || isArray(arg) ? JSON.stringify(arg) : arg).join(' ')}`; | ||
} | ||
return message; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since
name
is public on the superclass, do we need theoverride readonly
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is also readonly in the superclass, so I guess this is okay this way
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But why would you redeclare a variable that is already present in the superclass and visible just fine in the subclass? It's unnecessary for all three constructor params, IMO.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes basically I agree, but as I had to write a custom constructor (to initalize the log level) I have no other choice, as Typescript requires them to be overridden due to the same variable names, see
This parameter property must have an 'override' modifier because it overrides a member in base class 'OutputChannelImpl'.ts(4115)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This works for me:
The "readonly" is a property of the implicitly created field, not the constructor parameter.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks I was not aware of that, that works great! I pushed another update for this.