Skip to content

Commit

Permalink
Change log format (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
MakotoE authored May 27, 2019
1 parent 1c017ff commit 0e703d0
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 86 deletions.
19 changes: 8 additions & 11 deletions src/Logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,24 @@ import {Logger} from './Logger';
import * as assert from 'assert';

describe('Logger', () => {
it('log()', () => {
it('write()', () => {
const logger = new Logger();
assert.strictEqual(logger.log(), '[]');

logger.push('action0');
logger.push('action1', {a: 0});
const expected = '[{"name":"action0","args":{}},{"name":"action1","args":{"a":0}}]';
assert.strictEqual(logger.log().replace(/\s/g, ''), expected);
logger.write('action0');
logger.write('action1', {a: 0});
const expected = '{"name":"action0","args":{}}\n{"name":"action1","args":{"a":0}}\n';
assert.strictEqual(logger.log(), expected);
});

it('deepCopy()', () => {
it('addFileName()', () => {
const a = {a:0};
// @ts-ignore
const result = Logger.deepCopy(a);
a.a = 1;
const result = Logger.addFileName(a);
assert.deepStrictEqual(result, {a: 0});

const file = new File([], 'filename');
const importOptions = {options: {source: {file}}};
const expected = {options: {source: {file: {name: 'filename'}}}};
// @ts-ignore
assert.deepStrictEqual(Logger.deepCopy(importOptions), expected);
assert.deepStrictEqual(Logger.addFileName(importOptions), expected);
});
});
45 changes: 19 additions & 26 deletions src/Logger.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,36 @@
interface Record {
readonly name: string;
readonly args: {};
}

export class Logger {
public constructor() {
this._log = [];
this._log = '';
}

// Adds name and a deep copy of args if defined otherwise empty object, as a new record.
// Do not add sensitive information.
// Records name and args. Do not add sensitive information.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
public push(name: string, args?: {[key: string]: any}): void {
this._log.push(Object.freeze({name, args: args ? Logger.deepCopy(args) : {}}));
public write(name: string, args?: {[key: string]: any}): void {
this._log += JSON.stringify({name, args: args ? Logger.addFileName(args) : {}}) + '\n';
}

public log(): string {
return JSON.stringify(this._log, null, 2);
return this._log;
}

private readonly _log: Record[];

private static deepCopy(a: {}): {} {
const copy = JSON.parse(JSON.stringify(a));
private _log: string;

private static addFileName(args: {[key: string]: any}): {} {
// Replace file in ImportOptions with the filename
if (
'options' in copy
&& 'source' in copy.options
&& 'file' in copy.options.source
&& typeof copy.options.source.file === 'object'
&& 'options' in a
&& 'source' in (a as {options: {}}).options
&& 'file' in (a as {options: {source: {}}}).options.source
&& (a as {options: {source: {file: {}}}}).options.source.file instanceof File
'options' in args
&& 'source' in args.options
&& 'file' in args.options.source
&& typeof args.options.source.file === 'object'
&& 'options' in args
&& 'source' in (args as {options: {}}).options
&& 'file' in (args as {options: {source: {}}}).options.source
&& (args as {options: {source: {file: {}}}}).options.source.file instanceof File
) {
copy.options.source.file.name
= (a as {options: {source: {file: {name?: string}}}}).options.source.file.name;
const filename = (args as {options: {source: {file: {name?: string}}}})
.options.source.file.name;
args.options.source.file = {name: filename};
}
return copy;
return args;
}
}
39 changes: 0 additions & 39 deletions src/Store.test.tsx

This file was deleted.

18 changes: 9 additions & 9 deletions src/Store.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export class Store extends React.Component<{}, State> {
};

this._log = new Logger();
this._log.push('version', {version});
this._log.write('version', {version});

this._abortFlags = new AbortFlagArray();
}
Expand Down Expand Up @@ -93,11 +93,11 @@ export class Store extends React.Component<{}, State> {
initialized: true,
platform: environmentInfo.platform,
});
this._log.push('environmentInfo', environmentInfo);
this._log.write('environmentInfo', environmentInfo);
} catch (err) {
this.setParserError(new Error(Store.getErrorMessage(err)));
}
this._log.push('initAPI');
this._log.write('initAPI');
await this.checkLargeFile();
}

Expand All @@ -106,12 +106,12 @@ export class Store extends React.Component<{}, State> {
const aLargeExcelWorksheetProbablyHasThisManyCells = 1_000_000_000 / 20;
const largeFile = await this.worksheetArea() > aLargeExcelWorksheetProbablyHasThisManyCells;
this.setState({largeFile});
this._log.push('checkLargeFile');
this._log.write('checkLargeFile');
}

public setParserOutput = (parserOutput: ParserOutput) => {
this.setState({parserOutput});
this._log.push('setParserOutput', {parserOutput});
this._log.write('setParserOutput', {parserOutput});
}

public setParserError = (err: Error) => {
Expand All @@ -125,7 +125,7 @@ export class Store extends React.Component<{}, State> {
this.setState(state => ({
progress: {show: state.progress.show, aborting: true, percent: state.progress.percent},
}));
this._log.push('abort');
this._log.write('abort');
}

public import = async (options: Parser.ImportOptions): Promise<void> => {
Expand All @@ -149,7 +149,7 @@ export class Store extends React.Component<{}, State> {
state => ({progress: {show: !state.progress.show, aborting: false, percent: 1.0}})
);

this._log.push('import', {options});
this._log.write('import', {options});
}

public worksheetArea = async (): Promise<number> => {
Expand All @@ -159,7 +159,7 @@ export class Store extends React.Component<{}, State> {
} catch (err) {
this.setParserError(new Error(Store.getErrorMessage(err)));
}
this._log.push('worksheetArea');
this._log.write('worksheetArea');
return result;
}

Expand All @@ -185,7 +185,7 @@ export class Store extends React.Component<{}, State> {
state => ({progress: {show: !state.progress.show, aborting: false, percent: 1.0}}),
);

this._log.push('csvStringAndName', {options});
this._log.write('csvStringAndName', {options});
return result;
}

Expand Down
2 changes: 1 addition & 1 deletion src/components/About.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export class AboutComponent extends React.Component<Props, {}> {

public exportLog = () => {
const blob = new Blob([this.props.store.log()]);
FileSaver.saveAs(blob, 'csvImportExportLog.json');
FileSaver.saveAs(blob, 'csvImportExportLog.txt');
}
}

Expand Down

0 comments on commit 0e703d0

Please sign in to comment.