-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
plugin: Support LogOutputChannel (#12429)
- Support LogOutputChannel - Support LogLevel - Support in namespace/env: logLevel & onDidChangeLogLevel - Remark: Needs further extension of application to fully support all aspects of a LogOutputViewChannel (i.e. developer logger service, logging to file, extension of output view UI) Resolves #12017 Contributed on behalf of STMicroelectronics. Signed-off-by: Nina Doschek <ndoschek@eclipsesource.com>
- Loading branch information
Showing
9 changed files
with
264 additions
and
45 deletions.
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(name: string, proxy: OutputChannelRegistryMain, 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); | ||
} | ||
|
||
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.