Skip to content

Commit

Permalink
Feature: more verbose logging (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
emmercm authored Sep 27, 2022
1 parent 564f997 commit 1dd4f12
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 19 deletions.
1 change: 1 addition & 0 deletions src/console/singleBarFormatted.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export default class SingleBarFormatted {
}

private calculateEta(remaining: number): void {
// TODO(cemmer): find a better way to calculate rate
// cli-progress/lib/ETA.calculate()
const currentBufferSize = this.valueBuffer.length;
const buffer = Math.min(SingleBarFormatted.ETA_BUFFER_LENGTH, currentBufferSize);
Expand Down
5 changes: 5 additions & 0 deletions src/modules/argumentsParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ import Constants from '../constants.js';
import Options from '../types/options.js';
import ReleaseCandidate from '../types/releaseCandidate.js';

/**
* Parse a CLI argv string[] into {@link Options}.
*
* This class will not be run concurrently with any other class.
*/
export default class ArgumentsParser {
private readonly logger: Logger;

Expand Down
4 changes: 4 additions & 0 deletions src/modules/candidateFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@ export default class CandidateFilter {
const output = new Map<Parent, ReleaseCandidate[]>();

if (!parentsToCandidates.size) {
await this.progressBar.logDebug(`${dat.getName()}: No parents, so no candidates to filter`);
return output;
}

// Return early if there aren't any candidates
const totalReleaseCandidates = [...parentsToCandidates.values()]
.reduce((sum, rcs) => sum + rcs.length, 0);
if (!totalReleaseCandidates) {
await this.progressBar.logDebug(`${dat.getName()}: No parent has candidates`);
return output;
}

Expand All @@ -45,11 +47,13 @@ export default class CandidateFilter {
for (let i = 0; i < [...parentsToCandidates.entries()].length; i += 1) {
const [parent, releaseCandidates] = [...parentsToCandidates.entries()][i];
await this.progressBar.increment();
await this.progressBar.logDebug(`${dat.getName()}: Filtering ${parent.getName()}: ${releaseCandidates.length} candidates before`);

const filteredReleaseCandidates = releaseCandidates
.filter((rc) => this.preFilter(rc))
.sort((a, b) => this.sort(a, b))
.filter((rc, idx) => this.postFilter(idx));
await this.progressBar.logDebug(`${dat.getName()}: Filtering ${parent.getName()}: ${filteredReleaseCandidates.length} candidates after`);
output.set(parent, filteredReleaseCandidates);
}

Expand Down
17 changes: 8 additions & 9 deletions src/modules/candidateGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export default class CandidateGenerator {

const output = new Map<Parent, ReleaseCandidate[]>();
if (!inputRomFiles.length) {
await this.progressBar.logDebug(`${dat.getName()}: No input ROMs to make candidates from`);
return output;
}

Expand All @@ -40,7 +41,7 @@ export default class CandidateGenerator {
// TODO(cemmer): only do this once globally, not per DAT
// TODO(cemmer): ability to index files by some other property such as name
const hashCodeToInputFiles = await CandidateGenerator.indexFilesByHashCode(inputRomFiles);
await this.progressBar.logInfo(`${dat.getName()}: ${hashCodeToInputFiles.size} unique ROM CRC32s found`);
await this.progressBar.logInfo(`${dat.getName()}: ${hashCodeToInputFiles.size} unique ROMs found`);

// TODO(cemmer): ability to work without DATs, generating a parent/game/release per file
// For each parent, try to generate a parent candidate
Expand Down Expand Up @@ -71,6 +72,7 @@ export default class CandidateGenerator {
}
}

await this.progressBar.logInfo(`${dat.getName()}: Found ${releaseCandidates.length} candidates for ${parent}`);
output.set(parent, releaseCandidates);
}

Expand Down Expand Up @@ -124,9 +126,10 @@ export default class CandidateGenerator {
.map(([rom]) => rom);

// Ignore the Game if not every File is present
const missingRomFiles = game.getRoms().length - foundRomFiles.length;
if (missingRomFiles > 0) {
await this.logMissingRomFiles(game, release, missingRoms);
if (missingRoms.length > 0) {
if (foundRomFiles.length > 0) {
await this.logMissingRomFiles(game, release, missingRoms);
}
return undefined;
}

Expand All @@ -138,11 +141,7 @@ export default class CandidateGenerator {
release: Release | undefined,
missingRoms: ROM[],
): Promise<void> {
if (!missingRoms.length) {
return;
}

let message = `Missing ${missingRoms.toLocaleString()} file${missingRoms.length !== 1 ? 's' : ''} for: ${game.getName()}`;
let message = `Missing ${missingRoms.length.toLocaleString()} file${missingRoms.length !== 1 ? 's' : ''} for: ${game.getName()}`;
if (release?.getRegion()) {
message += ` (${release?.getRegion()})`;
}
Expand Down
2 changes: 2 additions & 0 deletions src/modules/datScanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export default class DATScanner extends Scanner {

// Scan files on disk for DATs (archives may yield more than one DAT)
private async getDatFiles(datFilePaths: string[]): Promise<File[]> {
await this.progressBar.logDebug('Enumerating DAT archives');
return (await async.mapLimit(
datFilePaths,
Constants.DAT_SCANNER_THREADS,
Expand All @@ -53,6 +54,7 @@ export default class DATScanner extends Scanner {

// Parse each file into a DAT
private async parseDatFiles(datFiles: File[]): Promise<DAT[]> {
await this.progressBar.logDebug('Parsing DAT files');
const results = (await async.mapLimit(
datFiles,
Constants.DAT_SCANNER_THREADS,
Expand Down
15 changes: 9 additions & 6 deletions src/modules/headerProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ import File from '../types/files/file.js';
import FileHeader from '../types/files/fileHeader.js';
import Options from '../types/options.js';

// TODO(cemmer): put debug statements in here
/**
* For every input ROM file found, attempt to find a matching header and resolve its
* header-less checksum.
*
* This class will not be run concurrently with any other class.
*/
export default class HeaderProcessor {
private readonly options: Options;

Expand All @@ -29,14 +34,10 @@ export default class HeaderProcessor {
async (inputFile, callback: AsyncResultCallback<File, Error>) => {
await this.progressBar.increment();

// Don't do anything if the file already has a header
if (inputFile.getFileHeader()) {
return callback(null, inputFile);
}

// Can get FileHeader from extension, use that
const headerForExtension = FileHeader.getForFilename(inputFile.getExtractedFilePath());
if (headerForExtension) {
await this.progressBar.logDebug(`${inputFile.toString()}: found header by extension: ${headerForExtension}`);
const fileWithHeader = await (
await inputFile.withFileHeader(headerForExtension)
).resolve();
Expand All @@ -48,6 +49,7 @@ export default class HeaderProcessor {
const headerForFile = await inputFile
.extractToStream((stream) => FileHeader.getForFileStream(stream));
if (headerForFile) {
await this.progressBar.logDebug(`${inputFile.toString()}: found header by contents: ${headerForExtension}`);
const fileWithHeader = await (
await inputFile.withFileHeader(headerForFile)
).resolve();
Expand All @@ -57,6 +59,7 @@ export default class HeaderProcessor {
}

// Should not get FileHeader
await this.progressBar.logDebug(`${inputFile.toString()}: no header found`);
return callback(null, inputFile);
},
);
Expand Down
10 changes: 7 additions & 3 deletions src/modules/outputCleaner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,24 @@ export default class OutputCleaner {
}

async clean(writtenFilesToExclude: File[]): Promise<number> {
await this.progressBar.logInfo('Cleaning files in output');

// If nothing was written, then don't clean anything
const outputFilePathsToExclude = writtenFilesToExclude
.map((file) => file.getFilePath());
if (!outputFilePathsToExclude.length) {
if (!writtenFilesToExclude.length) {
await this.progressBar.logInfo('No files were written, not cleaning output');
return 0;
}

const outputDir = this.options.getOutput();
const outputFilePathsToExclude = writtenFilesToExclude
.map((file) => file.getFilePath());

// If there is nothing to clean, then don't do anything
const filesToClean = (await fg(`${outputDir}/**`.replace(/\\/g, '/')))
.map((pathLike) => pathLike.replace(/[\\/]/g, path.sep))
.filter((file) => outputFilePathsToExclude.indexOf(file) === -1);
if (!filesToClean.length) {
await this.progressBar.logInfo('No files to clean');
return 0;
}

Expand Down
2 changes: 2 additions & 0 deletions src/modules/reportGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ export default class ReportGenerator {
}

async generate(datsStatuses: DATStatus[]): Promise<void> {
await this.progressBar.logInfo('Generating report');

const report = this.options.getOutputReport();
const append = (message: string): void => fs.appendFileSync(report, `${message.trimEnd()}\n`);

Expand Down
3 changes: 2 additions & 1 deletion src/modules/romScanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ import Scanner from './scanner.js';
*/
export default class ROMScanner extends Scanner {
async scan(): Promise<File[]> {
await this.progressBar.logInfo('Scanning ROM files');

await this.progressBar.setSymbol(Symbols.SEARCHING);
await this.progressBar.reset(0);

await this.progressBar.logInfo('Scanning ROM files');
const romFilePaths = await this.options.scanInputFilesWithoutExclusions();
await this.progressBar.reset(romFilePaths.length);
await this.progressBar.logInfo(`Found ${romFilePaths.length} ROM file${romFilePaths.length !== 1 ? 's' : ''}`);
Expand Down
2 changes: 2 additions & 0 deletions src/modules/statusGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ export default class StatusGenerator {
dat: DAT,
parentsToReleaseCandidates: Map<Parent, ReleaseCandidate[]>,
): Promise<DATStatus> {
await this.progressBar.logInfo('Generating report');

const datStatus = new DATStatus(dat, parentsToReleaseCandidates);

await this.progressBar.done(datStatus.toString(this.options));
Expand Down

0 comments on commit 1dd4f12

Please sign in to comment.