Skip to content

Commit

Permalink
logger refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
matepek committed Apr 18, 2023
1 parent df6cb0c commit 96b2936
Show file tree
Hide file tree
Showing 14 changed files with 116 additions and 89 deletions.
6 changes: 3 additions & 3 deletions src/ConfigOfExecGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { GazeWrapper, VSCFSWatcherWrapper, FSWatcher } from './util/FSWatcher';
import { readJSONSync } from 'fs-extra';
import { Spawner, DefaultSpawner, SpawnWithExecutor } from './Spawner';
import { RunTaskConfig, ExecutionWrapperConfig, FrameworkSpecificConfig } from './AdvancedExecutableInterface';
import { LoggerWrapper } from './LoggerWrapper';
import { Logger } from './Logger';
import { debugBreak } from './util/DevelopmentHelper';
import { FrameworkType } from './framework/Framework';
import { readFileSync } from 'fs';
Expand Down Expand Up @@ -525,15 +525,15 @@ export class ConfigOfExecGroup implements vscode.Disposable {
}
}

function checkEnvForPath(env: Record<string, string>, log: LoggerWrapper): void {
function checkEnvForPath(env: Record<string, string>, log: Logger): void {
if (process.platform === 'win32') {
checkPathVariance('PATH', env, log);
checkPathVariance('Path', env, log);
checkPathVariance('path', env, log);
}
}

function checkPathVariance(variance: string, env: Record<string, string>, log: LoggerWrapper): void {
function checkPathVariance(variance: string, env: Record<string, string>, log: Logger): void {
if (variance in env) {
if (env[variance].indexOf('/') != -1)
log.warn(`Env variable ${variance} contains slash on Windows: "${env[variance]}". That won't really work.`);
Expand Down
4 changes: 2 additions & 2 deletions src/Configurations.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as vscode from 'vscode';
import { LoggerWrapper } from './LoggerWrapper';
import { Logger } from './Logger';
import { ConfigOfExecGroup } from './ConfigOfExecGroup';
import { WorkspaceShared } from './WorkspaceShared';
import { hashString } from './Util';
Expand Down Expand Up @@ -66,7 +66,7 @@ export const setEnvKey = 'testMate.cpp.debug.setEnv';
export class Configurations {
private _cfg: vscode.WorkspaceConfiguration;

constructor(readonly _log: LoggerWrapper, private _workspaceFolderUri: vscode.Uri) {
constructor(readonly _log: Logger, private _workspaceFolderUri: vscode.Uri) {
this._cfg = vscode.workspace.getConfiguration(ConfigSectionBase, _workspaceFolderUri);
}

Expand Down
89 changes: 89 additions & 0 deletions src/Logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import * as util from 'vscode-test-adapter-util';
import { debugBreak } from './util/DevelopmentHelper';

///

export class Logger {
constructor() {
this._logger = new util.Log('testMate.cpp.log', undefined, 'C++ TestMate', { depth: 3 }, false);
}

private _logger: util.Log;

//eslint-disable-next-line
trace(msg: any, ...msgs: any[]): void {
process.env['TESTMATE_DEBUG'] && this._logger.debug(msg, ...msgs);
}

//eslint-disable-next-line
debug(msg: any, ...msgs: any[]): void {
this._logger.debug(msg, ...msgs);
}

//eslint-disable-next-line
debugS(msg: any, ...msgs: any[]): void {
this._logger.debug(msg, ...msgs);
}

//eslint-disable-next-line
setContext(name: string, context: { [key: string]: any } | null): void {
this._logger.info('context:' + name, context);
}

setTags(_tags: { [key: string]: string }): void {}

//eslint-disable-next-line
info(msg: any, ...msgs: any[]): void {
this._logger.info(msg, ...msgs);
}

//eslint-disable-next-line
infoS(_m: string, ...msg: any[]): void {
this._logger.info(...msg);
}

infoSWithTags(m: string, tags: { [key: string]: string }): void {
this._logger.info(m, tags);
}

//eslint-disable-next-line
warn(m: string, ...msg: any[]): void {
this._logger.warn(m, ...msg);
}

//eslint-disable-next-line
warnS(m: string, ...msg: any[]): void {
this._logger.warn(m, ...msg);
}

//eslint-disable-next-line
error(m: string, ...msg: any[]): void {
if (!m.startsWith('TODO')) debugBreak();
this._logger.error(m, ...msg);
}

//eslint-disable-next-line
errorS(m: string, ...msg: any[]): void {
this.error(m, ...msg);
}

//eslint-disable-next-line
exception(e: unknown, ...msg: unknown[]): void {
debugBreak();
this._logger.error(e, ...msg);
}

//eslint-disable-next-line
exceptionS(e: unknown, ...msg: unknown[]): void {
debugBreak();
this._logger.error(e, ...msg);
}

setNextInspectOptions(inspectOptions: util.InspectOptions): void {
this._logger.setNextInspectOptions(inspectOptions);
}

dispose(): void {
this._logger.dispose();
}
}
58 changes: 0 additions & 58 deletions src/LoggerWrapper.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/RunningExecutable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as ansi from 'ansi-colors';

import { ChildProcessWithoutNullStreams } from './util/FSWrapper';
import { AbstractTest } from './framework/AbstractTest';
import { LoggerWrapper } from './LoggerWrapper';
import { Logger } from './Logger';
import { promisify } from 'util';
import { CancellationToken, generateId } from './Util';
import { SpawnBuilder } from './Spawner';
Expand Down Expand Up @@ -122,7 +122,7 @@ export class RunningExecutable {
} catch {}
}

setPriorityAsync(log: LoggerWrapper): void {
setPriorityAsync(log: Logger): void {
const priority = 16;
let retryOnFailure = 5;

Expand Down
4 changes: 2 additions & 2 deletions src/Util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ export function milisecToStr(durationInMilisec: number): string {
import * as vscode from 'vscode';
import * as fs from 'fs';
import { promisify } from 'util';
import { LoggerWrapper } from './LoggerWrapper';
import { Logger } from './Logger';

type VersionT = [number, number, number];
export class GoogleTestVersionFinder {
Expand All @@ -189,7 +189,7 @@ export class GoogleTestVersionFinder {

private static _version: Promise<VersionT | undefined> | undefined = undefined;

static Get(log: LoggerWrapper): Promise<VersionT | undefined> {
static Get(log: Logger): Promise<VersionT | undefined> {
if (this._version === undefined) {
const cancellation = new vscode.CancellationTokenSource();

Expand Down
6 changes: 3 additions & 3 deletions src/WorkspaceManager.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as vscode from 'vscode';
import { Config, Configurations, setEnvKey } from './Configurations';
import { LoggerWrapper } from './LoggerWrapper';
import { Logger } from './Logger';
import {
createPythonIndexerForArray,
createPythonIndexerForPathVariable,
Expand All @@ -20,7 +20,7 @@ import { ProgressReporter } from './util/ProgressReporter';
export class WorkspaceManager implements vscode.Disposable {
constructor(
private readonly workspaceFolder: vscode.WorkspaceFolder,
private readonly log: LoggerWrapper,
private readonly log: Logger,
testItemManager: TestItemManager,
executableChanged: (e: Iterable<AbstractExecutable>) => void,
) {
Expand Down Expand Up @@ -272,7 +272,7 @@ export class WorkspaceManager implements vscode.Disposable {
if (this.initP === true) this.init(false);
}

private _getConfiguration(log: LoggerWrapper): Configurations {
private _getConfiguration(log: Logger): Configurations {
return new Configurations(log, this.workspaceFolder.uri);
}

Expand Down
6 changes: 3 additions & 3 deletions src/WorkspaceShared.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { LoggerWrapper } from './LoggerWrapper';
import { Logger } from './Logger';
import * as vscode from 'vscode';
import { TaskPool } from './util/TaskPool';
import { ResolveRuleAsync } from './util/ResolveRule';
Expand All @@ -11,7 +11,7 @@ import { AbstractExecutable } from './framework/AbstractExecutable';
export class WorkspaceShared {
constructor(
readonly workspaceFolder: vscode.WorkspaceFolder,
readonly log: LoggerWrapper,
readonly log: Logger,
readonly testController: TestItemManager,
readonly executeTask: (
taskName: string,
Expand Down Expand Up @@ -66,5 +66,5 @@ export class WorkspaceShared {
}

export interface ExecutableShared extends Readonly<FrameworkSpecificConfig> {
readonly log: LoggerWrapper;
readonly log: Logger;
}
4 changes: 2 additions & 2 deletions src/framework/GoogleBenchmark/GoogleBenchmarkExecutable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { AbstractTest } from '../AbstractTest';
import { CancellationFlag } from '../../Util';
import { TestGroupingConfig } from '../../TestGroupingInterface';
import { TestResultBuilder } from '../../TestResultBuilder';
import { LoggerWrapper } from '../../LoggerWrapper';
import { Logger } from '../../Logger';
import { TestItemParent } from '../../TestItemManager';

export class GoogleBenchmarkExecutable extends AbstractExecutable<GoogleBenchmarkTest> {
Expand Down Expand Up @@ -207,7 +207,7 @@ export class GoogleBenchmarkExecutable extends AbstractExecutable<GoogleBenchmar
}

function parseAndProcessTestCase(
log: LoggerWrapper,
log: Logger,
builder: TestResultBuilder<GoogleBenchmarkTest>,
metric: Record<string, unknown>,
): void {
Expand Down
4 changes: 2 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import * as vscode from 'vscode';
import { AbstractExecutable, TestsToRun } from './framework/AbstractExecutable';
import { LoggerWrapper } from './LoggerWrapper';
import { Logger } from './Logger';
import { WorkspaceManager } from './WorkspaceManager';
import { SharedTestTags } from './framework/SharedTestTags';
import { TestItemManager } from './TestItemManager';

///

export async function activate(context: vscode.ExtensionContext): Promise<void> {
const log = new LoggerWrapper('testMate.cpp.log', 'C++ TestMate');
const log = new Logger();
log.info('Activating extension');
const controller = vscode.tests.createTestController('testmatecpp', 'TestMate C++');
const workspace2manager = new Map<vscode.WorkspaceFolder, WorkspaceManager>();
Expand Down
4 changes: 2 additions & 2 deletions src/util/BuildProcessChecker.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { LoggerWrapper } from '../LoggerWrapper';
import { Logger } from '../Logger';
import find_process = require('find-process');

///

// not so nice, init in rootsuite in the future
export class BuildProcessChecker {
constructor(private readonly _log: LoggerWrapper) {}
constructor(private readonly _log: Logger) {}

private readonly _checkIntervalMillis = 2000;
// https://en.wikipedia.org/wiki/List_of_compilers#C++_compilers
Expand Down
8 changes: 2 additions & 6 deletions src/util/TextStreamParser.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import { LoggerWrapper } from '../LoggerWrapper';
import { Logger } from '../Logger';
import { debugBreak } from './DevelopmentHelper';
import { ParserInterface } from './ParserInterface';

export class TextStreamParser implements ParserInterface {
constructor(
private readonly log: LoggerWrapper,
rootProcessor: RootLineProcessor,
private readonly handleStdErr = true,
) {
constructor(private readonly log: Logger, rootProcessor: RootLineProcessor, private readonly handleStdErr = true) {
this.topProcessor = rootProcessor;
}

Expand Down
4 changes: 2 additions & 2 deletions src/util/XmlParser.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as htmlparser2 from 'htmlparser2';
import { LoggerWrapper } from '../LoggerWrapper';
import { Logger } from '../Logger';
import { debugBreak } from './DevelopmentHelper';
import { ParserInterface } from './ParserInterface';

Expand All @@ -15,7 +15,7 @@ export class XmlParser implements ParserInterface {
private readonly xmlTagProcessorStack: ProcessorFrame[] = [];
private topTagProcessor: ProcessorFrame;

constructor(private readonly log: LoggerWrapper, processor: XmlTagProcessor, onerrorCb: (error: Error) => void) {
constructor(private readonly log: Logger, processor: XmlTagProcessor, onerrorCb: (error: Error) => void) {
{
let resolver: () => void;
this.endP = new Promise<void>(resolve => (resolver = resolve));
Expand Down
4 changes: 2 additions & 2 deletions test/LogOutputContent.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as assert from 'assert';
import * as sinon from 'sinon';
import { LoggerWrapper } from '../src/LoggerWrapper';
import { Logger } from '../src/Logger';

///

Expand All @@ -18,7 +18,7 @@ export function expectedLoggedWarning(warning: string): void {

///

export const logger = new LoggerWrapper('testMate.cpp.log', `C++ TestMate`);
export const logger = new Logger();
// eslint-disable-next-line
const spyError: sinon.SinonSpy<any[], void> = sinon.spy(logger, 'error');
// eslint-disable-next-line
Expand Down

0 comments on commit 96b2936

Please sign in to comment.