-
-
Notifications
You must be signed in to change notification settings - Fork 295
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
137 additions
and
189 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import { log } from './logger/simple' | ||
import { log } from './logger' | ||
|
||
const seconds = 1000 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,109 @@ | ||
/* eslint-disable @typescript-eslint/no-empty-function, no-restricted-syntax */ | ||
/* eslint-disable @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-unsafe-return, @typescript-eslint/no-empty-function, no-restricted-syntax */ | ||
|
||
import type { Translators as Translator } from '../typings/translators' | ||
declare const TranslationWorker: { job: Translator.Worker.Job } | ||
import { $dump, run } from './logger/simple' | ||
declare const Zotero: any | ||
|
||
import { stringify } from './stringify' | ||
import { worker } from './client' | ||
import * as client from './client' | ||
const version = require('./../gen/version.js') | ||
export const run = `<${version} ${client.run}>` | ||
|
||
function toString(obj): string { | ||
try { | ||
if (typeof obj === 'string') return obj | ||
return stringify(obj, 0) | ||
} | ||
catch (err) { | ||
return stringify(err, 0) | ||
} | ||
declare const dump: (msg: string) => void | ||
|
||
export const discard = { | ||
log(): void {}, | ||
error(): void {}, | ||
warn(): void {}, | ||
debug(): void {}, | ||
info(): void {}, | ||
clear(): void {}, | ||
dir(): void {}, | ||
table(): void {}, | ||
} | ||
|
||
export const log = new class Logger { | ||
protected timestamp: number | ||
public prefix = '' | ||
function stringifyXPCOM(obj): string { | ||
if (!obj.QueryInterface) return '' | ||
if (obj.message) return `[XPCOM error ${ obj.message }]` | ||
if (obj.name) return `[XPCOM object ${ obj.name }]` | ||
return '[XPCOM object]' | ||
} | ||
|
||
private format({ error = false }, msg) { | ||
if (Array.isArray(msg)) msg = msg.map(toString).join(' ') | ||
function stringifyError(obj) { | ||
if (obj instanceof Error) return `[error: ${ obj.message || '<unspecified error>' }\n${ obj.stack }]` | ||
// guess it is an errorevent | ||
if (obj.error instanceof Error && obj.message) return `[errorevent: ${ obj.message } ${ stringifyError(obj.error) }]` | ||
if (typeof ErrorEvent !== 'undefined' && obj instanceof ErrorEvent) return `[errorevent: ${ obj.message || '<unspecified errorevent>' }]` | ||
return '' | ||
} | ||
|
||
let prefix = '' | ||
if (worker) { | ||
prefix += ' worker' | ||
if (typeof TranslationWorker !== 'undefined') prefix += `:${ TranslationWorker.job.translator }` | ||
function replacer() { | ||
const seen = new WeakSet | ||
return (key, value) => { | ||
if (typeof value === 'object' && value !== null) { | ||
if (seen.has(value)) return '[Circular]' | ||
seen.add(value) | ||
} | ||
|
||
if (error) prefix += ' error:' | ||
if (value === null) return value | ||
if (value instanceof Set) return [...value] | ||
if (value instanceof Map) return Object.fromEntries(value) | ||
|
||
return `{better-bibtex ${run} ${ this.prefix }${ prefix }} ${ msg }` | ||
} | ||
switch (typeof value) { | ||
case 'string': | ||
case 'number': | ||
case 'boolean': | ||
return value | ||
|
||
public get enabled(): boolean { | ||
return ( | ||
(typeof TranslationWorker !== 'undefined' && TranslationWorker.job.debugEnabled) | ||
|| !Zotero | ||
|| Zotero.Debug?.enabled | ||
|| Zotero.Prefs?.get('debug.store') | ||
) as boolean | ||
} | ||
case 'object': | ||
return stringifyXPCOM(value) || stringifyError(value) || value | ||
} | ||
|
||
public debug(...msg) { | ||
Zotero.debug(this.format({}, msg)) | ||
if (Array.isArray(value)) return value | ||
|
||
return undefined | ||
} | ||
} | ||
|
||
public info(msg: string) { | ||
Zotero.debug(this.format({}, msg)) | ||
function to_s(obj: any): string { | ||
if (typeof obj === 'string') return obj | ||
return JSON.stringify(obj, replacer(), 2) | ||
} | ||
|
||
export function format(...msg): void { | ||
msg.map(to_s).join(' ') | ||
} | ||
|
||
export const log = new class { | ||
public prefix = '' | ||
|
||
#prefix(error?: any) { | ||
return `{${ error ? 'error: ' : '' }${ client.worker ? 'worker: ' : '' }${this.prefix}better-bibtex: ${run}} ` | ||
} | ||
|
||
public error(...msg) { | ||
Zotero.debug(this.format({ error: true }, msg)) | ||
public debug(...msg): void { | ||
Zotero.debug(`${this.#prefix()}${format(...msg)}\n`) | ||
} | ||
|
||
public status({ error = false }, ...msg) { | ||
if (error || this.enabled) Zotero.debug(this.format({ error }, msg)) | ||
public info(...msg): void { | ||
Zotero.debug(`${this.#prefix()}${format(...msg)}\n`) | ||
} | ||
|
||
public async timed(msg: string, code: () => void | Promise<void>) { | ||
const start = Date.now() | ||
await code() | ||
this.debug(msg, 'took', Date.now() - start, 'ms') | ||
public error(...msg): void { | ||
Zotero.debug(`${this.#prefix(true)}${format(...msg)}\n`) | ||
} | ||
|
||
public dump(msg: string) { | ||
$dump(msg) | ||
public dump(msg: string, error?: Error): void { | ||
if (error) { | ||
dump(`${this.#prefix(error)}${format(msg, error)}\n`) | ||
} | ||
else { | ||
dump(`${this.#prefix()}${format(msg)}\n`) | ||
} | ||
} | ||
} | ||
|
||
export function $dump(msg: string, error?: Error): void { | ||
log.dump(msg, error) | ||
} | ||
|
||
export function trace(msg: string, mode = ''): void { | ||
dump(`trace${ mode }\t${ Date.now() }\t${ msg }\n`) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.