Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
- Ensure options to identify LogOutputChannel is never optional
- LogOutputChannelImpl extends OutputChannelImpl instead of vscode's AbstractMessageLogger
- Remove obsolete type converter again
- Update Changelog

Contributed on behalf of STMicroelectronics.

Signed-off-by: Nina Doschek <ndoschek@eclipsesource.com>
  • Loading branch information
ndoschek committed Apr 20, 2023
1 parent b40b2a6 commit 78b5498
Show file tree
Hide file tree
Showing 9 changed files with 145 additions and 104 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<a name="breaking_changes_1.37.0">[Breaking Changes:](#breaking_changes_1.37.0)</a>
- [core] Inject core preference into `DockPanelRenderer` constructor [12360](https://github.com/eclipse-theia/theia/pull/12360)
- [core] Introduced `ScrollableTabBar.updateTabs()` to fully render tabs [12360](https://github.com/eclipse-theia/theia/pull/12360)
- [plugin] removed enum `LogLevel` and namespace `env` from `plugin/src/theia-proposed.d.ts` [#12017](https://github.com/eclipse-theia/theia/pull/12429)

## v1.36.0 0 - 03/30/2023

Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-ext/src/common/plugin-api-rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ export interface TerminalServiceExt {
}
export interface OutputChannelRegistryExt {
createOutputChannel(name: string, pluginInfo: PluginInfo): theia.OutputChannel,
createOutputChannel(name: string, pluginInfo: PluginInfo, options?: { log: true }): theia.LogOutputChannel
createOutputChannel(name: string, pluginInfo: PluginInfo, options: { log: true }): theia.LogOutputChannel
}

export interface ConnectionMain {
Expand Down
7 changes: 4 additions & 3 deletions packages/plugin-ext/src/plugin/output-channel-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
import * as theia from '@theia/plugin';
import { PLUGIN_RPC_CONTEXT as Ext, OutputChannelRegistryExt, OutputChannelRegistryMain, PluginInfo } from '../common/plugin-api-rpc';
import { RPCProtocol } from '../common/rpc-protocol';
import { LogOutputChannelImpl } from './output-channel/logoutput-channel';
import { isObject } from '../common/types';
import { LogOutputChannelImpl } from './output-channel/log-output-channel';
import { OutputChannelImpl } from './output-channel/output-channel-item';

export class OutputChannelRegistryExtImpl implements OutputChannelRegistryExt {
Expand All @@ -29,13 +30,13 @@ export class OutputChannelRegistryExtImpl implements OutputChannelRegistryExt {
}

createOutputChannel(name: string, pluginInfo: PluginInfo): theia.OutputChannel;
createOutputChannel(name: string, pluginInfo: PluginInfo, options?: { log: true; }): theia.LogOutputChannel;
createOutputChannel(name: string, pluginInfo: PluginInfo, options: { log: true; }): theia.LogOutputChannel;
createOutputChannel(name: string, pluginInfo: PluginInfo, options?: { log: true; }): theia.OutputChannel | theia.LogOutputChannel {
name = name.trim();
if (!name) {
throw new Error('illegal argument \'name\'. must not be falsy');
}
const isLogOutput = options && typeof options === 'object' && options.log;
const isLogOutput = options && isObject(options);
return isLogOutput
? this.doCreateLogOutputChannel(name, pluginInfo)
: this.doCreateOutputChannel(name, pluginInfo);
Expand Down
132 changes: 132 additions & 0 deletions packages/plugin-ext/src/plugin/output-channel/log-output-channel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
// *****************************************************************************
// 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';

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 {
this.info(value);
}

override appendLine(value: string): void {
this.append(value + '\n');
}

protected log(level: theia.LogLevel, message: string): void {
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);
}

override dispose(): void {
super.dispose();
this.onDidChangeLogLevelEmitter.dispose();
}

// begin
// copied from vscode: https://github.com/Microsoft/vscode/blob/main/src/vs/platform/log/common/log.ts
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
private checkLogLevel(level: theia.LogLevel): boolean {
return this.logLevel <= level;
}

trace(message: string, ...args: any[]): void {
if (this.checkLogLevel(LogLevel.Trace)) {
this.log(LogLevel.Trace, this.format([message, ...args]));
}
}

debug(message: string, ...args: any[]): void {
if (this.checkLogLevel(LogLevel.Debug)) {
this.log(LogLevel.Debug, this.format([message, ...args]));
}
}

info(message: string, ...args: any[]): void {
if (this.checkLogLevel(LogLevel.Info)) {
this.log(LogLevel.Info, this.format([message, ...args]));
}
}

warn(message: string, ...args: any[]): void {
if (this.checkLogLevel(LogLevel.Warning)) {
this.log(LogLevel.Warning, this.format([message, ...args]));
}
}

error(message: string | Error, ...args: any[]): void {
if (this.checkLogLevel(LogLevel.Error)) {
if (message instanceof Error) {
const array = Array.prototype.slice.call(arguments) as unknown[];
array[0] = message.stack;
this.log(LogLevel.Error, this.format(array));
} else {
this.log(LogLevel.Error, this.format([message, ...args]));
}
}
}

private format(args: any): string {
let result = '';

for (let i = 0; i < args.length; i++) {
let a = args[i];

if (typeof a === 'object') {
try {
a = JSON.stringify(a);
} catch (e) { }
}

result += (i > 0 ? ' ' : '') + a;
}

return result;
}
// end

}
80 changes: 0 additions & 80 deletions packages/plugin-ext/src/plugin/output-channel/logoutput-channel.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class OutputChannelImpl implements theia.OutputChannel {

private disposed: boolean;

constructor(readonly name: string, private proxy: OutputChannelRegistryMain, private readonly pluginInfo: PluginInfo) {
constructor(readonly name: string, protected readonly proxy: OutputChannelRegistryMain, protected readonly pluginInfo: PluginInfo) {
}

dispose(): void {
Expand Down
4 changes: 3 additions & 1 deletion packages/plugin-ext/src/plugin/plugin-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,9 @@ export function createAPIFactory(
return statusBarMessageRegistryExt.createStatusBarItem(alignment, priority, id);
},
createOutputChannel(name: string, options?: { log: true }): any {
return outputChannelRegistryExt.createOutputChannel(name, pluginToPluginInfo(plugin), options);
return !options
? outputChannelRegistryExt.createOutputChannel(name, pluginToPluginInfo(plugin))
: outputChannelRegistryExt.createOutputChannel(name, pluginToPluginInfo(plugin), options);
},
createWebviewPanel(viewType: string,
title: string,
Expand Down
14 changes: 0 additions & 14 deletions packages/plugin-ext/src/plugin/type-converters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import { UriComponents } from '../common/uri-components';
import { isReadonlyArray } from '../common/arrays';
import { MarkdownString as MarkdownStringDTO } from '@theia/core/lib/common/markdown-rendering';
import { isObject } from '@theia/core/lib/common';
import { LogLevel as MonacoLogLevel } from '@theia/monaco-editor-core/esm/vs/platform/log/common/log';

const SIDE_GROUP = -2;
const ACTIVE_GROUP = -1;
Expand Down Expand Up @@ -1386,16 +1385,3 @@ export namespace DataTransfer {
return dataTransfer;
}
}

export function toLogLevel(logLevel: MonacoLogLevel): theia.LogLevel {
switch (logLevel) {
case MonacoLogLevel.Trace: return types.LogLevel.Trace;
case MonacoLogLevel.Debug: return types.LogLevel.Debug;
case MonacoLogLevel.Info: return types.LogLevel.Info;
case MonacoLogLevel.Warning: return types.LogLevel.Warning;
case MonacoLogLevel.Error: return types.LogLevel.Error;
case MonacoLogLevel.Critical: return types.LogLevel.Error /* the plugin API's max LogLevel is Error */;
case MonacoLogLevel.Off: return types.LogLevel.Off;
default: throw new Error(`Invalid log level ${logLevel}`);
}
}
7 changes: 3 additions & 4 deletions packages/plugin/src/theia.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5354,13 +5354,12 @@ export module '@theia/plugin' {
export function createOutputChannel(name: string): OutputChannel;

/**
* Creates a new {@link OutputChannel output channel} with the given name.
* If options are given, creates a new {@link OutputChannel output channel} with the given name.
* Creates a new {@link LogOutputChannel log output channel} with the given name.
*
* @param name Human-readable string which will be used to represent the channel in the UI.
* @param options optional; Options for the log output channel.
* @param options Options for the log output channel.
*/
export function createOutputChannel(name: string, options?: { log: true }): LogOutputChannel;
export function createOutputChannel(name: string, options: { log: true }): LogOutputChannel;

/**
* Create new terminal.
Expand Down

0 comments on commit 78b5498

Please sign in to comment.