Skip to content

Commit

Permalink
Merge branch 'use_inspect'
Browse files Browse the repository at this point in the history
  • Loading branch information
tamuratak committed Oct 1, 2023
2 parents 6ea6d56 + d91eb89 commit 4fb2138
Show file tree
Hide file tree
Showing 27 changed files with 89 additions and 122 deletions.
6 changes: 2 additions & 4 deletions src/commander.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,8 @@ export class Commander {
pdfFile = this.extension.manager.tex2pdf(this.extension.manager.rootFile)
}
return this.extension.locator.syncTeX(undefined, undefined, pdfFile)
} catch(e) {
if (e instanceof Error) {
this.extension.logger.logError(e)
}
} catch (e) {
this.extension.logger.logError(e)
throw e
}
}
Expand Down
13 changes: 5 additions & 8 deletions src/components/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type { Manager } from './manager'
import type { LwStatusBarItem } from './statusbaritem'
import { statPath } from '../lib/lwfs/lwfs'
import { ExternalPromise } from '../utils/externalpromise'
import { inspectCompact, inspectReadable } from '../utils/inspect'

const maxPrintLine = '10000'

Expand Down Expand Up @@ -60,9 +61,7 @@ export class Builder {
cp.execSync(`taskkill /F /T /PID ${pid}`, { timeout: 1000 })
}
} catch (e) {
if (e instanceof Error) {
this.extension.logger.error(`Error when killing child processes of the current process. ${e.message}`)
}
this.extension.logger.error(`Error when killing child processes of the current process. ${inspectReadable(e)}`)
} finally {
proc.kill()
this.extension.logger.info(`Kill the current process. PID: ${pid}`)
Expand Down Expand Up @@ -208,9 +207,7 @@ export class Builder {
await this.buildFinished(rootFile)
} catch (e) {
this.extension.statusbaritem.displayStatus('fail', 'Build failed.')
if (e instanceof Error) {
this.extension.logger.logError(e)
}
this.extension.logger.logError(e)
} finally {
releaseBuildMutex()
}
Expand Down Expand Up @@ -245,7 +242,7 @@ export class Builder {

private buildStep(rootFile: string, step: StepCommand, {stepIndex, totalStepsLength}: {stepIndex: number, totalStepsLength: number}) {
this.extension.logger.logCommand(`Recipe step ${stepIndex}`, step.command, step.args)
this.extension.logger.info(`Recipe step env: ${JSON.stringify(step.env)}`)
this.extension.logger.info(`Recipe step env: ${inspectCompact(step.env)}`)
const envVars = Object.create(null) as ProcessEnv
Object.keys(process.env).forEach(key => envVars[key] = process.env[key])
const currentEnv = step.env
Expand Down Expand Up @@ -305,7 +302,7 @@ export class Builder {
this.currentProcess = undefined
resultPromise.resolve()
}
} catch(e) {
} catch (e) {
resultPromise.reject(e)
}
})
Expand Down
5 changes: 2 additions & 3 deletions src/components/compilerloglib/biblogparser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { CompilerLog } from '../compilerlog'
import type { Completer } from '../../providers/completion'
import type { Logger } from '../logger'
import type { Manager } from '../manager'
import { inspectReadable } from '../../utils/inspect'

const multiLineWarning = /^Warning--(.+)\n--line (\d+) of file (.+)$/gm
const singleLineWarning = /^Warning--(.+) in ([^\s]+)\s*$/gm
Expand Down Expand Up @@ -38,9 +39,7 @@ export class BibLogParser {
try {
excludeRegexp = (configuration.get('message.bibtexlog.exclude') as string[]).map(regexp => new RegExp(regexp))
} catch (e) {
if (e instanceof Error) {
this.extension.logger.info(`latex-toybox.message.bibtexlog.exclude is invalid: ${e.message}`)
}
this.extension.logger.info(`latex-toybox.message.bibtexlog.exclude is invalid: ${inspectReadable(e)}`)
return
}
this.buildLog = []
Expand Down
5 changes: 2 additions & 3 deletions src/components/compilerloglib/latexlogparser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { LogEntry } from './core'
import type { CompilerLog } from '../compilerlog'
import type { Manager } from '../manager'
import type { Logger } from '../logger'
import { inspectReadable } from '../../utils/inspect'

const latexError = /^(?:(.*):(\d+):|!)(?: (.+) Error:)? (.+?)$/
const latexBox = /^((?:Over|Under)full \\[vh]box \([^)]*\)) in paragraph at lines (\d+)--(\d+)$/
Expand Down Expand Up @@ -80,9 +81,7 @@ export class LatexLogParser {
try {
excludeRegexp = (configuration.get('message.latexlog.exclude') as string[]).map(regexp => new RegExp(regexp))
} catch (e) {
if (e instanceof Error) {
this.extension.logger.info(`latex-toybox.message.latexlog.exclude is invalid: ${e.message}`)
}
this.extension.logger.info(`latex-toybox.message.latexlog.exclude is invalid: ${inspectReadable(e)}`)
return
}
// Compose the current file
Expand Down
3 changes: 2 additions & 1 deletion src/components/configuration.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as vscode from 'vscode'
import { Logger } from './logger'
import { inspectReadable } from '../utils/inspect'


export class Configuration {
Expand Down Expand Up @@ -35,7 +36,7 @@ export class Configuration {
const configuration = vscode.workspace.getConfiguration(undefined, workspace)
for(const config of this.configurationsToLog) {
const value = configuration.get(config)
this.extension.logger.info(`${config}: ${JSON.stringify(value, null, ' ')}`)
this.extension.logger.info(`${config}: ${inspectReadable(value)}`)
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/components/eventbus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type * as vscode from 'vscode'
import type {PdfViewerState} from '../../types/latex-toybox-protocol-types/index'
import { AwaitableEventEmitter } from './eventbuslib/awaitableeventemitter'
import type { Logger } from './logger'
import { inspectCompact } from '../utils/inspect'

export type EventName =
'auxupdated' |
Expand Down Expand Up @@ -41,7 +42,7 @@ export class EventBus {
}) {
this.allEmitters.forEach((emitter) => {
emitter.event((arg) => {
extension.logger.debug(`Event ${emitter.eventName} triggered. Payload: ${JSON.stringify(arg)}`)
extension.logger.debug(`Event ${emitter.eventName} triggered. Payload: ${inspectCompact(arg)}`)
})
})
}
Expand Down
18 changes: 8 additions & 10 deletions src/components/locator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type { Logger } from './logger'
import type { Manager } from './manager'
import type { Viewer } from './viewer'
import { ExternalPromise } from '../utils/externalpromise'
import { inspectCompact } from '../utils/inspect'

export type SyncTeXRecordForward = {
page: number,
Expand Down Expand Up @@ -164,9 +165,7 @@ export class Locator {
this.extension.viewer.syncTeX(pdfFile, record)
} catch (e) {
this.extension.logger.info('[SyncTexJs] Forward SyncTeX failed.')
if (e instanceof Error) {
this.extension.logger.logError(e)
}
this.extension.logger.logError(e)
}
} else {
const record = await this.invokeSyncTeXCommandForward(line, character, filePath, pdfFile)
Expand All @@ -177,7 +176,7 @@ export class Locator {
private invokeSyncTeXCommandForward(line: number, col: number, filePath: string, pdfFile: string): Thenable<SyncTeXRecordForward> {
const configuration = vscode.workspace.getConfiguration('latex-toybox')
const args = ['view', '-i', `${line}:${col + 1}:${filePath}`, '-o', pdfFile]
this.extension.logger.info(`Execute synctex with args ${JSON.stringify(args)}`)
this.extension.logger.info(`Execute synctex with args ${inspectCompact(args)}`)

const command = configuration.get('synctex.path') as string
const proc = cp.spawn(command, args, {cwd: path.dirname(pdfFile)})
Expand Down Expand Up @@ -227,7 +226,7 @@ export class Locator {
const configuration = vscode.workspace.getConfiguration('latex-toybox')

const args = ['edit', '-o', `${page}:${x}:${y}:${pdfPath}`]
this.extension.logger.info(`Executing synctex with args ${JSON.stringify(args)}`)
this.extension.logger.info(`Executing synctex with args: ${inspectCompact(args)}`)

const command = configuration.get('synctex.path') as string
const proc = cp.spawn(command, args, {cwd: path.dirname(pdfPath)})
Expand Down Expand Up @@ -279,9 +278,7 @@ export class Locator {
record = await this.synctexjs.syncTexJsBackward(Number(data.page), data.pos[0], data.pos[1], pdfPath)
} catch (e) {
this.extension.logger.info('[SyncTexJs] Backward SyncTeX failed.')
if (e instanceof Error) {
this.extension.logger.logError(e)
}
this.extension.logger.logError(e)
return
}
} else {
Expand All @@ -298,8 +295,9 @@ export class Locator {
record.input = ed
break
}
} catch(e) {
} catch (e) {
this.extension.logger.error(`[SyncTexJs] isSameRealPath throws error: ${record.input} and ${ed}`)
this.extension.logger.logError(e)
}
}

Expand All @@ -325,7 +323,7 @@ export class Locator {
editor.selection = new vscode.Selection(pos, pos)
await vscode.commands.executeCommand('revealLine', {lineNumber: row, at: 'center'})
this.animateToNotify(editor, pos)
} catch(e: unknown) {
} catch (e: unknown) {
if (e instanceof Error) {
this.extension.logger.logError(e)
}
Expand Down
9 changes: 5 additions & 4 deletions src/components/locatorlib/synctex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {iconvLiteSupportedEncodings} from '../../utils/convertfilename'
import {isSameRealPath} from '../../utils/pathnormalize'
import { existsPath, readFileAsBuffer } from '../../lib/lwfs/lwfs'
import type { ILogger } from '../../interfaces'
import { inspectCompact } from '../../utils/inspect'


class Rectangle {
Expand Down Expand Up @@ -110,7 +111,7 @@ export class SyncTexJs {
}

if (!await existsPath(synctexFile) && !await existsPath(synctexFileGz)) {
this.extension.logger.error(`[SyncTexJs] .synctex and .synctex.gz file not found: ${JSON.stringify({synctexFile, synctexFileGz})}`)
this.extension.logger.error(`[SyncTexJs] .synctex and .synctex.gz file not found: ${inspectCompact({synctexFile, synctexFileGz})}`)
}

throw new SyncTexJsError(`parseSyncTexForPdf failed with: ${pdfFile}`)
Expand Down Expand Up @@ -139,13 +140,13 @@ export class SyncTexJs {
}

async syncTexJsForward(line: number, filePath: string, pdfFile: string) {
this.extension.logger.info(`[SyncTexJs] Execute syncTexJsForward: ${JSON.stringify({pdfFile, filePath, line})}`)
this.extension.logger.info(`[SyncTexJs] Execute syncTexJsForward: ${inspectCompact({pdfFile, filePath, line})}`)
const pdfSyncObject = await this.parseSyncTexForPdf(pdfFile)
const inputFilePath = await this.findInputFilePathForward(filePath, pdfSyncObject)
if (inputFilePath === undefined) {
const inputFiles = Object.keys(pdfSyncObject.blockNumberLine)
const inputFilesStr = JSON.stringify(inputFiles, null, ' ')
throw new SyncTexJsError(`[SyncTexJs] No relevant entry of the tex file found in the synctex file: ${JSON.stringify({filePath, pdfFile, line, inputFilesStr})}`)
throw new SyncTexJsError(`[SyncTexJs] No relevant entry of the tex file found in the synctex file: ${inspectCompact({filePath, pdfFile, line, inputFilesStr})}`)
}

const linePageBlocks = pdfSyncObject.blockNumberLine[inputFilePath]
Expand Down Expand Up @@ -183,7 +184,7 @@ export class SyncTexJs {
}

async syncTexJsBackward(page: number, x: number, y: number, pdfPath: string) {
this.extension.logger.info(`[SyncTexJs] Execute syncTexJsBackward: ${JSON.stringify({pdfPath, page, x, y})}`)
this.extension.logger.info(`[SyncTexJs] Execute syncTexJsBackward: ${inspectCompact({pdfPath, page, x, y})}`)
const pdfSyncObject = await this.parseSyncTexForPdf(pdfPath)
const y0 = y - pdfSyncObject.offset.y
const x0 = x - pdfSyncObject.offset.x
Expand Down
18 changes: 4 additions & 14 deletions src/components/logger.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as vscode from 'vscode'
import { ILogger } from '../interfaces'
import { inspectCompact, inspectReadable } from '../utils/inspect'


export class Logger implements ILogger {
Expand All @@ -16,7 +17,7 @@ export class Logger implements ILogger {

logCommand(message: string, command: string, args: readonly string[] = []) {
this.info(message + ': ' + command)
this.info(message + ' args: ' + JSON.stringify(args))
this.info(message + ' args: ' + inspectCompact(args))
}

debug(message: string) {
Expand All @@ -27,19 +28,8 @@ export class Logger implements ILogger {
this.logPanel.error(message)
}

logError(e: Error) {
this.error(e.message)
if (e.stack) {
this.error(e.stack)
}
}

logOnRejected(e: unknown) {
if (e instanceof Error) {
this.logError(e)
} else {
this.error(String(e))
}
logError(e: unknown) {
this.error(inspectReadable(e))
}

showLog() {
Expand Down
3 changes: 2 additions & 1 deletion src/components/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import type { Commander } from '../commander'
import type { DuplicateLabels } from './duplicatelabels'
import type { CompletionUpdater } from './completionupdater'
import { ManagerWatcher } from './managerlib/managerwatcher'
import { inspectReadable } from '../utils/inspect'


/**
Expand Down Expand Up @@ -385,7 +386,7 @@ export class Manager {
try {
this.#rootFilePromise = rootFilePromise
const wsfolders = vscode.workspace.workspaceFolders?.map(e => e.uri.toString(true))
this.extension.logger.info(`Current workspace folders: ${JSON.stringify(wsfolders)}`)
this.extension.logger.info(`Current workspace folders: ${inspectReadable(wsfolders)}`)
this.localRootFile = undefined
const findMethods = [
() => this.finderUtils.findRootFromMagic(),
Expand Down
3 changes: 2 additions & 1 deletion src/components/managerlib/bibwatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { toKey } from '../../utils/tokey'
import type { LwFileWatcher } from './lwfilewatcher'
import type { Completer } from '../../providers/completion'
import type { Logger } from '../logger'
import { inspectReadable } from '../../utils/inspect'


export class BibWatcher {
Expand Down Expand Up @@ -66,7 +67,7 @@ export class BibWatcher {
}

logWatchedFiles() {
this.extension.logger.debug(`BibWatcher.bibsWatched: ${JSON.stringify(Array.from(this.watchedBibs))}`)
this.extension.logger.debug(`BibWatcher.bibsWatched: ${inspectReadable(this.watchedBibs)}`)
}

}
3 changes: 2 additions & 1 deletion src/components/managerlib/managerwatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as vscode from 'vscode'
import { toKey } from '../../utils/tokey'
import type { LwFileWatcher } from './lwfilewatcher'
import type { Logger } from '../logger'
import { inspectReadable } from '../../utils/inspect'


export class ManagerWatcher {
Expand Down Expand Up @@ -68,7 +69,7 @@ export class ManagerWatcher {
}

logWatchedFiles() {
this.extension.logger.debug(`ManagerWatcher.watchedFiles: ${JSON.stringify(Array.from(this.watchedFiles))}`)
this.extension.logger.debug(`ManagerWatcher.watchedFiles: ${inspectReadable(this.watchedFiles)}`)
}

}
3 changes: 2 additions & 1 deletion src/components/managerlib/pathutils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,9 @@ export class PathUtils {
return bibPath
}
}
} catch(e) {
} catch (e) {
this.extension.logger.info(`Cannot run kpsewhich to resolve .bib file: ${bib}`)
this.extension.logger.logError(e)
}
return undefined
}
Expand Down
5 changes: 3 additions & 2 deletions src/components/managerlib/pdfwatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { LwFileWatcher } from './lwfilewatcher'
import type { Logger } from '../logger'
import type { Viewer } from '../viewer'
import { sleep } from '../../utils/utils'
import { inspectReadable } from '../../utils/inspect'


export class PdfWatcher {
Expand Down Expand Up @@ -65,8 +66,8 @@ export class PdfWatcher {
}

logWatchedFiles() {
this.extension.logger.debug(`PdfWatcher.pdfsWatched: ${JSON.stringify(Array.from(this.watchedPdfs))}`)
this.extension.logger.debug(`PdfWatcher.ignoredPdfUris: ${JSON.stringify(Array.from(this.ignoredPdfUris))}`)
this.extension.logger.debug(`PdfWatcher.pdfsWatched: ${inspectReadable(this.watchedPdfs)}`)
this.extension.logger.debug(`PdfWatcher.ignoredPdfUris: ${inspectReadable(this.ignoredPdfUris)}`)
}

}
2 changes: 1 addition & 1 deletion src/components/mathpreview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export class MathPreview {
return new vscode.Hover(new vscode.MarkdownString(this.mputils.addDummyCodeBlock(`![equation](${md})`)), tex.range )
} catch(e) {
this.extension.logger.error(`Error while MathJax is rendering: ${typesetArg}`)
this.extension.logger.logOnRejected(e)
this.extension.logger.logError(e)
throw e
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/components/mathpreviewlib/hoverpreviewonref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ export class HoverPreviewOnRefProvider {
const xml = await this.mj.typeset(typesetArg, typesetOpts)
const svg = utils.svgToDataUrl(xml)
return svg
} catch(e) {
this.extension.logger.logOnRejected(e)
} catch (e) {
this.extension.logger.logError(e)
this.extension.logger.error(`Error when MathJax is rendering ${typesetArg}`)
throw e
}
Expand Down
Loading

0 comments on commit 4fb2138

Please sign in to comment.