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

Add logging functionality for command requests #2550

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
7 changes: 7 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -1679,6 +1679,13 @@
"category": "IBM i",
"icon": "$(plus)",
"enablement": "code-for-ibmi:connected"
},
{
"command": "code-for-ibmi.logs.show",
"title": "Show logs",
"category": "IBM i",
"icon": "$(output)",
"enablement": "code-for-ibmi:connected"
}
],
"keybindings": [
Expand Down
12 changes: 11 additions & 1 deletion src/Instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { VsCodeConfig } from "./config/Configuration";
import { EventEmitter } from "stream";
import { ConnectionStorage } from "./api/configuration/storage/ConnectionStorage";
import { VscodeTools } from "./ui/Tools";
import { RequestLogger } from "./api/requestLogger";

type IBMiEventSubscription = {
func: Function,
Expand All @@ -25,6 +26,7 @@ export interface ConnectionOptions {

export default class Instance {
private connection: IBMi | undefined;
private requestLogger = new RequestLogger();

private output = {
channel: vscode.window.createOutputChannel(`Code for IBM i`),
Expand All @@ -45,6 +47,9 @@ export default class Instance {
IBMi.connectionManager.configMethod = new VsCodeConfig();

this.emitter.event(e => this.processEvent(e));

// TODO: this should be controlled by configuration
this.requestLogger.setLoggingState(true);
}

focusOutput() {
Expand All @@ -55,14 +60,19 @@ export default class Instance {
return this.output.content;
}

getLogger(): RequestLogger {
return this.requestLogger;
}

private resetOutput() {
this.requestLogger.clear();
this.output.channel.clear();
this.output.content = ``;
this.output.writeCount = 0;
}

connect(options: ConnectionOptions): Promise<ConnectionResult> {
const connection = new IBMi();
const connection = new IBMi({requestLogger: this.requestLogger});

this.resetOutput();
connection.appendOutput = (message) => {
Expand Down
16 changes: 14 additions & 2 deletions src/api/IBMi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { AspInfo, CommandData, CommandResult, ConnectionData, IBMiMember, Remote
import { EventEmitter } from 'stream';
import { ConnectionConfig } from './configuration/config/types';
import { EditorPath } from '../typings';
import { RequestLogger } from './requestLogger';

export interface MemberParts extends IBMiMember {
basename: string
Expand Down Expand Up @@ -99,6 +100,8 @@ export default class IBMi {
private tempRemoteFiles: { [name: string]: string } = {};
defaultUserLibraries: string[] = [];

private requestLogger?: RequestLogger;

/**
* Used to store ASP numbers and their names
* Their names usually maps up to a directory in
Expand Down Expand Up @@ -196,7 +199,11 @@ export default class IBMi {
this.config = newConfig;
}

constructor() {
constructor(options: {requestLogger?: RequestLogger} = {}) {
if (options.requestLogger) {
this.requestLogger = options.requestLogger;
}

this.remoteFeatures = {
git: undefined,
grep: undefined,
Expand Down Expand Up @@ -1100,12 +1107,13 @@ export default class IBMi {
const command = commands.join(` && `);
const directory = options.directory || this.config?.homeDirectory;


this.appendOutput(`${directory}: ${command}\n`);
if (options && options.stdin) {
this.appendOutput(`${options.stdin}\n`);
}

const requestLog = this.requestLogger?.new(command, directory, options.stdin);

const result = await this.client!.execCommand(command, {
cwd: directory,
stdin: options.stdin,
Expand All @@ -1116,6 +1124,10 @@ export default class IBMi {
// Some simplification
if (result.code === null) result.code = 0;

if (requestLog) {
this.requestLogger?.end(requestLog, result.code, result.stdout, result.stderr);
}

this.appendOutput(JSON.stringify(result, null, 4) + `\n\n`);

return {
Expand Down
52 changes: 52 additions & 0 deletions src/api/requestLogger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
export interface RequestLog {
start: number;
end?: number;
command: string;
cwd?: string;
stdin?: string;
stdout?: string;
stderr?: string;
}

export class RequestLogger {
private collecting = false;
private uniqueName: string|undefined;
private log: RequestLog[] = [];

public setId(uniqueName: string) {
this.uniqueName = uniqueName;
this.log = [];
}

setLoggingState(state: boolean) {
this.collecting = state;
}

clear() {
this.log = [];
}

public getLogs() {
return this.log;
}

public new(command: string, cwd?: string, stdin?: string): RequestLog {
const entry: RequestLog = {
start: Date.now(),
command,
cwd,
stdin
};
return entry;
}

end(entry: RequestLog, code: number, stdout?: string, stderr?: string): void {
entry.end = Date.now();
entry.stdout = stdout;
entry.stderr = stderr;

if (this.collecting) {
this.log.push(entry);
}
}
}
21 changes: 21 additions & 0 deletions src/commands/logs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { commands, Disposable, ExtensionContext, window, workspace } from "vscode";
import Instance from "../Instance";

export function registerLoggingCommands(context: ExtensionContext, instance: Instance): Disposable[] {
return [
commands.registerCommand(`code-for-ibmi.logs.show`, async () => {
const logger = instance.getLogger();

const content = logger.getLogs();

if (content.length === 0) {
window.showInformationMessage(`No logs available.`);
return;
}

workspace.openTextDocument({ content: JSON.stringify(content, null, 2), language: `json` }).then(doc => {
window.showTextDocument(doc);
})
})
]
}
3 changes: 3 additions & 0 deletions src/instantiate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { QSysFS } from "./filesystems/qsys/QSysFs";
import { ActionsUI } from './webviews/actions';
import { VariablesUI } from "./webviews/variables";
import IBMi from "./api/IBMi";
import { registerLoggingCommands } from "./commands/logs";

export let instance: Instance;

Expand Down Expand Up @@ -82,6 +83,8 @@ export async function loadAllofExtension(context: vscode.ExtensionContext) {

...registerPasswordCommands(context, instance),

...registerLoggingCommands(context, instance),

vscode.commands.registerCommand("code-for-ibmi.updateConnectedBar", updateConnectedBar),
);

Expand Down
Loading